Search in sources :

Example 41 with URL

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

the class ObjectHelper method loadResourceAsURL.

/**
     * Attempts to load the given resource as a stream using the thread context
     * class loader or the class loader used to load this class
     *
     * @param name the name of the resource to load
     * @param loader optional classloader to attempt first
     * @return the stream or null if it could not be loaded
     */
public static URL loadResourceAsURL(String name, ClassLoader loader) {
    URL url = null;
    String resolvedName = resolveUriPath(name);
    if (loader != null) {
        url = loader.getResource(resolvedName);
    }
    if (url == null) {
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        if (contextClassLoader != null) {
            url = contextClassLoader.getResource(resolvedName);
        }
    }
    if (url == null) {
        url = ObjectHelper.class.getClassLoader().getResource(resolvedName);
    }
    if (url == null) {
        url = ObjectHelper.class.getResource(resolvedName);
    }
    return url;
}
Also used : URL(java.net.URL)

Example 42 with URL

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

the class ResourceHelper method resolveResourceAsInputStream.

/**
     * Resolves the resource.
     * <p/>
     * If possible recommended to use {@link #resolveMandatoryResourceAsUrl(org.apache.camel.spi.ClassResolver, String)}
     *
     * @param classResolver the class resolver to load the resource from the classpath
     * @param uri URI of the resource
     * @return the resource as an {@link InputStream}. Remember to close this stream after usage. Or <tt>null</tt> if not found.
     * @throws java.io.IOException is thrown if error loading the resource
     */
public static InputStream resolveResourceAsInputStream(ClassResolver classResolver, String uri) throws IOException {
    if (uri.startsWith("file:")) {
        uri = ObjectHelper.after(uri, "file:");
        uri = tryDecodeUri(uri);
        LOG.trace("Loading resource: {} from file system", uri);
        return new FileInputStream(uri);
    } else if (uri.startsWith("http:")) {
        URL url = new URL(uri);
        LOG.trace("Loading resource: {} from HTTP", uri);
        URLConnection con = url.openConnection();
        con.setUseCaches(false);
        try {
            return con.getInputStream();
        } catch (IOException e) {
            // leaking gaps in case of an exception
            if (con instanceof HttpURLConnection) {
                ((HttpURLConnection) con).disconnect();
            }
            throw e;
        }
    } else if (uri.startsWith("classpath:")) {
        uri = ObjectHelper.after(uri, "classpath:");
        uri = tryDecodeUri(uri);
    }
    // load from classpath by default
    String resolvedName = resolveUriPath(uri);
    LOG.trace("Loading resource: {} from classpath", resolvedName);
    return classResolver.loadResourceAsStream(resolvedName);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection)

Example 43 with URL

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

the class BodyAndHeaderConvertTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    exchange = new DefaultExchange(new DefaultCamelContext());
    exchange.setProperty("foo", 1234);
    Message message = exchange.getIn();
    message.setBody("<hello>world!</hello>");
    message.setHeader("bar", 567);
    message.addAttachmentObject("att", new DefaultAttachment(new URLDataSource(new URL("http://camel.apache.org/message.html"))));
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) URLDataSource(javax.activation.URLDataSource) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) URL(java.net.URL)

Example 44 with URL

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

the class ConverterTest method testFileToString.

public void testFileToString() throws Exception {
    URL resource = getClass().getResource("dummy.txt");
    assertNotNull("Cannot find resource!", resource);
    File file = new File(URLDecoder.decode(resource.getFile(), "UTF-8"));
    String text = converter.convertTo(String.class, file);
    assertNotNull("Should have returned a String!", text);
    text = text.trim();
    assertTrue("Text not read correctly: " + text, text.endsWith("Hello World!"));
}
Also used : File(java.io.File) URL(java.net.URL)

Example 45 with URL

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

the class IOConverterTest method testStringUrl.

public void testStringUrl() throws Exception {
    URL url = ObjectHelper.loadResourceAsURL("log4j2.properties");
    String s = IOConverter.toString(url, null);
    assertNotNull(s);
}
Also used : URL(java.net.URL)

Aggregations

URL (java.net.URL)8112 IOException (java.io.IOException)2006 Test (org.junit.Test)1653 File (java.io.File)1638 MalformedURLException (java.net.MalformedURLException)1165 HttpURLConnection (java.net.HttpURLConnection)1030 InputStream (java.io.InputStream)1028 ArrayList (java.util.ArrayList)633 URLConnection (java.net.URLConnection)515 InputStreamReader (java.io.InputStreamReader)473 URLClassLoader (java.net.URLClassLoader)451 BufferedReader (java.io.BufferedReader)390 HashMap (java.util.HashMap)361 URISyntaxException (java.net.URISyntaxException)286 URI (java.net.URI)269 Map (java.util.Map)259 FileInputStream (java.io.FileInputStream)227 List (java.util.List)205 FileOutputStream (java.io.FileOutputStream)194 OutputStream (java.io.OutputStream)194