Search in sources :

Example 36 with ContainerRequest

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));
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) MediaType(javax.ws.rs.core.MediaType) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Example 37 with ContainerRequest

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());
}
Also used : ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) ContainerResponse(org.glassfish.jersey.server.ContainerResponse) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Example 38 with ContainerRequest

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;
}
Also used : ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Resource(org.glassfish.jersey.server.model.Resource) Response(javax.ws.rs.core.Response) MediaType(javax.ws.rs.core.MediaType) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod)

Example 39 with ContainerRequest

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();
        }
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) AuthenticationFilterService(org.simbasecurity.api.service.thrift.AuthenticationFilterService) ActionDescriptor(org.simbasecurity.api.service.thrift.ActionDescriptor) THttpClient(org.apache.thrift.transport.THttpClient) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) TJSONProtocol(org.apache.thrift.protocol.TJSONProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) RequestData(org.simbasecurity.api.service.thrift.RequestData) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) THttpClient(org.apache.thrift.transport.THttpClient)

Aggregations

ContainerRequest (org.glassfish.jersey.server.ContainerRequest)39 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)17 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)14 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)10 Response (javax.ws.rs.core.Response)10 IOException (java.io.IOException)8 Resource (org.glassfish.jersey.server.model.Resource)8 URI (java.net.URI)7 ApplicationHandler (org.glassfish.jersey.server.ApplicationHandler)7 Test (org.junit.Test)7 MapPropertiesDelegate (org.glassfish.jersey.internal.MapPropertiesDelegate)6 MediaType (javax.ws.rs.core.MediaType)4 LoggingFeature (org.glassfish.jersey.logging.LoggingFeature)4 ContainerResponseWriter (org.glassfish.jersey.server.spi.ContainerResponseWriter)4 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 PropertiesDelegate (org.glassfish.jersey.internal.PropertiesDelegate)3 RequestContextBuilder (org.glassfish.jersey.server.RequestContextBuilder)3 Future (io.netty.util.concurrent.Future)2