Search in sources :

Example 11 with ResteasyProviderFactory

use of org.jboss.resteasy.spi.ResteasyProviderFactory in project java by wavefrontHQ.

the class APIContainer method createProviderFactory.

private ResteasyProviderFactory createProviderFactory() {
    ResteasyProviderFactory factory = new LocalResteasyProviderFactory(ResteasyProviderFactory.getInstance());
    factory.registerProvider(JsonNodeWriter.class);
    if (!factory.getClasses().contains(ResteasyJackson2Provider.class)) {
        factory.registerProvider(ResteasyJackson2Provider.class);
    }
    factory.register(GZIPDecodingInterceptor.class);
    if (proxyConfig.isGzipCompression()) {
        WriterInterceptor interceptor = new GZIPEncodingInterceptorWithVariableCompression(proxyConfig.getGzipCompressionLevel());
        factory.register(interceptor);
    } else {
        factory.register(DisableGZIPEncodingInterceptor.class);
    }
    factory.register(AcceptEncodingGZIPFilter.class);
    // add authorization header for all proxy endpoints, except for /checkin - since it's also
    // passed as a parameter, it's creating duplicate headers that cause the entire request to be
    // rejected by nginx. unfortunately, RESTeasy is not smart enough to handle that automatically.
    factory.register((ClientRequestFilter) context -> {
        if ((context.getUri().getPath().contains("/v2/wfproxy") || context.getUri().getPath().contains("/v2/source") || context.getUri().getPath().contains("/event")) && !context.getUri().getPath().endsWith("checkin")) {
            context.getHeaders().add("Authorization", "Bearer " + proxyConfig.getToken());
        }
    });
    return factory;
}
Also used : ResteasyJackson2Provider(org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider) JsonNodeWriter(com.wavefront.agent.JsonNodeWriter) SocketConfig(org.apache.http.config.SocketConfig) GZIPDecodingInterceptor(org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor) GZIPEncodingInterceptorWithVariableCompression(com.wavefront.agent.channel.GZIPEncodingInterceptorWithVariableCompression) RequestConfig(org.apache.http.client.config.RequestConfig) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) PasswordAuthentication(java.net.PasswordAuthentication) HttpClient(org.apache.http.client.HttpClient) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) ApacheHttpClient4Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine) ResteasyJackson2Provider(org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider) WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) AcceptEncodingGZIPFilter(org.jboss.resteasy.plugins.interceptors.encoding.AcceptEncodingGZIPFilter) ProxyV2API(com.wavefront.api.ProxyV2API) SourceTagAPI(com.wavefront.api.SourceTagAPI) Authenticator(java.net.Authenticator) SSLConnectionSocketFactoryImpl(com.wavefront.agent.SSLConnectionSocketFactoryImpl) HttpRequest(org.apache.http.HttpRequest) TimeUnit(java.util.concurrent.TimeUnit) EventAPI(com.wavefront.api.EventAPI) ProxyConfig(com.wavefront.agent.ProxyConfig) DisableGZIPEncodingInterceptor(com.wavefront.agent.channel.DisableGZIPEncodingInterceptor) ClientHttpEngine(org.jboss.resteasy.client.jaxrs.ClientHttpEngine) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) VisibleForTesting(com.google.common.annotations.VisibleForTesting) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) LocalResteasyProviderFactory(org.jboss.resteasy.client.jaxrs.internal.LocalResteasyProviderFactory) WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) LocalResteasyProviderFactory(org.jboss.resteasy.client.jaxrs.internal.LocalResteasyProviderFactory) LocalResteasyProviderFactory(org.jboss.resteasy.client.jaxrs.internal.LocalResteasyProviderFactory) GZIPEncodingInterceptorWithVariableCompression(com.wavefront.agent.channel.GZIPEncodingInterceptorWithVariableCompression)

Example 12 with ResteasyProviderFactory

use of org.jboss.resteasy.spi.ResteasyProviderFactory in project stdlib by petergeneric.

the class GuicedResteasy method configure.

/**
 * Try to initialise a ServletContainerDispatcher with the connection to the Guice REST services
 */
