org.glassfish.jersey.server.ContainerException Java Examples

The following examples show how to use org.glassfish.jersey.server.ContainerException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpHandlerContainer.java    From metrics with Apache License 2.0 6 votes vote down vote up
@Override
public OutputStream writeResponseStatusAndHeaders(final long contentLength, final ContainerResponse context)
        throws ContainerException {
    final MultivaluedMap<String, String> responseHeaders = context.getStringHeaders();
    final Headers serverHeaders = exchange.getResponseHeaders();
    for (final Map.Entry<String, List<String>> e : responseHeaders.entrySet()) {
        for (final String value : e.getValue()) {
            serverHeaders.add(e.getKey(), value);
        }
    }

    try {
        if (context.getStatus() == Response.Status.NO_CONTENT.getStatusCode()) {
            // Work around bug in LW HTTP server
            // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6886436
            exchange.sendResponseHeaders(context.getStatus(), -1);
        } else {
            exchange.sendResponseHeaders(context.getStatus(),
                    getResponseLength(contentLength));
        }
    } catch (final IOException ioe) {
        throw new ContainerException(LocalizationMessages.ERROR_RESPONSEWRITER_WRITING_HEADERS(), ioe);
    }

    return exchange.getResponseBody();
}
 
Example #2
Source File: UndertowResponseWriter.java    From jweb-cms with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public OutputStream writeResponseStatusAndHeaders(long contentLength, ContainerResponse responseContext) throws ContainerException {
    if (error != null) {
        exchange.setStatusCode(500);
        exchange.getResponseHeaders().add(new HttpString("Content-Type"), "text/plain");
        exchange.getResponseSender().send(Exceptions.stackTrace(error));
        return null;
    } else {
        exchange.startBlocking();
        exchange.setStatusCode(responseContext.getStatus());
        HeaderMap responseHeaders = exchange.getResponseHeaders();
        responseContext.getStringHeaders().forEach((key, value) -> {
            for (String v : value) {
                responseHeaders.add(new HttpString(key), v);
            }
        });
        exchange.setResponseContentLength(contentLength);
        return exchange.getOutputStream();
    }
}
 
Example #3
Source File: HttpHandlerContainer.java    From metrics with Apache License 2.0 5 votes vote down vote up
/**
 * Rethrow the original exception as required by JAX-RS, 3.3.4
 *
 * @param error throwable to be re-thrown
 */
private void rethrow(final Throwable error) {
    if (error instanceof RuntimeException) {
        throw (RuntimeException) error;
    } else {
        throw new ContainerException(error);
    }
}
 
Example #4
Source File: DefaultContainerResponseWriter.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public OutputStream writeResponseStatusAndHeaders(final long contentLength, final ContainerResponse responseContext)
        throws ContainerException {

    if (!stateUpdater.compareAndSet(this, STATE_REQUEST_HANDLING, STATE_RESPONSE_WRITING)) {
        // Request has been cancelled so we do not send a response and return no OutputStream for Jersey to write to
        return null;
    }

    final Publisher<Buffer> content = getResponseBufferPublisher(request);
    // contentLength is >= 0 if the entity content length in bytes is known to Jersey, otherwise -1
    if (content != null) {
        sendResponse(UNKNOWN_RESPONSE_LENGTH, content, responseContext);
        return null;
    } else if (contentLength == 0 || isHeadRequest()) {
        sendResponse(contentLength, null, responseContext);
        return null;
    } else if (contentLength > 0) {
        // Jersey has buffered the full response body: bypass streaming response and use an optimized path instead
        return new BufferedResponseOutputStream(serviceCtx.executionContext().bufferAllocator(),
                buf -> sendResponse(contentLength, Publisher.from(buf), responseContext));
    }

    // OIO adapted streaming response of unknown length
    final ConnectableBufferOutputStream os = new ConnectableBufferOutputStream(
            serviceCtx.executionContext().bufferAllocator());
    sendResponse(contentLength, os.connect(), responseContext);
    return new CopyingOutputStream(os);
}
 
Example #5
Source File: JRestlessHandlerContainer.java    From jrestless with Apache License 2.0 5 votes vote down vote up
public void close() {
	if (closed.compareAndSet(false, true)) {
		try {
			responseWriter.writeResponse(statusType, headers, entityOutputStream);
		} catch (IOException e) {
			LOG.error("failed to write response", e);
			throw new ContainerException(e);
		}
	} else {
		LOG.warn("request has already been closed");
	}
}
 
Example #6
Source File: TenacityContainerExceptionMapper.java    From tenacity with Apache License 2.0 5 votes vote down vote up
@Override
public Response toResponse(ContainerException exception) {
    if (TenacityExceptionMapper.isTenacityException(exception.getCause())) {
        return Response.status(statusCode).build();
    } else {
        return Response.serverError().build();
    }
}
 
Example #7
Source File: MockContainerResponseWriter.java    From jweb-cms with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public OutputStream writeResponseStatusAndHeaders(long contentLength, ContainerResponse responseContext) throws ContainerException {
    requestBuilder.response = responseContext;
    return output;
}