List of usage examples for java.net HttpURLConnection getHeaderField
public String getHeaderField(int n)
From source file:io.apiman.manager.ui.server.servlets.UrlFetchProxyServlet.java
/** * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) *//* ww w. j av a2 s . c o m*/ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String url = req.getHeader("X-Apiman-Url"); //$NON-NLS-1$ if (url == null) { resp.sendError(500, "No URL specified in X-Apiman-Url"); //$NON-NLS-1$ return; } URL remoteUrl = new URL(url); HttpURLConnection remoteConn = (HttpURLConnection) remoteUrl.openConnection(); InputStream remoteIS = null; OutputStream responseOS = null; try { remoteConn.connect(); Map<String, List<String>> headerFields = remoteConn.getHeaderFields(); for (String headerName : headerFields.keySet()) { if (headerName == null) { continue; } if (EXCLUDE_HEADERS.contains(headerName)) { continue; } String headerValue = remoteConn.getHeaderField(headerName); resp.setHeader(headerName, headerValue); } resp.setHeader("Cache-control", "no-cache, no-store, must-revalidate"); //$NON-NLS-1$ //$NON-NLS-2$ remoteIS = remoteConn.getInputStream(); responseOS = resp.getOutputStream(); IOUtils.copy(remoteIS, responseOS); resp.flushBuffer(); } catch (Exception e) { resp.sendError(500, e.getMessage()); } finally { IOUtils.closeQuietly(responseOS); IOUtils.closeQuietly(remoteIS); } }
From source file:org.sherlok.config.HttpConfigVariable.java
@Override public String getProcessedValue() throws ProcessConfigVariableException { File file = getPath();//from w ww . j a v a 2 s. com // if needed, download the file if (file == null || !file.exists()) { try { LOG.trace("Downloading file from " + url); // open an HTTP connection with the remote server URL remote = new URL(this.url); HttpURLConnection connection = (HttpURLConnection) remote.openConnection(); int status = connection.getResponseCode(); // make sure the request was successful if (status != HttpURLConnection.HTTP_OK) { throw new ProcessConfigVariableException("Status code from HTTP request is not 200: " + status); } // save the filename String disposition = connection.getHeaderField("Content-Disposition"); file = extractFileName(disposition); // save the remote file InputStream input = connection.getInputStream(); FileOutputStream output = FileUtils.openOutputStream(file); IOUtils.copy(input, output); output.close(); input.close(); connection.disconnect(); } catch (IOException e) { String msg = "Failed to download " + url; throw new ProcessConfigVariableException(msg, e); } } if (rutaCompatible) { return FileBased.getRelativePathToResources(file.getAbsoluteFile()); } else { return file.getAbsolutePath(); } }
From source file:org.apache.olingo.fit.tecsvc.http.DerivedAndMixedTypeTestITCase.java
@Test public void queryESCompCollDerivedXml() throws Exception { URL url = new URL(SERVICE_URI + "ESCompCollDerived?$format=xml"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HttpMethod.GET.name()); connection.connect();/*www. ja v a 2 s . co m*/ assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode()); assertEquals(ContentType.APPLICATION_XML, ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE))); final String content = IOUtils.toString(connection.getInputStream()); assertTrue(content.contains("<d:PropertyCompAno m:type=\"#olingo.odata.test1.CTBaseAno\">" + "<d:PropertyString>Num111</d:PropertyString>" + "<d:AdditionalPropString>Test123</d:AdditionalPropString>" + "</d:PropertyCompAno>" + "<d:CollPropertyCompAno m:type=\"#Collection(olingo.odata.test1.CTTwoPrimAno)\">" + "<m:element m:type=\"olingo.odata.test1.CTBaseAno\">" + "<d:PropertyString>TEST12345</d:PropertyString>" + "<d:AdditionalPropString>Additional12345</d:AdditionalPropString>")); }
From source file:com.sonar.it.jenkins.orchestrator.container.JenkinsDownloader.java
private File downloadUrl(String url, File toFile) { try {/*from w w w. j a v a 2 s .co m*/ FileUtils.forceMkdir(toFile.getParentFile()); HttpURLConnection conn; URL u = new URL(url); LOG.info("Download: " + u); // gets redirected multiple times, including from https -> http, so not done by Java while (true) { conn = (HttpURLConnection) u.openConnection(); conn.connect(); if (conn.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM || conn.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) { String newLocationHeader = conn.getHeaderField("Location"); LOG.info("Redirect: " + newLocationHeader); conn.disconnect(); u = new URL(newLocationHeader); continue; } break; } InputStream is = conn.getInputStream(); ByteStreams.copy(is, Files.asByteSink(toFile).openBufferedStream()); LOG.info("Downloaded to: " + toFile); return toFile; } catch (Exception e) { throw new IllegalStateException(e); } }
From source file:ms.sujinkim.com.test.api.FoscamSnapshotRunnable.java
public void run() { ToastManager manager = ToastManager.getInstance(); HttpURLConnection conn = null; String urlString = FoscamUrlFactory.getSnapshotUrl(camera); //DefaultHttpClient client = new DefaultHttpClient(); try {/*from w w w. j av a2 s .c om*/ URL url = new URL(urlString); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.connect(); int status = conn.getResponseCode(); if (status != 200) { manager.makeToast("Unable to take snapshot: " + status); conn.disconnect(); return; } String name = conn.getHeaderField(CONTENT_DISPOSITION);// ? name? ?. InputStream inputStream = conn.getInputStream(); if (inputStream != null) { writeSnapshot(name, inputStream); manager.makeToast("Saved \"" + name + "\""); } } catch (Exception e) { ToastManager.getInstance().makeToast("Unable to take snapshot: " + e.getMessage()); } finally { conn.disconnect(); } }
From source file:eu.fusepool.p3.transformer.client.TransformerClientImpl.java
private Entity getResponseEntity(HttpURLConnection connection) throws IOException, MimeTypeParseException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (connection.getResponseCode() < 400) { IOUtils.copy(connection.getInputStream(), baos); } else {/*from ww w . j a va 2 s .c o m*/ IOUtils.copy(connection.getErrorStream(), baos); } final byte[] bytes = baos.toByteArray(); final String resultContentTypeString = connection.getHeaderField("Content-Type"); final MimeType resultType = resultContentTypeString != null ? new MimeType(resultContentTypeString) : new MimeType("application", "octet-stream"); return new InputStreamEntity() { @Override public MimeType getType() { return resultType; } @Override public InputStream getData() throws IOException { return new ByteArrayInputStream(bytes); } }; }
From source file:net.sourceforge.pmd.docs.DeadLinksChecker.java
private int computeHttpResponse(String url) { try {//from w w w . j av a2 s. c o m final HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(url).openConnection(); httpURLConnection.setRequestMethod("HEAD"); httpURLConnection.setConnectTimeout(5000); httpURLConnection.setReadTimeout(15000); httpURLConnection.connect(); final int responseCode = httpURLConnection.getResponseCode(); String response = "HTTP " + responseCode; if (httpURLConnection.getHeaderField("Location") != null) { response += ", Location: " + httpURLConnection.getHeaderField("Location"); } LOG.fine("response: " + response + " on " + url); // success (HTTP 2xx) or redirection (HTTP 3xx) return responseCode; } catch (IOException ex) { LOG.fine("response: " + ex.getClass().getName() + " on " + url + " : " + ex.getMessage()); return 599; } }
From source file:com.jolira.testing.CachingRESTProxy.java
private void cacheResponse(final File queryDir, final HttpURLConnection connection, final InputStream in) throws IOException, FileNotFoundException { final String contentType = connection.getHeaderField(CONTENT_TYPE); final int code = connection.getResponseCode(); final String defaultContentType = getDefaultContentType(queryDir); final Map<String, List<String>> fields = connection.getHeaderFields(); final Collection<String> cookies = fields.get(SET_COOKIE); final boolean simple = code == SC_OK && equalsContentType(defaultContentType, contentType) && isEmpty(cookies); final File resourceFile = simple ? queryDir : getResourceFile(queryDir); copy(in, resourceFile);/* w ww.j av a 2s. c o m*/ if (simple) { return; } final Properties prps = new Properties(); if (contentType != null) { prps.put(CONTENT_TYPE, contentType); } if (cookies != null) { int idx = 0; for (final String cookie : cookies) { final String key = getCookieKey(idx++); prps.put(key, cookie); } } prps.put(STATUS_PROPERTY, Integer.toString(code)); final File propertiesFile = getPropertiesFile(queryDir); final OutputStream out = new FileOutputStream(propertiesFile); try { prps.store(out, "gerated by " + CachingRESTProxy.class); } finally { out.close(); } }
From source file:com.none.tom.simplerssreader.net.FeedDownloader.java
@SuppressWarnings("ConstantConditions") public static InputStream getInputStream(final Context context, final String feedUrl) { final ConnectivityManager manager = context.getSystemService(ConnectivityManager.class); final NetworkInfo info = manager.getActiveNetworkInfo(); if (info == null || !info.isConnected()) { ErrorHandler.setErrno(ErrorHandler.ERROR_NETWORK); LogUtils.logError("No network connection"); return null; }/* w w w.ja v a 2 s . c om*/ URL url; try { url = new URL(feedUrl); } catch (final MalformedURLException e) { ErrorHandler.setErrno(ErrorHandler.ERROR_HTTP_BAD_REQUEST, e); return null; } final boolean[] networkPrefs = DefaultSharedPrefUtils.getNetworkPreferences(context); final boolean useHttpTorProxy = networkPrefs[0]; final boolean httpsOnly = networkPrefs[1]; final boolean followRedir = networkPrefs[2]; for (int nrRedirects = 0;; nrRedirects++) { if (nrRedirects >= HTTP_NR_REDIRECTS_MAX) { ErrorHandler.setErrno(ErrorHandler.ERROR_HTTP_TOO_MANY_REDIRECTS); LogUtils.logError("Too many redirects"); return null; } HttpURLConnection conn = null; try { if (httpsOnly && url.getProtocol().equalsIgnoreCase("http")) { ErrorHandler.setErrno(ErrorHandler.ERROR_HTTPS_ONLY); LogUtils.logError("Attempting insecure connection"); return null; } if (useHttpTorProxy) { conn = (HttpURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getByName(HTTP_PROXY_TOR_IP), HTTP_PROXY_TOR_PORT))); } else { conn = (HttpURLConnection) url.openConnection(); } conn.setRequestProperty("Accept-Charset", StandardCharsets.UTF_8.name()); conn.setConnectTimeout(HTTP_URL_DEFAULT_TIMEOUT); conn.setReadTimeout(HTTP_URL_DEFAULT_TIMEOUT); conn.connect(); final int responseCode = conn.getResponseCode(); switch (responseCode) { case HttpURLConnection.HTTP_OK: return getInputStream(conn.getInputStream()); case HttpURLConnection.HTTP_MOVED_PERM: case HttpURLConnection.HTTP_MOVED_TEMP: case HttpURLConnection.HTTP_SEE_OTHER: case HTTP_TEMP_REDIRECT: if (followRedir) { final String location = conn.getHeaderField("Location"); url = new URL(url, location); if (responseCode == HttpURLConnection.HTTP_MOVED_PERM) { SharedPrefUtils.updateSubscriptionUrl(context, url.toString()); } continue; } ErrorHandler.setErrno(ErrorHandler.ERROR_HTTP_FOLLOW_REDIRECT_DENIED); LogUtils.logError("Couldn't follow redirect"); return null; default: if (responseCode < 0) { ErrorHandler.setErrno(ErrorHandler.ERROR_HTTP_RESPONSE); LogUtils.logError("No valid HTTP response"); return null; } else if (responseCode >= 400 && responseCode <= 500) { ErrorHandler.setErrno(ErrorHandler.ERROR_HTTP_CLIENT); } else if (responseCode >= 500 && responseCode <= 600) { ErrorHandler.setErrno(ErrorHandler.ERROR_HTTP_SERVER); } else { ErrorHandler.setErrno(ErrorHandler.ERROR_HTTP_UNHANDLED); } LogUtils.logError("Error " + responseCode + ": " + conn.getResponseMessage()); return null; } } catch (final IOException e) { if ((e instanceof ConnectException && e.getMessage().equals("Network is unreachable")) || e instanceof SocketTimeoutException || e instanceof UnknownHostException) { ErrorHandler.setErrno(ErrorHandler.ERROR_NETWORK, e); } else { ErrorHandler.setErrno(ErrorHandler.ERROR_HTTP_UNHANDLED, e); } return null; } finally { if (conn != null) { conn.disconnect(); } } } }
From source file:org.nuxeo.labs.operations.services.HTTPDownloadFile.java
@OperationMethod public Blob run() throws IOException { Blob result = null;/*from w w w . j av a2 s. c om*/ HttpURLConnection http = null; String error = ""; String resultStatus = ""; boolean isUnknownHost = false; try { URL theURL = new URL(url); http = (HttpURLConnection) theURL.openConnection(); HTTPUtils.addHeaders(http, headers, headersAsJSON); if (http.getResponseCode() == HttpURLConnection.HTTP_OK) { String fileName = ""; String disposition = http.getHeaderField("Content-Disposition"); String contentType = http.getContentType(); String encoding = http.getContentEncoding(); // Try to get a filename if (disposition != null) { // extracts file name from header field int index = disposition.indexOf("filename="); if (index > -1) { fileName = disposition.substring(index + 9); } } else { // extracts file name from URL fileName = url.substring(url.lastIndexOf("/") + 1, url.length()); } if (StringUtils.isEmpty(fileName)) { fileName = "DownloadedFile-" + java.util.UUID.randomUUID().toString(); } File tempFile = new File( getTempFolderPath() + File.separator + java.util.UUID.randomUUID().toString()); FileOutputStream outputStream = new FileOutputStream(tempFile); InputStream inputStream = http.getInputStream(); int bytesRead = -1; byte[] buffer = new byte[BUFFER_SIZE]; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.close(); inputStream.close(); result = new FileBlob(tempFile, contentType, encoding); result.setFilename(fileName); } } catch (Exception e) { error = e.getMessage(); if (e instanceof java.net.UnknownHostException) { isUnknownHost = true; } } finally { int status = 0; String statusMessage = ""; if (isUnknownHost) { // can't use our http variable status = 0; statusMessage = "UnknownHostException"; } else { // Still, other failures _before_ reaching the server may occur // => http.getResponseCode() and others would throw an error try { status = http.getResponseCode(); statusMessage = http.getResponseMessage(); } catch (Exception e) { statusMessage = "Error getting the status message itself"; } } ObjectMapper mapper = new ObjectMapper(); ObjectNode resultObj = mapper.createObjectNode(); resultObj.put("status", status); resultObj.put("statusMessage", statusMessage); resultObj.put("error", error); ObjectWriter ow = mapper.writer();// .withDefaultPrettyPrinter(); resultStatus = ow.writeValueAsString(resultObj); } ctx.put("httpDownloadFileStatus", resultStatus); return result; }