Search in sources :

Example 1 with HttpClientTransport

use of org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport in project epp.mpc by eclipse.

the class TransportFactoryTest method createClient.

private static HttpClientTransport createClient(HttpClientCustomizer... customizers) {
    if (customizers == null || customizers.length == 0) {
        return new HttpClientTransport();
    }
    List<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>();
    for (int i = 0; i < customizers.length; i++) {
        HttpClientCustomizer customizer = customizers[i];
        Dictionary<String, Object> serviceProperties = ServiceUtil.serviceName("org.eclipse.epp.mpc.core.transport.http.test.customizer." + i, ServiceUtil.serviceRanking(1000 + i, null));
        ServiceRegistration<?> registration = FrameworkUtil.getBundle(HttpClientCustomizer.class).getBundleContext().registerService(HttpClientCustomizer.class, customizer, serviceProperties);
        registrations.add(registration);
    }
    HttpClientTransport httpClientTransport;
    try {
        httpClientTransport = new HttpClientTransport();
    } finally {
        for (ServiceRegistration<?> registration : registrations) {
            registration.unregister();
        }
    }
    return httpClientTransport;
}
Also used : HttpClientTransport(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport) HttpClientCustomizer(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientCustomizer) ArrayList(java.util.ArrayList) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 2 with HttpClientTransport

use of org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport in project epp.mpc by eclipse.

the class TransportFactoryTest method interceptRequest.

private static AbortRequestCustomizer interceptRequest(HttpClientCustomizer... customizers) throws Exception {
    AbortRequestCustomizer abortRequestCustomizer = new AbortRequestCustomizer();
    HttpClientCustomizer[] mergedCustomizers;
    if (customizers == null || customizers.length == 0) {
        mergedCustomizers = new HttpClientCustomizer[] { abortRequestCustomizer };
    } else {
        mergedCustomizers = new HttpClientCustomizer[customizers.length + 1];
        System.arraycopy(customizers, 0, mergedCustomizers, 0, customizers.length);
        mergedCustomizers[customizers.length] = abortRequestCustomizer;
    }
    HttpClientTransport httpClientTransport = createClient(mergedCustomizers);
    HttpClient client = httpClientTransport.getClient();
    HttpContext context = new BasicHttpContext();
    try {
        client.execute(new HttpGet("http://localhost/test"), context);
        fail("Expected request execution to fail");
    } catch (ConnectionClosedException ex) {
    // ignore expected exception
    }
    return abortRequestCustomizer;
}
Also used : HttpClientCustomizer(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientCustomizer) HttpClientTransport(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) ConnectionClosedException(org.apache.http.ConnectionClosedException)

Example 3 with HttpClientTransport

use of org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport in project epp.mpc by eclipse.

the class TransportFactoryTest method testHttpClientTransportFallback.

@Test
public void testHttpClientTransportFallback() throws Exception {
    HttpClientTransport httpClientTransport = createFailingHttpClientTransport();
    HttpClientTransportFactory httpClientFactory = new HttpClientTransportFactory();
    httpClientFactory.setTransport(httpClientTransport);
    ITransportFactory secondaryFactory = Mockito.mock(ITransportFactory.class);
    ITransport secondaryTransport = Mockito.mock(ITransport.class);
    InputStream expectedResultStream = new ByteArrayInputStream("Secondary transport".getBytes("UTF-8"));
    Mockito.when(secondaryFactory.getTransport()).thenReturn(secondaryTransport);
    Mockito.when(secondaryTransport.stream(Matchers.<URI>any(), Matchers.<IProgressMonitor>any())).thenReturn(expectedResultStream);
    FallbackTransportFactory fallbackTransportFactory = new FallbackTransportFactory();
    fallbackTransportFactory.setPrimaryFactory(httpClientFactory);
    fallbackTransportFactory.setSecondaryFactory(secondaryFactory);
    InputStream stream = fallbackTransportFactory.getTransport().stream(URI.create("http://127.0.0.1:54321"), null);
    assertSame(expectedResultStream, stream);
}
Also used : HttpClientTransportFactory(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransportFactory) HttpClientTransport(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport) ByteArrayInputStream(java.io.ByteArrayInputStream) ITransport(org.eclipse.epp.mpc.core.service.ITransport) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FallbackTransportFactory(org.eclipse.epp.internal.mpc.core.util.FallbackTransportFactory) ITransportFactory(org.eclipse.epp.mpc.core.service.ITransportFactory) Test(org.junit.Test)

Example 4 with HttpClientTransport

use of org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport in project epp.mpc by eclipse.

the class TransportFactoryTest method createFailingHttpClientTransport.

