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;
}
});
}
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);
}
}
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;
}
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());
}
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);
}
}
}
Aggregations