Overriding HTTP methods

Due to security reasons, some corporate firewalls (HTTP proxies) support only the POST and GET methods. This restriction forces a REST API client to use only those HTTP methods that are allowed through the firewall. RESTful web API implementations can work around this restriction by letting the client override the HTTP method via the custom X-HTTP-Method-Override HTTP header. The REST API client can specify an X-HTTP-Method-Override request header with a string value containing either PUT, PATCH, or DELETE. When the server gets the request, the server replaces the POST method call with the method string value set for X-HTTP-Method-Override.

JAX-RS allows you to implement this behavior via prematching javax.ws.rs.container.ContainerRequestFilter. To learn more about ContainerRequestFilter, refer to the Understanding the filters and interceptors in JAX-RS section in Chapter 4, Advanced Features in the JAX-RS APIs:

//Other imports are omitted for brevity 
import javax.ws.rs.container.PreMatching; 
@Provider 
@PreMatching 
// A provider impl for handling the X-Http-Method-Override header  
public class HttpOverride implements ContainerRequestFilter { 
 
    public void filter(ContainerRequestContext ctx) { 
        String method = ctx.getHeaderString 
            ("X-Http-Method-Override"); 
        //override header should only be accepted on POST 
        if (method != null  
            && ctx.getMethod().equals("POST")) 
        //Set the method to the X-HTTP-Method-Override header value 
            ctx.setMethod(method); 
    } 
} 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.247.125