List of usage examples for java.net URLConnection setRequestProperty
public void setRequestProperty(String key, String value)
From source file:cc.creativecomputing.io.CCIOUtil.java
/** * Simplified method to open a Java InputStream. * <p>/*from w w w . ja v a 2 s. c om*/ * This method is useful if you want to easily open things from the data folder or from a URL, but want an * InputStream object so that you can use other Java methods to take more control of how the stream is read. * </p> * <p> * If the requested item doesn't exist, null is returned. This will also check to see if the user is asking for a * file whose name isn't properly capitalized. It is strongly recommended that libraries use this method to open * data files, so that the loading sequence is handled in the same way. * </p> * * @param theFilename The filename passed in can be: * <ul> * <li>An URL, for instance openStream("http://creativecomputing.cc/");</li> * <li>A file in the application's data folder</li> * <li>Another file to be opened locally</li> * </ul> */ static public InputStream createStream(String theFilename) { InputStream stream = null; // check if the filename makes sense if (theFilename == null || theFilename.length() == 0) return null; // safe to check for this as a url first. this will prevent online try { URL urlObject = new URL(theFilename); URLConnection con = urlObject.openConnection(); // try to be a browser some sources do not like bots con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)"); return con.getInputStream(); } catch (MalformedURLException mfue) { // not a url, that's fine } catch (FileNotFoundException fnfe) { // Java 1.5 likes to throw this when URL not available. } catch (IOException e) { return null; } // load resource from jar using the path with getResourceAsStream if (theFilename.contains(".jar!")) { String[] myParts = theFilename.split("!"); String myJarPath = myParts[0]; if (myJarPath.startsWith("file:")) { myJarPath = myJarPath.substring(5); } String myFilePath = myParts[1]; if (myFilePath.startsWith("/")) { myFilePath = myFilePath.substring(1); } try { @SuppressWarnings("resource") JarFile myJarFile = new JarFile(myJarPath); return myJarFile.getInputStream(myJarFile.getEntry(myFilePath)); } catch (IOException e) { e.printStackTrace(); } } // handle case sensitivity check try { // first see if it's in a data folder File file = dataFile(theFilename); if (!file.exists()) { // next see if it's just in this folder file = new File(CCSystem.applicationPath, theFilename); } if (file.exists()) { try { String filePath = file.getCanonicalPath(); String filenameActual = new File(filePath).getName(); // make sure there isn't a subfolder prepended to the name String filenameShort = new File(theFilename).getName(); // if the actual filename is the same, but capitalized // differently, warn the user. //if (filenameActual.equalsIgnoreCase(filenameShort) && //!filenameActual.equals(filenameShort)) { if (!filenameActual.equals(filenameShort)) { throw new RuntimeException("This file is named " + filenameActual + " not " + theFilename + ". Re-name it " + "or change your code."); } } catch (IOException e) { } } // if this file is ok, may as well just load it stream = new FileInputStream(file); if (stream != null) return stream; // have to break these out because a general Exception might // catch the RuntimeException being thrown above } catch (IOException ioe) { } catch (SecurityException se) { } try { // attempt to load from a local file, used when running as // an application, or as a signed applet try { // first try to catch any security exceptions try { stream = new FileInputStream(dataPath(theFilename)); if (stream != null) return stream; } catch (IOException e2) { } try { stream = new FileInputStream(appPath(theFilename)); if (stream != null) return stream; } catch (Exception e) { } // ignored try { stream = new FileInputStream(theFilename); if (stream != null) return stream; } catch (IOException e1) { } } catch (SecurityException se) { } // online, whups } catch (Exception e) { //die(e.getMessage(), e); e.printStackTrace(); } return null; }
From source file:org.xenmaster.monitoring.engine.Slot.java
public URLConnection getConnection() { try {/*from ww w . java 2 s . c o m*/ if (connection == null || connectToUpdates) { Host host = new Host(reference); URL url; if (connectToUpdates) { url = new URL("http://" + host.getAddress().getCanonicalHostName() + "/rrd_updates?start=" + ((lastPolled - 5000) / 1000) + "&host=true"); } else { url = new URL("http://" + host.getAddress().getCanonicalHostName() + "/host_rrd"); connectToUpdates = true; } URLConnection uc = url.openConnection(); byte[] auth = (Settings.getInstance().getString("Xen.User") + ':' + Settings.getInstance().getString("Xen.Password")).getBytes("UTF-8"); uc.setRequestProperty("Authorization", "Basic " + new String(Base64.encodeBase64(auth))); uc.connect(); lastPolled = System.currentTimeMillis(); connection = uc; } } catch (Exception ex) { busy = false; Logger.getLogger(getClass()).error("Failed to retrieve statistics", ex); } return connection; }
From source file:com.couchbase.client.vbucket.ConfigurationProviderHTTP.java
/** * Create a URL which has the appropriate headers to interact with the * service. Most exception handling is up to the caller. * * @param resource the URI either absolute or relative to the base for this * ClientManager/*from w ww . j a v a2s . co m*/ * @return * @throws java.io.IOException */ private URLConnection urlConnBuilder(URI base, URI resource) throws IOException { if (!resource.isAbsolute() && base != null) { resource = base.resolve(resource); } URL specURL = resource.toURL(); URLConnection connection = specURL.openConnection(); connection.setRequestProperty("Accept", "application/json"); connection.setRequestProperty("user-agent", "spymemcached vbucket client"); connection.setRequestProperty("X-memcachekv-Store-Client-" + "Specification-Version", CLIENT_SPEC_VER); if (restUsr != null) { try { connection.setRequestProperty("Authorization", buildAuthHeader(restUsr, restPwd)); } catch (UnsupportedEncodingException ex) { throw new IOException("Could not encode specified credentials for " + "HTTP request.", ex); } } return connection; }
From source file:eagle.jobrunning.crawler.RMResourceFetcher.java
private List<Object> doFetchRunningJobConfiguration(String appID) throws Exception { InputStream is = null;/*from w w w . j a v a 2s.com*/ try { checkUrl(); String jobID = JobUtils.getJobIDByAppID(appID); String urlString = jobRunningConfigServiceURLBuilder.build(selector.getSelectedUrl(), jobID); LOG.info("Going to fetch job completed information for " + jobID + " , url: " + urlString); final URLConnection connection = URLConnectionUtils.getConnection(urlString); connection.setRequestProperty(XML_HTTP_HEADER, XML_FORMAT); connection.setConnectTimeout(CONNECTION_TIMEOUT); connection.setReadTimeout(READ_TIMEOUT); is = connection.getInputStream(); Map<String, String> configs = XmlHelper.getConfigs(is); return Arrays.asList((Object) configs); } finally { if (is != null) { try { is.close(); } catch (Exception e) { } } } }
From source file:org.apache.eagle.security.hive.jobrunning.HiveJobFetchSpout.java
private boolean fetchRunningConfig(AppInfo appInfo, List<MRJob> mrJobs) { InputStream is = null;/*from w ww . j a v a 2 s .c o m*/ for (MRJob mrJob : mrJobs) { String confURL = appInfo.getTrackingUrl() + Constants.MR_JOBS_URL + "/" + mrJob.getId() + "/" + Constants.MR_CONF_URL + "?" + Constants.ANONYMOUS_PARAMETER; try { LOG.info("fetch job conf from {}", confURL); final URLConnection connection = URLConnectionUtils.getConnection(confURL); connection.setRequestProperty(XML_HTTP_HEADER, XML_FORMAT); connection.setConnectTimeout(CONNECTION_TIMEOUT); connection.setReadTimeout(READ_TIMEOUT); is = connection.getInputStream(); Map<String, String> hiveQueryLog = new HashMap<>(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document dt = db.parse(is); Element element = dt.getDocumentElement(); NodeList propertyList = element.getElementsByTagName("property"); int length = propertyList.getLength(); for (int i = 0; i < length; i++) { Node property = propertyList.item(i); String key = property.getChildNodes().item(0).getTextContent(); String value = property.getChildNodes().item(1).getTextContent(); hiveQueryLog.put(key, value); } if (hiveQueryLog.containsKey(Constants.HIVE_QUERY_STRING)) { collector.emit(new ValuesArray(appInfo.getUser(), mrJob.getId(), Constants.ResourceType.JOB_CONFIGURATION, hiveQueryLog), mrJob.getId()); } } catch (Exception e) { LOG.warn("fetch job conf from {} failed, {}", confURL, e); e.printStackTrace(); return false; } finally { Utils.closeInputStream(is); } } return true; }
From source file:dk.dma.vessel.track.store.AisStoreClient.java
public List<PastTrackPos> getPastTrack(int mmsi, Integer minDist, Duration age) { // Determine URL age = age != null ? age : Duration.parse(pastTrackTtl); minDist = minDist == null ? Integer.valueOf(pastTrackMinDist) : minDist; ZonedDateTime now = ZonedDateTime.now(); String from = now.format(DateTimeFormatter.ISO_INSTANT); ZonedDateTime end = now.minus(age); String to = end.format(DateTimeFormatter.ISO_INSTANT); String interval = String.format("%s/%s", to, from); String url = String.format("%s?mmsi=%d&interval=%s", aisViewUrl, mmsi, interval); final List<PastTrackPos> track = new ArrayList<>(); try {/* www. j a v a 2 s.c om*/ long t0 = System.currentTimeMillis(); // TEST url = url + "&filter=" + URLEncoder.encode("(s.country not in (GBR)) & (s.region!=808)", "UTF-8"); // Set up a few timeouts and fetch the attachment URLConnection con = new URL(url).openConnection(); con.setConnectTimeout(10 * 1000); // 10 seconds con.setReadTimeout(60 * 1000); // 1 minute if (!StringUtils.isEmpty(aisAuthHeader)) { con.setRequestProperty("Authorization", aisAuthHeader); } try (InputStream in = con.getInputStream(); BufferedInputStream bin = new BufferedInputStream(in)) { AisReader aisReader = AisReaders.createReaderFromInputStream(bin); aisReader.registerPacketHandler(new Consumer<AisPacket>() { @Override public void accept(AisPacket p) { AisMessage message = p.tryGetAisMessage(); if (message == null || !(message instanceof IVesselPositionMessage)) { return; } VesselTarget target = new VesselTarget(); target.merge(p, message); if (!target.checkValidPos()) { return; } track.add(new PastTrackPos(target.getLat(), target.getLon(), target.getCog(), target.getSog(), target.getLastPosReport())); } }); aisReader.start(); try { aisReader.join(); } catch (InterruptedException e) { return null; } } LOG.info(String.format("Read %d past track positions in %d ms", track.size(), System.currentTimeMillis() - t0)); } catch (IOException e) { LOG.error("Failed to make REST query: " + url); throw new InternalError("REST endpoint failed"); } LOG.info("AisStore returned track with " + track.size() + " points"); return PastTrack.downSample(track, minDist, age.toMillis()); }
From source file:contestWebsite.ContactUs.java
@Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Query query = new Query("user") .setFilter(new FilterPredicate("name", FilterOperator.EQUAL, req.getParameter("name"))); List<Entity> users = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(3)); Entity feedback = new Entity("feedback"); if (users.size() != 0) { feedback.setProperty("user-id", users.get(0).getProperty("user-id")); }/* w w w . jav a 2s .c o m*/ String name = escapeHtml4(req.getParameter("name")); String school = escapeHtml4(req.getParameter("school")); String comment = escapeHtml4(req.getParameter("text")); String email = escapeHtml4(req.getParameter("email")); HttpSession sess = req.getSession(true); sess.setAttribute("name", name); sess.setAttribute("school", school); sess.setAttribute("email", email); sess.setAttribute("comment", comment); Entity contestInfo = Retrieve.contestInfo(); if (!(Boolean) sess.getAttribute("nocaptcha")) { URL reCaptchaURL = new URL("https://www.google.com/recaptcha/api/siteverify"); String charset = java.nio.charset.StandardCharsets.UTF_8.name(); String reCaptchaQuery = String.format("secret=%s&response=%s&remoteip=%s", URLEncoder.encode((String) contestInfo.getProperty("privateKey"), charset), URLEncoder.encode(req.getParameter("g-recaptcha-response"), charset), URLEncoder.encode(req.getRemoteAddr(), charset)); final URLConnection connection = new URL(reCaptchaURL + "?" + reCaptchaQuery).openConnection(); connection.setRequestProperty("Accept-Charset", charset); String response = CharStreams.toString(CharStreams.newReaderSupplier(new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return connection.getInputStream(); } }, Charsets.UTF_8)); try { JSONObject JSONResponse = new JSONObject(response); if (!JSONResponse.getBoolean("success")) { resp.sendRedirect("/contactUs?captchaError=1"); return; } } catch (JSONException e) { e.printStackTrace(); resp.sendRedirect("/contactUs?captchaError=1"); return; } } feedback.setProperty("name", name); feedback.setProperty("school", school); feedback.setProperty("email", email); feedback.setProperty("comment", new Text(comment)); feedback.setProperty("resolved", false); Transaction txn = datastore.beginTransaction(); try { datastore.put(feedback); txn.commit(); Session session = Session.getDefaultInstance(new Properties(), null); String appEngineEmail = (String) contestInfo.getProperty("account"); try { Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(appEngineEmail, "Tournament Website Admin")); msg.addRecipient(Message.RecipientType.TO, new InternetAddress((String) contestInfo.getProperty("email"), "Contest Administrator")); msg.setSubject("Question about tournament from " + name); msg.setReplyTo(new InternetAddress[] { new InternetAddress(req.getParameter("email"), name), new InternetAddress(appEngineEmail, "Tournament Website Admin") }); VelocityEngine ve = new VelocityEngine(); ve.init(); VelocityContext context = new VelocityContext(); context.put("name", name); context.put("email", email); context.put("school", school); context.put("message", comment); StringWriter sw = new StringWriter(); Velocity.evaluate(context, sw, "questionEmail", ((Text) contestInfo.getProperty("questionEmail")).getValue()); msg.setContent(sw.toString(), "text/html"); Transport.send(msg); } catch (MessagingException e) { e.printStackTrace(); resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); return; } resp.sendRedirect("/contactUs?updated=1"); sess.invalidate(); } catch (Exception e) { e.printStackTrace(); resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); } finally { if (txn.isActive()) { txn.rollback(); } } }
From source file:org.apache.cayenne.rop.http.HttpROPConnector.java
protected InputStream doRequest(Map<String, String> params) throws IOException { URLConnection connection = new URL(url).openConnection(); if (readTimeout != null) { connection.setReadTimeout(readTimeout.intValue()); }//from w ww . j a v a2s .co m addAuthHeader(connection); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("charset", "utf-8"); try (OutputStream output = connection.getOutputStream()) { output.write(ROPUtil.getParamsAsString(params).getBytes(StandardCharsets.UTF_8)); output.flush(); } return connection.getInputStream(); }
From source file:org.t3.metamediamanager.AllocineProvider.java
/** * Generates the url of the request and download the json result * @param method//from w ww . ja va 2s .c om * @param data * @return text content (json string) * @throws ProviderException */ public String makeRequest(String method, LinkedHashMap<String, String> data) throws ProviderException { String queryUrl = API_URL + "/" + method; Date date = new Date(); String sed = (new SimpleDateFormat("yMMdd")).format(date); String builtQuery = paramsToString(data); String sig; sig = new String(Base64.encodeBase64(SHA(SECRET_KEY + builtQuery + "&sed=" + sed))); try { queryUrl += "?" + builtQuery + "&sed=" + sed + "&sig=" + URLEncoder.encode(sig, CHARSET).replace("+", "%20"); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } System.out.println(queryUrl); //We know have the url of the json to download try { URLConnection connection = new URL(queryUrl).openConnection(); connection.setRequestProperty("Accept-Charset", CHARSET); InputStream response; response = connection.getInputStream(); String rep = ""; BufferedReader in = new BufferedReader(new InputStreamReader(response)); String inputLine; while ((inputLine = in.readLine()) != null) rep += inputLine; System.out.println(rep); return rep; } catch (IOException e) { throw new ProviderException("Allocin : erreur lors de la connexion : " + e.getMessage()); } }
From source file:fr.gael.drb.impl.http.HttpNode.java
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override/* ww w. j ava 2 s.c o m*/ public Object getImpl(Class api) { if (api.isAssignableFrom(InputStream.class)) { try { URLConnection urlConnect = this.url.openConnection(); if (this.url.getUserInfo() != null) { // HTTP Basic Authentication. String userpass = this.url.getUserInfo(); String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes())); urlConnect.setRequestProperty("Authorization", basicAuth); } urlConnect.setDoInput(true); urlConnect.setUseCaches(false); return urlConnect.getInputStream(); } catch (IOException e) { return null; } } return null; }