Search in sources :

Example 1 with URLStreamHandler

use of java.net.URLStreamHandler in project agera by google.

the class HttpFunctionsTest method onlyOnce.

@BeforeClass
public static void onlyOnce() throws Throwable {
    mockHttpURLConnection = mock(HttpURLConnection.class);
    URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {

        @Override
        public URLStreamHandler createURLStreamHandler(final String s) {
            return s.equals(TEST_PROTOCOL) ? new URLStreamHandler() {

                @Override
                protected URLConnection openConnection(final URL url) throws IOException {
                    return mockHttpURLConnection;
                }
            } : null;
        }
    });
}
Also used : URLStreamHandler(java.net.URLStreamHandler) HttpURLConnection(java.net.HttpURLConnection) URLStreamHandlerFactory(java.net.URLStreamHandlerFactory) Matchers.anyString(org.mockito.Matchers.anyString) URL(java.net.URL) BeforeClass(org.junit.BeforeClass)

Example 2 with URLStreamHandler

use of java.net.URLStreamHandler in project voldemort by voldemort.

the class VoldemortUtils method modifyURL.

public static String modifyURL(String originalUrl, String newProtocol, int newPort, boolean omitPort) {
    if (newProtocol == null || newProtocol.isEmpty() || (!omitPort && newPort < 0)) {
        return originalUrl;
    }
    try {
        // Create handler to avoid unknown protocol error when parsing URL string. Actually this handler will do
        // nothing.
        URLStreamHandler handler = new URLStreamHandler() {

            @Override
            protected URLConnection openConnection(URL u) throws IOException {
                return null;
            }
        };
        URL url = new URL(null, originalUrl, handler);
        logger.info("Existing protocol = " + url.getProtocol() + " and port = " + url.getPort());
        if (omitPort) {
            // The URL constructor will omit the port if we pass -1 in the port.
            newPort = -1;
        }
        URL newUrl = new URL(newProtocol, url.getHost(), newPort, url.getFile(), handler);
        logger.info("New protocol = " + newUrl.getProtocol() + " and port = " + newUrl.getPort());
        return newUrl.toString();
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("URL is not in valid format. URL:" + originalUrl);
    }
}
Also used : URLStreamHandler(java.net.URLStreamHandler) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL)

Example 3 with URLStreamHandler

use of java.net.URLStreamHandler in project sling by apache.

the class ClassLoaderResourceProviderChildrenTest method mockClassLoader.

private ClassLoader mockClassLoader(String... paths) throws MalformedURLException, IOException {
    final ClassLoader cl = Mockito.mock(ClassLoader.class);
    final JarURLConnection conn = Mockito.mock(JarURLConnection.class);
    final URLStreamHandler handler = new URLStreamHandler() {

        @Override
        protected URLConnection openConnection(final URL url) throws IOException {
            if (throwExceptionOnOpenConnection) {
                throw new IOException("Throwing up for testing that");
            }
            return conn;
        }
    };
    final JarFile f = Mockito.mock(JarFile.class);
    final URL url = new URL("jar://some.jar", "localhost", 1234, "some.jar", handler);
    final Vector<JarEntry> entries = new Vector<JarEntry>();
    for (String path : paths) {
        entries.add(new JarEntry(path));
    }
    when(cl.getResource(Matchers.contains("install"))).thenReturn(url);
    when(conn.getJarFile()).thenReturn(f);
    when(f.entries()).thenReturn(entries.elements());
    return cl;
}
Also used : URLStreamHandler(java.net.URLStreamHandler) JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) Vector(java.util.Vector) URL(java.net.URL)

Example 4 with URLStreamHandler

use of java.net.URLStreamHandler in project hibernate-orm by hibernate.

the class JarVisitorTest method testJarVisitorFactory.

