Search in sources :

Example 16 with HttpURLConnection

use of java.net.HttpURLConnection in project che by eclipse.

the class OAuthAuthenticator method getJson.

protected <O> O getJson(String getUserUrl, Class<O> userClass) throws OAuthAuthenticationException {
    HttpURLConnection urlConnection = null;
    InputStream urlInputStream = null;
    try {
        urlConnection = (HttpURLConnection) new URL(getUserUrl).openConnection();
        urlInputStream = urlConnection.getInputStream();
        return JsonHelper.fromJson(urlInputStream, userClass, null);
    } catch (JsonParseException | IOException e) {
        throw new OAuthAuthenticationException(e.getMessage(), e);
    } finally {
        if (urlInputStream != null) {
            try {
                urlInputStream.close();
            } catch (IOException ignored) {
            }
        }
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) IOException(java.io.IOException) JsonParseException(org.eclipse.che.commons.json.JsonParseException) URL(java.net.URL)

Example 17 with HttpURLConnection

use of java.net.HttpURLConnection in project jetty.project by eclipse.

the class SerialRestServlet method doGet.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    long start = System.nanoTime();
    String[] keywords = sanitize(request.getParameter(ITEMS_PARAM)).split(",");
    Queue<Map<String, String>> results = new LinkedList<Map<String, String>>();
    // make all requests serially
    for (String itemName : keywords) {
        URL url = new URL(restURL(itemName));
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        Map query = (Map) JSON.parse(new BufferedReader(new InputStreamReader(connection.getInputStream())));
        Object[] auctions = (Object[]) query.get("Item");
        if (auctions != null) {
            for (Object o : auctions) results.add((Map) o);
        }
    }
    // Generate the response
    String thumbs = generateThumbs(results);
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<html><head>");
    out.println(STYLE);
    out.println("</head><body><small>");
    long now = System.nanoTime();
    long total = now - start;
    out.print("<b>Blocking: " + sanitize(request.getParameter(ITEMS_PARAM)) + "</b><br/>");
    out.print("Total Time: " + ms(total) + "ms<br/>");
    out.print("Thread held (<span class='red'>red</span>): " + ms(total) + "ms<br/>");
    out.println("<img border='0px' src='asyncrest/red.png'   height='20px' width='" + width(total) + "px'>");
    out.println("<hr />");
    out.println(thumbs);
    out.println("</small>");
    out.println("</body></html>");
    out.close();
}
Also used : InputStreamReader(java.io.InputStreamReader) LinkedList(java.util.LinkedList) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) BufferedReader(java.io.BufferedReader) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 18 with HttpURLConnection

use of java.net.HttpURLConnection in project jetty.project by eclipse.

the class JstlTest method testUrlsBasic.

@Test
public void testUrlsBasic() throws IOException {
    HttpURLConnection http = (HttpURLConnection) baseUri.resolve("/urls.jsp").toURL().openConnection();
    assertThat("http response", http.getResponseCode(), is(200));
    try (InputStream input = http.getInputStream()) {
        String resp = IO.toString(input, StandardCharsets.UTF_8);
        assertThat("Response should be JSP processed", resp, not(containsString("<c:url")));
        assertThat("Response", resp, containsString("[c:url value] = /ref.jsp;jsessionid="));
        assertThat("Response", resp, containsString("[c:url param] = ref.jsp;key=value;jsessionid="));
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 19 with HttpURLConnection

use of java.net.HttpURLConnection in project jetty.project by eclipse.

the class IncludedServletTest method testTopWithIncludedHeader.

@Test
public void testTopWithIncludedHeader() throws IOException {
    URI uri = baseUri.resolve("/top");
    System.out.println("GET (String): " + uri.toASCIIString());
    InputStream in = null;
    InputStreamReader reader = null;
    HttpURLConnection connection = null;
    try {
        connection = (HttpURLConnection) uri.toURL().openConnection();
        connection.connect();
        if (HttpURLConnection.HTTP_OK != connection.getResponseCode()) {
            String body = getPotentialBody(connection);
            String err = String.format("GET request failed (%d %s) %s%n%s", connection.getResponseCode(), connection.getResponseMessage(), uri.toASCIIString(), body);
            throw new IOException(err);
        }
        in = connection.getInputStream();
        reader = new InputStreamReader(in);
        StringWriter writer = new StringWriter();
        IO.copy(reader, writer);
        String response = writer.toString();
        // System.out.printf("Response%n%s",response);
        assertThat("Response", response, containsString("<h2> Hello, this is the top page."));
        assertThat("Response", response, containsString("<h3> This is the included page"));
        assertThat("Response Header[main-page-key]", connection.getHeaderField("main-page-key"), is("main-page-value"));
        assertThat("Response Header[included-page-key]", connection.getHeaderField("included-page-key"), is("included-page-value"));
    } finally {
        IO.close(reader);
        IO.close(in);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) StringWriter(java.io.StringWriter) InputStream(java.io.InputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) URI(java.net.URI) Test(org.junit.Test)

Example 20 with HttpURLConnection

use of java.net.HttpURLConnection in project jetty.project by eclipse.

the class JspAndDefaultWithoutAliasesTest method testGetReference.

@Test
public void testGetReference() throws Exception {
    URI uri = serverURI.resolve(path);
    HttpURLConnection conn = null;
    try {
        conn = (HttpURLConnection) uri.toURL().openConnection();
        conn.setConnectTimeout(1000);
        conn.setReadTimeout(1000);
        assertResponse(conn);
    } finally {
        conn.disconnect();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) URI(java.net.URI) Test(org.junit.Test)

Aggregations

HttpURLConnection (java.net.HttpURLConnection)3831 URL (java.net.URL)2447 IOException (java.io.IOException)1634 InputStream (java.io.InputStream)1082 InputStreamReader (java.io.InputStreamReader)692 Test (org.junit.Test)650 BufferedReader (java.io.BufferedReader)573 OutputStream (java.io.OutputStream)466 MalformedURLException (java.net.MalformedURLException)372 URLConnection (java.net.URLConnection)248 HashMap (java.util.HashMap)216 OutputStreamWriter (java.io.OutputStreamWriter)208 Map (java.util.Map)199 Gson (com.google.gson.Gson)190 ByteArrayOutputStream (java.io.ByteArrayOutputStream)186 ArrayList (java.util.ArrayList)168 ExecutionException (java.util.concurrent.ExecutionException)161 File (java.io.File)159 AsyncTask (android.os.AsyncTask)158 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)157