Search in sources :

Example 26 with ProcessingException

use of javax.ws.rs.ProcessingException in project jersey by jersey.

the class EmptyEntityTest method testReceiveEmptyInteger.

@Test
public void testReceiveEmptyInteger() {
    WebTarget target = target("empty/getempty");
    final Response response = target.request("text/plain").get();
    assertEquals(200, response.getStatus());
    try {
        response.readEntity(Integer.class);
        fail("ProcessingException expected.");
    } catch (ProcessingException ex) {
        assertSame(NoContentException.class, ex.getCause().getClass());
    }
    try {
        target.request("text/plain").get(Integer.class);
        fail("ResponseProcessingException expected.");
    } catch (ResponseProcessingException ex) {
        assertSame(NoContentException.class, ex.getCause().getClass());
    }
}
Also used : Response(javax.ws.rs.core.Response) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) WebTarget(javax.ws.rs.client.WebTarget) NoContentException(javax.ws.rs.core.NoContentException) ProcessingException(javax.ws.rs.ProcessingException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) Test(org.junit.Test)

Example 27 with ProcessingException

use of javax.ws.rs.ProcessingException in project jersey by jersey.

the class ItemStoreResourceITCase method testItemsStore.

/**
     * Test the item addition, addition event broadcasting and item retrieval from {@link ItemStoreResource}.
     *
     * @throws Exception in case of a test failure.
     */