@Test
@TestForIssue(jiraKey = "HHH-6806")
public void testJarVisitorFactory() throws Exception {
    final File explodedPar = buildExplodedPar();
    final File defaultPar = buildDefaultPar();
    addPackageToClasspath(explodedPar, defaultPar);
    //setting URL to accept vfs based protocol
    URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {

        public URLStreamHandler createURLStreamHandler(String protocol) {
            if ("vfszip".equals(protocol) || "vfsfile".equals(protocol))
                return new URLStreamHandler() {

                    protected URLConnection openConnection(URL u) throws IOException {
                        return null;
                    }
                };
            return null;
        }
    });
    URL jarUrl = defaultPar.toURL();
    ArchiveDescriptor descriptor = StandardArchiveDescriptorFactory.INSTANCE.buildArchiveDescriptor(jarUrl);
    assertEquals(JarFileBasedArchiveDescriptor.class.getName(), descriptor.getClass().getName());
    jarUrl = explodedPar.toURL();
    descriptor = StandardArchiveDescriptorFactory.INSTANCE.buildArchiveDescriptor(jarUrl);
    assertEquals(ExplodedArchiveDescriptor.class.getName(), descriptor.getClass().getName());
    jarUrl = new URL(defaultPar.toURL().toExternalForm().replace("file:", "vfszip:"));
    descriptor = StandardArchiveDescriptorFactory.INSTANCE.buildArchiveDescriptor(jarUrl);
    assertEquals(JarFileBasedArchiveDescriptor.class.getName(), descriptor.getClass().getName());
    jarUrl = new URL(explodedPar.toURL().toExternalForm().replace("file:", "vfsfile:"));
    descriptor = StandardArchiveDescriptorFactory.INSTANCE.buildArchiveDescriptor(jarUrl);
    assertEquals(ExplodedArchiveDescriptor.class.getName(), descriptor.getClass().getName());
}
Also used : URLStreamHandler(java.net.URLStreamHandler) JarFileBasedArchiveDescriptor(org.hibernate.boot.archive.internal.JarFileBasedArchiveDescriptor) URLStreamHandlerFactory(java.net.URLStreamHandlerFactory) ExplodedArchiveDescriptor(org.hibernate.boot.archive.internal.ExplodedArchiveDescriptor) JarFileBasedArchiveDescriptor(org.hibernate.boot.archive.internal.JarFileBasedArchiveDescriptor) JarProtocolArchiveDescriptor(org.hibernate.boot.archive.internal.JarProtocolArchiveDescriptor) ExplodedArchiveDescriptor(org.hibernate.boot.archive.internal.ExplodedArchiveDescriptor) ArchiveDescriptor(org.hibernate.boot.archive.spi.ArchiveDescriptor) File(java.io.File) URL(java.net.URL) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 5 with URLStreamHandler

use of java.net.URLStreamHandler in project camel by apache.

the class SplunkConnectionFactory method createService.

public synchronized Service createService(CamelContext camelContext) {
    final ServiceArgs args = new ServiceArgs();
    if (host != null) {
        args.setHost(host);
    }
    if (port > 0) {
        args.setPort(port);
    }
    if (scheme != null) {
        args.setScheme(scheme);
    }
    if (app != null) {
        args.setApp(app);
    }
    if (owner != null) {
        args.setOwner(owner);
    }
    args.setUsername(username);
    args.setPassword(password);
    // (wls i'm looking at you)
    if (isUseSunHttpsHandler()) {
        String sunHandlerClassName = "sun.net.www.protocol.https.Handler";
        Class<URLStreamHandler> clazz = camelContext.getClassResolver().resolveClass(sunHandlerClassName, URLStreamHandler.class);
        if (clazz != null) {
            URLStreamHandler handler = camelContext.getInjector().newInstance(clazz);
            args.setHTTPSHandler(handler);
            LOG.debug("using the URLStreamHandler {} for {}", handler, args);
        } else {
            LOG.warn("could not resolve and use the URLStreamHandler class '{}'", sunHandlerClassName);
        }
    }
    ExecutorService executor = camelContext.getExecutorServiceManager().newSingleThreadExecutor(this, "DefaultSplunkConnectionFactory");
    Future<Service> future = executor.submit(new Callable<Service>() {

        public Service call() throws Exception {
            if (Service.DEFAULT_SCHEME.equals(getScheme())) {
                LOG.debug("Https in use. Setting SSL protocol to {}", getSslProtocol());
                HttpService.setSslSecurityProtocol(getSslProtocol());
            }
            return Service.connect(args);
        }
    });
    try {
        Service service = null;
        if (connectionTimeout > 0) {
            service = future.get(connectionTimeout, TimeUnit.MILLISECONDS);
        } else {
            service = future.get();
        }
        LOG.info("Successfully connected to Splunk");
        return service;
    } catch (Exception e) {
        throw new RuntimeException(String.format("could not connect to Splunk Server @ %s:%d - %s", host, port, e.getMessage()));
    } finally {
        if (executor != null) {
            camelContext.getExecutorServiceManager().shutdownNow(executor);
        }
    }
}
Also used : URLStreamHandler(java.net.URLStreamHandler) ServiceArgs(com.splunk.ServiceArgs) ExecutorService(java.util.concurrent.ExecutorService) Service(com.splunk.Service) HttpService(com.splunk.HttpService) ExecutorService(java.util.concurrent.ExecutorService)

Aggregations

URLStreamHandler (java.net.URLStreamHandler)7 URL (java.net.URL)5 IOException (java.io.IOException)2 URLStreamHandlerFactory (java.net.URLStreamHandlerFactory)2 Vector (java.util.Vector)2 Request (com.iplanet.services.comm.share.Request)1 RequestSet (com.iplanet.services.comm.share.RequestSet)1 Response (com.iplanet.services.comm.share.Response)1 SSOException (com.iplanet.sso.SSOException)1 HttpService (com.splunk.HttpService)1 Service (com.splunk.Service)1 ServiceArgs (com.splunk.ServiceArgs)1 AuthException (com.sun.identity.authentication.service.AuthException)1 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)1 AMSecurityPropertiesException (com.sun.identity.security.AMSecurityPropertiesException)1 L10NMessageImpl (com.sun.identity.shared.locale.L10NMessageImpl)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 Constructor (java.lang.reflect.Constructor)1 HttpURLConnection (java.net.HttpURLConnection)1