List of usage examples for java.net HttpURLConnection getHeaderField
public String getHeaderField(int n)
From source file:org.broad.igv.util.HttpUtils.java
/** * Compare a local and remote resource, returning true if it is believed that the * remote file is newer than the local file * * @param file/*from w w w .java2 s . c om*/ * @param url * @param compareContentLength Whether to use the content length to compare files. If false, only * the modified date is used * @return true if the files are the same or the local file is newer, false if the remote file has been modified wrt the local one. * @throws IOException */ public boolean remoteIsNewer(File file, URL url, boolean compareContentLength) throws IOException { if (!file.exists()) { return false; } HttpURLConnection conn = openConnection(url, null, "HEAD"); // Check content-length first long contentLength = -1; String contentLengthString = conn.getHeaderField("Content-Length"); if (contentLengthString != null) { try { contentLength = Long.parseLong(contentLengthString); } catch (NumberFormatException e) { log.error("Error parsing content-length string: " + contentLengthString + " from URL: " + url.toString()); contentLength = -1; } } if (contentLength != file.length()) { return true; } // Compare last-modified dates String lastModifiedString = conn.getHeaderField("Last-Modified"); if (lastModifiedString == null) { return false; } else { HttpDate date = new HttpDate(); date.parse(lastModifiedString); long remoteModifiedTime = date.getTime(); long localModifiedTime = file.lastModified(); return remoteModifiedTime > localModifiedTime; } }
From source file:uk.bl.wa.annotation.AnnotationsFromAct.java
/** * Performs login operation to ACT, setting Cookie and CSRF. * @throws IOException/*from www.ja va 2 s . c o m*/ */ private void actLogin() throws IOException { Config loginConf = ConfigFactory.parseFile(new File("credentials.conf")); URL login = new URL(loginConf.getString("act.login")); LOG.info("Logging in at " + login); HttpURLConnection connection = (HttpURLConnection) login.openConnection(); StringBuilder credentials = new StringBuilder(); credentials.append(loginConf.getString("act.username")); credentials.append(":"); credentials.append(loginConf.getString("act.password")); connection.setRequestProperty("Authorization", "Basic " + Base64.byteArrayToBase64(credentials.toString().getBytes())); connection.setRequestProperty("Content-Type", "text/plain"); Scanner scanner; if (connection.getResponseCode() != 200) { scanner = new Scanner(connection.getErrorStream()); scanner.useDelimiter("\\Z"); throw new IOException(scanner.next()); } else { scanner = new Scanner(connection.getInputStream()); } scanner.useDelimiter("\\Z"); this.csrf = scanner.next(); this.cookie = connection.getHeaderField("set-cookie"); }
From source file:hudson.remoting.Engine.java
@SuppressWarnings({ "ThrowableInstanceNeverThrown" }) @Override//w ww . ja va2 s . co m public void run() { try { boolean first = true; while (true) { if (first) { first = false; } else { if (noReconnect) return; // exit } listener.status("Locating server among " + candidateUrls); Throwable firstError = null; String port = null; for (URL url : candidateUrls) { String s = url.toExternalForm(); if (!s.endsWith("/")) s += '/'; URL salURL = new URL(s + "tcpSlaveAgentListener/"); // find out the TCP port HttpURLConnection con = (HttpURLConnection) salURL.openConnection(); if (con instanceof HttpURLConnection && credentials != null) { String encoding = new String(Base64.encodeBase64(credentials.getBytes())); con.setRequestProperty("Authorization", "Basic " + encoding); } try { try { con.setConnectTimeout(30000); con.setReadTimeout(60000); con.connect(); } catch (IOException x) { if (firstError == null) { firstError = new IOException( "Failed to connect to " + salURL + ": " + x.getMessage()).initCause(x); } continue; } port = con.getHeaderField("X-Hudson-JNLP-Port"); if (con.getResponseCode() != 200) { if (firstError == null) firstError = new Exception(salURL + " is invalid: " + con.getResponseCode() + " " + con.getResponseMessage()); continue; } if (port == null) { if (firstError == null) firstError = new Exception(url + " is not Hudson"); continue; } } finally { con.disconnect(); } // this URL works. From now on, only try this URL hudsonUrl = url; firstError = null; candidateUrls = Collections.singletonList(hudsonUrl); break; } if (firstError != null) { listener.error(firstError); return; } Socket s = connect(port); listener.status("Handshaking"); DataOutputStream dos = new DataOutputStream(s.getOutputStream()); BufferedInputStream in = new BufferedInputStream(s.getInputStream()); dos.writeUTF("Protocol:JNLP2-connect"); Properties props = new Properties(); props.put("Secret-Key", secretKey); props.put("Node-Name", slaveName); if (cookie != null) props.put("Cookie", cookie); ByteArrayOutputStream o = new ByteArrayOutputStream(); props.store(o, null); dos.writeUTF(o.toString("UTF-8")); String greeting = readLine(in); if (greeting.startsWith("Unknown protocol")) { LOGGER.info("The server didn't understand the v2 handshake. Falling back to v1 handshake"); s.close(); s = connect(port); in = new BufferedInputStream(s.getInputStream()); dos = new DataOutputStream(s.getOutputStream()); dos.writeUTF("Protocol:JNLP-connect"); dos.writeUTF(secretKey); dos.writeUTF(slaveName); greeting = readLine(in); // why, oh why didn't I use DataOutputStream when writing to the network? if (!greeting.equals(GREETING_SUCCESS)) { onConnectionRejected(greeting); continue; } } else { if (greeting.equals(GREETING_SUCCESS)) { Properties responses = readResponseHeaders(in); cookie = responses.getProperty("Cookie"); } else { onConnectionRejected(greeting); continue; } } final Socket socket = s; final Channel channel = new Channel("channel", executor, in, new BufferedOutputStream(s.getOutputStream())); PingThread t = new PingThread(channel) { protected void onDead() { try { if (!channel.isInClosed()) { LOGGER.info("Ping failed. Terminating the socket."); socket.close(); } } catch (IOException e) { LOGGER.log(SEVERE, "Failed to terminate the socket", e); } } }; t.start(); listener.status("Connected"); channel.join(); listener.status("Terminated"); t.interrupt(); // make sure the ping thread is terminated listener.onDisconnect(); if (noReconnect) return; // exit // try to connect back to the server every 10 secs. waitForServerToBack(); } } catch (Throwable e) { listener.error(e); } }
From source file:org.wso2.carbon.appmanager.integration.ui.Util.HttpUtil.java
public static HttpResponse doPut(URL endpoint, String postBody, Map<String, String> headers) throws Exception { HttpURLConnection urlConnection = null; try {/*from w w w. j ava 2 s . c o m*/ urlConnection = (HttpURLConnection) endpoint.openConnection(); try { urlConnection.setRequestMethod("PUT"); } catch (ProtocolException e) { throw new Exception("Shouldn't happen: HttpURLConnection doesn't support POST??", e); } urlConnection.setDoOutput(true); if (headers != null && headers.size() > 0) { Iterator<String> itr = headers.keySet().iterator(); while (itr.hasNext()) { String key = itr.next(); urlConnection.setRequestProperty(key, headers.get(key)); } } OutputStream out = urlConnection.getOutputStream(); try { Writer writer = new OutputStreamWriter(out, "UTF-8"); writer.write(postBody); writer.close(); } catch (IOException e) { throw new Exception("IOException while puting data", e); } finally { if (out != null) { out.close(); } } // Get the response StringBuilder sb = new StringBuilder(); BufferedReader rd = null; try { rd = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); String line; while ((line = rd.readLine()) != null) { sb.append(line); } } catch (FileNotFoundException ignored) { } finally { if (rd != null) { rd.close(); } } Iterator<String> itr = urlConnection.getHeaderFields().keySet().iterator(); Map<String, String> responseHeaders = new HashMap(); while (itr.hasNext()) { String key = itr.next(); if (key != null) { responseHeaders.put(key, urlConnection.getHeaderField(key)); } } return new HttpResponse(sb.toString(), urlConnection.getResponseCode(), responseHeaders); } catch (IOException e) { throw new Exception("Connection error (is server running at " + endpoint + " ?): " + e); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } }
From source file:edu.umn.cs.spatialHadoop.nasa.HTTPFileSystem.java
/** * Returns the status of a file. This method is designed specifically to work * with LP DAAC archive and will not work correctly with other web sites. * Since HTTP does not tell whether a URL points to a file or directory, * we assume that URLs ending with HDF, XML and JPG are files while anything * else is considered a directory./*w w w. ja va2s . co m*/ */ @Override public FileStatus getFileStatus(Path f) throws IOException { f = f.makeQualified(this); URL url = f.toUri().toURL(); int retryCount = HTTPFileSystem.retries; HttpURLConnection connection = null; try { while (connection == null && retryCount-- > 0) { try { connection = (HttpURLConnection) url.openConnection(); } catch (java.net.SocketException e) { if (retryCount == 0) throw e; LOG.info("Error accessing file '" + url + "'. Trials left: " + retryCount); try { ; Thread.sleep(1000); } catch (InterruptedException e1) { } } catch (java.net.UnknownHostException e) { if (retryCount == 0) throw e; LOG.info("Error accessing file '" + url + "'. Trials left: " + retryCount); try { Thread.sleep(1000); } catch (InterruptedException e1) { } } } if (connection == null) throw new RuntimeException("Could not connect to " + f); String lengthStr = connection.getHeaderField("content-Length"); long length = lengthStr == null ? -1 : Long.parseLong(lengthStr); if (length == -1) LOG.info("Unknown HTTP file length " + length); long modificationTime = connection.getLastModified(); if (modificationTime == 0) modificationTime = connection.getDate(); // Hard coded to work with LP DAAC archives boolean isdir = !f.getName().matches("(?i:([^*\\?])*\\.(hdf|xml|jpg|gz|bz2|zip|txt|csv|tsv)$)"); return new FileStatus(length, isdir, 1, BLOCK_SIZE, modificationTime, 0, null, null, null, f); } finally { if (connection != null) connection.disconnect(); } }
From source file:alluxio.client.rest.S3ClientRestApiTest.java
@Test public void getObjectMetadata() throws Exception { final String bucket = "bucket"; createBucketRestCall(bucket);/*from ww w .j a v a 2s.c om*/ final String objectKey = bucket + AlluxioURI.SEPARATOR + "object.txt"; final byte[] objectContent = CommonUtils.randomAlphaNumString(10).getBytes(); createObjectRestCall(objectKey, objectContent, null, NO_PARAMS); HttpURLConnection connection = getObjectMetadataRestCall(objectKey); URIStatus status = mFileSystem.getStatus(new AlluxioURI(AlluxioURI.SEPARATOR + objectKey)); // remove the milliseconds from the last modification time because the accuracy of HTTP dates // is up to seconds. long lastModified = status.getLastModificationTimeMs() / 1000 * 1000; Assert.assertEquals(lastModified, connection.getLastModified()); Assert.assertEquals(String.valueOf(status.getLength()), connection.getHeaderField(S3Constants.S3_CONTENT_LENGTH_HEADER)); }
From source file:org.jets3t.tests.TestRestS3Service.java
public void testUrlSigning() throws Exception { RestS3Service service = (RestS3Service) getStorageService(getCredentials()); StorageBucket bucket = createBucketForTest("testUrlSigning"); String bucketName = bucket.getName(); try {/*from ww w. ja va 2s .c om*/ // Create test object, with private ACL String dataString = "Text for the URL Signing test object..."; S3Object object = new S3Object("Testing URL Signing", dataString); object.setContentType("text/html"); object.addMetadata(service.getRestMetadataPrefix() + "example-header", "example-value"); object.setAcl(AccessControlList.REST_CANNED_PRIVATE); // Determine what the time will be in 5 minutes. Calendar cal = Calendar.getInstance(); cal.add(Calendar.MINUTE, 5); Date expiryDate = cal.getTime(); // Create a signed HTTP PUT URL. String signedPutUrl = service.createSignedPutUrl(bucket.getName(), object.getKey(), object.getMetadataMap(), expiryDate, false); // Put the object in S3 using the signed URL (no AWS credentials required) RestS3Service restS3Service = new RestS3Service(null); restS3Service.putObjectWithSignedUrl(signedPutUrl, object); // Ensure the object was created. StorageObject objects[] = service.listObjects(bucketName, object.getKey(), null); assertEquals("Signed PUT URL failed to put/create object", objects.length, 1); // Change the object's content-type and ensure the signed PUT URL disallows the put. object.setContentType("application/octet-stream"); try { restS3Service.putObjectWithSignedUrl(signedPutUrl, object); fail("Should not be able to use a signed URL for an object with a changed content-type"); } catch (ServiceException e) { object.setContentType("text/html"); } // Add an object header and ensure the signed PUT URL disallows the put. object.addMetadata(service.getRestMetadataPrefix() + "example-header-2", "example-value"); try { restS3Service.putObjectWithSignedUrl(signedPutUrl, object); fail("Should not be able to use a signed URL for an object with changed metadata"); } catch (ServiceException e) { object.removeMetadata(service.getRestMetadataPrefix() + "example-header-2"); } // Change the object's name and ensure the signed PUT URL uses the signed name, not the object name. String originalName = object.getKey(); object.setKey("Testing URL Signing 2"); object.setDataInputStream(new ByteArrayInputStream(dataString.getBytes())); object = restS3Service.putObjectWithSignedUrl(signedPutUrl, object); assertEquals("Ensure returned object key is renamed based on signed PUT URL", originalName, object.getKey()); // Test last-resort MD5 sanity-check for uploaded object when ETag is missing. S3Object objectWithoutETag = new S3Object("Object Without ETag"); objectWithoutETag.setContentType("text/html"); String objectWithoutETagSignedPutURL = service.createSignedPutUrl(bucket.getName(), objectWithoutETag.getKey(), objectWithoutETag.getMetadataMap(), expiryDate, false); objectWithoutETag.setDataInputStream(new ByteArrayInputStream(dataString.getBytes())); objectWithoutETag.setContentLength(dataString.getBytes().length); restS3Service.putObjectWithSignedUrl(objectWithoutETagSignedPutURL, objectWithoutETag); service.deleteObject(bucketName, objectWithoutETag.getKey()); // Ensure we can't get the object with a normal URL. String s3Url = "https://s3.amazonaws.com"; URL url = new URL(s3Url + "/" + bucket.getName() + "/" + RestUtils.encodeUrlString(object.getKey())); assertEquals("Expected denied access (403) error", 403, ((HttpURLConnection) url.openConnection()).getResponseCode()); // Create a signed HTTP GET URL. String signedGetUrl = service.createSignedGetUrl(bucket.getName(), object.getKey(), expiryDate, false); // Ensure the signed URL can retrieve the object. url = new URL(signedGetUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); assertEquals("Expected signed GET URL (" + signedGetUrl + ") to retrieve object with response code 200", 200, conn.getResponseCode()); // Sanity check the data in the S3 object. String objectData = (new BufferedReader(new InputStreamReader(conn.getInputStream()))).readLine(); assertEquals("Unexpected data content in S3 object", dataString, objectData); // Confirm we got the expected Content-Type assertEquals("text/html", conn.getHeaderField("content-type")); // Modify response data via special "response-*" request parameters signedGetUrl = service.createSignedUrl("GET", bucket.getName(), object.getKey(), "response-content-type=text/plain&response-content-encoding=latin1", null, // No headers (expiryDate.getTime() / 1000) // Expiry time after epoch in seconds ); url = new URL(signedGetUrl); conn = (HttpURLConnection) url.openConnection(); assertEquals("text/plain", conn.getHeaderField("content-type")); assertEquals("latin1", conn.getHeaderField("content-encoding")); // Clean up. service.deleteObject(bucketName, object.getKey()); } finally { cleanupBucketForTest("testUrlSigning"); } }
From source file:easyshop.downloadhelper.HttpPageGetter.java
public HttpPage getDHttpPage(PageRef url, String charSet) { count++;// w ww .ja va 2 s . c om log.debug("getURL(" + count + ")"); if (url.getUrlStr() == null) { ConnResponse conRes = new ConnResponse(null, null, 0, 0, 0); return new OriHttpPage(-1, null, null, null, conRes, null); } URL requestedURL = null; try { requestedURL = new URL(url.getUrlStr()); } catch (MalformedURLException e1) { // TODO Auto-generated catch block log.error("wrong urlstr" + url.getUrlStr()); ConnResponse conRes = new ConnResponse(null, null, 0, 0, 0); return new OriHttpPage(-1, null, null, null, conRes, null); } ; // System.out.println(""+requestedURL.toExternalForm()); URL referer = null; try { log.debug("Creating HTTP connection to " + requestedURL); HttpURLConnection conn = (HttpURLConnection) requestedURL.openConnection(); if (referer != null) { log.debug("Setting Referer header to " + referer); conn.setRequestProperty("Referer", referer.toExternalForm()); } if (userAgent != null) { log.debug("Setting User-Agent to " + userAgent); conn.setRequestProperty("User-Agent", userAgent); } // DateFormat dateFormat=DateFormat.getDateInstance(); // conn.setRequestProperty("If-Modlfied-Since",dateFormat.parse("2005-08-15 20:18:30").toGMTString()); conn.setUseCaches(false); // conn.setRequestProperty("connection","keep-alive"); for (Iterator it = conn.getRequestProperties().keySet().iterator(); it.hasNext();) { String key = (String) it.next(); if (key == null) { break; } String value = conn.getHeaderField(key); // System.out.println("Request header " + key + ": " + value); } log.debug("Opening URL"); long startTime = System.currentTimeMillis(); conn.connect(); String resp = conn.getResponseMessage(); log.debug("Remote server response: " + resp); int code = conn.getResponseCode(); if (code != 200) { log.error("Could not get connection for code=" + code); System.err.println("Could not get connection for code=" + code); ConnResponse conRes = new ConnResponse(null, null, 0, 0, code); return new HttpPage(requestedURL.toExternalForm(), null, conRes, null); } // if (conn.getContentLength()<=0||conn.getContentLength()>10000000){ // log.error("Content length==0"); // System.err.println("Content length==0"); // ConnResponse conRes=new ConnResponse(null,null,null,0,0,-100); // return new URLObject(-1,requestedURL, null,null,conRes); // } String respStr = conn.getHeaderField(0); long serverDate = conn.getDate(); // log.info("Server response: " + respStr); for (int i = 1; i < conn.getHeaderFields().size(); i++) { String key = conn.getHeaderFieldKey(i); if (key == null) { break; } String value = conn.getHeaderField(key); // System.out.println("Received header " + key + ": " + value); // log.debug("Received header " + key + ": " + value); } // log.debug("Getting buffered input stream from remote connection"); log.debug("start download(" + count + ")"); BufferedInputStream remoteBIS = new BufferedInputStream(conn.getInputStream()); ByteArrayOutputStream baos = new ByteArrayOutputStream(10240); byte[] buf = new byte[1024]; int bytesRead = 0; while (bytesRead >= 0) { baos.write(buf, 0, bytesRead); bytesRead = remoteBIS.read(buf); } // baos.write(remoteBIS.read(new byte[conn.getContentLength()])); // remoteBIS.close(); byte[] content = baos.toByteArray(); long timeTaken = System.currentTimeMillis() - startTime; if (timeTaken < 100) timeTaken = 500; int bytesPerSec = (int) ((double) content.length / ((double) timeTaken / 1000.0)); // log.info("Downloaded " + content.length + " bytes, " + bytesPerSec + " bytes/sec"); if (content.length < conn.getContentLength()) { log.warn("Didn't download full content for URL: " + url); // failureCount++; ConnResponse conRes = new ConnResponse(conn.getContentType(), null, content.length, serverDate, code); return new HttpPage(requestedURL.toExternalForm(), null, conRes, conn.getContentType()); } log.debug("download(" + count + ")"); ConnResponse conRes = new ConnResponse(conn.getContentType(), null, conn.getContentLength(), serverDate, code); String c = charSet; if (c == null) c = conRes.getCharSet(); HttpPage obj = new HttpPage(requestedURL.toExternalForm(), content, conRes, c); return obj; } catch (IOException ioe) { log.warn("Caught IO Exception: " + ioe.getMessage(), ioe); failureCount++; ConnResponse conRes = new ConnResponse(null, null, 0, 0, 0); return new HttpPage(requestedURL.toExternalForm(), null, conRes, null); } catch (Exception e) { log.warn("Caught Exception: " + e.getMessage(), e); failureCount++; ConnResponse conRes = new ConnResponse(null, null, 0, 0, 0); return new HttpPage(requestedURL.toExternalForm(), null, conRes, null); } }
From source file:com.connectsdk.service.AirPlayService.java
@Override public void displayImage(final String url, String mimeType, String title, String description, String iconSrc, final LaunchListener listener) { Util.runInBackground(new Runnable() { @Override/*from www . j a v a 2s .c o m*/ public void run() { ResponseListener<Object> responseListener = new ResponseListener<Object>() { @Override public void onSuccess(Object response) { LaunchSession launchSession = new LaunchSession(); launchSession.setService(AirPlayService.this); launchSession.setSessionType(LaunchSessionType.Media); Util.postSuccess(listener, new MediaLaunchObject(launchSession, AirPlayService.this)); } @Override public void onError(ServiceCommandError error) { Util.postError(listener, error); } }; String uri = getRequestURL("photo"); byte[] payload = null; try { URL imagePath = new URL(url); HttpURLConnection connection = (HttpURLConnection) imagePath.openConnection(); connection.setInstanceFollowRedirects(true); connection.setDoInput(true); connection.connect(); int responseCode = connection.getResponseCode(); boolean redirect = (responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_SEE_OTHER); if (redirect) { String newPath = connection.getHeaderField("Location"); URL newImagePath = new URL(newPath); connection = (HttpURLConnection) newImagePath.openConnection(); connection.setInstanceFollowRedirects(true); connection.setDoInput(true); connection.connect(); } InputStream input = connection.getInputStream(); Bitmap myBitmap = BitmapFactory.decodeStream(input); ByteArrayOutputStream stream = new ByteArrayOutputStream(); myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); payload = stream.toByteArray(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>( AirPlayService.this, uri, payload, responseListener); request.setHttpMethod(ServiceCommand.TYPE_PUT); request.send(); } }); }
From source file:org.wso2.carbon.appmgt.sample.deployer.http.HttpHandler.java
/** * This method is used to do a http post request * * @param url request url//from www .j av a 2 s.c o m * @param payload Content of the post request * @param sessionId sessionId for authentication * @param contentType content type of the post request * @return response * @throws java.io.IOException - Throws this when failed to fulfill a http post request */ public String doPostHttp(String url, String payload, String sessionId, String contentType) throws IOException { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); //add reuqest header con.setRequestMethod("POST"); //con.setRequestProperty("User-Agent", USER_AGENT); if (!sessionId.equals("") && !sessionId.equals("none")) { con.setRequestProperty("Cookie", "JSESSIONID=" + sessionId); } con.setRequestProperty("Content-Type", contentType); con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(payload); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); if (responseCode == 200) { BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); if (sessionId.equals("")) { String session_id = response.substring((response.lastIndexOf(":") + 3), (response.lastIndexOf("}") - 2)); return session_id; } else if (sessionId.equals("appmSamlSsoTokenId")) { return con.getHeaderField("Set-Cookie").split(";")[0].split("=")[1]; } else if (sessionId.equals("header")) { return con.getHeaderField("Set-Cookie").split("=")[1].split(";")[0]; } else { return response.toString(); } } return null; }