private static HttpClientTransport createFailingHttpClientTransport() {
    return new HttpClientTransport() {

        @Override
        protected Response execute(Request request, URI uri) throws ClientProtocolException, IOException {
            HttpResponse mockResponse = mockResponse(mockStatusLine(503, "Expected test error"), null);
            try {
                Constructor<Response> ctor = Response.class.getDeclaredConstructor(HttpResponse.class);
                ctor.setAccessible(true);
                return ctor.newInstance(mockResponse);
            } catch (Exception e) {
                try {
                    fail("Failed to create response");
                } catch (AssertionError ae) {
                    ae.initCause(e);
                    throw ae;
                }
                return null;
            }
        }
    };
}
Also used : HttpResponse(org.apache.http.HttpResponse) Response(org.apache.http.client.fluent.Response) HttpClientTransport(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport) Request(org.apache.http.client.fluent.Request) HttpRequest(org.apache.http.HttpRequest) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) ServiceUnavailableException(org.eclipse.epp.mpc.core.service.ServiceUnavailableException) HttpException(org.apache.http.HttpException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) ConnectionClosedException(org.apache.http.ConnectionClosedException)

Example 5 with HttpClientTransport

use of org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport in project epp.mpc by eclipse.

the class MappedTransportFactory method getTransport.

public ITransport getTransport() {
    return new ITransport() {

        private final ITransport delegateTransport = delegate.getTransport();

        public InputStream stream(URI location, IProgressMonitor monitor) throws FileNotFoundException, ServiceUnavailableException, CoreException {
            URI mapped = mapping.get(location);
            if (mapped == null) {
                if (location.getQuery() != null || location.getFragment() != null) {
                    try {
                        URI stripped = new URI(location.getScheme(), location.getHost(), location.getPath(), null);
                        mapped = mapping.get(stripped);
                    } catch (URISyntaxException e) {
                    }
                }
            }
            if (mapped == null) {
                mapped = location;
            } else if (!mapped.getScheme().toLowerCase().startsWith("http")) {
                ITransport nonHttpTransport = getNonHttpTransport(delegate, delegateTransport);
                if (nonHttpTransport != null) {
                    return nonHttpTransport.stream(mapped, monitor);
                }
            }
            return delegateTransport.stream(mapped, monitor);
        }

        private ITransport getNonHttpTransport(ITransportFactory delegate, ITransport delegateTransport) {
            ITransport nonHttpTransport = delegateTransport;
            if (delegate instanceof HttpClientTransportFactory) {
                FallbackTransportFactory fallbackFactory = new FallbackTransportFactory();
                fallbackFactory.setPrimaryFactory(delegate);
                delegate = fallbackFactory;
            }
            if (delegate instanceof FallbackTransportFactory) {
                FallbackTransportFactory fallbackFactory = (FallbackTransportFactory) delegate;
                ITransportFactory primaryFactory = fallbackFactory.getPrimaryFactory();
                if (primaryFactory instanceof HttpClientTransportFactory) {
                    ITransportFactory secondaryFactory = fallbackFactory.getFallbackFactory();
                    if (secondaryFactory != null) {
                        nonHttpTransport = secondaryFactory.getTransport();
                    }
                }
            }
            if (nonHttpTransport instanceof HttpClientTransport) {
                nonHttpTransport = new JavaPlatformTransportFactory().getTransport();
            }
            return nonHttpTransport;
        }
    };
}
Also used : HttpClientTransportFactory(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransportFactory) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) HttpClientTransport(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport) ITransport(org.eclipse.epp.mpc.core.service.ITransport) FallbackTransportFactory(org.eclipse.epp.internal.mpc.core.util.FallbackTransportFactory) URISyntaxException(java.net.URISyntaxException) ITransportFactory(org.eclipse.epp.mpc.core.service.ITransportFactory) URI(java.net.URI) JavaPlatformTransportFactory(org.eclipse.epp.internal.mpc.core.util.JavaPlatformTransportFactory)

Aggregations

HttpClientTransport (org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport)5 URI (java.net.URI)2 ConnectionClosedException (org.apache.http.ConnectionClosedException)2 HttpClientCustomizer (org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientCustomizer)2 HttpClientTransportFactory (org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransportFactory)2 FallbackTransportFactory (org.eclipse.epp.internal.mpc.core.util.FallbackTransportFactory)2 ITransport (org.eclipse.epp.mpc.core.service.ITransport)2 ITransportFactory (org.eclipse.epp.mpc.core.service.ITransportFactory)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 HttpException (org.apache.http.HttpException)1 HttpRequest (org.apache.http.HttpRequest)1 HttpResponse (org.apache.http.HttpResponse)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 HttpClient (org.apache.http.client.HttpClient)1 Request (org.apache.http.client.fluent.Request)1 Response (org.apache.http.client.fluent.Response)1