Network Deployment (Distributed operating systems), v8.0 > End-to-end paths > Web services - RESTful services > Use JAX-RS context objects to obtain more information about requests > 4. Add context fields and parameters to obtain information about requests.
Obtaining HTTP headers using HttpHeaders objects
Use Java API for RESTful Web Services (JAX-RS), you can use the HttpHeaders object to access request headers.
By using an injected HttpHeaders object with the JAX-RS runtime environment, the HTTP request headers information is known and available for modification. The @javax.ws.rs.core.Context annotation indicates that a context object is injected. The javax.ws.rs.core.HttpHeaders interface is the interface of the object to inject.
Procedure
- Determine if your method signature can be modified. If we have created a JAX-RS specific resource method, you can usually modify the resource method signature and add parameters easily because the method does not have constraints on the method signature. However, if your JAX-RS resource method was developed by re-using code and adapting existing classes and methods to add JAX-RS functionality, there might be constraints on the method signature by previously existing code that calls the method. In this case, you might not be able to modify the method signature.
- If a resource method signature can be modified, add a javax.ws.rs.core.HttpHeaders parameter that is annotated with the @javax.ws.rs.core.Context annotation to the method. When the resource method is invoked, the JAX-RS runtime environment passes an object that implements the HttpHeaders object; for example:
@Path("/contextexample") public class RootResource { @GET public String getResource(@Context HttpHeaders //publib.boulder.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=/ Headers) { /* This calls a method on //publib.boulder.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=/ Headers. */ return "The client sent a header value of " + headers.getRequestHeaders().getFirst("CustomHeader") + " for CustomHeader"; } }
- If a resource method signature cannot be modified and the class is a root resource, add a javax.ws.rs.core.HttpHeaders field that is annotated with the @javax.ws.rs.core.Context annotation to the class. When the resource is instantiated for a request, an object that implements HttpHeaders is injected; for example:
@Path("/contextexample") public class RootResource { @Context HttpHeaders //publib.boulder.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=/ Headers; @GET public String getResource() { /* This calls a method on //publib.boulder.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=/ Headers. */ return "The client sent a header value of " + headers.getRequestHeaders().getFirst("CustomHeader") + " for CustomHeader"; } }
Results
The resource method uses information from the HTTP headers that is sent in the request to determine an appropriate response.
Example
The following example illustrates a resource that returns a different greeting depending on the setting of the Accept-Language header.
In the following code snippet, the @Context HttpHeaders parameter is added to the method signature. Notice that the @Context annotation and the type declaration are in the parameters list of the getGreeting method.
import java.util.List; import java.util.Locale; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; @Path("/contexttest") public class HelloWorldInMyLanguage { /** * @return Returns the string in the preferred language. */ @GET @Produces(MediaType.TEXT_PLAIN) public String getGreeting(@Context HttpHeaders //publib.boulder.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=/ Headers) { List <Locale> locales = //publib.boulder.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=/ Headers.getAcceptableLanguages(); if (locales.size() == 0) { return "Hello!"; } Locale locale = locales.get(0); if (locale.equals(Locale.FRENCH)) { return "Bonjour!"; } else if (locale.equals(Locale.GERMAN)) { return "Guten Tag!"; } else { return "Hello!"; } }Alternatively, you can choose to declare the @Context HttpHeaders //publib.boulder.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=/ Headers property as a field in the class; for example:
import java.util.List; import java.util.Locale; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; @Path("/contexttest") public class HelloWorldInMyLanguage { @Context HttpHeaders //publib.boulder.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=/ Headers; /** * @return Returns the string in the preferred language. */ @GET @Produces(MediaType.TEXT_PLAIN) public String getGreeting() { List <Locale> locales = //publib.boulder.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=/ Headers.getAcceptableLanguages(); if (locales.size() == 0) { return "Hello!"; } Locale locale = locales.get(0); if (locale.equals(Locale.FRENCH)) { return "Bonjour!"; } else if (locale.equals(Locale.GERMAN)) { return "Guten Tag!"; } else { return "Hello!"; } } }
Use JAX-RS context objects to obtain more information about requests
Obtaining information about URIs using UriInfo objects
Evaluate request preconditions using Request objects
Determine security information using SecurityContext objects
Related
Web services specifications and APIs