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 { | |
} |
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) { | |
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
@Priority(1) | |
@Provider | |
public class InvalidRequestFilter implements ContainerRequestFilter{ | |
@Override | |
public void filter(ContainerRequestContext requestContext) throws IOException { | |
Response resForInvalidRequest = kontrol(requestContext); | |
if(resForInvalidRequest!=null) | |
requestContext.abortWith(resForInvalidRequest); | |
} | |
private Response kontrol(ContainerRequestContext requestContext){ | |
UriInfo uriInfo=requestContext.getUriInfo(); | |
String path=uriInfo.getPath(); | |
if(path.equals("orders/history")){ | |
String msg = String.format("The feature is not yet supported: %s%n", path); | |
CacheControl cc=new CacheControl(); | |
cc.setNoStore(true); | |
Response response=Response.status(Response.Status.NOT_IMPLEMENTED) | |
.cacheControl(cc) | |
.entity(msg) | |
.build(); | |
return response; | |
} | |
return null; | |
} | |
} |
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 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.core.MultivaluedMap; | |
import javax.ws.rs.core.UriInfo; | |
import javax.ws.rs.ext.Provider; | |
@Priority(2) | |
@Provider | |
public class LogFilter implements ContainerRequestFilter, ContainerResponseFilter { | |
@Override | |
public void filter(ContainerRequestContext reqContext) throws IOException { | |
System.out.println("-- request info --"); | |
UriInfo uriInfo = reqContext.getUriInfo(); | |
log(uriInfo, reqContext.getHeaders()); | |
} | |
@Override | |
public void filter(ContainerRequestContext reqContext, | |
ContainerResponseContext resContext) throws IOException { | |
System.out.println("-- response info --"); | |
UriInfo uriInfo = reqContext.getUriInfo(); | |
log(uriInfo, resContext.getHeaders()); | |
} | |
private void log(UriInfo uriInfo, MultivaluedMap<String, ?> headers) { | |
System.out.println("Path: " + uriInfo.getPath()); | |
System.out.println("HEADERS:"); | |
headers.entrySet().forEach(h -> System.out.println(h.getKey() + ": " + h.getValue())); | |
} | |
} |
Umarım faydalı olmuştur.Görüşmek üzere.
Hiç yorum yok:
Yorum Gönder