Search in sources :

Example 1 with ContainerRequest

use of org.glassfish.jersey.server.ContainerRequest in project jersey by jersey.

the class TraceSupportTest method configure.

@Override
protected Application configure() {
    ResourceConfig config = new ResourceConfig(TracingResource.class);
    config.register(new LoggingFeature(LOGGER, LoggingFeature.Verbosity.PAYLOAD_ANY));
    final Resource.Builder resourceBuilder = Resource.builder(ROOT_PATH_PROGRAMMATIC);
    resourceBuilder.addMethod(TRACE.NAME).handledBy(new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext request) {
            if (request == null) {
                return Response.noContent().build();
            } else {
                return Response.ok(stringify((ContainerRequest) request), MediaType.TEXT_PLAIN).build();
            }
        }
    });
    return config.registerResources(resourceBuilder.build());
}
Also used : Response(javax.ws.rs.core.Response) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) LoggingFeature(org.glassfish.jersey.logging.LoggingFeature) Resource(org.glassfish.jersey.server.model.Resource) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ResourceConfig(org.glassfish.jersey.server.ResourceConfig)

Example 2 with ContainerRequest

use of org.glassfish.jersey.server.ContainerRequest in project jersey by jersey.

the class GrizzlyHttpContainer method service.

@Override
public void service(final Request request, final Response response) {
    final ResponseWriter responseWriter = new ResponseWriter(response, configSetStatusOverSendError);
    try {
        logger.debugLog("GrizzlyHttpContainer.service(...) started");
        URI baseUri = getBaseUri(request);
        URI requestUri = getRequestUri(request);
        final ContainerRequest requestContext = new ContainerRequest(baseUri, requestUri, request.getMethod().getMethodString(), getSecurityContext(request), new GrizzlyRequestPropertiesDelegate(request));
        requestContext.setEntityStream(request.getInputStream());
        for (final String headerName : request.getHeaderNames()) {
            requestContext.headers(headerName, request.getHeaders(headerName));
        }
        requestContext.setWriter(responseWriter);
        requestContext.setRequestScopedInitializer(injectionManager -> {
            injectionManager.<Ref<Request>>getInstance(RequestTYPE).set(request);
            injectionManager.<Ref<Response>>getInstance(ResponseTYPE).set(response);
        });
        appHandler.handle(requestContext);
    } finally {
        logger.debugLog("GrizzlyHttpContainer.service(...) finished");
    }
}
Also used : Response(org.glassfish.grizzly.http.server.Response) ContainerResponse(org.glassfish.jersey.server.ContainerResponse) ContainerResponseWriter(org.glassfish.jersey.server.spi.ContainerResponseWriter) Request(org.glassfish.grizzly.http.server.Request) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) URI(java.net.URI)

Example 3 with ContainerRequest

use of org.glassfish.jersey.server.ContainerRequest in project jersey by jersey.

the class JettyHttpContainer method handle.

@Override
public void handle(final String target, final Request request, final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse) throws IOException, ServletException {
    final Response response = Response.getResponse(httpServletResponse);
    final ResponseWriter responseWriter = new ResponseWriter(request, response, configSetStatusOverSendError);
    final URI baseUri = getBaseUri(request);
    final URI requestUri = getRequestUri(request, baseUri);
    try {
        final ContainerRequest requestContext = new ContainerRequest(baseUri, requestUri, request.getMethod(), getSecurityContext(request), new MapPropertiesDelegate());
        requestContext.setEntityStream(request.getInputStream());
        final Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            final String headerName = headerNames.nextElement();
            String headerValue = request.getHeader(headerName);
            requestContext.headers(headerName, headerValue == null ? "" : headerValue);
        }
        requestContext.setWriter(responseWriter);
        requestContext.setRequestScopedInitializer(injectionManager -> {
            injectionManager.<Ref<Request>>getInstance(REQUEST_TYPE).set(request);
            injectionManager.<Ref<Response>>getInstance(RESPONSE_TYPE).set(response);
        });
        // Mark the request as handled before generating the body of the response
        request.setHandled(true);
        appHandler.handle(requestContext);
    } catch (final Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : Response(org.eclipse.jetty.server.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) ContainerResponse(org.glassfish.jersey.server.ContainerResponse) MapPropertiesDelegate(org.glassfish.jersey.internal.MapPropertiesDelegate) ContainerResponseWriter(org.glassfish.jersey.server.spi.ContainerResponseWriter) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) URI(java.net.URI) ServletException(javax.servlet.ServletException) URISyntaxException(java.net.URISyntaxException) ContainerException(org.glassfish.jersey.server.ContainerException) IOException(java.io.IOException)

Example 4 with ContainerRequest

use of org.glassfish.jersey.server.ContainerRequest in project jersey by jersey.

the class SimpleContainer method handle.

