use of org.glassfish.jersey.server.ContainerRequest in project jersey by jersey.
the class EncodingFilterTest method testNoEntity.
@Test
public void testNoEntity() throws IOException {
EncodingFilter filter = initializeAndGetFilter();
ContainerRequest request = RequestContextBuilder.from("/resource", "GET").header(HttpHeaders.ACCEPT_ENCODING, "gzip").build();
ContainerResponse response = new ContainerResponse(request, Response.ok().build());
filter.filter(request, response);
assertNull(response.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING));
assertNull(response.getHeaders().getFirst(HttpHeaders.VARY));
}
use of org.glassfish.jersey.server.ContainerRequest in project jersey by jersey.
the class PropertyValidationTest method assertApply.
private void assertApply(int responseStatus, ResourceConfig resourceConfig, URI uri) throws InterruptedException, ExecutionException {
final ApplicationHandler applicationHandler = new ApplicationHandler(resourceConfig);
final ContainerRequest requestContext = new ContainerRequest(uri, uri, "POST", null, new MapPropertiesDelegate());
final ContainerResponse containerResponse = applicationHandler.apply(requestContext).get();
assertEquals(responseStatus, containerResponse.getStatus());
}
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());
}
use of org.glassfish.jersey.server.ContainerRequest in project jersey by jersey.
the class EncodingFilterTest method testEncoding.
private void testEncoding(String expected, String... accepted) throws IOException {
EncodingFilter filter = initializeAndGetFilter();
RequestContextBuilder builder = RequestContextBuilder.from("/resource", "GET");
for (String a : accepted) {
builder.header(HttpHeaders.ACCEPT_ENCODING, a);
}
ContainerRequest request = builder.build();
ContainerResponse response = new ContainerResponse(request, Response.ok("OK!").build());
filter.filter(request, response);
if (response.getStatus() != 200) {
throw new WebApplicationException(Response.status(response.getStatus()).build());
}
assertEquals(expected, response.getHeaderString(HttpHeaders.CONTENT_ENCODING));
assertEquals(HttpHeaders.ACCEPT_ENCODING, response.getHeaderString(HttpHeaders.VARY));
}
use of org.glassfish.jersey.server.ContainerRequest in project jersey by jersey.
the class JdkHttpHandlerContainer method handle.
@Override
public void handle(final HttpExchange exchange) throws IOException {
/**
* This is a URI that contains the path, query and fragment components.
*/
URI exchangeUri = exchange.getRequestURI();
/**
* The base path specified by the HTTP context of the HTTP handler. It
* is in decoded form.
*/
String decodedBasePath = exchange.getHttpContext().getPath();
// Ensure that the base path ends with a '/'
if (!decodedBasePath.endsWith("/")) {
if (decodedBasePath.equals(exchangeUri.getPath())) {
/**
* This is an edge case where the request path does not end in a
* '/' and is equal to the context path of the HTTP handler.
* Both the request path and base path need to end in a '/'
* Currently the request path is modified.
*
* TODO support redirection in accordance with resource configuration feature.
*/
exchangeUri = UriBuilder.fromUri(exchangeUri).path("/").build();
}
decodedBasePath += "/";
}
/*
* The following is madness, there is no easy way to get the complete
* URI of the HTTP request!!
*
* TODO this is missing the user information component, how can this be obtained?
*/
final boolean isSecure = exchange instanceof HttpsExchange;
final String scheme = isSecure ? "https" : "http";
final URI baseUri = getBaseUri(exchange, decodedBasePath, scheme);
final URI requestUri = getRequestUri(exchange, baseUri);
final ResponseWriter responseWriter = new ResponseWriter(exchange);
final ContainerRequest requestContext = new ContainerRequest(baseUri, requestUri, exchange.getRequestMethod(), getSecurityContext(exchange.getPrincipal(), isSecure), new MapPropertiesDelegate());
requestContext.setEntityStream(exchange.getRequestBody());
requestContext.getHeaders().putAll(exchange.getRequestHeaders());
requestContext.setWriter(responseWriter);
try {
appHandler.handle(requestContext);
} finally {
// if the response was not committed yet by the JerseyApplication
// then commit it and log warning
responseWriter.closeAndLogWarning();
}
}
Aggregations