Search in sources :

Example 36 with ResourceConfig

use of org.glassfish.jersey.server.ResourceConfig in project jersey by jersey.

the class MultiPartTest method testMultiPartResource.

@Test
public void testMultiPartResource() throws Exception {
    final ResourceConfig resourceConfig = new ResourceConfig(MultiPartResource.class).register(new MultiPartFeature());
    final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig);
    Client c = ClientBuilder.newClient(new ClientConfig().register(MultiPartFeature.class));
    final Response response = c.target(baseUri).path("/multipart-simple").request().buildGet().invoke();
    MultiPart result = response.readEntity(MultiPart.class);
    System.out.println("RESULT = " + result);
    checkEntity("This is the only segment", (BodyPartEntity) result.getBodyParts().get(0).getEntity());
    server.shutdownNow();
}
Also used : Response(javax.ws.rs.core.Response) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) MultiPartFeature(org.glassfish.jersey.media.multipart.MultiPartFeature) HttpServer(org.glassfish.grizzly.http.server.HttpServer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) Test(org.junit.Test)

Example 37 with ResourceConfig

use of org.glassfish.jersey.server.ResourceConfig in project jersey by jersey.

the class PackageScanningTest method testSimpleResource.

@Test
public void testSimpleResource() throws Exception {
    final ResourceConfig resourceConfig = new ResourceConfig().packages(SimpleResource.class.getPackage().getName());
    final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig);
    _testScannedResources(server);
}
Also used : HttpServer(org.glassfish.grizzly.http.server.HttpServer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Example 38 with ResourceConfig

use of org.glassfish.jersey.server.ResourceConfig in project jersey by jersey.

the class ResourceBundleTest method testBadResource.

@Test
public void testBadResource() throws Exception {
    final ResourceConfig resourceConfig = new ResourceConfig(BadResource.class);
    ByteArrayOutputStream logOutput = new ByteArrayOutputStream();
    Handler logHandler = new StreamHandler(logOutput, new SimpleFormatter());
    GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig, false);
    // TODO: there should be a better way to get the log output!
    final Enumeration<String> loggerNames = LogManager.getLogManager().getLoggerNames();
    while (loggerNames.hasMoreElements()) {
        String name = loggerNames.nextElement();
        if (name.startsWith("org.glassfish")) {
            LogManager.getLogManager().getLogger(Errors.class.getName()).addHandler(logHandler);
        }
    }
    GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig, false);
    logOutput.flush();
    final String logOutputAsString = logOutput.toString();
    Assert.assertFalse(logOutputAsString.contains("[failed to localize]"));
    Assert.assertTrue(logOutputAsString.contains("BadResource"));
}
Also used : StreamHandler(java.util.logging.StreamHandler) SimpleFormatter(java.util.logging.SimpleFormatter) StreamHandler(java.util.logging.StreamHandler) Handler(java.util.logging.Handler) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 39 with ResourceConfig

use of org.glassfish.jersey.server.ResourceConfig in project jersey by jersey.

the class SseTest method testSse.

@Test
public void testSse() throws Exception {
    final ResourceConfig resourceConfig = new ResourceConfig(SseResource.class, SseFeature.class);
    final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig);
    Client c = ClientBuilder.newClient();
    c.register(SseFeature.class);
    final List<String> data = new LinkedList<String>();
    final CountDownLatch latch = new CountDownLatch(2);
    final EventSource eventSource = new EventSource(c.target(baseUri).path("/sse")) {

        @Override
        public void onEvent(InboundEvent event) {
            try {
                data.add(event.readData());
                latch.countDown();
            } catch (ProcessingException e) {
            // ignore
            }
        }
    };
    assertTrue(latch.await(2, TimeUnit.SECONDS));
    eventSource.close();
    assertEquals(2, data.size());
    server.shutdownNow();
}
Also used : EventSource(org.glassfish.jersey.media.sse.EventSource) InboundEvent(org.glassfish.jersey.media.sse.InboundEvent) HttpServer(org.glassfish.grizzly.http.server.HttpServer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Client(javax.ws.rs.client.Client) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 40 with ResourceConfig

use of org.glassfish.jersey.server.ResourceConfig in project jersey by jersey.

the class AbstractJsonOsgiIntegrationTest method testJson.

@Test
public void testJson() throws Exception {
    final Feature jsonProviderFeature = getJsonProviderFeature();
    final Client client = ClientBuilder.newClient();
    final ResourceConfig resourceConfig = new ResourceConfig(JsonResource.class);
    if (jsonProviderFeature != null) {
        client.register(jsonProviderFeature);
        resourceConfig.register(jsonProviderFeature);
    }
    HttpServer server = null;
    try {
        server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig);
        final String result = client.target(baseUri).path("/json").request(MediaType.APPLICATION_JSON).get(String.class);
        System.out.println("RESULT = " + result);
        assertThat(result, containsString("Jim"));
    } finally {
        if (server != null) {
            server.shutdownNow();
        }
    }
}
Also used : HttpServer(org.glassfish.grizzly.http.server.HttpServer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Client(javax.ws.rs.client.Client) Feature(javax.ws.rs.core.Feature) Test(org.junit.Test)

Aggregations

ResourceConfig (org.glassfish.jersey.server.ResourceConfig)358 Test (org.junit.Test)135 ApplicationHandler (org.glassfish.jersey.server.ApplicationHandler)105 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)62 LoggingFeature (org.glassfish.jersey.logging.LoggingFeature)33 ServletContainer (org.glassfish.jersey.servlet.ServletContainer)29 Response (javax.ws.rs.core.Response)28 HttpServer (org.glassfish.grizzly.http.server.HttpServer)28 URI (java.net.URI)24 Resource (org.glassfish.jersey.server.model.Resource)24 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)23 IOException (java.io.IOException)22 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)22 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)18 Server (org.eclipse.jetty.server.Server)17 ApplicationInfoListener (org.glassfish.jersey.server.internal.monitoring.ApplicationInfoListener)17 MonitoringEventListener (org.glassfish.jersey.server.internal.monitoring.MonitoringEventListener)17 MBeanExposer (org.glassfish.jersey.server.internal.monitoring.jmx.MBeanExposer)17 MetricRegistry (com.codahale.metrics.MetricRegistry)15 ContainerRequest (org.glassfish.jersey.server.ContainerRequest)15