Http service context in java

Java HttpContext tutorial with examples

HttpContext represents a mapping between the root URI path of a web service to a HttpHandler which is invoked to handle requests destined for that path on the associated container.

Introduction

HttpContext represents a mapping between the root URI path of a web service to a HttpHandler which is invoked to handle requests destined for that path on the associated container.

Container provides the implementation for this and it matches web service requests to corresponding HttpContext objects.

Example

The following code shows how to use HttpContext from javax.xml.ws.spi.http.

import java.util.Set; import javax.xml.ws.spi.http.HttpContext; import javax.xml.ws.spi.http.HttpHandler; public class WSHttpContext extends HttpContext private String path; public WSHttpContext(String path) < this.path = path; >/* w w w . d e m o 2 s . c om */ @Override public Object getAttribute(String name) < return null; > @Override public SetString> getAttributeNames() < return null; > @Override public String getPath() < return path; > protected HttpHandler getHandler() < return handler; > >
import java.io.IOException; import java.util.Collections; import java.util.Set; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.ws.spi.http.HttpContext; public class HttpContextImpl extends HttpContext private final HttpContextServlet servlet; private final String path; public HttpContextImpl(HttpContextServlet servlet, String path) < this.servlet = servlet; this.path = path; >// w ww . d e m o 2 s . co m @Override public String getPath() < return servlet.getMountPath() + path; > @Override public Object getAttribute(String name) < return null; > @Override public SetString> getAttributeNames() < return Collections.emptySet(); > void dispose() < servlet.unregister(path, this); > void service(HttpServletRequest request, HttpServletResponse response) throws IOException < handler.handle(new HttpExchangeImpl(this, request, response)); > >
import java.util.Set; import javax.xml.ws.spi.http.HttpContext; import javax.xml.ws.spi.http.HttpHandler; import org.eclipse.jetty.jaxws.JAXWSHandler.JettyAttributeInfo; public class JettyHttpContext extends HttpContext final JettyAttributeInfo info; final String context; final String path; JettyHttpContext(JettyAttributeInfo info, String context, String path) < this.info = info; this.context = context; this.path = path; >/*w w w . d e m o 2 s . c o m */ @Override public Object getAttribute(String name) < return info.getAttribute(name); > @Override public SetString> getAttributeNames() < return info.getAttributeNames(); > @Override public String getPath() < return path; > public String getContext() < return context; > public HttpHandler getHandler() < return this.handler; > >

  • Java ServiceDelegate tutorial with examples
  • Java ServiceDelegate getPort(QName portName, Class serviceEndpointInterface)
  • Java ServiceDelegate getPort(Class serviceEndpointInterface, WebServiceFeature. features)
  • Java HttpContext tutorial with examples
  • Java HttpExchange tutorial with examples
  • Java javax.xml.ws.wsaddressing W3CEndpointReference
  • Java W3CEndpointReference tutorial with examples

demo2s.com | Email: | Demo Source and Support. All rights reserved.

Источник

Http service context in java

HttpContext represents a mapping between the root URI path of an application to a HttpHandler which is invoked to handle requests destined for that path on the associated HttpServer or HttpsServer. HttpContext instances are created by the create methods in HttpServer and HttpsServer A chain of Filter objects can be added to a HttpContext. All exchanges processed by the context can be pre- and post-processed by each Filter in the chain.

Constructor Summary

Method Summary

returns a mutable Map, which can be used to pass configuration and other data to Filter modules and to the context’s exchange handler.

Methods inherited from class java.lang.Object

Constructor Detail

HttpContext

Method Detail

getHandler

setHandler

getPath

getServer

getAttributes

public abstract MapString,Object> getAttributes()

returns a mutable Map, which can be used to pass configuration and other data to Filter modules and to the context’s exchange handler. Every attribute stored in this Map will be visible to every HttpExchange processed by this context

getFilters

returns this context’s list of Filters. This is the actual list used by the server when dispatching requests so modifications to this list immediately affect the the handling of exchanges.

setAuthenticator

