This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mycompany.filterpriorities; | |
import java.io.IOException; | |
import javax.annotation.Priority; | |
import javax.ws.rs.container.ContainerRequestContext; | |
import javax.ws.rs.container.ContainerRequestFilter; | |
import javax.ws.rs.container.ContainerResponseContext; | |
import javax.ws.rs.container.ContainerResponseFilter; | |
import javax.ws.rs.ext.Provider; | |
@Priority(1) | |
@Provider | |
public class TimeFilter implements ContainerRequestFilter, ContainerResponseFilter { | |
@Override | |
public void filter(ContainerRequestContext reqContext) throws IOException { | |
System.out.println("-- TimeFilter request --"); | |
reqContext.setProperty("start-time", System.currentTimeMillis()); | |
} | |
@Override | |
public void filter(ContainerRequestContext reqContext, | |
ContainerResponseContext resContext) throws IOException { | |
System.out.println("-- TimeFilter response --"); | |
long startTime = (long) reqContext.getProperty("start-time"); | |
System.out.printf("Time taken for request %s: %s milli secs%n", | |
reqContext.getUriInfo().getPath(), | |
System.currentTimeMillis() - startTime | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mycompany.filterpriorities; | |
import javax.annotation.Priority; | |
import javax.ws.rs.container.ContainerRequestContext; | |
import javax.ws.rs.container.ContainerRequestFilter; | |
import javax.ws.rs.container.ContainerResponseContext; | |
import javax.ws.rs.container.ContainerResponseFilter; | |
import javax.ws.rs.core.MultivaluedMap; | |
import javax.ws.rs.core.UriInfo; | |
import javax.ws.rs.ext.Provider; | |
import java.io.IOException; | |
@Priority(2) | |
@Provider | |
public class LogFilter implements ContainerRequestFilter, ContainerResponseFilter { | |
@Override | |
public void filter(ContainerRequestContext reqContext) throws IOException { | |
System.out.println("-- req headers --"); | |
log(reqContext.getUriInfo(), reqContext.getHeaders()); | |
} | |
@Override | |
public void filter(ContainerRequestContext reqContext, | |
ContainerResponseContext resContext) throws IOException { | |
System.out.println("-- res headers --"); | |
log(reqContext.getUriInfo(), resContext.getHeaders()); | |
} | |
private void log(UriInfo uriInfo, MultivaluedMap<String, ?> headers) { | |
System.out.println("Path: " + uriInfo.getPath()); | |
headers.entrySet().forEach(h -> System.out.println(h.getKey() + ": " + h.getValue())); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PathParam; | |
@Path("/") | |
public class MyResource { | |
@GET | |
@Path("{path:.*}") | |
public String getResponse(@PathParam("path") String path) { | |
System.out.printf("creating response for '%s'%n", path); | |
return "dummy-response for " + path; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.ws.rs.ApplicationPath; | |
import javax.ws.rs.core.Application; | |
@ApplicationPath("") | |
public class ApplicationConfig extends Application { | |
} |
Ve programımızı derleyip payara sunucumuzundaki çıktılarımıza göz atalım.
Hiç yorum yok:
Yorum Gönder