List of usage examples for java.net HttpURLConnection disconnect
public abstract void disconnect();
From source file:com.google.gdocsfs.GoogleDocs.java
public final void download(URL source, File target) throws IOException { InputStream is = null;// www . j a va2s .com FileOutputStream fos = null; HttpURLConnection connection = null; try { connection = getConnection(source, "GET"); connection.connect(); is = connection.getInputStream(); fos = new FileOutputStream(target); int readCount; byte[] data = new byte[BLOCK_SIZE]; while ((readCount = is.read(data)) > 0) { fos.write(data, 0, readCount); } } finally { if (fos != null) { fos.flush(); fos.close(); } if (is != null) { is.close(); } if (connection != null) { connection.disconnect(); } } }
From source file:com.tc.util.io.ServerURL.java
public String getServerVersion(PwProvider pwProvider) throws IOException { for (int i = 0; i < 3; i++) { HttpURLConnection urlConnection = createSecureConnection(pwProvider); try {//from w w w. j av a 2 s. co m urlConnection.connect(); int responseCode = urlConnection.getResponseCode(); if (responseCode == 200) { return urlConnection.getHeaderField(VERSION_HEADER); } else { logger.info("Failed to retrieve header field, response code : " + responseCode + ", headers :\n" + readHeaderFields(urlConnection) + " body : \n[" + readLines(urlConnection) + "]"); } } finally { urlConnection.disconnect(); } logger.info("Retrying connection since response code != 200"); ThreadUtil.reallySleep(50); } throw new IOException( "Cannot retrieve " + VERSION_HEADER + " header from server url after 3 tries : " + theURL); }
From source file:de.thingweb.discovery.TDRepository.java
/** * Update existing TD/*from w ww . j a v a2s . co m*/ * * @param key in repository (/td/{id}) * @param content JSON-LD * @throws Exception in case of error */ public void updateTD(String key, byte[] content) throws Exception { URL url = new URL("http://" + repository_uri + key); HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); httpCon.setDoOutput(true); httpCon.setRequestProperty("content-type", "application/ld+json"); httpCon.setRequestMethod("PUT"); OutputStream out = httpCon.getOutputStream(); out.write(content); out.close(); // InputStream is = httpCon.getInputStream(); // ByteArrayOutputStream baos = new ByteArrayOutputStream(); // int b; // while ((b = is.read()) != -1) { // baos.write(b); // } int responseCode = httpCon.getResponseCode(); httpCon.disconnect(); if (responseCode != 200) { // error throw new RuntimeException("ResponseCodeError: " + responseCode); } }
From source file:eu.seaclouds.platform.planner.optimizerTest.service.OptimizerServiceTestIT.java
@Test(enabled = TestConstants.EnabledTest) public void testPresenceHeartbeat() { log.info("=== TEST for presence of OPTIMIZER service heartbeat in " + BASE_URL + "==="); String outputLine;// w w w. j av a 2s . c o m String completeOutput = ""; try { URL url = new URL(BASE_URL + "heartbeat"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); Assert.assertTrue(conn.getResponseCode() == 200, "Error requesting heartbeat in " + BASE_URL + ". Instead of 200 we have received code " + conn.getResponseCode()); BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); while ((outputLine = br.readLine()) != null) { completeOutput += NL + outputLine; } conn.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Assert.assertTrue(completeOutput.contains("alive"), "Optimizer NOT alive. Response of heartbeat is: " + completeOutput); log.info("=== TEST for presence of OPTIMIZER service heartbeat in " + BASE_URL + " FINISHED ==="); }
From source file:eu.codeplumbers.cosi.api.tasks.DeleteFileTask.java
private String deleteFileRequest(File file) { String result = ""; URL urlO = null;/*from w ww . j av a 2 s . c o m*/ try { urlO = new URL(url + file.getRemoteId() + "/"); HttpURLConnection conn = (HttpURLConnection) urlO.openConnection(); conn.setConnectTimeout(5000); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); conn.setRequestProperty("Authorization", authHeader); conn.setDoOutput(false); conn.setDoInput(true); conn.setRequestMethod("DELETE"); // read the response InputStream in = new BufferedInputStream(conn.getInputStream()); StringWriter writer = new StringWriter(); IOUtils.copy(in, writer, "UTF-8"); result = writer.toString(); in.close(); conn.disconnect(); } catch (MalformedURLException e) { result = e.getLocalizedMessage(); } catch (ProtocolException e) { result = e.getLocalizedMessage(); e.printStackTrace(); } catch (IOException e) { result = e.getLocalizedMessage(); } return result; }
From source file:eu.seaclouds.platform.planner.optimizerTest.service.OptimizerServiceOpenShiftTest.java
@Test(enabled = TestConstants.EnabledTestOpenShift) public void testPresenceHeartbeat() { log.info("=== TEST for presence of OPTIMIZER service heartbeat in " + BASE_URL + "==="); String outputLine;/* w ww.j a v a2s. co m*/ String completeOutput = ""; try { URL url = new URL(BASE_URL + "heartbeat"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); Assert.assertTrue(conn.getResponseCode() == 200, "Error requesting heartbeat in " + BASE_URL + ". Instead of 200 we have received code " + conn.getResponseCode()); BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); while ((outputLine = br.readLine()) != null) { completeOutput += NL + outputLine; } conn.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Assert.assertTrue(completeOutput.contains("alive"), "Optimizer NOT alive. Response of heartbeat is: " + completeOutput); log.info("=== TEST for presence of OPTIMIZER service heartbeat in " + BASE_URL + " FINISHED ==="); }
From source file:eu.seaclouds.platform.planner.optimizerTest.service.OptimizerServiceTestIT.java
@Test(enabled = false) public void testPresenceHeartbeatSNAPSHOT() { String urlstring = "http://localhost:8080/optimizer-0.8.0-SNAPSHOT/optimizer/"; log.info("=== TEST for presence of OPTIMIZER service heartbeat in " + urlstring + "==="); String outputLine;/*w w w . j ava2s . c o m*/ String completeOutput = ""; try { URL url = new URL(urlstring + "heartbeat"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); Assert.assertTrue(conn.getResponseCode() == 200, "Error requesting heartbeat in " + urlstring + " . Instead of 200 we have received code " + conn.getResponseCode()); BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); while ((outputLine = br.readLine()) != null) { completeOutput += NL + outputLine; } conn.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Assert.assertTrue(completeOutput.contains("alive"), "Optimizer NOT alive. Response of heartbeat is: " + completeOutput); log.info("=== TEST for presence of OPTIMIZER service heartbeatin " + urlstring + " FINISHED ==="); }
From source file:eu.seaclouds.platform.planner.optimizerTest.service.OptimizerServiceTestIT.java
@Test(enabled = false) public void testPresenceHeartbeatDIRECT() { String urlstring = "http://localhost:8080/optimizer/"; log.info("=== TEST for presence of OPTIMIZER service heartbeat in " + "urlstring " + "==="); String outputLine;//from w w w . j a v a2s . co m String completeOutput = ""; try { URL url = new URL(urlstring + "heartbeat"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); Assert.assertTrue(conn.getResponseCode() == 200, "Error requesting heartbeat in " + urlstring + " . Instead of 200 we have received code " + conn.getResponseCode()); BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); while ((outputLine = br.readLine()) != null) { completeOutput += NL + outputLine; } conn.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Assert.assertTrue(completeOutput.contains("alive"), "Optimizer NOT alive. Response of heartbeat is: " + completeOutput); log.info("=== TEST for presence of OPTIMIZER service heartbeatin " + "urlstring " + " FINISHED ==="); }
From source file:eu.seaclouds.platform.planner.optimizerTest.service.OptimizerServiceOpenShiftTest.java
@Test(enabled = false) public void testPresenceHeartbeatSNAPSHOT() { log.info("=== TEST for presence of OPTIMIZER service DEPLOYED IN OPENSHIFT heartbeat in " + BASE_URL + " ==="); String outputLine;/*from w ww .j a va 2 s . co m*/ String completeOutput = ""; try { URL url = new URL(BASE_URL + "heartbeat"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); Assert.assertTrue(conn.getResponseCode() == 200, "Error requesting heartbeat in " + BASE_URL + " . Instead of 200 we have received code " + conn.getResponseCode()); BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); while ((outputLine = br.readLine()) != null) { completeOutput += NL + outputLine; } conn.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Assert.assertTrue(completeOutput.contains("alive"), "Optimizer NOT alive. Response of heartbeat is: " + completeOutput); log.info("=== TEST for presence of OPTIMIZER service heartbeat IN OPENSHIFT " + BASE_URL + " FINISHED ==="); }
From source file:com.joyent.manta.client.MantaClientSigningIT.java
@Test public final void testCanCreateSignedHEADUriFromPath() throws IOException { if (config.isClientEncryptionEnabled()) { throw new SkipException("Signed URLs are not decrypted by the client"); }// ww w .j a va 2 s .c om final String name = UUID.randomUUID().toString(); final String path = testPathPrefix + name; mantaClient.put(path, TEST_DATA); // This will throw an error if the newly inserted object isn't present mantaClient.head(path); Instant expires = Instant.now().plus(1, ChronoUnit.HOURS); URI uri = mantaClient.getAsSignedURI(path, "HEAD", expires); HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection(); try { connection.setReadTimeout(3000); connection.setRequestMethod("HEAD"); connection.connect(); Map<String, List<String>> headers = connection.getHeaderFields(); if (connection.getResponseCode() != 200) { Assert.fail(IOUtils.toString(connection.getErrorStream(), Charset.defaultCharset())); } Assert.assertNotNull(headers); Assert.assertEquals(TEST_DATA.length(), connection.getContentLength()); } finally { connection.disconnect(); } }