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