Example usage for java.net URLStreamHandler URLStreamHandler

List of usage examples for java.net URLStreamHandler URLStreamHandler

Introduction

In this page you can find the example usage for java.net URLStreamHandler URLStreamHandler.

Prototype

URLStreamHandler

Source Link

Usage

From source file:org.apache.jmeter.protocol.http.util.LoopbackHttpClientSocketFactory.java

/**
 * Convenience method to set up the necessary HttpClient protocol and URL handler.
 *
 * Only works for HttpClient, because it's not possible (or at least very difficult)
 * to provide a different socket factory for the HttpURLConnection class.
 *//*from   w  w w.  j av  a  2 s  .co  m*/
public static void setup() {
    final String LOOPBACK = "loopback"; // $NON-NLS-1$

    // This ensures tha HttpClient knows about the protocol
    Protocol.registerProtocol(LOOPBACK, new Protocol(LOOPBACK, new LoopbackHttpClientSocketFactory(), 1));

    // Now allow the URL handling to work.
    URLStreamHandlerFactory ushf = new URLStreamHandlerFactory() {
        @Override
        public URLStreamHandler createURLStreamHandler(String protocol) {
            if (protocol.equalsIgnoreCase(LOOPBACK)) {
                return new URLStreamHandler() {
                    @Override
                    protected URLConnection openConnection(URL u) throws IOException {
                        return null;// not needed for HttpClient
                    }
                };
            }
            return null;
        }
    };

    java.net.URL.setURLStreamHandlerFactory(ushf);
}

From source file:org.apache.taverna.robundle.TestBundles.java

@Test
public void openBundleURLNonFile() throws Exception {
    final URL url = getClass().getResource("/workflowrun.bundle.zip");
    assertNotNull(url);//w  w  w . j  av a 2 s .co m
    URLStreamHandler handler = new URLStreamHandler() {
        @Override
        protected URLConnection openConnection(URL u) throws IOException {
            return url.openConnection();
        }
    };
    URL testUrl = new URL("test", "test", 0, "test", handler);
    try (Bundle b = Bundles.openBundle(testUrl)) {
        checkWorkflowrunBundle(b);
    }
}

From source file:org.auraframework.util.javascript.MultiStreamReaderTest.java

private URL getStringStreamURL(final String content) throws MalformedURLException {
    return new URL("string", null, 0, "" + System.nanoTime(), new URLStreamHandler() {
        @Override/*from  w w  w.j  a  v a2 s.  c o m*/
        protected URLConnection openConnection(URL u) throws IOException {
            return new StringStreamURLConnection(u, content);
        }
    });
}

From source file:org.bimserver.plugins.classloaders.FileJarClassLoader.java

@Override
public URL findResource(String name) {
    try {/*from  w ww.  j  a v a  2s  . c om*/
        final Lazy<InputStream> lazyInputStream = findPath(name);
        if (lazyInputStream != null) {
            try {
                URL baseUrl = new URL("file:" + name);
                URL url = new URL(baseUrl, name, 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 lazyInputStream.get();
                            }
                        };
                    }
                });
                return url;
            } catch (MalformedURLException e) {
                LOGGER.error("", e);
            }
        } else {
            LOGGER.debug("File not found: " + name + " (in " + jarFile.getFileName().toString() + ")");
        }
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return null;
}

From source file:org.bimserver.plugins.classloaders.JarClassLoader.java

@Override
protected URL findResource(final String name) {
    if (map.containsKey(name)) {
        try {/*from   w w w . j a  va2 s .c  o  m*/
            return new URL(new URL("jar:" + jarFile.toURI().toURL() + "!/" + name), name,
                    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 InflaterInputStream(new ByteArrayInputStream(map.get(name)));
                                }
                            };
                        }
                    });
        } catch (MalformedURLException e) {
            LOGGER.error("", e);
        }
    }
    return null;
}

From source file:org.bimserver.plugins.classloaders.MemoryJarClassLoader.java

@Override
public URL findResource(final String name) {
    if (map.containsKey(name)) {
        try {//from w  w w.ja  va  2  s  .c o  m
            return new URL(new URL("jar:" + jarFile.toURI().toURL() + "!/" + name), name,
                    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 InflaterInputStream(new ByteArrayInputStream(map.get(name)));
                                }
                            };
                        }
                    });
        } catch (MalformedURLException e) {
            LOGGER.error("", e);
        }
    }
    return null;
}

From source file:org.bimserver.plugins.JarClassLoader.java

@Override
protected URL findResource(final String name) {
    if (map.containsKey(name)) {
        try {//from  w w  w . j  av  a 2  s .c  o  m
            return new URL(new URL("jar:" + jarFile.toURI().toURL() + "!/" + name), name,
                    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(map.get(name));
                                }
                            };
                        }
                    });
        } catch (MalformedURLException e) {
            LOGGER.error("", e);
        }
    }
    return null;
}

From source file:org.eclipse.orion.server.filesystem.git.GitFileStore.java