@Override
public void handle(final Request request, final Response response) {
    final ResponseWriter responseWriter = new ResponseWriter(response, scheduler);
    final URI baseUri = getBaseUri(request);
    final URI requestUri = getRequestUri(request, baseUri);
    try {
        final ContainerRequest requestContext = new ContainerRequest(baseUri, requestUri, request.getMethod(), getSecurityContext(request), new MapPropertiesDelegate());
        requestContext.setEntityStream(request.getInputStream());
        for (final String headerName : request.getNames()) {
            requestContext.headers(headerName, request.getValue(headerName));
        }
        requestContext.setWriter(responseWriter);
        requestContext.setRequestScopedInitializer(injectionManager -> {
            injectionManager.<Ref<Request>>getInstance(RequestTYPE).set(request);
            injectionManager.<Ref<Response>>getInstance(ResponseTYPE).set(response);
        });
        appHandler.handle(requestContext);
    } catch (final Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        if (!responseWriter.isSuspended()) {
            close(response);
        }
    }
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) Response(org.simpleframework.http.Response) MapPropertiesDelegate(org.glassfish.jersey.internal.MapPropertiesDelegate) ContainerResponseWriter(org.glassfish.jersey.server.spi.ContainerResponseWriter) Request(org.simpleframework.http.Request) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) ContainerException(org.glassfish.jersey.server.ContainerException) IOException(java.io.IOException)

Example 5 with ContainerRequest

use of org.glassfish.jersey.server.ContainerRequest in project jersey by jersey.

the class WebComponent method serviceImpl.

/**
     * Dispatch client requests to a resource class.
     *
     * @param baseUri         the base URI of the request.
     * @param requestUri      the URI of the request.
     * @param servletRequest  the {@link javax.servlet.http.HttpServletRequest} object that
     *                        contains the request the client made to
     *                        the Web component.
     * @param servletResponse the {@link javax.servlet.http.HttpServletResponse} object that
     *                        contains the response the Web component returns
     *                        to the client.
     * @return returns {@link ResponseWriter}, Servlet's {@link org.glassfish.jersey.server.spi.ContainerResponseWriter}
     *         implementation, into which processed request response was written to.
     * @throws java.io.IOException            if an input or output error occurs
     *                                        while the Web component is handling the
     *                                        HTTP request.
     * @throws javax.servlet.ServletException if the HTTP request cannot be handled.
     */
/* package */
ResponseWriter serviceImpl(final URI baseUri, final URI requestUri, final HttpServletRequest servletRequest, final HttpServletResponse servletResponse) throws ServletException, IOException {
    final ResponseWriter responseWriter = new ResponseWriter(forwardOn404, configSetStatusOverSendError, servletResponse, asyncExtensionDelegate.createDelegate(servletRequest, servletResponse), backgroundTaskScheduler);
    try {
        final ContainerRequest requestContext = new ContainerRequest(baseUri, requestUri, servletRequest.getMethod(), getSecurityContext(servletRequest), new ServletPropertiesDelegate(servletRequest));
        initContainerRequest(requestContext, servletRequest, servletResponse, responseWriter);
        appHandler.handle(requestContext);
    } catch (final HeaderValueException hve) {
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.log(Level.FINE, LocalizationMessages.HEADER_VALUE_READ_FAILED(), hve);
        }
        final Response.Status status = Response.Status.BAD_REQUEST;
        if (configSetStatusOverSendError) {
            servletResponse.reset();
            //noinspection deprecation
            servletResponse.setStatus(status.getStatusCode(), status.getReasonPhrase());
        } else {
            servletResponse.sendError(status.getStatusCode(), status.getReasonPhrase());
        }
    } catch (final Exception e) {
        throw new ServletException(e);
    }
    return responseWriter;
}
Also used : ServletException(javax.servlet.ServletException) HeaderValueException(org.glassfish.jersey.message.internal.HeaderValueException) ResponseWriter(org.glassfish.jersey.servlet.internal.ResponseWriter) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ServletException(javax.servlet.ServletException) HeaderValueException(org.glassfish.jersey.message.internal.HeaderValueException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException)

Aggregations

ContainerRequest (org.glassfish.jersey.server.ContainerRequest)39 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)17 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)14 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)10 Response (javax.ws.rs.core.Response)10 IOException (java.io.IOException)8 Resource (org.glassfish.jersey.server.model.Resource)8 URI (java.net.URI)7 ApplicationHandler (org.glassfish.jersey.server.ApplicationHandler)7 Test (org.junit.Test)7 MapPropertiesDelegate (org.glassfish.jersey.internal.MapPropertiesDelegate)6 MediaType (javax.ws.rs.core.MediaType)4 LoggingFeature (org.glassfish.jersey.logging.LoggingFeature)4 ContainerResponseWriter (org.glassfish.jersey.server.spi.ContainerResponseWriter)4 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 PropertiesDelegate (org.glassfish.jersey.internal.PropertiesDelegate)3 RequestContextBuilder (org.glassfish.jersey.server.RequestContextBuilder)3 Future (io.netty.util.concurrent.Future)2