List of usage examples for java.net URLStreamHandler URLStreamHandler
URLStreamHandler
From source file:HttpConnections.DiffURIBuilder.java
public URL getFinalURIString() { URL endpoint = null;//from w w w. j av a 2s . c om try { endpoint = new URL(new URL(getFinalURI().build().toString()), "", new URLStreamHandler() { @Override protected URLConnection openConnection(URL url) throws IOException { URL target = new URL(url.toString()); URLConnection connection = target.openConnection(); // Connection settings connection.setConnectTimeout(30000); // 20 sec connection.setReadTimeout(60000); // 30 sec return (connection); } }); } catch (MalformedURLException ex) { Logger.getLogger(DiffURIBuilder.class.getName()).log(Level.SEVERE, null, ex); } catch (URISyntaxException ex) { Logger.getLogger(DiffURIBuilder.class.getName()).log(Level.SEVERE, null, ex); } //System.out.println("Soap URL:"+endpoint); return endpoint; }
From source file:org.calrissian.mango.jms.stream.JmsFileTransferSupportTest.java
public void testFullCycle() throws Exception { try {// w ww. j a va2s .c o m URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() { @Override public URLStreamHandler createURLStreamHandler(String protocol) { if ("testprot".equals(protocol)) return new URLStreamHandler() { @Override protected URLConnection openConnection(URL u) throws IOException { return new URLConnection(u) { @Override public void connect() throws IOException { } @Override public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(TEST_STR.getBytes()); } @Override public String getContentType() { return "content/notnull"; } }; } }; return null; } }); } catch (Error ignored) { } final ActiveMQTopic ft = new ActiveMQTopic("testFileTransfer"); ThreadPoolTaskExecutor te = new ThreadPoolTaskExecutor(); te.initialize(); JmsFileSenderListener listener = new JmsFileSenderListener(); final String hashAlgorithm = "MD5"; listener.setHashAlgorithm(hashAlgorithm); listener.setStreamRequestDestination(ft); listener.setPieceSize(9); listener.setTaskExecutor(te); ConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"); cf = new SingleTopicConnectionFactory(cf, "test"); JmsTemplate jmsTemplate = new JmsTemplate(); jmsTemplate.setConnectionFactory(cf); jmsTemplate.setReceiveTimeout(60000); listener.setJmsTemplate(jmsTemplate); Connection conn = cf.createConnection(); conn.start(); Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = sess.createConsumer(ft); consumer.setMessageListener(listener); JmsFileReceiver receiver = new JmsFileReceiver(); receiver.setHashAlgorithm(hashAlgorithm); receiver.setStreamRequestDestination(ft); receiver.setJmsTemplate(jmsTemplate); receiver.setPieceSize(9); JmsFileReceiverInputStream stream = (JmsFileReceiverInputStream) receiver.receiveStream("testprot:test"); StringBuilder buffer = new StringBuilder(); int read; while ((read = stream.read()) >= 0) { buffer.append((char) read); } stream.close(); assertEquals(TEST_STR, buffer.toString()); conn.stop(); }
From source file:com.nabla.wapp.report.server.StreamResolvingResourceLocator.java
private static URLStreamHandler getURLStreamHandler(final InputStream resource) { return new URLStreamHandler() { @Override/*from w w w . j a v a2s. co m*/ protected URLConnection openConnection(URL url) throws IOException { return new ResourceConnection(url, resource); } }; }
From source file:com.jaspersoft.jasperserver.api.engine.jasperreports.util.PermissionsListProtectionDomainProvider.java
protected CodeSource getCodeSource() { try {//from w ww . j a v a2 s. c o m URL location = new URL(null, "repo:/", new URLStreamHandler() { protected URLConnection openConnection(URL u) throws IOException { throw new IOException("Cannot read from repo:/ location"); } }); return new CodeSource(location, (Certificate[]) null); } catch (MalformedURLException e) { throw new JSExceptionWrapper(e); } }
From source file:com.puppycrawl.tools.checkstyle.api.LocalizedMessageTest.java
private static URL getMockUrl(final URLConnection connection) throws IOException { final URLStreamHandler handler = new URLStreamHandler() { @Override//from w w w. j a v a 2 s . co m protected URLConnection openConnection(final URL url) { return connection; } }; return new URL("http://foo.bar", "foo.bar", 80, "", handler); }
From source file:com.samczsun.helios.bootloader.Bootloader.java
private static void loadSWTLibrary() throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { String name = getOSName();// www.j a v a 2 s . com if (name == null) throw new IllegalArgumentException("Cannot determine OS"); String arch = getArch(); if (arch == null) throw new IllegalArgumentException("Cannot determine architecture"); String swtLocation = "/swt/org.eclipse.swt." + name + "." + arch + "-" + Constants.SWT_VERSION + ".jar"; System.out.println("Loading SWT version " + swtLocation.substring(5)); InputStream swtIn = Bootloader.class.getResourceAsStream(swtLocation); if (swtIn == null) throw new IllegalArgumentException("SWT library not found"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); IOUtils.copy(swtIn, outputStream); ByteArrayInputStream swt = new ByteArrayInputStream(outputStream.toByteArray()); URL.setURLStreamHandlerFactory(protocol -> { //JarInJar! if (protocol.equals("swt")) { return new URLStreamHandler() { protected URLConnection openConnection(URL u) { return new URLConnection(u) { public void connect() { } public InputStream getInputStream() { return swt; } }; } protected void parseURL(URL u, String spec, int start, int limit) { // Don't parse or it's too slow } }; } return null; }); ClassLoader classLoader = Bootloader.class.getClassLoader(); Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); method.setAccessible(true); method.invoke(classLoader, new URL("swt://load")); System.out.println("Loaded SWT Library"); }
From source file:com.heliosdecompiler.helios.bootloader.Bootloader.java
private static byte[] loadSWTLibrary() throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, ClassNotFoundException { String name = getOSName();//from ww w . j a va 2 s. c om if (name == null) throw new IllegalArgumentException("Cannot determine OS"); String arch = getArch(); if (arch == null) throw new IllegalArgumentException("Cannot determine architecture"); String artifactId = "org.eclipse.swt." + name + "." + arch; String swtLocation = artifactId + "-" + SWT_VERSION + ".jar"; System.out.println("Loading SWT version " + swtLocation); byte[] data = null; File savedJar = new File(Constants.DATA_DIR, swtLocation); if (savedJar.isDirectory() && !savedJar.delete()) throw new IllegalArgumentException("Saved file is a directory and could not be deleted"); if (savedJar.exists() && savedJar.canRead()) { try { System.out.println("Loading from saved file"); InputStream inputStream = new FileInputStream(savedJar); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); copy(inputStream, outputStream); data = outputStream.toByteArray(); } catch (IOException exception) { System.out.println("Failed to load from saved file."); exception.printStackTrace(System.out); } } if (data == null) { InputStream fromJar = Bootloader.class.getResourceAsStream("/swt/" + swtLocation); if (fromJar != null) { try { System.out.println("Loading from within JAR"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); copy(fromJar, outputStream); data = outputStream.toByteArray(); } catch (IOException exception) { System.out.println("Failed to load within JAR"); exception.printStackTrace(System.out); } } } if (data == null) { URL url = new URL("https://maven-eclipse.github.io/maven/org/eclipse/swt/" + artifactId + "/" + SWT_VERSION + "/" + swtLocation); try { System.out.println("Loading over the internet"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); if (connection.getResponseCode() == 200) { InputStream fromURL = connection.getInputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); copy(fromURL, outputStream); data = outputStream.toByteArray(); } else { throw new IOException(connection.getResponseCode() + ": " + connection.getResponseMessage()); } } catch (IOException exception) { System.out.println("Failed to load over the internet"); exception.printStackTrace(System.out); } } if (data == null) { throw new IllegalArgumentException("Failed to load SWT"); } if (!savedJar.exists()) { try { System.out.println("Writing to saved file"); if (savedJar.createNewFile()) { FileOutputStream fileOutputStream = new FileOutputStream(savedJar); fileOutputStream.write(data); fileOutputStream.close(); } else { throw new IOException("Could not create new file"); } } catch (IOException exception) { System.out.println("Failed to write to saved file"); exception.printStackTrace(System.out); } } byte[] dd = data; URL.setURLStreamHandlerFactory(protocol -> { //JarInJar! if (protocol.equals("swt")) { return new URLStreamHandler() { protected URLConnection openConnection(URL u) { return new URLConnection(u) { public void connect() { } public InputStream getInputStream() { return new ByteArrayInputStream(dd); } }; } protected void parseURL(URL u, String spec, int start, int limit) { // Don't parse or it's too slow } }; } return null; }); ClassLoader classLoader = Bootloader.class.getClassLoader(); Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); method.setAccessible(true); method.invoke(classLoader, new URL("swt://load")); return data; }
From source file:com.adaptris.core.MarshallingBaseCase.java
public void testUnmarshalFromUrlWithException() throws Exception { AdaptrisMarshaller marshaller = createMarshaller(); // Ha, anonymous URLStreamHandler to the rescue. URL failingUrl = new URL("http", "development.adaptris.com", 80, "index.html", new URLStreamHandler() { @Override/* w ww .j ava 2s . com*/ protected URLConnection openConnection(URL u) throws IOException { throw new IOException("testUnmarshalFromUrl"); } }); try { marshaller.unmarshal(failingUrl); fail(); } catch (CoreException e) { assertNotNull(e.getCause()); assertEquals(IOException.class, e.getCause().getClass()); assertEquals("testUnmarshalFromUrl", e.getCause().getMessage()); } }
From source file:hudson.FilePathTest.java
private URL someUrlToZipFile(final URLConnection con) throws IOException { final URLStreamHandler urlHandler = new URLStreamHandler() { @Override/*from ww w. ja va2 s . c o m*/ protected URLConnection openConnection(URL u) throws IOException { return con; } }; return new URL("http", "some-host", 0, "/some-path.zip", urlHandler); }
From source file:org.apache.hedwig.jms.spi.HedwigConnectionImpl.java
private ClientConfiguration loadConfig() throws JMSException { ClientConfiguration config = new ClientConfiguration(); // TODO: This is not very extensible and useful ... we need to pick the info from // configuration specified by user, NOT only from static files ! // Also, we need to be able to support multiple configuration in a single client ! // We need a better solution .... try {/*from www. j a v a2 s .c o m*/ // 1. try to load the client configuration as specified from a // system property if (System.getProperty(HEDWIG_CLIENT_CONFIG_FILE) != null) { File configFile = new File(System.getProperty(HEDWIG_CLIENT_CONFIG_FILE)); if (!configFile.exists()) { throw new JMSException( "Cannot create connection: cannot find Hedwig client configuration file specified as [" + System.getProperty(HEDWIG_CLIENT_CONFIG_FILE) + "]"); } config.loadConf(configFile.toURI().toURL()); } else { // 2. try to load a "hedwig-client.cfg" file from the classpath config.loadConf(new URL(null, "classpath://hedwig-client.cfg", new URLStreamHandler() { protected URLConnection openConnection(URL u) throws IOException { // rely on the relevant classloader - not system classloader. final URL resourceUrl = HedwigConnectionImpl.this.getClass().getClassLoader() .getResource(u.getPath()); return resourceUrl.openConnection(); } })); } } catch (MalformedURLException e) { JMSException je = new JMSException("Cannot load Hedwig client configuration file " + e); je.setLinkedException(e); throw je; } catch (ConfigurationException e) { JMSException je = new JMSException("Cannot load Hedwig client configuration " + e); je.setLinkedException(e); throw je; } /* System.out.println("getConsumedMessagesBufferSize : " + config.getConsumedMessagesBufferSize()); System.out.println("getDefaultServerHost : " + config.getDefaultServerHost()); System.out.println("isSSLEnabled : " + config.isSSLEnabled()); System.out.println("getMaximumMessageSize : " + config.getMaximumMessageSize()); System.out.println("getMaximumOutstandingMessages : " + config.getMaximumOutstandingMessages()); System.out.println("getMaximumServerRedirects : " + config.getMaximumServerRedirects()); System.out.println("getServerAckResponseTimeout : " + config.getServerAckResponseTimeout()); */ return config; }