@Test
public void testItemsStore() throws Exception {
    final List<String> items = Collections.unmodifiableList(Arrays.asList("foo", "bar", "baz"));
    final WebTarget itemsTarget = target("items");
    // countdown on all events
    final CountDownLatch latch = new CountDownLatch(items.size() * MAX_LISTENERS * 2);
    final List<Queue<Integer>> indexQueues = new ArrayList<Queue<Integer>>(MAX_LISTENERS);
    final EventSource[] sources = new EventSource[MAX_LISTENERS];
    final AtomicInteger sizeEventsCount = new AtomicInteger(0);
    for (int i = 0; i < MAX_LISTENERS; i++) {
        final int id = i;
        final EventSource es = EventSource.target(itemsTarget.path("events")).named("SOURCE " + id).build();
        sources[id] = es;
        final Queue<Integer> indexes = new ConcurrentLinkedQueue<Integer>();
        indexQueues.add(indexes);
        es.register(new EventListener() {

            @Override
            @SuppressWarnings("MagicNumber")
            public void onEvent(InboundEvent inboundEvent) {
                try {
                    if (inboundEvent.getName() == null) {
                        final String data = inboundEvent.readData();
                        LOGGER.info("[-i-] SOURCE " + id + ": Received event id=" + inboundEvent.getId() + " data=" + data);
                        indexes.add(items.indexOf(data));
                    } else if ("size".equals(inboundEvent.getName())) {
                        sizeEventsCount.incrementAndGet();
                    }
                } catch (ProcessingException ex) {
                    LOGGER.log(Level.SEVERE, "[-x-] SOURCE " + id + ": Error getting event data.", ex);
                    indexes.add(-999);
                } finally {
                    latch.countDown();
                }
            }
        });
    }
    try {
        open(sources);
        for (String item : items) {
            postItem(itemsTarget, item);
        }
        assertTrue("Waiting to receive all events has timed out.", latch.await((1000 + MAX_LISTENERS * EventSource.RECONNECT_DEFAULT) * getAsyncTimeoutMultiplier(), TimeUnit.MILLISECONDS));
        // need to force disconnect on server in order for EventSource.close(...) to succeed with HttpUrlConnection
        sendCommand(itemsTarget, "disconnect");
    } finally {
        close(sources);
    }
    String postedItems = itemsTarget.request().get(String.class);
    for (String item : items) {
        assertTrue("Item '" + item + "' not stored on server.", postedItems.contains(item));
    }
    int queueId = 0;
    for (Queue<Integer> indexes : indexQueues) {
        for (int i = 0; i < items.size(); i++) {
            assertTrue("Event for '" + items.get(i) + "' not received in queue " + queueId, indexes.contains(i));
        }
        assertEquals("Not received the expected number of events in queue " + queueId, items.size(), indexes.size());
        queueId++;
    }
    assertEquals("Number of received 'size' events does not match.", items.size() * MAX_LISTENERS, sizeEventsCount.get());
}
Also used : ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EventSource(org.glassfish.jersey.media.sse.EventSource) InboundEvent(org.glassfish.jersey.media.sse.InboundEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WebTarget(javax.ws.rs.client.WebTarget) EventListener(org.glassfish.jersey.media.sse.EventListener) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Queue(java.util.Queue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ProcessingException(javax.ws.rs.ProcessingException) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 28 with ProcessingException

use of javax.ws.rs.ProcessingException in project jersey by jersey.

the class ParameterValueHelper method getParameterValues.

/**
     * Get the array of parameter values.
     *
     * @param valueProviders a list of value providers.
     * @return array of parameter values provided by the value providers.
     */
public static Object[] getParameterValues(List<ParamValueFactoryWithSource<?>> valueProviders) {
    final Object[] params = new Object[valueProviders.size()];
    try {
        int entityProviderIndex = -1;
        int index = 0;
        for (ParamValueFactoryWithSource<?> paramValProvider : valueProviders) {
            // entity provider has to be called last; see JERSEY-2642
            if (paramValProvider.getSource().equals(Parameter.Source.ENTITY)) {
                entityProviderIndex = index++;
                continue;
            }
            params[index++] = paramValProvider.get();
        }
        if (entityProviderIndex != -1) {
            params[entityProviderIndex] = valueProviders.get(entityProviderIndex).get();
        }
        return params;
    } catch (WebApplicationException e) {
        throw e;
    } catch (MessageBodyProviderNotFoundException e) {
        throw new NotSupportedException(e);
    } catch (ProcessingException e) {
        throw e;
    } catch (RuntimeException e) {
        if (e.getCause() instanceof WebApplicationException) {
            throw (WebApplicationException) e.getCause();
        }
        throw new MappableException("Exception obtaining parameters", e);
    }
}
Also used : MappableException(org.glassfish.jersey.server.internal.process.MappableException) WebApplicationException(javax.ws.rs.WebApplicationException) MessageBodyProviderNotFoundException(org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException) NotSupportedException(javax.ws.rs.NotSupportedException) ProcessingException(javax.ws.rs.ProcessingException)

Example 29 with ProcessingException

use of javax.ws.rs.ProcessingException in project jersey by jersey.

the class WadlGeneratorConfig method createWadlGenerator.

/**
     * Create a new instance of {@link org.glassfish.jersey.server.wadl.WadlGenerator}, based on the {@link WadlGeneratorDescription}s
     * provided by {@link #configure()}.
     *
     * @return the initialized {@link org.glassfish.jersey.server.wadl.WadlGenerator}
     */
public WadlGenerator createWadlGenerator(InjectionManager injectionManager) {
    final WadlGenerator wadlGenerator;
    final List<WadlGeneratorDescription> wadlGeneratorDescriptions;
    try {
        wadlGeneratorDescriptions = configure();
    } catch (final Exception e) {
        throw new ProcessingException(LocalizationMessages.ERROR_WADL_GENERATOR_CONFIGURE(), e);
    }
    for (final WadlGeneratorDescription desc : wadlGeneratorDescriptions) {
        desc.setConfiguratorClass(this.getClass());
    }
    try {
        wadlGenerator = WadlGeneratorLoader.loadWadlGeneratorDescriptions(injectionManager, wadlGeneratorDescriptions);
    } catch (final Exception e) {
        throw new ProcessingException(LocalizationMessages.ERROR_WADL_GENERATOR_LOAD(), e);
    }
    return wadlGenerator;
}
Also used : WadlGenerator(org.glassfish.jersey.server.wadl.WadlGenerator) ProcessingException(javax.ws.rs.ProcessingException) ProcessingException(javax.ws.rs.ProcessingException)

Example 30 with ProcessingException

use of javax.ws.rs.ProcessingException in project jersey by jersey.

the class WadlBuilder method generateMethod.

private com.sun.research.ws.wadl.Method generateMethod(final org.glassfish.jersey.server.model.Resource parentResource, final Map<String, Param> wadlResourceParams, final org.glassfish.jersey.server.model.ResourceMethod resourceMethod) {
    try {
        if (!detailedWadl && resourceMethod.isExtended()) {
            return null;
        }
        com.sun.research.ws.wadl.Method wadlMethod = _wadlGenerator.createMethod(parentResource, resourceMethod);
        // generate the request part
        Request wadlRequest = generateRequest(parentResource, resourceMethod, wadlResourceParams);
        if (wadlRequest != null) {
            wadlMethod.setRequest(wadlRequest);
        }
        // generate the response part
        final List<Response> responses = generateResponses(parentResource, resourceMethod);
        if (responses != null) {
            wadlMethod.getResponse().addAll(responses);
        }
        return wadlMethod;
    } catch (Exception e) {
        throw new ProcessingException(LocalizationMessages.ERROR_WADL_BUILDER_GENERATION_METHOD(resourceMethod, parentResource), e);
    }
}
Also used : Response(com.sun.research.ws.wadl.Response) Request(com.sun.research.ws.wadl.Request) ProcessingException(javax.ws.rs.ProcessingException) ProcessingException(javax.ws.rs.ProcessingException)

Aggregations

ProcessingException (javax.ws.rs.ProcessingException)91 Test (org.junit.Test)32 IOException (java.io.IOException)26 Response (javax.ws.rs.core.Response)20 WebTarget (javax.ws.rs.client.WebTarget)19 JerseyTest (org.glassfish.jersey.test.JerseyTest)16 WebApplicationException (javax.ws.rs.WebApplicationException)11 CountDownLatch (java.util.concurrent.CountDownLatch)9 Client (javax.ws.rs.client.Client)9 ResponseProcessingException (javax.ws.rs.client.ResponseProcessingException)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 ClientRequest (org.glassfish.jersey.client.ClientRequest)8 ClientResponse (org.glassfish.jersey.client.ClientResponse)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 URI (java.net.URI)6 NoContentException (javax.ws.rs.core.NoContentException)6 OutputStream (java.io.OutputStream)5 Map (java.util.Map)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 EventSource (org.glassfish.jersey.media.sse.EventSource)5