protected void configure(ServletContainerDispatcher dispatcher) throws ServletException {
    // Make sure we are registered with the Guice registry
    registry.register(this, true);
    // Configure the dispatcher
    final Registry resteasyRegistry;
    final ResteasyProviderFactory providerFactory;
    {
        final ResteasyRequestResponseFactory converter = new ResteasyRequestResponseFactory(dispatcher);
        dispatcher.init(context, bootstrap, converter, converter);
        if (filterConfig != null)
            dispatcher.getDispatcher().getDefaultContextObjects().put(FilterConfig.class, filterConfig);
        if (servletConfig != null)
            dispatcher.getDispatcher().getDefaultContextObjects().put(ServletConfig.class, servletConfig);
        resteasyRegistry = dispatcher.getDispatcher().getRegistry();
        providerFactory = dispatcher.getDispatcher().getProviderFactory();
    }
    // Register the REST provider classes
    for (Class<?> providerClass : ResteasyProviderRegistry.getClasses()) {
        log.debug("Registering REST providers: " + providerClass.getName());
        providerFactory.registerProvider(providerClass);
    }
    // Register the REST provider singletons
    for (Object provider : ResteasyProviderRegistry.getSingletons()) {
        log.debug("Registering REST provider singleton: " + provider);
        providerFactory.registerProviderInstance(provider);
    }
    providerFactory.registerProviderInstance(new LogReportMessageBodyReader());
    // Register the JAXBContext provider
    providerFactory.registerProviderInstance(jaxbContextResolver);
    // Register the exception mapper
    {
        // Register the ExceptionMapper for ApplicationException
        providerFactory.register(this.exceptionMapper);
        log.trace("ExceptionMapper registered for ApplicationException");
    }
    // Register the REST resources
    for (RestResource resource : RestResourceRegistry.getResources()) {
        log.debug("Registering REST resource: " + resource.getResourceClass().getName());
        resteasyRegistry.addResourceFactory(new ResteasyGuiceResource(injector, resource.getResourceClass()));
    }
}
Also used : RestResource(com.peterphi.std.guice.serviceregistry.rest.RestResource) Registry(org.jboss.resteasy.spi.Registry) MetricRegistry(com.codahale.metrics.MetricRegistry) GuiceRegistry(com.peterphi.std.guice.apploader.impl.GuiceRegistry) RestResourceRegistry(com.peterphi.std.guice.serviceregistry.rest.RestResourceRegistry) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) LogReportMessageBodyReader(com.peterphi.std.guice.common.logging.logreport.jaxrs.LogReportMessageBodyReader)

Example 13 with ResteasyProviderFactory

use of org.jboss.resteasy.spi.ResteasyProviderFactory in project candlepin by candlepin.

the class CandlepinContextListenerTest method prepareForInitialization.

private void prepareForInitialization() {
    evt = mock(ServletContextEvent.class);
    ctx = mock(ServletContext.class);
    Registry registry = mock(Registry.class);
    ResteasyProviderFactory rpfactory = mock(ResteasyProviderFactory.class);
    when(evt.getServletContext()).thenReturn(ctx);
    when(ctx.getAttribute(eq(Registry.class.getName()))).thenReturn(registry);
    when(ctx.getAttribute(eq(ResteasyProviderFactory.class.getName()))).thenReturn(rpfactory);
    when(ctx.getAttribute(eq(CandlepinContextListener.CONFIGURATION_NAME))).thenReturn(config);
}
Also used : ServletContext(javax.servlet.ServletContext) Registry(org.jboss.resteasy.spi.Registry) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 14 with ResteasyProviderFactory

use of org.jboss.resteasy.spi.ResteasyProviderFactory in project java by wavefrontHQ.

the class AbstractAgent method createAgentService.

/**
 * Create RESTeasy proxies for remote calls via HTTP.
 */
protected WavefrontAPI createAgentService() {
    ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance();
    factory.registerProvider(JsonNodeWriter.class);
    if (!factory.getClasses().contains(ResteasyJackson2Provider.class)) {
        factory.registerProvider(ResteasyJackson2Provider.class);
    }
    if (httpUserAgent == null) {
        httpUserAgent = "Wavefront-Proxy/" + props.getString("build.version");
    }
    ClientHttpEngine httpEngine;
    if (javaNetConnection) {
        httpEngine = new JavaNetConnectionEngine() {

            @Override
            protected HttpURLConnection createConnection(ClientInvocation request) throws IOException {
                HttpURLConnection connection = (HttpURLConnection) request.getUri().toURL().openConnection();
                connection.setRequestProperty("User-Agent", httpUserAgent);
                connection.setRequestMethod(request.getMethod());
                // 5s
                connection.setConnectTimeout(httpConnectTimeout);
                // 60s
                connection.setReadTimeout(httpRequestTimeout);
                if (connection instanceof HttpsURLConnection) {
                    HttpsURLConnection secureConnection = (HttpsURLConnection) connection;
                    secureConnection.setSSLSocketFactory(new SSLSocketFactoryImpl(HttpsURLConnection.getDefaultSSLSocketFactory(), httpRequestTimeout));
                }
                return connection;
            }
        };
    } else {
        HttpClient httpClient = HttpClientBuilder.create().useSystemProperties().setUserAgent(httpUserAgent).setMaxConnTotal(httpMaxConnTotal).setMaxConnPerRoute(httpMaxConnPerRoute).setConnectionTimeToLive(1, TimeUnit.MINUTES).setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(httpRequestTimeout).build()).setSSLSocketFactory(new SSLConnectionSocketFactoryImpl(SSLConnectionSocketFactory.getSystemSocketFactory(), httpRequestTimeout)).setRetryHandler(new DefaultHttpRequestRetryHandler(httpAutoRetries, true)).setDefaultRequestConfig(RequestConfig.custom().setContentCompressionEnabled(true).setRedirectsEnabled(true).setConnectTimeout(httpConnectTimeout).setConnectionRequestTimeout(httpConnectTimeout).setSocketTimeout(httpRequestTimeout).build()).build();
        final ApacheHttpClient4Engine apacheHttpClient4Engine = new ApacheHttpClient4Engine(httpClient, true);
        // avoid using disk at all
        apacheHttpClient4Engine.setFileUploadInMemoryThresholdLimit(100);
        apacheHttpClient4Engine.setFileUploadMemoryUnit(ApacheHttpClient4Engine.MemoryUnit.MB);
        httpEngine = apacheHttpClient4Engine;
    }
    ResteasyClient client = new ResteasyClientBuilder().httpEngine(httpEngine).providerFactory(factory).register(GZIPDecodingInterceptor.class).register(gzipCompression ? GZIPEncodingInterceptor.class : DisableGZIPEncodingInterceptor.class).register(AcceptEncodingGZIPFilter.class).build();
    ResteasyWebTarget target = client.target(server);
    return target.proxy(WavefrontAPI.class);
}
Also used : ResteasyJackson2Provider(org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) GZIPDecodingInterceptor(org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor) ApacheHttpClient4Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) ClientInvocation(org.jboss.resteasy.client.jaxrs.internal.ClientInvocation) IOException(java.io.IOException) AcceptEncodingGZIPFilter(org.jboss.resteasy.plugins.interceptors.encoding.AcceptEncodingGZIPFilter) ClientHttpEngine(org.jboss.resteasy.client.jaxrs.ClientHttpEngine) HttpURLConnection(java.net.HttpURLConnection) GZIPEncodingInterceptor(org.jboss.resteasy.plugins.interceptors.encoding.GZIPEncodingInterceptor) HttpClient(org.apache.http.client.HttpClient) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 15 with ResteasyProviderFactory

