use of org.glassfish.jersey.server.ContainerRequest in project jersey by jersey.
the class OptionsTest method testRequestNoType.
@Test
public void testRequestNoType() throws ExecutionException, InterruptedException {
ApplicationHandler application = new ApplicationHandler(new ResourceConfig(WadlResource.class));
final ContainerRequest request = RequestContextBuilder.from("/resource", "OPTIONS").build();
final ContainerResponse response = application.apply(request).get();
Assert.assertEquals(200, response.getStatus());
final MediaType type = response.getMediaType();
Assert.assertTrue(type.equals(MediaTypes.WADL_TYPE) || type.equals(MediaType.TEXT_HTML_TYPE) || type.equals(MediaType.TEXT_PLAIN));
}
use of org.glassfish.jersey.server.ContainerRequest in project jersey by jersey.
the class ResourcePathOverrideTest method testOverride.
@Test
public void testOverride() throws Exception {
ResourceConfig resourceConfig = new ResourceConfig(HelloResource.class);
Resource.Builder resourceBuilder = Resource.builder(HelloResource.class).path("hello2");
resourceBuilder.addChildResource("world").addMethod("GET").produces("text/plain").handledBy(new Inflector<ContainerRequestContext, String>() {
@Override
public String apply(ContainerRequestContext request) {
return "Hello World!";
}
});
final Resource resource = resourceBuilder.build();
resourceConfig.registerResources(resource);
ApplicationHandler app = createApplication(resourceConfig);
ContainerRequest request;
request = RequestContextBuilder.from("/hello", "GET").build();
assertEquals("Hello!", app.apply(request).get().getEntity());
request = RequestContextBuilder.from("/hello2", "GET").build();
assertEquals("Hello!", app.apply(request).get().getEntity());
request = RequestContextBuilder.from("/hello2/world", "GET").build();
final ContainerResponse response = app.apply(request).get();
assertEquals(200, response.getStatus());
assertEquals("Hello World!", response.getEntity());
}
use of org.glassfish.jersey.server.ContainerRequest in project jersey by jersey.
the class App method createApp.
/**
* Create example application resource configuration.
*
* @return initialized resource configuration of the example application.
*/
public static ResourceConfig createApp() {
MediaType[] jsonAndTextTypes = new MediaType[] { MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE };
final ResourceConfig resourceConfig = new ResourceConfig(ClipboardResource.class, ClipboardDataProvider.ApplicationJson.class, ClipboardDataProvider.TextPlain.class);
final Resource.Builder resourceBuilder = Resource.builder("echo");
ResourceMethod.Builder rmBuilder = resourceBuilder.addMethod("POST");
rmBuilder.consumes(jsonAndTextTypes).produces(jsonAndTextTypes).handledBy(new Inflector<ContainerRequestContext, Response>() {
@Override
public Response apply(ContainerRequestContext request) {
ClipboardData data = (request != null) ? ((ContainerRequest) request).readEntity(ClipboardData.class) : null;
return Response.ok(data).build();
}
});
resourceConfig.registerResources(resourceBuilder.build());
return resourceConfig;
}
use of org.glassfish.jersey.server.ContainerRequest in project simba-os by cegeka.
the class JerseyBasicAuthenticationFilter method filter.
@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
ContainerRequest containerRequest = (ContainerRequest) containerRequestContext.getRequest();
Map<String, String> requestParameters = toMap(containerRequestContext.getUriInfo().getQueryParameters());
List<String> auth = containerRequest.getRequestHeader("authorization");
if (auth == null || auth.isEmpty()) {
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
String[] credentials = decode(auth.get(0));
requestParameters.put(AuthenticationConstants.USERNAME, credentials[0]);
requestParameters.put(AuthenticationConstants.PASSWORD, credentials[1]);
RequestData requestData = new RequestData(requestParameters, toMap(containerRequest.getRequestHeaders()), containerRequest.getAbsolutePath().toString(), simbaWebURL, null, /* SSO Token */
null, /* Client IP */
false, false, false, false, false, containerRequest.getMethod(), RequestUtil.HOST_SERVER_NAME, null, null);
THttpClient tHttpClient = null;
try {
tHttpClient = new THttpClient(simbaWebURL + "/authenticationService");
TProtocol tProtocol = new TJSONProtocol(tHttpClient);
AuthenticationFilterService.Client authenticationClient = new AuthenticationFilterService.Client(tProtocol);
ActionDescriptor actionDescriptor = authenticationClient.processRequest(requestData, "wsLoginChain");
if (!actionDescriptor.getActionTypes().contains(ActionType.DO_FILTER_AND_SET_PRINCIPAL)) {
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
} catch (Exception e) {
e.printStackTrace();
throw new WebApplicationException(e, Response.Status.UNAUTHORIZED);
} finally {
if (tHttpClient != null) {
tHttpClient.close();
}
}
}
Aggregations