List of usage examples for java.net URLConnection setRequestProperty
public void setRequestProperty(String key, String value)
From source file:co.pugo.convert.ConvertServlet.java
/** * retrieve URLConnection for source document * @return URLConnection//from w ww . j a v a2 s . c o m * @throws IOException */ private URLConnection getSourceUrlConnection(String source, String token) throws IOException { URLConnection urlConnection; String sourceUrl = URLDecoder.decode(source, "UTF-8"); URL url = new URL(sourceUrl); urlConnection = url.openConnection(); if (token != null) { urlConnection.setRequestProperty("Authorization", "Bearer " + token); } return urlConnection; }
From source file:org.sakaiproject.component.app.help.RestConfigurationImpl.java
/** * @see org.sakaiproject.api.app.help.RestConfiguration#getCorpusDocument() *///from w w w . j a va2 s . co m public String getCorpusDocument() { if (LOG.isDebugEnabled()) { LOG.debug("getCorpusDocument()"); } URL url = null; StringBuilder sBuffer = new StringBuilder(); BufferedReader br = null; try { url = new URL(getRestCorpusUrl()); URLConnection urlConnection = url.openConnection(); String basicAuthUserPass = getRestCredentials(); String encoding = Base64.encodeBase64(basicAuthUserPass.getBytes("utf-8")).toString(); urlConnection.setRequestProperty("Authorization", "Basic " + encoding); br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()), 512); int readReturn = 0; char[] cbuf = new char[512]; while ((readReturn = br.read(cbuf, 0, 512)) != -1) { sBuffer.append(cbuf, 0, readReturn); } } catch (MalformedURLException e) { LOG.error("Malformed URL in REST document: " + url.getPath(), e); } catch (IOException e) { LOG.error("Could not open connection to REST document: " + url.getPath(), e); } finally { try { if (br != null) { br.close(); } } catch (IOException e) { LOG.error("error closing corpus doc", e); } } return sBuffer.toString(); }
From source file:com.opoopress.maven.plugins.plugin.downloader.ProgressURLDownloader.java
private void downloadInternal(URL url, File destination) throws IOException { OutputStream out = null;//from w w w . j a v a2s . co m URLConnection conn; InputStream in = null; try { //URL url = address.toURL(); conn = url.openConnection(); //user agent final String userAgentValue = calculateUserAgent(); conn.setRequestProperty("User-Agent", userAgentValue); //do not set gzip header if download zip file if (useGzip) { conn.setRequestProperty("Accept-Encoding", "gzip"); } if (!useCache) { conn.setRequestProperty("Pragma", "no-cache"); } in = conn.getInputStream(); out = new BufferedOutputStream(new FileOutputStream(destination)); copy(in, out); if (checkContentLength) { long contentLength = conn.getContentLengthLong(); if (contentLength > 0 && contentLength != destination.length()) { throw new IllegalArgumentException("File length mismatch. expected: " + contentLength + ", actual: " + destination.length()); } } if (keepLastModified) { long lastModified = conn.getLastModified(); if (lastModified > 0) { destination.setLastModified(lastModified); } } } finally { logMessage(""); if (in != null) { in.close(); } if (out != null) { out.close(); } } }
From source file:org.jlibrary.core.http.client.HTTPDelegate.java
/** * Excecutes a void request/* www.j ava 2s.c o m*/ * * @param methodName Name of the method to execute * @param params Method params * @param returnClass Class for the return object. If the class is InputStream this method will return * the HTTP request input stream * @param inputStream Stream for reading contents that will be sent * * @throws Exception If there is any problem running the request */ public Object doRequest(String methodName, Object[] params, Class returnClass, InputStream inputStream) throws Exception { try { logger.debug(servletURL.toString()); logger.debug("opening connection"); URLConnection conn = servletURL.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setAllowUserInteraction(false); conn.setRequestProperty("Content-type", "text/plain"); //write request and params: OutputStream stream = conn.getOutputStream(); // write streaming header if necessary if (inputStream != null) { stream.write(ByteUtils.intToByteArray("stream-input".getBytes().length)); stream.write("stream-input".getBytes()); stream.flush(); } if (returnClass == InputStream.class) { stream.write(ByteUtils.intToByteArray("stream-output".getBytes().length)); stream.write("stream-output".getBytes()); stream.flush(); } // Write method stream.write(ByteUtils.intToByteArray(methodName.getBytes().length)); stream.write(methodName.getBytes()); //stream.flush(); // Write parameters stream.write(ByteUtils.intToByteArray(params.length)); for (int i = 0; i < params.length; i++) { byte[] content = SerializationUtils.serialize((Serializable) params[i]); stream.write(ByteUtils.intToByteArray(content.length)); stream.write(content); stream.flush(); } if (inputStream != null) { IOUtils.copy(inputStream, stream); } //stream.flush(); //stream.close(); //read response: InputStream input = conn.getInputStream(); if (returnClass == InputStream.class) { // Contents will be read from outside return input; } ObjectInputStream objInput = new ObjectInputStream(input); Object o = objInput.readObject(); if (o == null) { return null; } stream.close(); if (o instanceof Exception) { throw (Exception) o; } else { if (returnClass == null) { return null; } // try to cast try { returnClass.cast(o); } catch (ClassCastException cce) { String msg = "Unexpected response from execution servlet: " + o; logger.error(msg); throw new Exception(msg); } } return o; } catch (ClassNotFoundException e) { throw new Exception(e); } catch (ClassCastException e) { throw new Exception(e); } catch (IOException e) { throw new Exception(e); } }
From source file:com.moviejukebox.tools.WebBrowser.java
private void sendHeader(URLConnection cnx) { // send browser properties for (Map.Entry<String, String> browserProperty : browserProperties.entrySet()) { cnx.setRequestProperty(browserProperty.getKey(), browserProperty.getValue()); if (LOG.isTraceEnabled()) { LOG.trace("setRequestProperty:{}='{}'", browserProperty.getKey(), browserProperty.getValue()); }// w w w.ja v a 2 s . c o m } // send cookies String cookieHeader = createCookieHeader(cnx); if (!cookieHeader.isEmpty()) { cnx.setRequestProperty("Cookie", cookieHeader); if (LOG.isTraceEnabled()) { LOG.trace("Cookie:{}", cookieHeader); } } checkRequest(cnx); }
From source file:com.freedomotic.plugins.devices.tcw122bcm.Tcw122bcm.java
private void changeRelayStatus(String hostname, int hostport, String relayNumber, int control) { try {//www. j a v a 2 s. c o m String authString = USERNAME + ":" + PASSWORD; //System.out.println("auth string: " + authString); byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); String authStringEnc = new String(authEncBytes); //System.out.println("Base64 encoded auth string: " + authStringEnc); //FOR DEBUG //Create a URL for the desired page URL url = new URL("http://" + hostname + ":" + hostport + "/?" + relayNumber + "=" + control); LOG.info("Freedomotic sends the command " + url); URLConnection urlConnection = url.openConnection(); // if required set the authentication if (HTTP_AUTHENTICATION.equalsIgnoreCase("true")) { urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc); } InputStream is = urlConnection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); int numCharsRead; char[] charArray = new char[1024]; StringBuffer sb = new StringBuffer(); while ((numCharsRead = isr.read(charArray)) > 0) { sb.append(charArray, 0, numCharsRead); } String result = sb.toString(); } catch (MalformedURLException e) { LOG.severe("Change relay status malformed URL " + e.toString()); } catch (IOException e) { LOG.severe("Change relay status IOexception" + e.toString()); } }
From source file:com.carvoyant.modularinput.Program.java
@Override public void streamEvents(InputDefinition inputs, EventWriter ew) throws MalformedDataException, XMLStreamException, IOException { Service splunkSvc = getService(inputs.getServerUri(), inputs.getSessionKey()); for (String inputName : inputs.getInputs().keySet()) { String clientId = ((SingleValueParameter) inputs.getInputs().get(inputName).get("clientId")).getValue(); String clientSecret = ((SingleValueParameter) inputs.getInputs().get(inputName).get("clientSecret")) .getValue();/*from www . j a v a 2 s .c o m*/ String token = ((SingleValueParameter) inputs.getInputs().get(inputName).get("token")).getValue(); String refreshToken = ((SingleValueParameter) inputs.getInputs().get(inputName).get("refreshToken")) .getValue(); SingleValueParameter expDate = (SingleValueParameter) inputs.getInputs().get(inputName) .get("expirationDate"); long expirationDate = expDate.getLong(); long expirationWindow = TimeUnit.DAYS.toMillis(1); long currentTime = System.currentTimeMillis(); // If the access token is going to expire, then refresh it if ((expirationDate - currentTime) < expirationWindow) { JSONObject tokenJson = getRefreshToken(ew, clientId, clientSecret, refreshToken); if (tokenJson != null) { token = tokenJson.getString("access_token"); refreshToken = tokenJson.getString("refresh_token"); int ttl = tokenJson.getInt("expires_in"); expirationDate = System.currentTimeMillis() + (long) ttl * 1000; // Update this configuration with the new settings String[] splunkApiInputName = inputName.split("://"); updateInput(ew, splunkSvc, splunkApiInputName[1], clientId, clientSecret, token, refreshToken, expirationDate); } else { throw new XMLStreamException("Cannot not refresh the token for " + inputName); } } // Get Carvoyant Data try { URL url = new URL("https://api.carvoyant.com/v1/api/account/data/?sinceLastCall=true"); // URL url = new URL("https://sandbox-api.carvoyant.com/sandbox/api/account/data/?sinceLastCall=true"); URLConnection urlConnection = url.openConnection(); String basicAuth = "Bearer " + token; urlConnection.setRequestProperty("Authorization", basicAuth); BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { JSONObject obj = new JSONObject(inputLine); JSONArray arr = obj.getJSONArray("data"); for (int i = 0; i < arr.length(); i++) { String timeString = arr.getJSONObject(i).getString("timestamp"); DateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmssZ"); Date date = format.parse(timeString); Event event = new Event(); event.setStanza(inputName); event.setTime(date); event.setData(arr.getJSONObject(i).toString()); ew.writeEvent(event); } } in.close(); } catch (IOException ioe) { throw new XMLStreamException("Could not retrieve Carvoyant Data", ioe); } catch (ParseException pe) { throw new XMLStreamException("Could not parse Carvoyant Data", pe); } } }
From source file:org.apache.synapse.config.SynapseConfigUtils.java
/** * Return an OMElement from a URL source * * @param urlStr a URL string//from ww w . ja v a2 s. c o m * @param synapseHome synapse home parameter to be used * @return an OMElement of the resource * @throws IOException for invalid URL's or IO errors */ public static OMNode getOMElementFromURL(String urlStr, String synapseHome) throws IOException { URL url = getURLFromPath(urlStr, synapseHome); if (url == null) { return null; } URLConnection connection = null; //If url contains http basic authentication parameters. if (url.getUserInfo() != null) { String protocol = url.getProtocol(); if ("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol)) { //Create new url excluding user info URL newUrl = new URL(protocol, url.getHost(), url.getPort(), url.getFile()); connection = getURLConnection(newUrl); String encoding = new String(new Base64().encode(url.getUserInfo().getBytes())); connection.setRequestProperty("Authorization", "Basic " + encoding); } else { handleException("Unsuported protocol [" + protocol + "]. Supports only http and https with " + "basic authentication"); } } else { connection = getURLConnection(url); } if (connection == null) { if (log.isDebugEnabled()) { log.debug("Cannot create a URLConnection for given URL : " + urlStr); } return null; } InputStream inStream = connection.getInputStream(); try { StAXOMBuilder builder = new StAXOMBuilder(inStream); OMElement doc = builder.getDocumentElement(); doc.build(); return doc; } catch (Exception e) { if (log.isDebugEnabled()) { log.info("Content at URL : " + url + " is non XML.."); } return readNonXML(url); } finally { try { inStream.close(); } catch (IOException e) { log.warn("Error while closing the input stream to: " + url, e); } } }
From source file:de.tudarmstadt.ukp.dkpro.core.api.datasets.DatasetFactory.java
/** * Verify/download/update artifact in cache. Execute post-download actions. *//*from w w w. j a va 2 s. com*/ private void materialize(DatasetDescription aDataset) throws IOException { Path root = resolve(aDataset); Collection<ArtifactDescription> artifacts = aDataset.getArtifacts().values(); // First validate if local copies are still up-to-date boolean reload = false; packageValidationLoop: for (ArtifactDescription artifact : artifacts) { Path cachedFile = resolve(aDataset, artifact); if (!Files.exists(cachedFile)) { continue; } if (artifact.getSha1() != null) { String actual = getDigest(cachedFile, "SHA1"); if (!artifact.getSha1().equals(actual)) { LOG.info("Local SHA1 hash mismatch on [" + cachedFile + "] - expected [" + artifact.getSha1() + "] - actual [" + actual + "]"); reload = true; break packageValidationLoop; } else { LOG.info("Local SHA1 hash verified on [" + cachedFile + "] - [" + actual + "]"); } } } // If any of the packages are outdated, clear the cache and download again if (reload) { LOG.info("Clearing local cache for [" + root + "]"); FileUtils.deleteQuietly(root.toFile()); } for (ArtifactDescription artifact : artifacts) { Path cachedFile = resolve(aDataset, artifact); if (Files.exists(cachedFile)) { continue; } if (artifact.getText() != null) { Files.createDirectories(cachedFile.getParent()); LOG.info("Creating [" + cachedFile + "]"); try (Writer out = Files.newBufferedWriter(cachedFile, StandardCharsets.UTF_8)) { out.write(artifact.getText()); } } if (artifact.getUrl() != null) { Files.createDirectories(cachedFile.getParent()); MessageDigest sha1; try { sha1 = MessageDigest.getInstance("SHA1"); } catch (NoSuchAlgorithmException e) { throw new IOException(e); } URL source = new URL(artifact.getUrl()); LOG.info("Fetching [" + cachedFile + "]"); URLConnection connection = source.openConnection(); connection.setRequestProperty("User-Agent", "Java"); try (InputStream is = connection.getInputStream()) { DigestInputStream sha1Filter = new DigestInputStream(is, sha1); Files.copy(sha1Filter, cachedFile); if (artifact.getSha1() != null) { String sha1Hex = new String(Hex.encodeHex(sha1Filter.getMessageDigest().digest())); if (!artifact.getSha1().equals(sha1Hex)) { String message = "SHA1 mismatch. Expected [" + artifact.getSha1() + "] but got [" + sha1Hex + "]."; LOG.error(message); throw new IOException(message); } } } } } // Perform a post-fetch action such as unpacking Path postActionCompleteMarker = resolve(aDataset).resolve(".postComplete"); if (!Files.exists(postActionCompleteMarker)) { for (ArtifactDescription artifact : artifacts) { Path cachedFile = resolve(aDataset, artifact); List<ActionDescription> actions = artifact.getActions(); if (actions != null && !actions.isEmpty()) { try { for (ActionDescription action : actions) { LOG.info("Post-download action [" + action.getAction() + "]"); Class<? extends Action_ImplBase> implClass = actionRegistry.get(action.getAction()); if (implClass == null) { throw new IllegalStateException( "Unknown or unsupported action [" + action.getAction() + "]"); } Action_ImplBase impl = implClass.newInstance(); impl.apply(action, aDataset, artifact, cachedFile); } } catch (IllegalStateException e) { throw e; } catch (IOException e) { throw e; } catch (Exception e) { throw new IllegalStateException(e); } } } Files.createFile(postActionCompleteMarker); } }
From source file:fr.jetoile.hadoopunit.integrationtest.IntegrationBootstrapTest.java
@Test public void hdfsShouldStart() throws Exception { assertThat(Utils.available("127.0.0.1", configuration.getInt(HadoopUnitConfig.HDFS_NAMENODE_HTTP_PORT_KEY))) .isFalse();/*from w ww . j av a2 s.c o m*/ // org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration(); // conf.set("fs.default.name", "hdfs://127.0.0.1:" + configuration.getInt(Config.HDFS_NAMENODE_PORT_KEY)); // // URI uri = URI.create ("hdfs://127.0.0.1:" + configuration.getInt(Config.HDFS_NAMENODE_PORT_KEY)); // // FileSystem hdfsFsHandle = FileSystem.get (uri, conf); FileSystem hdfsFsHandle = HdfsUtils.INSTANCE.getFileSystem(); FSDataOutputStream writer = hdfsFsHandle .create(new Path(configuration.getString(HadoopUnitConfig.HDFS_TEST_FILE_KEY))); writer.writeUTF(configuration.getString(HadoopUnitConfig.HDFS_TEST_STRING_KEY)); writer.close(); // Read the file and compare to test string FSDataInputStream reader = hdfsFsHandle .open(new Path(configuration.getString(HadoopUnitConfig.HDFS_TEST_FILE_KEY))); assertEquals(reader.readUTF(), configuration.getString(HadoopUnitConfig.HDFS_TEST_STRING_KEY)); reader.close(); hdfsFsHandle.close(); URL url = new URL(String.format("http://localhost:%s/webhdfs/v1?op=GETHOMEDIRECTORY&user.name=guest", configuration.getInt(HadoopUnitConfig.HDFS_NAMENODE_HTTP_PORT_KEY))); URLConnection connection = url.openConnection(); connection.setRequestProperty("Accept-Charset", "UTF-8"); BufferedReader response = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line = response.readLine(); response.close(); assertThat("{\"Path\":\"/user/guest\"}").isEqualTo(line); }