Search in sources :

Example 81 with URLConnection

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);
            }
        });
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) RestResponse(com.linkedin.r2.message.rest.RestResponse) ArrayList(java.util.ArrayList) ByteString(com.linkedin.data.ByteString) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URI(java.net.URI) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) RestLiAttachmentReader(com.linkedin.restli.common.attachments.RestLiAttachmentReader) Test(org.testng.annotations.Test)

Example 82 with URLConnection

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);
}
Also used : URLConnection(java.net.URLConnection)

Example 83 with URLConnection

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;
}
Also used : Bitmap(android.graphics.Bitmap) InputStream(java.io.InputStream) URLConnection(java.net.URLConnection) URL(java.net.URL)

Example 84 with URLConnection

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();
}
Also used : URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 85 with URLConnection

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) {
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URL(java.net.URL) Test(org.junit.Test)

Aggregations

URLConnection (java.net.URLConnection)950 URL (java.net.URL)654 IOException (java.io.IOException)378 HttpURLConnection (java.net.HttpURLConnection)310 InputStream (java.io.InputStream)279 InputStreamReader (java.io.InputStreamReader)227 BufferedReader (java.io.BufferedReader)208 Test (org.junit.Test)177 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)145 File (java.io.File)95 MalformedURLException (java.net.MalformedURLException)91 MockResponse (okhttp3.mockwebserver.MockResponse)76 BufferedInputStream (java.io.BufferedInputStream)57 JarURLConnection (java.net.JarURLConnection)57 OutputStream (java.io.OutputStream)55 FileOutputStream (java.io.FileOutputStream)52 FileInputStream (java.io.FileInputStream)47 ArrayList (java.util.ArrayList)47 GZIPInputStream (java.util.zip.GZIPInputStream)37 URI (java.net.URI)35