Search in sources :

Example 16 with ProcessingException

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

the class MonitoringEventListener method onEvent.

@Override
public void onEvent(final ApplicationEvent event) {
    final ApplicationEvent.Type type = event.getType();
    switch(type) {
        case INITIALIZATION_START:
            break;
        case RELOAD_FINISHED:
        case INITIALIZATION_FINISHED:
            this.monitoringStatisticsProcessor = new MonitoringStatisticsProcessor(injectionManager, this);
            this.monitoringStatisticsProcessor.startMonitoringWorker();
            break;
        case DESTROY_FINISHED:
            if (monitoringStatisticsProcessor != null) {
                try {
                    monitoringStatisticsProcessor.shutDown();
                } catch (final InterruptedException e) {
                    Thread.currentThread().interrupt();
                    throw new ProcessingException(LocalizationMessages.ERROR_MONITORING_SHUTDOWN_INTERRUPTED(), e);
                }
            }
            // onDestroy
            final List<DestroyListener> listeners = injectionManager.getAllInstances(DestroyListener.class);
            for (final DestroyListener listener : listeners) {
                try {
                    listener.onDestroy();
                } catch (final Exception e) {
                    LOGGER.log(Level.WARNING, LocalizationMessages.ERROR_MONITORING_STATISTICS_LISTENER_DESTROY(listener.getClass()), e);
                }
            }
            break;
    }
}
Also used : ApplicationEvent(org.glassfish.jersey.server.monitoring.ApplicationEvent) DestroyListener(org.glassfish.jersey.server.monitoring.DestroyListener) ProcessingException(javax.ws.rs.ProcessingException) ProcessingException(javax.ws.rs.ProcessingException)

Example 17 with ProcessingException

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

the class MBeanExposer method registerMBean.

/**
     * Register the MBean with the given postfix name.
     *
     * @param mbean MBean to be registered.
     * @param namePostfix Postfix of the object name in the pattern ",[property]=[value]...". Example
     *                    ",subType=Requests,details=Execution"
     */
void registerMBean(Object mbean, String namePostfix) {
    final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    final String name = domain + namePostfix;
    try {
        synchronized (LOCK) {
            if (destroyed.get()) {
                // already destroyed
                return;
            }
            final ObjectName objectName = new ObjectName(name);
            if (mBeanServer.isRegistered(objectName)) {
                LOGGER.log(Level.WARNING, LocalizationMessages.WARNING_MONITORING_MBEANS_BEAN_ALREADY_REGISTERED(objectName));
                mBeanServer.unregisterMBean(objectName);
            }
            mBeanServer.registerMBean(mbean, objectName);
        }
    } catch (JMException e) {
        throw new ProcessingException(LocalizationMessages.ERROR_MONITORING_MBEANS_REGISTRATION(name), e);
    }
}
Also used : JMException(javax.management.JMException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) ProcessingException(javax.ws.rs.ProcessingException)

Example 18 with ProcessingException

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

the class OAuth1ClientFilter method filter.

@Override
public void filter(ClientRequestContext request) throws IOException {
    final ConsumerCredentials consumerFromProperties = (ConsumerCredentials) request.getProperty(OAuth1ClientSupport.OAUTH_PROPERTY_CONSUMER_CREDENTIALS);
    request.removeProperty(OAuth1ClientSupport.OAUTH_PROPERTY_CONSUMER_CREDENTIALS);
    final AccessToken tokenFromProperties = (AccessToken) request.getProperty(OAuth1ClientSupport.OAUTH_PROPERTY_ACCESS_TOKEN);
    request.removeProperty(OAuth1ClientSupport.OAUTH_PROPERTY_ACCESS_TOKEN);
    OAuth1Parameters parameters = (OAuth1Parameters) request.getProperty(OAuth1ClientSupport.OAUTH_PROPERTY_OAUTH_PARAMETERS);
    if (parameters == null) {
        parameters = (OAuth1Parameters) request.getConfiguration().getProperty(OAuth1ClientSupport.OAUTH_PROPERTY_OAUTH_PARAMETERS);
    } else {
        request.removeProperty(OAuth1ClientSupport.OAUTH_PROPERTY_OAUTH_PARAMETERS);
    }
    OAuth1Secrets secrets = (OAuth1Secrets) request.getProperty(OAuth1ClientSupport.OAUTH_PROPERTY_OAUTH_SECRETS);
    if (secrets == null) {
        secrets = (OAuth1Secrets) request.getConfiguration().getProperty(OAuth1ClientSupport.OAUTH_PROPERTY_OAUTH_SECRETS);
    } else {
        request.removeProperty(OAuth1ClientSupport.OAUTH_PROPERTY_OAUTH_SECRETS);
    }
    if (request.getHeaders().containsKey("Authorization")) {
        return;
    }
    // Make modifications to clones.
    final OAuth1Parameters paramCopy = parameters.clone();
    final OAuth1Secrets secretsCopy = secrets.clone();
    checkParametersConsistency(paramCopy, secretsCopy);
    if (consumerFromProperties != null) {
        paramCopy.consumerKey(consumerFromProperties.getConsumerKey());
        secretsCopy.consumerSecret(consumerFromProperties.getConsumerSecret());
    }
    if (tokenFromProperties != null) {
        paramCopy.token(tokenFromProperties.getToken());
        secretsCopy.tokenSecret(tokenFromProperties.getAccessTokenSecret());
    }
    if (paramCopy.getTimestamp() == null) {
        paramCopy.setTimestamp();
    }
    if (paramCopy.getNonce() == null) {
        paramCopy.setNonce();
    }
    try {
        oAuthSignature.get().sign(new RequestWrapper(request, messageBodyWorkers.get()), paramCopy, secretsCopy);
    } catch (OAuth1SignatureException se) {
        throw new ProcessingException(LocalizationMessages.ERROR_REQUEST_SIGNATURE(), se);
    }
}
Also used : OAuth1Parameters(org.glassfish.jersey.oauth1.signature.OAuth1Parameters) OAuth1SignatureException(org.glassfish.jersey.oauth1.signature.OAuth1SignatureException) OAuth1Secrets(org.glassfish.jersey.oauth1.signature.OAuth1Secrets) ProcessingException(javax.ws.rs.ProcessingException)

