List of usage examples for java.net URLConnection getHeaderField
public String getHeaderField(int n)
From source file:net.sf.taverna.t2.security.credentialmanager.impl.HTTPAuthenticatorIT.java
@Test() public void failsWithoutAuthenticator() throws Exception { URL url = new URL("http://localhost:" + PORT + "/test.html"); URLConnection c = url.openConnection(); assertEquals("HTTP/1.1 401 Unauthorized", c.getHeaderField(0)); }
From source file:org.jboss.test.cluster.httpsessionreplication.HttpSessionReplicationUnitTestCase.java
/** * Makes a HTTP Connection//w ww . j av a2 s . c om * @param urlname * @throws Exception */ private void makeConnection(String urlname) throws Exception { getLog().debug("Enter makeConnection"); try { // Step 1: Create URLConnection for URL URL url = new URL(urlname); URLConnection conn = url.openConnection(); // List all the response headers from the server. for (int i = 0;; i++) { String hname = conn.getHeaderFieldKey(i); String hvalue = conn.getHeaderField(i); getLog().debug("hname=" + hname + "::" + "value=" + hvalue); if (hname == null && hvalue == null) { // No more headers break; } if (hname == null) { getLog().debug("Response from Apache=" + hvalue); // The header value contains the server's HTTP version if (hvalue.indexOf("200") < 0 && hvalue.indexOf("301") < 0 && hvalue.indexOf("302") < 0) fail(urlname + " Down"); break; } } } catch (Exception e) { getLog().debug(e); } }
From source file:ubic.basecode.ontology.OntologyLoader.java
/** * Load an ontology into memory. Use this type of model when fast access is critical and memory is available. * If load from URL fails, attempt to load from disk cache under @cacheName. * /*from w w w . j a va2 s .c o m*/ * @param url * @param spec e.g. OWL_MEM_TRANS_INF * @param cacheName unique name of this ontology, will be used to load from disk in case of failed url connection * @return */ public static OntModel loadMemoryModel(String url, OntModelSpec spec, String cacheName) { StopWatch timer = new StopWatch(); timer.start(); OntModel model = getMemoryModel(url, spec); URLConnection urlc = null; int tries = 0; while (tries < MAX_CONNECTION_TRIES) { try { urlc = new URL(url).openConnection(); // help ensure mis-configured web servers aren't causing trouble. urlc.setRequestProperty("Accept", "application/rdf+xml"); try { HttpURLConnection c = (HttpURLConnection) urlc; c.setInstanceFollowRedirects(true); } catch (ClassCastException e) { // not via http, using a FileURLConnection. } if (tries > 0) { log.info("Retrying connecting to " + url + " [" + tries + "/" + MAX_CONNECTION_TRIES + " of max tries"); } else { log.info("Connecting to " + url); } urlc.connect(); // Will error here on bad URL if (urlc instanceof HttpURLConnection) { String newUrl = urlc.getHeaderField("Location"); if (StringUtils.isNotBlank(newUrl)) { log.info("Redirect to " + newUrl); urlc = new URL(newUrl).openConnection(); // help ensure mis-configured web servers aren't causing trouble. urlc.setRequestProperty("Accept", "application/rdf+xml"); urlc.connect(); } } break; } catch (IOException e) { // try to recover. log.error(e + " retrying?"); tries++; } } if (urlc != null) { try (InputStream in = urlc.getInputStream();) { Reader reader; if (cacheName != null) { // write tmp to disk File tempFile = getTmpDiskCachePath(cacheName); if (tempFile == null) { reader = new InputStreamReader(in); } else { tempFile.getParentFile().mkdirs(); Files.copy(in, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); reader = new FileReader(tempFile); } } else { // Skip the cache reader = new InputStreamReader(in); } assert reader != null; try (BufferedReader buf = new BufferedReader(reader);) { model.read(buf, url); } log.info("Load model: " + timer.getTime() + "ms"); } catch (IOException e) { log.error(e.getMessage(), e); } } if (cacheName != null) { File f = getDiskCachePath(cacheName); File tempFile = getTmpDiskCachePath(cacheName); File oldFile = getOldDiskCachePath(cacheName); if (model.isEmpty()) { // Attempt to load from disk cache if (f == null) { throw new RuntimeException( "Ontology cache directory required to load from disk: ontology.cache.dir"); } if (f.exists() && !f.isDirectory()) { try (BufferedReader buf = new BufferedReader(new FileReader(f));) { model.read(buf, url); // We successfully loaded the cached ontology. Copy the loaded ontology to oldFile // so that we don't recreate indices during initialization based on a false change in // the ontology. Files.copy(f.toPath(), oldFile.toPath(), StandardCopyOption.REPLACE_EXISTING); log.info("Load model from disk: " + timer.getTime() + "ms"); } catch (IOException e) { log.error(e.getMessage(), e); throw new RuntimeException( "Ontology failed load from URL (" + url + ") and disk cache: " + cacheName); } } else { throw new RuntimeException("Ontology failed load from URL (" + url + ") and disk cache does not exist: " + cacheName); } } else { // Model was successfully loaded into memory from URL with given cacheName // Save cache to disk (rename temp file) log.info("Caching ontology to disk: " + cacheName); if (f != null) { try { // Need to compare previous to current so instead of overwriting we'll move the old file f.createNewFile(); Files.move(f.toPath(), oldFile.toPath(), StandardCopyOption.REPLACE_EXISTING); Files.move(tempFile.toPath(), f.toPath(), StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { log.error(e.getMessage(), e); } } else { log.warn("Ontology cache directory required to save to disk: ontology.cache.dir"); } } } assert !model.isEmpty(); return model; }
From source file:org.pentaho.reporting.libraries.resourceloader.loader.URLResourceData.java
private void readMetaData(final URLConnection c) { modificationDate = c.getHeaderFieldDate("last-modified", -1); if (modificationDate <= 0) { if (isFixBrokenWebServiceDateHeader()) { modificationDate = System.currentTimeMillis(); } else {/*from w w w . jav a2 s . c om*/ modificationDate = -1; } } contentLength = new Long(c.getContentLength()); contentType = c.getHeaderField("content-type"); metaDataOK = true; lastDateMetaDataRead = System.currentTimeMillis(); }
From source file:org.apache.jmeter.protocol.http.control.CacheManager.java
/** * Save the Last-Modified, Etag, and Expires headers if the result is cacheable. * Version for Java implementation./*from w w w . jav a 2 s . c om*/ * @param conn connection * @param res result */ public void saveDetails(URLConnection conn, HTTPSampleResult res) { if (isCacheable(res) && !hasVaryHeader(conn)) { String lastModified = conn.getHeaderField(HTTPConstants.LAST_MODIFIED); String expires = conn.getHeaderField(HTTPConstants.EXPIRES); String etag = conn.getHeaderField(HTTPConstants.ETAG); String url = conn.getURL().toString(); String cacheControl = conn.getHeaderField(HTTPConstants.CACHE_CONTROL); String date = conn.getHeaderField(HTTPConstants.DATE); setCache(lastModified, cacheControl, expires, etag, url, date); } }
From source file:com.amazonaws.eclipse.sdk.ui.AbstractSdkManager.java
private void downloadSdkFromCloudFront(File destination, IProgressMonitor monitor, int totalUnitsOfWork) throws IOException { if (cloudfrontDownloadUrl == null) { throw new IllegalStateException("No CloudFront endpoint is provided."); }//from ww w .j a v a 2 s .co m monitor.subTask("Downloading latest SDK from CloudFront"); JavaSdkPlugin.getDefault().getLog().log(new Status(Status.INFO, JavaSdkPlugin.PLUGIN_ID, "Downloading the SDK from CloudFront to location " + destination.getAbsolutePath())); URL sourceUrl = new URL(cloudfrontDownloadUrl); URLConnection connection = sourceUrl.openConnection(); long totalBytes; String contentLength = connection.getHeaderField("Content-Length"); try { totalBytes = Long.parseLong(contentLength); } catch (NumberFormatException e) { totalBytes = -1; } InputStream input = connection.getInputStream(); try { FileOutputStream output = new FileOutputStream(destination); try { copyWithProgressMonitor(input, output, monitor, totalUnitsOfWork, totalBytes); } finally { IOUtils.closeQuietly(output); } } finally { IOUtils.closeQuietly(input); } if (!destination.exists()) { throw new IllegalStateException( destination.getAbsolutePath() + " does not exist " + "after the SDK download completes."); } }
From source file:net.sf.taverna.t2.security.credentialmanager.impl.HTTPAuthenticatorIT.java
@Test() public void withAuthenticator() throws Exception { assertEquals("Unexpected calls to password provider", 0, HTTPAuthenticatorServiceUsernameAndPasswordProvider.getCalls()); // Set the authenticator to our Credential Manager-backed one that also // counts calls to itself CountingAuthenticator authenticator = new CountingAuthenticator(credentialManager); assertEquals("Unexpected calls to authenticator", 0, authenticator.calls); Authenticator.setDefault(authenticator); // FixedPasswordProvider.setUsernamePassword(new UsernamePassword( // USERNAME, PASSWORD)); URL url = new URL("http://localhost:" + PORT + "/test.html"); httpAuthProvider.setServiceUsernameAndPassword(url.toURI(), new UsernamePassword(USERNAME, PASSWORD)); URLConnection c = url.openConnection(); c.connect();//from w w w . j ava2 s . c om try { c.getContent(); } catch (Exception ex) { } System.out.println(c.getHeaderField(0)); assertEquals("Did not invoke authenticator", 1, authenticator.calls); assertEquals("Did not invoke our password provider", 1, HTTPAuthenticatorServiceUsernameAndPasswordProvider.getCalls()); assertEquals("HTTP/1.1 200 OK", c.getHeaderField(0)); assertEquals("Unexpected prompt/realm", REALM, httpAuthProvider.getRequestMessage()); assertEquals("Unexpected URI", url.toURI().toASCIIString() + "#" + REALM, HTTPAuthenticatorServiceUsernameAndPasswordProvider.getServiceURI().toASCIIString()); // And test Java's cache: URLConnection c2 = url.openConnection(); c2.connect(); assertEquals("HTTP/1.1 200 OK", c2.getHeaderField(0)); assertEquals("JVM invoked our authenticator again instead of caching", 1, authenticator.calls); assertEquals("Invoked our password provider again instead of caching", 1, HTTPAuthenticatorServiceUsernameAndPasswordProvider.getCalls()); }
From source file:net.sf.taverna.t2.security.credentialmanager.impl.HTTPAuthenticatorIT.java
@Test() public void withAuthenticatorResetJava() throws Exception { assertTrue("Could not reset JVMs authCache, ignore on non-Sun JVM", credentialManager.resetAuthCache()); assertEquals("Unexpected calls to password provider", 0, HTTPAuthenticatorServiceUsernameAndPasswordProvider.getCalls()); CountingAuthenticator authenticator = new CountingAuthenticator(credentialManager); assertEquals("Unexpected calls to authenticator", 0, authenticator.calls); Authenticator.setDefault(authenticator); // FixedPasswordProvider.setUsernamePassword(new UsernamePassword( // USERNAME, PASSWORD)); URL url = new URL("http://localhost:" + PORT + "/test.html"); httpAuthProvider.setServiceUsernameAndPassword(url.toURI(), new UsernamePassword(USERNAME, PASSWORD)); URLConnection c = url.openConnection(); c.connect();/* w w w. j av a 2 s.c om*/ try { c.getContent(); } catch (Exception ex) { } assertEquals("HTTP/1.1 200 OK", c.getHeaderField(0)); assertEquals("Did not invoke authenticator", 1, authenticator.calls); assertEquals("Did not invoke our password provider", 1, HTTPAuthenticatorServiceUsernameAndPasswordProvider.getCalls()); assertEquals("Unexpected prompt/realm", REALM, httpAuthProvider.getRequestMessage()); assertEquals("Unexpected URI", url.toURI().toASCIIString() + "#" + REALM, HTTPAuthenticatorServiceUsernameAndPasswordProvider.getServiceURI().toASCIIString()); // And without Java's cache: assertTrue("Could not reset VMs authCache, ignore on non-Sun VM", credentialManager.resetAuthCache()); URLConnection c2 = url.openConnection(); c2.connect(); assertEquals("HTTP/1.1 200 OK", c2.getHeaderField(0)); assertEquals("Did not invoke our authenticator again", 2, authenticator.calls); assertEquals("Did not invoke our password provider again", 2, HTTPAuthenticatorServiceUsernameAndPasswordProvider.getCalls()); }
From source file:org.wandora.application.tools.extractors.ocr.OCRExtractor.java
@Override public boolean _extractTopicsFrom(URL u, TopicMap t) throws Exception { boolean success = false; URLConnection uc; File f = new File(TEMP_PATH + "_temp.dat"); if (getWandora() != null) { uc = getWandora().wandoraHttpAuthorizer.getAuthorizedAccess(u); } else {//from w w w. j a v a 2s . com uc = u.openConnection(); Wandora.initUrlConnection(uc); } String name = uc.getHeaderField("Content-Disposition"); InputStream is = uc.getInputStream(); try { FileOutputStream fos = new FileOutputStream(f); try { byte[] buffer = new byte[4096]; for (int n; (n = is.read(buffer)) != -1;) fos.write(buffer, 0, n); } finally { fos.close(); } } catch (Exception e) { e.printStackTrace(); } finally { is.close(); } try { String si = u.toString(); Locator l = new Locator(si); Topic documentTopic = t.getTopic(si); if (documentTopic == null) documentTopic = t.createTopic(); documentTopic.addSubjectIdentifier(l); documentTopic.setSubjectLocator(l); if (name != null) { documentTopic.setBaseName(name); documentTopic.setDisplayName("en", name); } documentTopic.addSubjectIdentifier(new Locator(si)); success = processFile(f, t, documentTopic); } catch (Exception e) { e.printStackTrace(); } finally { f.delete(); } return success; }
From source file:org.eclipse.kura.core.deployment.download.impl.HttpDownloadCountingOutputStream.java
@Override public void startWork() throws KuraException { this.executor = Executors.newSingleThreadExecutor(); this.future = this.executor.submit(new Callable<Void>() { @Override/*from ww w . j a v a 2 s . c om*/ public Void call() throws Exception { URL localUrl = null; boolean shouldAuthenticate = false; try { shouldAuthenticate = HttpDownloadCountingOutputStream.this.options.getUsername() != null && HttpDownloadCountingOutputStream.this.options.getPassword() != null && !(HttpDownloadCountingOutputStream.this.options.getUsername().trim().isEmpty() && !HttpDownloadCountingOutputStream.this.options.getPassword().trim() .isEmpty()); if (shouldAuthenticate) { Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( HttpDownloadCountingOutputStream.this.options.getUsername(), HttpDownloadCountingOutputStream.this.options.getPassword().toCharArray()); } }); } localUrl = new URL(HttpDownloadCountingOutputStream.this.m_downloadURL); URLConnection urlConnection = localUrl.openConnection(); int connectTimeout = getConnectTimeout(); int readTimeout = getPropReadTimeout(); urlConnection.setConnectTimeout(connectTimeout); urlConnection.setReadTimeout(readTimeout); testConnectionProtocol(urlConnection); HttpDownloadCountingOutputStream.this.is = localUrl.openStream(); String s = urlConnection.getHeaderField("Content-Length"); s_logger.info("Content-lenght: " + s); setTotalBytes(s != null ? Integer.parseInt(s) : -1); postProgressEvent(HttpDownloadCountingOutputStream.this.options.getClientId(), 0, HttpDownloadCountingOutputStream.this.totalBytes, DOWNLOAD_STATUS.IN_PROGRESS, null); int bufferSize = getBufferSize(); if (bufferSize == 0 && getTotalBytes() > 0) { int newSize = Math.round(HttpDownloadCountingOutputStream.this.totalBytes / 100 * 1); bufferSize = newSize; setBufferSize(newSize); } else if (bufferSize == 0) { int newSize = 1024 * 4; bufferSize = newSize; setBufferSize(newSize); } long numBytes = IOUtils.copyLarge(HttpDownloadCountingOutputStream.this.is, HttpDownloadCountingOutputStream.this, new byte[bufferSize]); postProgressEvent(HttpDownloadCountingOutputStream.this.options.getClientId(), numBytes, HttpDownloadCountingOutputStream.this.totalBytes, DOWNLOAD_STATUS.COMPLETED, null); } catch (IOException e) { postProgressEvent(HttpDownloadCountingOutputStream.this.options.getClientId(), getByteCount(), HttpDownloadCountingOutputStream.this.totalBytes, DOWNLOAD_STATUS.FAILED, e.getMessage()); throw new KuraConnectException(e); } finally { if (HttpDownloadCountingOutputStream.this.is != null) { try { HttpDownloadCountingOutputStream.this.is.close(); } catch (IOException e) { } } try { close(); } catch (IOException e) { } localUrl = null; if (shouldAuthenticate) { Authenticator.setDefault(null); } } return null; } }); try { this.future.get(); } catch (ExecutionException ex) { throw new KuraException(KuraErrorCode.INTERNAL_ERROR, ex); } catch (InterruptedException ex) { throw new KuraException(KuraErrorCode.INTERNAL_ERROR, ex); } }