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);
}
}
}
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);
}
}
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();
}
}
Aggregations