public GitFileStore(String s, String authority) {
    try {//from w w  w.j  a v a2s . c  om
        gitUrl = new URL(null, s, new URLStreamHandler() {

            @Override
            protected URLConnection openConnection(URL u) throws IOException {
                // never called, see #openInputStream(int, IProgressMonitor)
                return null;
            }
        });
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException(e);
    }

    if (authority == null)
        throw new IllegalArgumentException("authority cannot be null");
    this.authority = authority;
    if (gitUrl.getQuery() == null)
        throw new IllegalArgumentException("missing query");
}

From source file:org.sakaiproject.compilatio.util.CompilatioAPIUtil.java

public static Document callCompilatioReturnDocument(String apiURL, Map<String, String> parameters,
        String secretKey, final int timeout) throws TransientSubmissionException, SubmissionException {

    SOAPConnectionFactory soapConnectionFactory;
    Document xmlDocument = null;//  w  w  w .  jav a2 s  .c o  m
    try {
        soapConnectionFactory = SOAPConnectionFactory.newInstance();

        SOAPConnection soapConnection = soapConnectionFactory.createConnection();

        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();
        SOAPEnvelope envelope = soapPart.getEnvelope();
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyAction = soapBody.addChildElement(parameters.get("action"));
        parameters.remove("action");
        // api key
        SOAPElement soapBodyKey = soapBodyAction.addChildElement("key");
        soapBodyKey.addTextNode(secretKey);

        Set<Entry<String, String>> ets = parameters.entrySet();
        Iterator<Entry<String, String>> it = ets.iterator();
        while (it.hasNext()) {
            Entry<String, String> param = it.next();
            SOAPElement soapBodyElement = soapBodyAction.addChildElement(param.getKey());
            soapBodyElement.addTextNode(param.getValue());
        }

        URL endpoint = new URL(null, apiURL, new URLStreamHandler() {
            @Override
            protected URLConnection openConnection(URL url) throws IOException {
                URL target = new URL(url.toString());
                URLConnection connection = target.openConnection();
                // Connection settings
                connection.setConnectTimeout(timeout);
                connection.setReadTimeout(timeout);
                return (connection);
            }
        });

        SOAPMessage soapResponse = soapConnection.call(soapMessage, endpoint);

        // loading the XML document
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        soapResponse.writeTo(out);
        DocumentBuilderFactory builderfactory = DocumentBuilderFactory.newInstance();
        builderfactory.setNamespaceAware(true);

        DocumentBuilder builder = builderfactory.newDocumentBuilder();
        xmlDocument = builder.parse(new InputSource(new StringReader(out.toString())));
        soapConnection.close();

    } catch (UnsupportedOperationException | SOAPException | IOException | ParserConfigurationException
            | SAXException e) {
        log.error(e);
    }
    return xmlDocument;

}

From source file:org.sakaiproject.contentreview.compilatio.util.CompilatioAPIUtil.java

public static Document callCompilatioReturnDocument(String apiURL, Map<String, String> parameters,
        String secretKey, final int timeout, Proxy proxy, boolean isMultipart)
        throws TransientSubmissionException, SubmissionException {

    SOAPConnectionFactory soapConnectionFactory;
    Document xmlDocument = null;/*from  w  w w.  j a va 2  s. c  o m*/
    try {
        soapConnectionFactory = SOAPConnectionFactory.newInstance();

        SOAPConnection soapConnection = soapConnectionFactory.createConnection();

        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();
        SOAPEnvelope envelope = soapPart.getEnvelope();
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyAction = soapBody.addChildElement(parameters.get("action"));
        parameters.remove("action");
        // api key
        SOAPElement soapBodyKey = soapBodyAction.addChildElement("key");
        soapBodyKey.addTextNode(secretKey);

        Set<Entry<String, String>> ets = parameters.entrySet();
        Iterator<Entry<String, String>> it = ets.iterator();
        while (it.hasNext()) {
            Entry<String, String> param = it.next();
            SOAPElement soapBodyElement = soapBodyAction.addChildElement(param.getKey());
            soapBodyElement.addTextNode(param.getValue());
        }

        URL endpoint = new URL(null, apiURL, new URLStreamHandler() {
            @Override
            protected URLConnection openConnection(URL url) throws IOException {
                URL target = new URL(url.toString());
                URLConnection connection = target.openConnection();
                // Connection settings
                connection.setConnectTimeout(timeout);
                connection.setReadTimeout(timeout);
                return (connection);
            }
        });

        SOAPMessage soapResponse = soapConnection.call(soapMessage, endpoint);

        // loading the XML document
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        soapResponse.writeTo(out);
        DocumentBuilderFactory builderfactory = DocumentBuilderFactory.newInstance();
        builderfactory.setNamespaceAware(true);

        DocumentBuilder builder = builderfactory.newDocumentBuilder();
        xmlDocument = builder.parse(new InputSource(new StringReader(out.toString())));
        soapConnection.close();

    } catch (UnsupportedOperationException | SOAPException | IOException | ParserConfigurationException
            | SAXException e) {
        log.error(e);
    }
    return xmlDocument;

}