public abstract Authenticator setAuthenticator(Authenticator auth)

Sets the Authenticator for this HttpContext. Once an authenticator is establised on a context, all client requests must be authenticated, and the given object will be invoked to validate each request. Each call to this method replaces any previous value set.

getAuthenticator

Источник

Java Guides

Although HTTP headers can be injected using the @HeaderParam annotation, JAX-RS also provides the facility of injecting an instance of the HttpHeaders interface (as an instance variable or method parameter). This is useful when you want to iterate over all possible headers rather than injecting a specific header value by name

@Path("testinject") public class InjectURIDetails< //localhost:8080//testinject/httpheaders @GET @Path("httpheaders") public void test(@Context HttpHeaders headers)< System.out.println("ALL headers -- "+ headers.getRequestHeaders().toString()); System.out.println("'Accept' header -- "+ headers.getHeaderString("Accept")); System.out.println("'TestCookie' value -- "+ headers.getCookies().get("TestCookie").getValue()); > > @GET @Produces(MediaType.APPLICATION_JSON) public Response getAllHttpHeaders(final @Context HttpHeaders httpHeaders)< return Response.ok(httpHeaders.getRequestHeaders()).build(); > @GET @Path("/") @Produces(MediaType.APPLICATION_JSON) public Response getSpecifiedHeader(final @PathParam("header-param") String header_param, final @Context HttpHeaders httpHeaders)< return Response.ok(httpHeaders.getRequestHeader(header_param)).build(); >

3. HTTP URI details

UriInfo is another interface whose instance can be injected by JAX-RS (as an instance variable or method parameter). Use this instance to fetch additional details related to the request URI and its parameters (query, path)

@Path("testinject") public class InjectURIDetails< //localhost:8080//testinject/uriinfo @GET @Path("uriinfo") public void test(@Context UriInfo uriDetails)< System.out.println("ALL query parameters -- "+ uriDetails.getQueryParameters().toString()); System.out.println("'id' query parameter -- "+ uriDetails.getQueryParameters.get("id")); System.out.println("Complete URI -- "+ uriDetails.getRequestUri()); > >

4. HttpServletRequest Details

@Path("/remote-address") public class HttpServletRequestResource < @GET @Produces(MediaType.APPLICATION_JSON) public Response getRemoteAddress(final @Context HttpServletRequest httpServletRequest)< return Response.ok(httpServletRequest.getRemoteAddr()).build(); > >

5. More examples using @Context Annotations

@Path("/resource-context") public class ResourceContextResource < @GET @Path("/add") @Produces(MediaType.APPLICATION_JSON) public Response get(final @Context ResourceContext resourceContext, final @Context UriInfo uriInfo) < final CalculatorResource calculatorResource = resourceContext.getResource(CalculatorResource.class); int x = Integer.valueOf(uriInfo.getQueryParameters().getFirst("x")); int y = Integer.valueOf(uriInfo.getQueryParameters().getFirst("y")); return Response.ok(calculatorResource.add(x, y)).build(); > >
@Path("/security-context") public class SecurityContextResource < @GET @Produces(MediaType.APPLICATION_JSON) public Response sayHello(final @Context SecurityContext securityContext) < return Response.ok(securityContext.isUserInRole("guest")).build(); > >
@Path("servlet-config") public class ServletConfigResource < @GET @Produces(MediaType.APPLICATION_JSON) public Response getServletName(final @Context ServletConfig servletConfig)< return Response.ok(servletConfig.getServletName()).build(); > >
@Path("servlet-context") public class ServletContextResource < @GET @Produces(MediaType.APPLICATION_JSON) public Response getContextPath(final @Context ServletContext servletContext) < return Response.ok(servletContext.getContextPath()).build(); > >

6. Conclusion

This post explained the important @Context JAX-RS annotation.
Learn more about Jersey Rest framework on Jersey Rest Developer Guide.
All the code of this article is available over on Github. This is a Maven-based project, so it should be easy to import and run as it is.

Источник

Читайте также:  Version java windows 10 64 bit
Оцените статью