Example 19 with ProcessingException

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

the class AuthCodeGrantImpl method finish.

@Override
public TokenResult finish(final String authorizationCode, final String state) {
    if (!this.authorizationProperties.get(OAuth2Parameters.STATE).equals(state)) {
        throw new IllegalArgumentException(LocalizationMessages.ERROR_FLOW_WRONG_STATE());
    }
    accessTokenProperties.put(OAuth2Parameters.CODE, authorizationCode);
    final Form form = new Form();
    for (final Map.Entry<String, String> entry : accessTokenProperties.entrySet()) {
        form.param(entry.getKey(), entry.getValue());
    }
    final Response response = client.target(accessTokenUri).request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
    if (response.getStatus() != 200) {
        throw new ProcessingException(LocalizationMessages.ERROR_FLOW_REQUEST_ACCESS_TOKEN(response.getStatus()));
    }
    this.tokenResult = response.readEntity(TokenResult.class);
    return tokenResult;
}
Also used : Response(javax.ws.rs.core.Response) Form(javax.ws.rs.core.Form) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) ProcessingException(javax.ws.rs.ProcessingException)

Example 20 with ProcessingException

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

the class InMemoryConnector method apply.

/**
     * {@inheritDoc}
     * <p/>
     * Transforms client-side request to server-side and invokes it on provided application ({@link ApplicationHandler}
     * instance).
     *
     * @param clientRequest client side request to be invoked.
     */
@Override
public ClientResponse apply(final ClientRequest clientRequest) {
    PropertiesDelegate propertiesDelegate = new MapPropertiesDelegate();
    final ContainerRequest containerRequest = new ContainerRequest(baseUri, clientRequest.getUri(), clientRequest.getMethod(), null, propertiesDelegate);
    containerRequest.getHeaders().putAll(clientRequest.getStringHeaders());
    final ByteArrayOutputStream clientOutput = new ByteArrayOutputStream();
    if (clientRequest.getEntity() != null) {
        clientRequest.setStreamProvider(new OutboundMessageContext.StreamProvider() {

            @Override
            public OutputStream getOutputStream(int contentLength) throws IOException {
                final MultivaluedMap<String, Object> clientHeaders = clientRequest.getHeaders();
                if (contentLength != -1 && !clientHeaders.containsKey(HttpHeaders.CONTENT_LENGTH)) {
                    containerRequest.getHeaders().putSingle(HttpHeaders.CONTENT_LENGTH, String.valueOf(contentLength));
                }
                return clientOutput;
            }
        });
        clientRequest.enableBuffering();
        try {
            clientRequest.writeEntity();
        } catch (IOException e) {
            final String msg = "Error while writing entity to the output stream.";
            LOGGER.log(Level.SEVERE, msg, e);
            throw new ProcessingException(msg, e);
        }
    }
    containerRequest.setEntityStream(new ByteArrayInputStream(clientOutput.toByteArray()));
    boolean followRedirects = ClientProperties.getValue(clientRequest.getConfiguration().getProperties(), ClientProperties.FOLLOW_REDIRECTS, true);
    final InMemoryResponseWriter inMemoryResponseWriter = new InMemoryResponseWriter();
    containerRequest.setWriter(inMemoryResponseWriter);
    containerRequest.setSecurityContext(new SecurityContext() {

        @Override
        public Principal getUserPrincipal() {
            return null;
        }

        @Override
        public boolean isUserInRole(String role) {
            return false;
        }

        @Override
        public boolean isSecure() {
            return false;
        }

        @Override
        public String getAuthenticationScheme() {
            return null;
        }
    });
    appHandler.handle(containerRequest);
    return tryFollowRedirects(followRedirects, createClientResponse(clientRequest, inMemoryResponseWriter), new ClientRequest(clientRequest));
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) OutboundMessageContext(org.glassfish.jersey.message.internal.OutboundMessageContext) MapPropertiesDelegate(org.glassfish.jersey.internal.MapPropertiesDelegate) ByteArrayInputStream(java.io.ByteArrayInputStream) SecurityContext(javax.ws.rs.core.SecurityContext) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) MapPropertiesDelegate(org.glassfish.jersey.internal.MapPropertiesDelegate) PropertiesDelegate(org.glassfish.jersey.internal.PropertiesDelegate) Principal(java.security.Principal) ClientRequest(org.glassfish.jersey.client.ClientRequest) 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