Search in sources :

Example 1 with ITransport

use of org.eclipse.epp.mpc.core.service.ITransport in project epp.mpc by eclipse.

the class TransportFactoryTest method testTransportFactoryInstance.

@Test
public void testTransportFactoryInstance() {
    ITransport transport = TransportFactory.createTransport();
    assertNotNull(transport);
}
Also used : ITransport(org.eclipse.epp.mpc.core.service.ITransport) Test(org.junit.Test)

Example 2 with ITransport

use of org.eclipse.epp.mpc.core.service.ITransport 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 3 with ITransport

use of org.eclipse.epp.mpc.core.service.ITransport in project epp.mpc by eclipse.

the class TransportFactoryTest method testStream.

@Test
public void testStream() throws Exception {
    ITransport transport = ServiceHelper.getTransportFactory().getTransport();
    URI uri = new URI("http://www.eclipse.org/index.php");
    InputStream stream = transport.stream(uri, new NullProgressMonitor());
    assertNotNull(stream);
    stream.close();
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ITransport(org.eclipse.epp.mpc.core.service.ITransport) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) URI(java.net.URI) Test(org.junit.Test)

Example 4 with ITransport

use of org.eclipse.epp.mpc.core.service.ITransport 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

ITransport (org.eclipse.epp.mpc.core.service.ITransport)4 Test (org.junit.Test)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 URI (java.net.URI)2 HttpClientTransport (org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport)2 HttpClientTransportFactory (org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransportFactory)2 FallbackTransportFactory (org.eclipse.epp.internal.mpc.core.util.FallbackTransportFactory)2 ITransportFactory (org.eclipse.epp.mpc.core.service.ITransportFactory)2 URISyntaxException (java.net.URISyntaxException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 JavaPlatformTransportFactory (org.eclipse.epp.internal.mpc.core.util.JavaPlatformTransportFactory)1