List of usage examples for java.net HttpURLConnection getHeaderField
public String getHeaderField(int n)
From source file:org.apache.olingo.fit.tecsvc.http.ODataVersionConformanceITCase.java
@Test public void invalidODataMaxVersionHeader1() throws Exception { URL url = new URL(SERVICE_URI + "ESAllPrim"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HttpMethod.GET.name()); connection.setRequestProperty(HttpHeader.ODATA_MAX_VERSION, "3.0"); connection.connect();/* w w w .java 2s . co m*/ assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), connection.getResponseCode()); assertEquals("4.0", connection.getHeaderField(HttpHeader.ODATA_VERSION)); final String content = IOUtils.toString(connection.getErrorStream()); assertTrue(content.contains("OData version '3.0' is not supported.")); }
From source file:org.nuxeo.s3utils.test.TestS3TempSignedUrl.java
protected File downloadFile(String url) throws IOException { File resultFile = null;//from w ww .j a v a 2 s. c om HttpURLConnection http = null; int BUFFER_SIZE = 4096; 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"); 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(); } String tempDir = System.getProperty("java.io.tmpdir"); resultFile = new File(tempDir, fileName); FileOutputStream outputStream = new FileOutputStream(resultFile); 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(); } return resultFile; }
From source file:net.technicpack.launchercore.mirror.download.Download.java
@Override @SuppressWarnings("unused") public void run() { ReadableByteChannel rbc = null; FileOutputStream fos = null;//from ww w. j a v a 2 s. c o m try { HttpURLConnection conn = Utils.openHttpConnection(url); int response = conn.getResponseCode(); int responseFamily = response / 100; if (responseFamily == 3) { String redirUrlText = conn.getHeaderField("Location"); if (redirUrlText != null && !redirUrlText.isEmpty()) { URL redirectUrl = null; try { redirectUrl = new URL(redirUrlText); } catch (MalformedURLException ex) { throw new DownloadException("Invalid Redirect URL: " + url, ex); } conn = Utils.openHttpConnection(redirectUrl); response = conn.getResponseCode(); responseFamily = response / 100; } } if (responseFamily != 2) { throw new DownloadException("The server issued a " + response + " response code."); } InputStream in = getConnectionInputStream(conn); size = conn.getContentLength(); outFile = new File(outPath); outFile.delete(); rbc = Channels.newChannel(in); fos = new FileOutputStream(outFile); stateChanged(); Thread progress = new MonitorThread(Thread.currentThread(), rbc); progress.start(); fos.getChannel().transferFrom(rbc, 0, size > 0 ? size : Integer.MAX_VALUE); in.close(); rbc.close(); progress.interrupt(); synchronized (timeoutLock) { if (isTimedOut) { return; } } if (size > 0) { if (size == outFile.length()) { result = Result.SUCCESS; } } else { result = Result.SUCCESS; } } catch (ClosedByInterruptException ex) { result = Result.FAILURE; return; } catch (PermissionDeniedException e) { exception = e; result = Result.PERMISSION_DENIED; } catch (DownloadException e) { exception = e; result = Result.FAILURE; } catch (Exception e) { exception = e; e.printStackTrace(); } finally { IOUtils.closeQuietly(fos); IOUtils.closeQuietly(rbc); } }
From source file:org.apache.olingo.fit.tecsvc.http.SelectOnComplexPropertiesITCase.java
@Test public void queryESKeyColPropertyComp2() throws Exception { URL url = new URL(SERVICE_URI + "ESKeyNav(1)/CollPropertyComp" + "?$select=PropertyInt16"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HttpMethod.GET.name()); connection.setRequestProperty(HttpHeader.ACCEPT, "application/json;odata.metadata=minimal"); connection.connect();//w w w . jav a 2 s .c o m assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode()); assertEquals(ContentType.JSON, ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE))); final String content = IOUtils.toString(connection.getInputStream()); assertTrue( content.contains("\"value\":[{\"PropertyInt16\":1},{\"PropertyInt16\":2},{\"PropertyInt16\":3}]")); }
From source file:org.apache.olingo.fit.tecsvc.http.BasicHttpITCase.java
@Test public void testAccept() throws Exception { URL url = new URL(SERVICE_URI); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HttpMethod.GET.name()); connection.setRequestProperty(HttpHeader.ACCEPT, "application/json;q=0.2;odata.metadata=minimal"); connection.connect();// w ww . ja va2s. co m assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode()); assertEquals(ContentType.JSON, ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE))); }
From source file:org.apache.olingo.fit.tecsvc.http.SelectOnComplexPropertiesITCase.java
@Test public void queryESKeyPropertyCompCompNav() throws Exception { URL url = new URL( SERVICE_URI + "ESKeyNav(1)/PropertyCompCompNav" + "?$select=PropertyCompNav/PropertyInt16"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HttpMethod.GET.name()); connection.setRequestProperty(HttpHeader.ACCEPT, "application/json;odata.metadata=minimal"); connection.connect();//from w ww . j a va 2 s. com assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode()); assertEquals(ContentType.JSON, ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE))); final String content = IOUtils.toString(connection.getInputStream()); assertTrue(content.contains("\"PropertyCompNav\":{\"PropertyInt16\":1}")); connection.disconnect(); }
From source file:org.apache.olingo.fit.tecsvc.http.SelectOnComplexPropertiesITCase.java
@Test public void queryESKeyColPropertyComp1() throws Exception { URL url = new URL(SERVICE_URI + "ESKeyNav(1)/CollPropertyComp" + "?$select=PropertyComp/PropertyString"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HttpMethod.GET.name()); connection.setRequestProperty(HttpHeader.ACCEPT, "application/json;odata.metadata=minimal"); connection.connect();/* w w w. j av a2s . c o m*/ assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode()); assertEquals(ContentType.JSON, ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE))); final String content = IOUtils.toString(connection.getInputStream()); assertTrue(content.contains( "\"value\":[{\"PropertyComp\":" + "{\"PropertyString\":\"First Resource - positive values\"}}," + "{\"PropertyComp\":{\"PropertyString\":\"First Resource - positive values\"}}," + "{\"PropertyComp\":{\"PropertyString\":\"First Resource - positive values\"}}]")); }
From source file:org.apache.olingo.fit.tecsvc.http.BasicHttpITCase.java
@Test public void testODataMaxVersion() throws Exception { URL url = new URL(SERVICE_URI); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HttpMethod.GET.name()); connection.setRequestProperty(HttpHeader.ODATA_MAX_VERSION, "4.0"); connection.setRequestProperty(HttpHeader.ACCEPT, "*/*"); connection.connect();/*from w w w. jav a 2 s . c om*/ assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode()); assertEquals("4.0", connection.getHeaderField(HttpHeader.ODATA_VERSION)); }
From source file:org.apache.hadoop.mapreduce.v2.app.webapp.TestAMWebApp.java
@Test public void testMRWebAppRedirection() throws Exception { String[] schemePrefix = { WebAppUtils.HTTP_PREFIX, WebAppUtils.HTTPS_PREFIX }; for (String scheme : schemePrefix) { MRApp app = new MRApp(2, 2, true, this.getClass().getName(), true) { @Override//from ww w .j a v a 2s.c om protected ClientService createClientService(AppContext context) { return new MRClientService(context); } }; Configuration conf = new Configuration(); conf.set(YarnConfiguration.PROXY_ADDRESS, "9.9.9.9"); conf.set(YarnConfiguration.YARN_HTTP_POLICY_KEY, scheme.equals(WebAppUtils.HTTPS_PREFIX) ? Policy.HTTPS_ONLY.name() : Policy.HTTP_ONLY.name()); webProxyBase = "/proxy/" + app.getAppID(); conf.set("hadoop.http.filter.initializers", TestAMFilterInitializer.class.getName()); Job job = app.submit(conf); String hostPort = NetUtils .getHostPortString(((MRClientService) app.getClientService()).getWebApp().getListenerAddress()); URL httpUrl = new URL("http://" + hostPort + "/mapreduce"); HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); conn.setInstanceFollowRedirects(false); conn.connect(); String expectedURL = scheme + conf.get(YarnConfiguration.PROXY_ADDRESS) + ProxyUriUtils.getPath(app.getAppID(), "/mapreduce"); Assert.assertEquals(expectedURL, conn.getHeaderField(HttpHeaders.LOCATION)); Assert.assertEquals(HttpStatus.SC_MOVED_TEMPORARILY, conn.getResponseCode()); app.waitForState(job, JobState.SUCCEEDED); app.verifyCompleted(); } }
From source file:io.sloeber.core.managers.Manager.java
/** * copy a url locally taking into account redirections * * @param url/*www. j a v a2 s. com*/ * @param localFile */ @SuppressWarnings("nls") private static void myCopy(URL url, File localFile) { try { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(5000); conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8"); conn.addRequestProperty("User-Agent", "Mozilla"); conn.addRequestProperty("Referer", "google.com"); // normally, 3xx is redirect int status = conn.getResponseCode(); if (status != HttpURLConnection.HTTP_OK) { if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER) Files.copy(new URL(conn.getHeaderField("Location")).openStream(), localFile.toPath(), REPLACE_EXISTING); } else { Files.copy(url.openStream(), localFile.toPath(), REPLACE_EXISTING); } } catch (Exception e) { Common.log(new Status(IStatus.WARNING, Activator.getId(), "Failed to download url " + url, e)); } }