Search in sources :

Example 1 with ResponseWriter

use of org.glassfish.jersey.servlet.internal.ResponseWriter 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)

Example 2 with ResponseWriter

use of org.glassfish.jersey.servlet.internal.ResponseWriter in project jersey by jersey.

the class ServletContainer method doFilter.

private void doFilter(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain, final String requestURI, final String servletPath, final String queryString) throws IOException, ServletException {
    // if we match the static content regular expression lets delegate to
    // the filter chain to use the default container servlets & handlers
    final Pattern p = getStaticContentPattern();
    if (p != null && p.matcher(servletPath).matches()) {
        chain.doFilter(request, response);
        return;
    }
    if (filterContextPath != null) {
        if (!servletPath.startsWith(filterContextPath)) {
            throw new ContainerException(LocalizationMessages.SERVLET_PATH_MISMATCH(servletPath, filterContextPath));
        //TODO:
        //            } else if (servletPath.length() == filterContextPath.length()) {
        //                // Path does not end in a slash, may need to redirect
        //                if (webComponent.getResourceConfig().getFeature(ResourceConfig.FEATURE_REDIRECT)) {
        //                    URI l = UriBuilder.fromUri(request.getRequestURL().toString()).
        //                            path("/").
        //                            replaceQuery(queryString).build();
        //
        //                    response.setStatus(307);
        //                    response.setHeader("Location", l.toASCIIString());
        //                    return;
        //                } else {
        //                    requestURI += "/";
        //                }
        }
    }
    final URI baseUri;
    final URI requestUri;
    try {
        final UriBuilder absoluteUriBuilder = UriBuilder.fromUri(request.getRequestURL().toString());
        // depending on circumstances, use the correct path to replace in the absolute request URI
        final String pickedUrlMapping = pickUrlMapping(request.getRequestURL().toString(), filterUrlMappings);
        final String replacingPath = pickedUrlMapping != null ? pickedUrlMapping : (filterContextPath != null ? filterContextPath : "");
        baseUri = absoluteUriBuilder.replacePath(request.getContextPath()).path(replacingPath).path("/").build();
        requestUri = absoluteUriBuilder.replacePath(requestURI).replaceQuery(ContainerUtils.encodeUnsafeCharacters(queryString)).build();
    } catch (final IllegalArgumentException iae) {
        setResponseForInvalidUri(response, iae);
        return;
    }
    final ResponseWriter responseWriter = serviceImpl(baseUri, requestUri, request, response);
    if (webComponent.forwardOn404 && !response.isCommitted()) {
        boolean hasEntity = false;
        Response.StatusType status = null;
        if (responseWriter.responseContextResolved()) {
            final ContainerResponse responseContext = responseWriter.getResponseContext();
            hasEntity = responseContext.hasEntity();
            status = responseContext.getStatusInfo();
        }
        if (!hasEntity && status == Response.Status.NOT_FOUND) {
            // lets clear the response to OK before we forward to the next in the chain
            // as OK is the default set by servlet containers before filters/servlets do any work
            // so lets hide our footsteps and pretend we were never in the chain at all and let the
            // next filter or servlet return the 404 if they can't find anything to return
            //
            // We could add an optional flag to disable this step if anyone can ever find a case where
            // this causes a problem, though I suspect any problems will really be with downstream
            // servlets not correctly setting an error status if they cannot find something to return
            response.setStatus(HttpServletResponse.SC_OK);
            chain.doFilter(request, response);
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) ContainerResponse(org.glassfish.jersey.server.ContainerResponse) Response(javax.ws.rs.core.Response) ServletResponse(javax.servlet.ServletResponse) Pattern(java.util.regex.Pattern) ContainerResponse(org.glassfish.jersey.server.ContainerResponse) ContainerException(org.glassfish.jersey.server.ContainerException) ResponseWriter(org.glassfish.jersey.servlet.internal.ResponseWriter) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI)

Aggregations

ResponseWriter (org.glassfish.jersey.servlet.internal.ResponseWriter)2 IOException (java.io.IOException)1 URI (java.net.URI)1 PrivilegedActionException (java.security.PrivilegedActionException)1 Pattern (java.util.regex.Pattern)1 ServletException (javax.servlet.ServletException)1 ServletResponse (javax.servlet.ServletResponse)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Response (javax.ws.rs.core.Response)1 UriBuilder (javax.ws.rs.core.UriBuilder)1 HeaderValueException (org.glassfish.jersey.message.internal.HeaderValueException)1 ContainerException (org.glassfish.jersey.server.ContainerException)1 ContainerRequest (org.glassfish.jersey.server.ContainerRequest)1 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)1