Search in sources :

Example 6 with ContainerException

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

the class ResponseWriter method writeResponseStatusAndHeaders.

@Override
public OutputStream writeResponseStatusAndHeaders(final long contentLength, final ContainerResponse responseContext) throws ContainerException {
    this.responseContext.complete(responseContext);
    // first set the content length, so that if headers have an explicit value, it takes precedence over this one
    if (responseContext.hasEntity() && contentLength != -1 && contentLength < Integer.MAX_VALUE) {
        response.setContentLength((int) contentLength);
    }
    // Note that the writing of headers MUST be performed before
    // the invocation of sendError as on some Servlet implementations
    // modification of the response headers will have no effect
    // after the invocation of sendError.
    final MultivaluedMap<String, String> headers = getResponseContext().getStringHeaders();
    for (final Map.Entry<String, List<String>> e : headers.entrySet()) {
        final Iterator<String> it = e.getValue().iterator();
        if (!it.hasNext()) {
            continue;
        }
        final String header = e.getKey();
        if (response.containsHeader(header)) {
            // replace any headers previously set with values from Jersey container response.
            response.setHeader(header, it.next());
        }
        while (it.hasNext()) {
            response.addHeader(header, it.next());
        }
    }
    final String reasonPhrase = responseContext.getStatusInfo().getReasonPhrase();
    if (reasonPhrase != null) {
        response.setStatus(responseContext.getStatus(), reasonPhrase);
    } else {
        response.setStatus(responseContext.getStatus());
    }
    if (!responseContext.hasEntity()) {
        return null;
    } else {
        try {
            final OutputStream outputStream = response.getOutputStream();
            // so that any Servlet filters in the chain can still write to the response after us.
            return new NonCloseableOutputStreamWrapper(outputStream);
        } catch (final IOException e) {
            throw new ContainerException(e);
        }
    }
}
Also used : ContainerException(org.glassfish.jersey.server.ContainerException) OutputStream(java.io.OutputStream) List(java.util.List) IOException(java.io.IOException) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Map(java.util.Map)

Example 7 with ContainerException

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

the class ResponseWriter method failure.

@Override
public void failure(final Throwable error) {
    try {
        if (!response.isCommitted()) {
            try {
                if (configSetStatusOverSendError) {
                    response.reset();
                    //noinspection deprecation
                    response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Request failed.");
                } else {
                    response.sendError(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Request failed.");
                }
            } catch (final IllegalStateException ex) {
                // a race condition externally committing the response can still occur...
                LOGGER.log(Level.FINER, "Unable to reset failed response.", ex);
            } catch (final IOException ex) {
                throw new ContainerException(LocalizationMessages.EXCEPTION_SENDING_ERROR_RESPONSE(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Request failed."), ex);
            } finally {
                asyncExt.complete();
            }
        }
    } finally {
        requestTimeoutHandler.close();
        rethrow(error);
    }
}
Also used : ContainerException(org.glassfish.jersey.server.ContainerException) IOException(java.io.IOException)

Example 8 with ContainerException

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

the class JspTemplateProcessor method writeTo.

@Override
public void writeTo(final String templateReference, final Viewable viewable, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream out) throws IOException {
    if (!(viewable instanceof ResolvedViewable)) {
        // This should not happen with default MVC message body writer implementation
        throw new IllegalArgumentException(LocalizationMessages.ERROR_VIEWABLE_INCORRECT_INSTANCE());
    }
    // SPI could supply instance of ResolvedViewable but we would like to keep the backward
    // compatibility, so the cast is here.
    final ResolvedViewable resolvedViewable = (ResolvedViewable) viewable;
    final TracingLogger tracingLogger = TracingLogger.getInstance(containerRequestProvider.get().getPropertiesDelegate());
    if (tracingLogger.isLogEnabled(MvcJspEvent.JSP_FORWARD)) {
        tracingLogger.log(MvcJspEvent.JSP_FORWARD, templateReference, resolvedViewable.getModel());
    }
    final RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(templateReference);
    if (dispatcher == null) {
        throw new ContainerException(LocalizationMessages.NO_REQUEST_DISPATCHER_FOR_RESOLVED_PATH(templateReference));
    }
    final RequestDispatcher wrapper = new RequestDispatcherWrapper(dispatcher, getBasePath(), resolvedViewable);
    // OutputStream and Writer for HttpServletResponseWrapper.
    final ServletOutputStream responseStream = new ServletOutputStream() {

        @Override
        public void write(final int b) throws IOException {
            out.write(b);
        }
    };
    final PrintWriter responseWriter = new PrintWriter(new OutputStreamWriter(responseStream, getEncoding()));
    try {
        wrapper.forward(requestProviderRef.get().get(), new HttpServletResponseWrapper(responseProviderRef.get().get()) {

            @Override
            public ServletOutputStream getOutputStream() throws IOException {
                return responseStream;
            }

            @Override
            public PrintWriter getWriter() throws IOException {
                return responseWriter;
            }
        });
    } catch (final Exception e) {
        throw new ContainerException(e);
    } finally {
        responseWriter.flush();
    }
}
Also used : ResolvedViewable(org.glassfish.jersey.server.mvc.spi.ResolvedViewable) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) TracingLogger(org.glassfish.jersey.message.internal.TracingLogger) IOException(java.io.IOException) RequestDispatcher(javax.servlet.RequestDispatcher) IOException(java.io.IOException) ContainerException(org.glassfish.jersey.server.ContainerException) ContainerException(org.glassfish.jersey.server.ContainerException) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter)

Aggregations

ContainerException (org.glassfish.jersey.server.ContainerException)8 IOException (java.io.IOException)4 OutputStreamWriter (java.io.OutputStreamWriter)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)2 Response (javax.ws.rs.core.Response)2 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)2 TemplateException (freemarker.template.TemplateException)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 URI (java.net.URI)1 Charset (java.nio.charset.Charset)1 List (java.util.List)1 Pattern (java.util.regex.Pattern)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 ServletResponse (javax.servlet.ServletResponse)1