use of org.jboss.resteasy.spi.ResteasyProviderFactory in project syndesis-qe by syndesisio.

the class EndpointClient method getClient.

public static Client getClient(ResteasyJackson2Provider jackson2Provider) {
    final ApacheHttpClient43Engine engine = new ApacheHttpClient43Engine(createAllTrustingClient());
    final ResteasyProviderFactory providerFactory = new LocalResteasyProviderFactory();
    providerFactory.register(jackson2Provider).register(// needed for GET application/octet-stream in PublicAPI to export zip
    new InputStreamProvider()).register(// needed to POST mutipart form data (necessary for API provider + PublicAPI)
    new MultipartFormDataWriter()).register(// needed to serialize text/plain (again for API provider)
    new StringTextStar()).register(new ErrorLogger());
    ResteasyClientBuilder clientBuilder = (ResteasyClientBuilder) ResteasyClientBuilder.newBuilder();
    clientBuilder.providerFactory(providerFactory);
    clientBuilder.httpEngine(engine);
    return clientBuilder.build();
}
Also used : MultipartFormDataWriter(org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataWriter) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) InputStreamProvider(org.jboss.resteasy.plugins.providers.InputStreamProvider) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) LocalResteasyProviderFactory(org.jboss.resteasy.client.jaxrs.internal.LocalResteasyProviderFactory) LocalResteasyProviderFactory(org.jboss.resteasy.client.jaxrs.internal.LocalResteasyProviderFactory) ApacheHttpClient43Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient43Engine) StringTextStar(org.jboss.resteasy.plugins.providers.StringTextStar)

Aggregations

ResteasyProviderFactory (org.jboss.resteasy.spi.ResteasyProviderFactory)19 ResteasyClientBuilder (org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder)8 ResteasyWebTarget (org.jboss.resteasy.client.jaxrs.ResteasyWebTarget)6 HttpClient (org.apache.http.client.HttpClient)5 ApacheHttpClient4Engine (org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine)5 Client (javax.ws.rs.client.Client)4 ResteasyClient (org.jboss.resteasy.client.jaxrs.ResteasyClient)4 LocalResteasyProviderFactory (org.jboss.resteasy.client.jaxrs.internal.LocalResteasyProviderFactory)4 ResteasyJackson2Provider (org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider)4 HashMap (java.util.HashMap)2 ServletContext (javax.servlet.ServletContext)2 Configuration (javax.ws.rs.core.Configuration)2 Dispatcher (org.jboss.resteasy.core.Dispatcher)2 Registry (org.jboss.resteasy.spi.Registry)2 JaxRSBootstrapException (com.adeptj.modules.jaxrs.resteasy.JaxRSBootstrapException)1 JaxRSProviderTracker (com.adeptj.modules.jaxrs.resteasy.internal.JaxRSProviderTracker)1 JaxRSResourceTracker (com.adeptj.modules.jaxrs.resteasy.internal.JaxRSResourceTracker)1 MetricRegistry (com.codahale.metrics.MetricRegistry)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 GuiceRegistry (com.peterphi.std.guice.apploader.impl.GuiceRegistry)1