use of java.net.URLConnection in project rest.li by linkedin.
the class TestParseqTraceDebugRequestHandler method testStaticContent.
/**
* Tests the static content retrieval from the parseq trace debug request handler. It enumerates through all
* files imported into the JAR containing the parseq trace debug request handler, skips the ones that should
* not be served and verifies the rest can be retrieved. This test makes sure all files we import are actually
* servicable by the parseq trace debug request handler.
* @throws IOException
*/
@Test
public void testStaticContent() throws IOException {
ClassLoader classLoader = ParseqTraceDebugRequestHandler.class.getClassLoader();
//Collect all files under tracevis folder in the jar containing the parseq trace debug request handler.
Enumeration<URL> resources = classLoader.getResources(ParseqTraceDebugRequestHandler.class.getName().replace('.', '/') + ".class");
List<String> files = new ArrayList<String>();
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
URLConnection urlConnection = url.openConnection();
if (urlConnection instanceof JarURLConnection) {
JarURLConnection jarURLConnection = (JarURLConnection) urlConnection;
JarFile jar = jarURLConnection.getJarFile();
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry currentEntry = entries.nextElement();
if (!currentEntry.isDirectory()) {
String entry = currentEntry.getName();
if (entry.startsWith("tracevis/")) {
files.add(entry);
}
}
}
}
}
Assert.assertTrue(files.size() > 0);
// All other files should be retrievable from the parseq trace debug request handler.
for (String file : files) {
final String mimeType = determineMediaType(file);
final URI uri = URI.create("http://host/abc/12/__debug/parseqtrace/" + file.substring(file.indexOf('/') + 1));
executeRequestThroughParseqDebugHandler(uri, new RequestExecutionCallback<RestResponse>() {
@Override
public void onError(Throwable e, RequestExecutionReport executionReport, RestLiAttachmentReader requestAttachmentReader, RestLiResponseAttachments responseAttachments) {
Assert.fail("Static content cannot be retrieved for " + uri.toString());
}
@Override
public void onSuccess(RestResponse result, RequestExecutionReport executionReport, RestLiResponseAttachments responseAttachments) {
Assert.assertEquals(result.getHeader(RestConstants.HEADER_CONTENT_TYPE), mimeType);
}
});
}
}
use of java.net.URLConnection in project android-common by litesuits.
the class FileUtils method copyURLToFile.
/**
* Copies bytes from the URL <code>source</code> to a file
* <code>destination</code>. The directories up to <code>destination</code>
* will be created if they don't already exist. <code>destination</code>
* will be overwritten if it already exists.
*
* @param source the <code>URL</code> to copy bytes from, must not be {@code null}
* @param destination the non-directory <code>File</code> to write bytes to
* (possibly overwriting), must not be {@code null}
* @param connectionTimeout the number of milliseconds until this method
* will timeout if no connection could be established to the <code>source</code>
* @param readTimeout the number of milliseconds until this method will
* timeout if no data could be read from the <code>source</code>
* @throws java.io.IOException if <code>source</code> URL cannot be opened
* @throws java.io.IOException if <code>destination</code> is a directory
* @throws java.io.IOException if <code>destination</code> cannot be written
* @throws java.io.IOException if <code>destination</code> needs creating but can't be
* @throws java.io.IOException if an IO error occurs during copying
* @since 2.0
*/
public static void copyURLToFile(URL source, File destination, int connectionTimeout, int readTimeout) throws IOException {
URLConnection connection = source.openConnection();
connection.setConnectTimeout(connectionTimeout);
connection.setReadTimeout(readTimeout);
InputStream input = connection.getInputStream();
copyInputStreamToFile(input, destination);
}
use of java.net.URLConnection in project android-smart-image-view by loopj.
the class WebImage method getBitmapFromUrl.
private Bitmap getBitmapFromUrl(String url) {
Bitmap bitmap = null;
try {
URLConnection conn = new URL(url).openConnection();
conn.setConnectTimeout(CONNECT_TIMEOUT);
conn.setReadTimeout(READ_TIMEOUT);
bitmap = BitmapFactory.decodeStream((InputStream) conn.getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
use of java.net.URLConnection in project camel by apache.
the class StreamConsumer method resolveStreamFromUrl.
private InputStream resolveStreamFromUrl() throws IOException {
String u = endpoint.getUrl();
ObjectHelper.notEmpty(u, "url");
LOG.debug("About to read from url: {}", u);
URL url = new URL(u);
URLConnection c = url.openConnection();
return c.getInputStream();
}
use of java.net.URLConnection in project okhttp by square.
the class URLConnectionTest method urlHostWithNul.
@Test
public void urlHostWithNul() throws Exception {
URLConnection urlConnection = urlFactory.open(new URL("http://host /"));
try {
urlConnection.getInputStream();
fail();
} catch (UnknownHostException expected) {
}
}
Aggregations