List of usage examples for java.net URLConnection addRequestProperty
public void addRequestProperty(String key, String value)
From source file:com.adobe.phonegap.contentsync.Sync.java
private static void addHeadersToRequest(URLConnection connection, JSONObject headers) { try {/* w ww . j a va2s .co m*/ for (Iterator<?> iter = headers.keys(); iter.hasNext();) { String headerKey = iter.next().toString(); JSONArray headerValues = headers.optJSONArray(headerKey); if (headerValues == null) { headerValues = new JSONArray(); headerValues.put(headers.getString(headerKey)); } connection.setRequestProperty(headerKey, headerValues.getString(0)); for (int i = 1; i < headerValues.length(); ++i) { connection.addRequestProperty(headerKey, headerValues.getString(i)); } } } catch (JSONException e1) { // No headers to be manipulated! } }
From source file:edu.ucuenca.authorsdisambiguation.nwd.NWD.java
public synchronized String Http(String s, String prefix) throws SQLException, IOException { String get = Cache.getInstance().get(prefix + s); String resp = ""; if (get != null) { //System.out.print("."); resp = get;//from w w w. ja va2s .c o m } else { final URL url = new URL(s); final URLConnection connection = url.openConnection(); connection.setConnectTimeout(60000); connection.setReadTimeout(60000); connection.addRequestProperty("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0"); connection.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); final Scanner reader = new Scanner(connection.getInputStream(), "UTF-8"); while (reader.hasNextLine()) { final String line = reader.nextLine(); resp += line + "\n"; } reader.close(); Cache.getInstance().put(prefix + s, resp); } return resp; }
From source file:net.milkbowl.vault.Vault.java
public double updateCheck(double currentVersion) { try {/* w w w. j av a2 s .co m*/ URL url = new URL("https://api.curseforge.com/servermods/files?projectids=33184"); URLConnection conn = url.openConnection(); conn.setReadTimeout(5000); conn.addRequestProperty("User-Agent", "Vault Update Checker"); conn.setDoOutput(true); final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); final String response = reader.readLine(); final JSONArray array = (JSONArray) JSONValue.parse(response); if (array.size() == 0) { this.getLogger().warning("No files found, or Feed URL is bad."); return currentVersion; } // Pull the last version from the JSON newVersionTitle = ((String) ((JSONObject) array.get(array.size() - 1)).get("name")).replace("Vault", "") .trim(); return Double.valueOf(newVersionTitle.replaceFirst("\\.", "").trim()); } catch (Exception e) { log.info("There was an issue attempting to check for the latest version."); } return currentVersion; }
From source file:com.dsh105.commodus.data.Updater.java
private boolean read() { try {// w w w. j a v a 2s. co m final URLConnection conn = this.url.openConnection(); conn.setConnectTimeout(5000); if (this.apiKey != null) { conn.addRequestProperty("X-API-Key", this.apiKey); } conn.addRequestProperty("User-Agent", "Updater (by Gravity)"); conn.setDoOutput(true); final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); final String response = reader.readLine(); final JSONArray array = (JSONArray) JSONValue.parse(response); if (array.size() == 0) { this.plugin.getLogger() .warning("The updater could not find any files for the project id " + this.id); this.result = UpdateResult.FAIL_BADID; return false; } this.versionName = (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.TITLE_VALUE); this.versionLink = (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.LINK_VALUE); this.versionType = (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.TYPE_VALUE); this.versionGameVersion = (String) ((JSONObject) array.get(array.size() - 1)) .get(Updater.VERSION_VALUE); return true; } catch (final IOException e) { if (e.getMessage().contains("HTTP response code: 403")) { this.plugin.getLogger() .warning("dev.bukkit.org rejected the API key provided in plugins/Updater/config.yml"); this.plugin.getLogger().warning("Please double-check your configuration to ensure it is correct."); this.result = UpdateResult.FAIL_APIKEY; } else { this.plugin.getLogger().warning("The updater could not contact dev.bukkit.org for updating."); this.plugin.getLogger().warning( "If you have not recently modified your configuration and this is the first time you are seeing this message, the site may be experiencing temporary downtime."); this.result = UpdateResult.FAIL_DBO; } e.printStackTrace(); return false; } }
From source file:info.magnolia.cms.exchange.simple.SimpleSyndicator.java
/** * add deactivation request header fields * * @param connection/* w ww.j a v a 2 s. c o m*/ */ protected void addDeactivationHeaders(URLConnection connection) { connection.setRequestProperty(AUTHORIZATION, this.basicCredentials); connection.addRequestProperty(REPOSITORY_NAME, this.repositoryName); connection.addRequestProperty(WORKSPACE_NAME, this.workspaceName); connection.addRequestProperty(PATH, this.path); connection.addRequestProperty(ACTION, DE_ACTIVATE); }
From source file:net.antidot.semantic.rdf.model.impl.sesame.SesameDataSet.java
/** * Import data from URI source Request is made with proper HTTP ACCEPT * header and will follow redirects for proper LOD source negotiation * /*from w w w . j av a2s .c om*/ * @param urlstring * absolute URI of the data source * @param format * RDF format to request/parse from data source */ public void addURI(String urlstring, RDFFormat format) { try { RepositoryConnection con = currentRepository.getConnection(); try { URL url = new URL(urlstring); URLConnection uricon = (URLConnection) url.openConnection(); uricon.addRequestProperty("accept", format.getDefaultMIMEType()); InputStream instream = uricon.getInputStream(); con.add(instream, urlstring, format); } finally { con.close(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:bammerbom.ultimatecore.bukkit.UltimateUpdater.java
/** * Make a connection to the BukkitDev API and request the newest file's * details.//from w ww . ja va2 s .c o m * * @return true if successful. */ private boolean read() { try { final URLConnection conn = this.url.openConnection(); conn.setConnectTimeout(5000); conn.addRequestProperty("User-Agent", UltimateUpdater.USER_AGENT); conn.setDoOutput(true); final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); final String response = reader.readLine(); final JSONArray array = (JSONArray) JSONValue.parse(response); if (array.isEmpty()) { r.log("No updates found."); this.result = UpdateResult.FAIL_BADID; return false; } UltimateUpdater.versionName = (String) ((Map) array.get(array.size() - 1)) .get(UltimateUpdater.TITLE_VALUE); this.versionLink = (String) ((Map) array.get(array.size() - 1)).get(UltimateUpdater.LINK_VALUE); this.versionType = (String) ((Map) array.get(array.size() - 1)).get(UltimateUpdater.TYPE_VALUE); this.versionGameVersion = (String) ((Map) array.get(array.size() - 1)) .get(UltimateUpdater.VERSION_VALUE); return true; } catch (final IOException e) { if (e.getMessage().contains("HTTP response code: 403")) { r.log("Invalid API key."); this.result = UpdateResult.FAIL_APIKEY; } else { r.log("Could not connect to bukkit.org, update check failed. " + (e.getCause() != null ? "(" + e.getCause() + ")" : "")); this.result = UpdateResult.FAIL_DBO; } return false; } }
From source file:com.taiter.ce.Main.java
public void updateCheck() { try {//from ww w.j a v a 2s. co m Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "[CE] Checking for updates..."); URLConnection connection = updateListURL.openConnection(); connection.setConnectTimeout(5000); connection.addRequestProperty("User-Agent", "Custom Enchantments - Update Checker"); connection.setDoOutput(true); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String response = reader.readLine(); JSONArray array = (JSONArray) JSONValue.parse(response); JSONObject newestUpdate = (JSONObject) array.get(array.size() - 1); newVersion = newestUpdate.get("name").toString().replace("Custom Enchantments ", "").trim(); newMD5 = newestUpdate.get("md5").toString(); int newLength = newVersion.length(); int currentLength = currentVersion.length(); double versionNew; double versionCurrent; Boolean newHasSubVersion = false; Boolean currentHasSubVersion = false; try { versionNew = Double.parseDouble(newVersion); } catch (NumberFormatException ex) { newHasSubVersion = true; versionNew = Double.parseDouble(newVersion.substring(0, newVersion.length() - 1)); } try { versionCurrent = Double.parseDouble(currentVersion); } catch (NumberFormatException ex) { currentHasSubVersion = true; versionCurrent = Double.parseDouble(currentVersion.substring(0, currentVersion.length() - 1)); } if ((versionNew > versionCurrent) || ((versionNew == versionCurrent) && newHasSubVersion && currentHasSubVersion && ((byte) newVersion.toCharArray()[newLength - 1] > (byte) currentVersion.toCharArray()[currentLength - 1]))) { hasUpdate = true; updateDownloadURL = new URL(newestUpdate.get("downloadUrl").toString().replace("\\.", "")); Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "[CE] A new update is available!"); Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "[CE] The new version is " + ChatColor.AQUA + newVersion + ChatColor.GREEN + "."); Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "[CE] You are currently using " + ChatColor.AQUA + currentVersion + ChatColor.GREEN + "."); Bukkit.getConsoleSender().sendMessage( ChatColor.GREEN + "[CE] You can use '/ce update applyupdate' to update automatically."); } else { hasUpdate = false; Bukkit.getConsoleSender() .sendMessage(ChatColor.GREEN + "[CE] You are using the latest Version of CE!"); } hasChecked = true; } catch (IOException ioex) { Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "[CE] Failed to check for updates"); } }
From source file:org.diorite.impl.metrics.Metrics.java
/** * Generic method that posts a plugin to the metrics website */// www . ja va2 s . co m private void postPlugin(final boolean isPing) throws IOException { final String serverVersion = this.getFullServerVersion(); final int playersOnline = this.getPlayersOnline(); // END server software specific section -- all code below does not use any code outside of this class / Java // Construct the post data final StringBuilder json = new StringBuilder(1024); json.append('{'); // The plugin's description file containg all of the plugin data such as name, version, author, etc appendJSONPair(json, "guid", this.getUUID()); appendJSONPair(json, "plugin_version", this.core.getVersion()); appendJSONPair(json, "server_version", serverVersion); appendJSONPair(json, "players_online", Integer.toString(playersOnline)); // New data as of R6 final String osname = System.getProperty("os.name"); String osarch = System.getProperty("os.arch"); final String osversion = System.getProperty("os.version"); final String java_version = System.getProperty("java.version"); final int coreCount = Runtime.getRuntime().availableProcessors(); // normalize os arch .. amd64 -> x86_64 if (osarch.equals("amd64")) { osarch = "x86_64"; } appendJSONPair(json, "osname", osname); appendJSONPair(json, "osarch", osarch); appendJSONPair(json, "osversion", osversion); appendJSONPair(json, "cores", Integer.toString(coreCount)); appendJSONPair(json, "auth_mode", (this.core.getOnlineMode() == OnlineMode.FALSE) ? "0" : "1"); appendJSONPair(json, "java_version", java_version); // If we're pinging, append it if (isPing) { appendJSONPair(json, "ping", "1"); } if (!this.graphs.isEmpty()) { synchronized (this.graphs) { json.append(','); json.append('"'); json.append("graphs"); json.append('"'); json.append(':'); json.append('{'); boolean firstGraph = true; for (final DynamicMetricsGraph graph : this.graphs) { final StringBuilder graphJson = new StringBuilder(); graphJson.append('{'); for (final MetricsPlotter plotter : graph.getPlotters()) { appendJSONPair(graphJson, plotter.getColumnName(), Integer.toString(plotter.getValue())); } graphJson.append('}'); if (!firstGraph) { json.append(','); } json.append(escapeJSON(graph.getName())); json.append(':'); json.append(graphJson); firstGraph = false; } json.append('}'); } } // close json json.append('}'); // Create the url final URL url = new URL(BASE_URL); // Connect to the website final URLConnection connection; connection = url.openConnection(); final byte[] uncompressed = json.toString().getBytes(); final byte[] compressed = gzip(json.toString()); // Headers connection.addRequestProperty("User-Agent", "MCStats/" + REVISION); connection.addRequestProperty("Content-Type", "application/json"); connection.addRequestProperty("Content-Encoding", "gzip"); connection.addRequestProperty("Content-Length", Integer.toString(compressed.length)); connection.addRequestProperty("Accept", "application/json"); connection.addRequestProperty("Connection", "close"); connection.setDoOutput(true); if (CoreMain.isEnabledDebug()) { SpammyError.out("[Metrics] Prepared request for Diorite uncompressed=" + uncompressed.length + " compressed=" + compressed.length, (int) TimeUnit.MINUTES.toSeconds(5), this.hashCode() + 1); } // Write the data try (final OutputStream os = connection.getOutputStream()) { os.write(compressed); os.flush(); } String response; try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { response = reader.readLine(); } if ((response == null) || response.startsWith("ERR") || response.startsWith("7")) { if (response == null) { response = "null"; } else if (response.startsWith("7")) { response = response.substring(response.startsWith("7,") ? 2 : 1); } throw new IOException(response); } else { // Is this the first update this hour? if (response.equals("1") || response.contains("This is your first update this hour")) { synchronized (this.graphs) { this.graphs.forEach(DynamicMetricsGraph::resetPlotters); } } } }
From source file:org.apache.hadoop.mapred.task.reduce.Fetcher.java
/** * The crux of the matter...//w w w. ja va 2 s.c o m * * @param host * {@link MapHost} from which we need to shuffle available * map-outputs. */ private void copyFromHost(MapHost host) throws IOException { // Get completed maps on 'host' List<TaskAttemptID> maps = scheduler.getMapsForHost(host); // Sanity check to catch hosts with only 'OBSOLETE' maps, // especially at the tail of large jobs if (maps.size() == 0) { return; } LOG.debug("Fetcher " + id + " going to fetch from " + host); if (LOG.isDebugEnabled()) { for (TaskAttemptID tmp : maps) { LOG.debug(tmp); } } // List of maps to be fetched yet Set<TaskAttemptID> remaining = new HashSet<TaskAttemptID>(maps); // Construct the url and connect DataInputStream input; boolean connectSucceeded = false; try { URL url = getMapOutputURL(host, maps); URLConnection connection = url.openConnection(); // generate hash of the url String msgToEncode = SecureShuffleUtils.buildMsgFrom(url); String encHash = SecureShuffleUtils.hashFromString(msgToEncode, jobTokenSecret); // put url hash into http header connection.addRequestProperty(SecureShuffleUtils.HTTP_HEADER_URL_HASH, encHash); // set the read timeout connection.setReadTimeout(readTimeout); connect(connection, connectionTimeout); connectSucceeded = true; input = new DataInputStream(connection.getInputStream()); // get the replyHash which is HMac of the encHash we sent to the // server //TODO restore identify verify // String replyHash = connection // .getHeaderField(SecureShuffleUtils.HTTP_HEADER_REPLY_URL_HASH); // if (replyHash == null) // { // throw new IOException("security validation of TT Map output failed"); // } // LOG.debug("url=" + msgToEncode + ";encHash=" + encHash + ";replyHash=" + replyHash); // verify that replyHash is HMac of encHash // SecureShuffleUtils.verifyReply(replyHash, encHash, jobTokenSecret); LOG.info("for url=" + msgToEncode + " sent hash and receievd reply"); } catch (IOException ie) { ioErrs.increment(1); LOG.warn("Failed to connect to " + host + " with " + remaining.size() + " map outputs", ie); // If connect did not succeed, just mark all the maps as failed, // indirectly penalizing the host if (!connectSucceeded) { for (TaskAttemptID left : remaining) { scheduler.copyFailed(left, host, connectSucceeded); } } else { // If we got a read error at this stage, it implies there was a // problem // with the first map, typically lost map. So, penalize only // that map // and add the rest TaskAttemptID firstMap = maps.get(0); scheduler.copyFailed(firstMap, host, connectSucceeded); } // Add back all the remaining maps, WITHOUT marking them as failed for (TaskAttemptID left : remaining) { scheduler.putBackKnownMapOutput(host, left); } return; } try { // Loop through available map-outputs and fetch them // On any error, good becomes false and we exit after putting back // the remaining maps to the yet_to_be_fetched list boolean good = true; while (!remaining.isEmpty() && good) { good = copyMapOutput(host, input, remaining); } IOUtils.cleanup(LOG, input); // Sanity check if (good && !remaining.isEmpty()) { throw new IOException( "server didn't return all expected map outputs: " + remaining.size() + " left."); } } finally { for (TaskAttemptID left : remaining) { scheduler.putBackKnownMapOutput(host, left); } } }