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