List of usage examples for java.net HttpURLConnection getContentEncoding
public String getContentEncoding()
From source file:org.apache.flink.runtime.webmonitor.WebFrontendITCase.java
@Test public void testResponseHeaders() throws Exception { // check headers for successful json response URL taskManagersUrl = new URL("http://localhost:" + getRestPort() + "/taskmanagers"); HttpURLConnection taskManagerConnection = (HttpURLConnection) taskManagersUrl.openConnection(); taskManagerConnection.setConnectTimeout(100000); taskManagerConnection.connect();//from w w w . j av a 2 s.com if (taskManagerConnection.getResponseCode() >= 400) { // error! InputStream is = taskManagerConnection.getErrorStream(); String errorMessage = IOUtils.toString(is, ConfigConstants.DEFAULT_CHARSET); throw new RuntimeException(errorMessage); } // we don't set the content-encoding header Assert.assertNull(taskManagerConnection.getContentEncoding()); Assert.assertEquals("application/json; charset=UTF-8", taskManagerConnection.getContentType()); // check headers in case of an error URL notFoundJobUrl = new URL("http://localhost:" + getRestPort() + "/jobs/dontexist"); HttpURLConnection notFoundJobConnection = (HttpURLConnection) notFoundJobUrl.openConnection(); notFoundJobConnection.setConnectTimeout(100000); notFoundJobConnection.connect(); if (notFoundJobConnection.getResponseCode() >= 400) { // we don't set the content-encoding header Assert.assertNull(notFoundJobConnection.getContentEncoding()); Assert.assertEquals("application/json; charset=UTF-8", notFoundJobConnection.getContentType()); } else { throw new RuntimeException("Request for non-existing job did not return an error."); } }
From source file:org.jmxtrans.embedded.config.EtcdKVStore.java
private String httpGET(URL base, String key) { InputStream is = null;// w w w. j av a 2 s . c om HttpURLConnection conn = null; String json = null; try { URL url = new URL(base + "/v2/keys/" + key); conn = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY); conn.setRequestMethod("GET"); conn.setConnectTimeout(2000); conn.setReadTimeout(2000); conn.connect(); int respCode = conn.getResponseCode(); if (respCode == 404) { return null; } else if (respCode > 400) { return HTTP_ERR; } is = conn.getInputStream(); String contentEncoding = conn.getContentEncoding() != null ? conn.getContentEncoding() : "UTF-8"; json = IOUtils.toString(is, contentEncoding); } catch (MalformedURLException e) { json = HTTP_ERR; // nothing to do, try next server } catch (ProtocolException e) { // nothing to do, try next server json = HTTP_ERR; } catch (IOException e) { // nothing to do, try next server json = HTTP_ERR; } finally { if (is != null) { try { is.close(); } catch (IOException e) { // nothing to do, try next server } } if (conn != null) { conn.disconnect(); } } return json; }
From source file:eu.peppol.smp.SmpContentRetrieverImpl.java
/** * Gets the XML content of a given url, wrapped in an InputSource object. *///www .ja v a 2 s .c om @Override public InputSource getUrlContent(URL url) { HttpURLConnection httpURLConnection = null; try { httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.connect(); } catch (IOException e) { throw new IllegalStateException("Unable to connect to " + url + " ; " + e.getMessage(), e); } try { if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_UNAVAILABLE) throw new TryAgainLaterException(url, httpURLConnection.getHeaderField("Retry-After")); if (httpURLConnection.getResponseCode() != HttpURLConnection.HTTP_OK) throw new ConnectionException(url, httpURLConnection.getResponseCode()); } catch (IOException e) { throw new RuntimeException("Problem reading URL data at " + url.toExternalForm(), e); } try { String encoding = httpURLConnection.getContentEncoding(); InputStream in = new BOMInputStream(httpURLConnection.getInputStream()); InputStream result; if (encoding != null && encoding.equalsIgnoreCase(ENCODING_GZIP)) { result = new GZIPInputStream(in); } else if (encoding != null && encoding.equalsIgnoreCase(ENCODING_DEFLATE)) { result = new InflaterInputStream(in); } else { result = in; } String xml = readInputStreamIntoString(result); return new InputSource(new StringReader(xml)); } catch (Exception e) { throw new RuntimeException("Problem reading URL data at " + url.toExternalForm(), e); } }
From source file:uk.ac.gate.cloud.client.RestClient.java
/** * Read a response or error message from the given connection, * handling any 303 redirect responses if <code>followRedirects</code> * is true.// w w w.ja v a2s . c om */ private <T> T readResponseOrError(HttpURLConnection connection, TypeReference<T> responseType, boolean followRedirects) throws RestClientException { InputStream stream = null; try { int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_NO_CONTENT) { // successful response with no content storeHeaders(connection); return null; } String encoding = connection.getContentEncoding(); if ("gzip".equalsIgnoreCase(encoding)) { stream = new GZIPInputStream(connection.getInputStream()); } else { stream = connection.getInputStream(); } if (responseCode < 300 || responseCode >= 400 || !followRedirects) { try { return MAPPER.readValue(stream, responseType); } finally { storeHeaders(connection); stream.close(); } } else { // redirect - all redirects we care about from the GATE Cloud // APIs are 303. We have to follow them manually to make // authentication work properly. String location = connection.getHeaderField("Location"); // consume body IOUtils.copy(stream, new NullOutputStream()); IOUtils.closeQuietly(stream); // follow the redirect return get(location, responseType); } } catch (Exception e) { readError(connection); return null; // unreachable, as readError always throws exception } }
From source file:com.commsen.jwebthumb.WebThumbService.java
/** * Helper method used to actually send {@link WebThumb} request and check for common errors. * //from w ww. ja v a2s . c o m * @param webThumb the request to send * @return connection to extract the response from * @throws WebThumbException if any error occurs */ private HttpURLConnection sendWebThumb(WebThumb webThumb) throws WebThumbException { try { HttpURLConnection connection = (HttpURLConnection) new URL(WEB_THUMB_URL).openConnection(); connection.setInstanceFollowRedirects(false); connection.setDoOutput(true); connection.setRequestMethod("POST"); SimpleXmlSerializer.generateRequest(webThumb, connection.getOutputStream()); int responseCode = connection.getResponseCode(); String contentType = getContentType(connection); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Request sent! Got response: " + responseCode + " " + connection.getResponseMessage()); LOGGER.fine("Response content type: " + contentType); LOGGER.fine("Response content encoding: " + connection.getContentEncoding()); LOGGER.fine("Response content length: " + connection.getContentLength()); } if (responseCode == HttpURLConnection.HTTP_OK) { if (CONTENT_TYPE_TEXT_PLAIN.equals(contentType)) { throw new WebThumbException( "Server side error: " + IOUtils.toString(connection.getInputStream())); } if (!CONTENT_TYPE_TEXT_XML.equals(contentType)) { throw new WebThumbException("Unknown content type in response: " + contentType); } return connection; } else { throw new WebThumbException("Server side error: " + connection.getResponseCode() + ") " + connection.getResponseMessage()); } } catch (MalformedURLException e) { throw new WebThumbException("failed to send request", e); } catch (IOException e) { throw new WebThumbException("failed to send request", e); } }
From source file:tvhchgen.Service.java
/** * Save the content of the Url to the given path * @param urlStr//from w w w . jav a 2 s . c o m * @param outPath * @return */ public boolean saveUrl(String urlStr, String outPath) { InputStream is = null; try { //System.out.println( "Getting: " + urlStr ); URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); HttpURLConnection.setFollowRedirects(true); // allow both GZip and Deflate (ZLib) encodings conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); conn.setRequestProperty("User-Agent", DEFAULT_USER_AGENT); conn.setRequestProperty("Referer", DEFAULT_REFERER); String encoding = conn.getContentEncoding(); InputStream inStr; // create the appropriate stream wrapper based on // the encoding type if (encoding != null && encoding.equalsIgnoreCase("gzip")) { inStr = new GZIPInputStream(conn.getInputStream()); } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) { inStr = new InflaterInputStream(conn.getInputStream(), new Inflater(true)); } else { inStr = conn.getInputStream(); } //System.out.println( filePath ); File file = new File(outPath); if (!file.exists()) { file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); ReadableByteChannel rbc = Channels.newChannel(inStr); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); return true; } } catch (Exception e) { System.out.println("Exception: " + e.toString()); } finally { if (is != null) { try { is.close(); } catch (Exception e) { System.out.println("Exception: " + e.toString()); } } } return false; }
From source file:at.ac.tuwien.dsg.celar.mela.analysisservice.SpaceAndPathwayAnalysisServiceAPI.java
private String redirectToCost(String path) { URL url = null;/*from ww w . j a v a 2 s . c om*/ HttpURLConnection connection = null; String response = ""; try { url = new URL("http://" + costServiceIP + ":8182" + path); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-Type", "application/xml"); connection.setRequestProperty("Accept", "application/xml"); InputStream errorStream = connection.getErrorStream(); if (errorStream != null) { BufferedReader reader = new BufferedReader(new InputStreamReader(errorStream)); String line; while ((line = reader.readLine()) != null) { Logger.getLogger(SpaceAndPathwayAnalysisServiceAPI.class.getName()).log(Level.SEVERE, line); } } InputStream inputStream = connection.getInputStream(); String encoding = connection.getContentEncoding(); encoding = encoding == null ? "UTF-8" : encoding; response = IOUtils.toString(inputStream, encoding); } catch (Exception e) { // Logger.getLogger(MELA_API.class.getName()).log(Level.SEVERE, e.getMessage(), e); Logger.getLogger(SpaceAndPathwayAnalysisServiceAPI.class.getName()).log(Level.WARNING, "Trying to connect to MELA COST - failing ... . Retrying later"); } finally { if (connection != null) { connection.disconnect(); } } return response; }
From source file:org.apache.jmeter.protocol.http.sampler.HTTPJavaImpl.java
/** * Reads the response from the URL connection. * * @param conn/*from w w w . j a v a 2s . c om*/ * URL from which to read response * @param res * {@link SampleResult} to read response into * @return response content * @exception IOException * if an I/O exception occurs */ protected byte[] readResponse(HttpURLConnection conn, SampleResult res) throws IOException { BufferedInputStream in; final int contentLength = conn.getContentLength(); if ((contentLength == 0) && OBEY_CONTENT_LENGTH) { log.info("Content-Length: 0, not reading http-body"); res.setResponseHeaders(getResponseHeaders(conn)); res.latencyEnd(); return NULL_BA; } // works OK even if ContentEncoding is null boolean gzipped = HTTPConstants.ENCODING_GZIP.equals(conn.getContentEncoding()); InputStream instream = null; try { instream = new CountingInputStream(conn.getInputStream()); if (gzipped) { in = new BufferedInputStream(new GZIPInputStream(instream)); } else { in = new BufferedInputStream(instream); } } catch (IOException e) { if (!(e.getCause() instanceof FileNotFoundException)) { log.error("readResponse: " + e.toString()); Throwable cause = e.getCause(); if (cause != null) { log.error("Cause: " + cause); if (cause instanceof Error) { throw (Error) cause; } } } // Normal InputStream is not available InputStream errorStream = conn.getErrorStream(); if (errorStream == null) { log.info("Error Response Code: " + conn.getResponseCode() + ", Server sent no Errorpage"); res.setResponseHeaders(getResponseHeaders(conn)); res.latencyEnd(); return NULL_BA; } log.info("Error Response Code: " + conn.getResponseCode()); if (gzipped) { in = new BufferedInputStream(new GZIPInputStream(errorStream)); } else { in = new BufferedInputStream(errorStream); } } catch (Exception e) { log.error("readResponse: " + e.toString()); Throwable cause = e.getCause(); if (cause != null) { log.error("Cause: " + cause); if (cause instanceof Error) { throw (Error) cause; } } in = new BufferedInputStream(conn.getErrorStream()); } // N.B. this closes 'in' byte[] responseData = readResponse(res, in, contentLength); if (instream != null) { res.setBodySize(((CountingInputStream) instream).getCount()); instream.close(); } return responseData; }
From source file:prototypes.ws.proxy.soap.proxy.ProxyServlet.java
/** * Recept all request.//from w w w .j a v a2 s . c o m * * @param request * @param response * @throws ServletException * @throws IOException */ @Override protected void doRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpURLConnection httpConn = null; LOGGER.debug("doRequest"); BackendExchange backendExchange = RequestContext.getBackendExchange(request); LOGGER.trace("BackendExchange Hashcode : {}", Integer.toHexString(backendExchange.hashCode())); try { URL targetUrl = Requests.resolveTargetUrl(request, backendExchange.getUri()); httpConn = prepareBackendConnection(targetUrl, request, backendExchange.getRequestHeaders()); // save final state of request headers backendExchange.setRequestHeaders(httpConn.getRequestProperties()); // Send request byte[] body = backendExchange.getRequestBody(); backendExchange.start(); boolean gzipped = false; try { if (body.length > 0) { httpConn.getOutputStream().write(body); } else { LOGGER.warn("Body Empty"); } gzipped = "gzip".equals(httpConn.getContentEncoding()); // Get response. If response is gzipped, uncompress it backendExchange.setResponseBody(Streams.getBytes(httpConn.getInputStream(), gzipped)); } catch (java.net.SocketTimeoutException ex) { throw new IOException("Time out : " + ex.getMessage(), ex); } catch (IOException ex) { LOGGER.warn("Failed to read target response body {}", ex); backendExchange.setResponseBody(Streams.getBytes(httpConn.getErrorStream(), gzipped)); } finally { backendExchange.stop(); } // Stores infos backendExchange.setResponseCode(httpConn.getResponseCode()); backendExchange.setResponseHeaders(httpConn.getHeaderFields()); // Specific error code treatment switch (backendExchange.getResponseCode()) { case 0: // No response LOGGER.debug("ResponseCode = 0 !!!"); Requests.sendErrorServer(request, response, String.format(ProxyErrorConstants.EMPTY_RESPONSE, targetUrl.toString())); return; case 404: LOGGER.debug("404 returned"); Requests.sendErrorServer(request, response, String.format(ProxyErrorConstants.NOT_FOUND, targetUrl.toString()), 404); return; default: break; } // return response with filtered headers List<String> respHeadersToIgnore = new ArrayList<String>(RESP_HEADERS_TO_IGNORE); addResponseHeaders(response, backendExchange, respHeadersToIgnore); response.setStatus(backendExchange.getResponseCode()); response.getOutputStream().write(backendExchange.getResponseBody()); } catch (IllegalStateException e1) { // bad url Requests.sendErrorClient(request, response, e1.getMessage()); } catch (ClassCastException ex) { // bad url Requests.sendErrorClient(request, response, ex.getMessage()); } catch (IOException ex) { LOGGER.error("Backend call in ERROR"); // bad call Requests.sendErrorServer(request, response, ex.getMessage()); } catch (Exception ex) { LOGGER.error("Error during proxying : {}", ex); // protect from all exceptions Requests.sendInternalErrorServer(request, response, ex.getMessage()); } finally { LOGGER.trace("BackendExchange Hashcode : {}", Integer.toHexString(backendExchange.hashCode())); LOGGER.debug("BackendExchange : {}", backendExchange); if (httpConn != null) { try { httpConn.disconnect(); } catch (Exception ex) { LOGGER.warn("Error on disconnect {}", ex); } } } }
From source file:org.nuxeo.http.blobprovider.HttpBlobProvider.java
/** * Sends a HEAD request to get the info without downloading the file. * <p>/*ww w .ja v a 2s.c o m*/ * If an error occurs, returns null. * * @param urlStr * @return the BlobInfo * @since 8.1 */ public BlobInfo guessInfosFromURL(String urlStr) { BlobInfo bi = null; String attrLowerCase; try { URL url = new URL(urlStr); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); addHeaders(connection, urlStr); connection.setRequestMethod("HEAD"); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { bi = new BlobInfo(); bi.mimeType = connection.getContentType(); // Remove possible ...;charset="something" int idx = bi.mimeType.indexOf(";"); if (idx >= 0) { bi.mimeType = bi.mimeType.substring(0, idx); } bi.encoding = connection.getContentEncoding(); bi.length = connection.getContentLengthLong(); if (bi.length < 0) { bi.length = 0L; } String disposition = connection.getHeaderField("Content-Disposition"); String fileName = null; if (disposition != null) { String[] attributes = disposition.split(";"); for (String attr : attributes) { attrLowerCase = attr.toLowerCase(); if (attrLowerCase.contains("filename=")) { attr = attr.trim(); // Remove filename= fileName = attr.substring(9); idx = fileName.indexOf("\""); if (idx > -1) { fileName = fileName.substring(idx + 1, fileName.lastIndexOf("\"")); } bi.filename = fileName; break; } else if (attrLowerCase.contains("filename*=utf-8''")) { attr = attr.trim(); // Remove filename= fileName = attr.substring(17); idx = fileName.indexOf("\""); if (idx > -1) { fileName = fileName.substring(idx + 1, fileName.lastIndexOf("\"")); } fileName = java.net.URLDecoder.decode(fileName, "UTF-8"); bi.filename = fileName; break; } } } else { // Try from the url idx = urlStr.lastIndexOf("/"); if (idx > -1) { fileName = urlStr.substring(idx + 1); bi.filename = java.net.URLDecoder.decode(fileName, "UTF-8"); } } } } catch (Exception e) { // Whatever the error, we fail. No need to be // granular here. bi = null; } return bi; }