List of usage examples for java.net URLConnection setConnectTimeout
public void setConnectTimeout(int timeout)
From source file:eu.semlibproject.annotationserver.restapis.ServicesAPI.java
/** * Implement a simple proxy//from w w w. j a v a 2s. co m * * @param requestedURL the requested URL * @param req the HttpServletRequest * @return */ @GET @Path("proxy") public Response proxy(@QueryParam(SemlibConstants.URL_PARAM) String requestedURL, @Context HttpServletRequest req) { BufferedReader in = null; try { URL url = new URL(requestedURL); URLConnection urlConnection = url.openConnection(); int proxyConnectionTimeout = ConfigManager.getInstance().getProxyAPITimeout(); // Set base properties urlConnection.setUseCaches(false); urlConnection.setConnectTimeout(proxyConnectionTimeout * 1000); // set max response timeout 15 sec String acceptedDataFormat = req.getHeader(SemlibConstants.HTTP_HEADER_ACCEPT); if (StringUtils.isNotBlank(acceptedDataFormat)) { urlConnection.addRequestProperty(SemlibConstants.HTTP_HEADER_ACCEPT, acceptedDataFormat); } // Open the connection urlConnection.connect(); if (urlConnection instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) urlConnection; int statusCode = httpConnection.getResponseCode(); if (statusCode == HttpURLConnection.HTTP_MOVED_TEMP || statusCode == HttpURLConnection.HTTP_MOVED_PERM) { // Follow the redirect String newLocation = httpConnection.getHeaderField(SemlibConstants.HTTP_HEADER_LOCATION); httpConnection.disconnect(); if (StringUtils.isNotBlank(newLocation)) { return this.proxy(newLocation, req); } else { return Response.status(statusCode).build(); } } else if (statusCode == HttpURLConnection.HTTP_OK) { // Send the response StringBuilder sbf = new StringBuilder(); // Check if the contentType is supported boolean contentTypeSupported = false; String contentType = httpConnection.getHeaderField(SemlibConstants.HTTP_HEADER_CONTENT_TYPE); List<String> supportedMimeTypes = ConfigManager.getInstance().getProxySupportedMimeTypes(); if (contentType != null) { for (String cMime : supportedMimeTypes) { if (contentType.equals(cMime) || contentType.contains(cMime)) { contentTypeSupported = true; break; } } } if (!contentTypeSupported) { httpConnection.disconnect(); return Response.status(Status.NOT_ACCEPTABLE).build(); } String contentEncoding = httpConnection.getContentEncoding(); if (StringUtils.isBlank(contentEncoding)) { contentEncoding = "UTF-8"; } InputStreamReader inStrem = new InputStreamReader((InputStream) httpConnection.getContent(), Charset.forName(contentEncoding)); in = new BufferedReader(inStrem); String inputLine; while ((inputLine = in.readLine()) != null) { sbf.append(inputLine); sbf.append("\r\n"); } in.close(); httpConnection.disconnect(); return Response.status(statusCode).header(SemlibConstants.HTTP_HEADER_CONTENT_TYPE, contentType) .entity(sbf.toString()).build(); } else { httpConnection.disconnect(); return Response.status(statusCode).build(); } } return Response.status(Status.BAD_REQUEST).build(); } catch (MalformedURLException ex) { logger.log(Level.SEVERE, null, ex); return Response.status(Status.BAD_REQUEST).build(); } catch (IOException ex) { logger.log(Level.SEVERE, null, ex); return Response.status(Status.INTERNAL_SERVER_ERROR).build(); } finally { if (in != null) { try { in.close(); } catch (IOException ex) { logger.log(Level.SEVERE, null, ex); return Response.status(Status.INTERNAL_SERVER_ERROR).build(); } } } }
From source file:dreamboxdataservice.DreamboxDataService.java
private void getEPGData(TvDataUpdateManager updateManager, Channel ch) { try {//from w w w . ja v a 2s . c om URL url = new URL("http://" + mProperties.getProperty("ip", "") + "/web/epgservice?sRef=" + StringUtils.replace(StringUtils.replace(ch.getId().substring(5), "_", ":"), " ", "%20")); URLConnection connection = url.openConnection(); String userpassword = mProperties.getProperty("username", "") + ":" + IOUtilities .xorEncode(mProperties.getProperty("password", ""), DreamboxSettingsPanel.PASSWORDSEED); String encoded = new String(Base64.encodeBase64(userpassword.getBytes())); connection.setRequestProperty("Authorization", "Basic " + encoded); connection.setConnectTimeout(60); InputStream stream = connection.getInputStream(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); DreamboxChannelHandler handler = new DreamboxChannelHandler(updateManager, ch); saxParser.parse(stream, handler); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:dreamboxdataservice.DreamboxDataService.java
/** * @param service Service-ID//from ww w . j a v a2s . co m * @return Data of specific service */ public TreeMap<String, String> getServiceData(String service) { try { URL url = new URL("http://" + mProperties.getProperty("ip", "") + "/web/getservices?sRef=" + service); URLConnection connection = url.openConnection(); String userpassword = mProperties.getProperty("username", "") + ":" + IOUtilities .xorEncode(mProperties.getProperty("password", ""), DreamboxSettingsPanel.PASSWORDSEED); String encoded = new String(Base64.encodeBase64(userpassword.getBytes())); connection.setRequestProperty("Authorization", "Basic " + encoded); connection.setConnectTimeout(10); InputStream stream = connection.getInputStream(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); DreamboxHandler handler = new DreamboxHandler(); saxParser.parse(stream, handler); return handler.getData(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:dreamboxdataservice.DreamboxDataService.java
/** * @param service Service-ID/*from ww w . jav a2 s .c om*/ * @return Data of specific service */ public TreeMap<String, String> getServiceDataBonquets(String service) { try { URL url = new URL("http://" + mProperties.getProperty("ip", "") + "/web/getservices?bRef=" + service); URLConnection connection = url.openConnection(); String userpassword = mProperties.getProperty("username", "") + ":" + IOUtilities .xorEncode(mProperties.getProperty("password", ""), DreamboxSettingsPanel.PASSWORDSEED); String encoded = new String(Base64.encodeBase64(userpassword.getBytes())); connection.setRequestProperty("Authorization", "Basic " + encoded); connection.setConnectTimeout(10); InputStream stream = connection.getInputStream(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); DreamboxHandler handler = new DreamboxHandler(); saxParser.parse(stream, handler); return handler.getData(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:bammerbom.ultimatecore.bukkit.UltimateUpdater.java
/** * Make a connection to the BukkitDev API and request the newest file's * details.//from w w w.j a v a 2 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:edu.hackathon.perseus.core.httpSpeedTest.java
private double doUpload(String phpFile, InputStream uploadFileIs, String fileName) { URLConnection conn = null; OutputStream os = null;/* w w w. j a v a2s. c o m*/ InputStream is = null; double bw = 0.0; try { String response = ""; Date oldTime = new Date(); URL url = new URL(amazonDomain + "/" + phpFile); String boundary = "---------------------------4664151417711"; conn = url.openConnection(); conn.setDoOutput(true); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); byte[] fileData = new byte[uploadFileIs.available()]; uploadFileIs.read(fileData); uploadFileIs.close(); String message1 = "--" + boundary + CrLf; message1 += "Content-Disposition: form-data;"; message1 += "name=\"uploadedfile\"; filename=\"" + fileName + "\"" + CrLf; message1 += "Content-Type: text/plain; charset=UTF-8" + CrLf + CrLf; // the file is sent between the messages in the multipart message. String message2 = CrLf + "--" + boundary + "--" + CrLf; conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); int contentLenght = message1.length() + message2.length() + fileData.length; // might not need to specify the content-length when sending chunked data. conn.setRequestProperty("Content-Length", String.valueOf(contentLenght)); os = conn.getOutputStream(); os.write(message1.getBytes()); // SEND THE IMAGE int index = 0; int size = 1024; do { if ((index + size) > fileData.length) { size = fileData.length - index; } os.write(fileData, index, size); index += size; } while (index < fileData.length); os.write(message2.getBytes()); os.flush(); is = conn.getInputStream(); char buff = 512; int len; byte[] data = new byte[buff]; do { len = is.read(data); if (len > 0) { response += new String(data, 0, len); } } while (len > 0); if (response.equals("200")) { Date newTime = new Date(); double milliseconds = newTime.getTime() - oldTime.getTime(); bw = ((double) contentLenght * 8) / (milliseconds * (double) 1000); } } catch (Exception e) { System.out.println("Exception is fired in upload test. error:" + e.getMessage()); } finally { try { os.close(); } catch (Exception e) { //System.out.println("Exception is fired in os.close. error:" + e.getMessage()); } try { is.close(); } catch (Exception e) { //System.out.println("Exception is fired in is.close. error:" + e.getMessage()); } } return bw; }
From source file:us.derfers.tribex.rapids.Loader.java
/** * Loads scripts and styles from link tags. * @param linkElement The element from which to load the content from. * @param engine The ScriptEngine to run links to scripts in. * @return//w ww. j a v a 2 s .c om */ private boolean parseLinks(Element linkElement, ScriptEngine engine) { //Check and see if the <link> tag contains a rel and href attribute if (linkElement.getAttribute("href") != null) { //If it links to a stylesheet if ((linkElement.getAttributeNode("rel") != null && linkElement.getAttributeNode("rel").getTextContent().equals("stylesheet")) || linkElement.getAttributeNode("href").getTextContent().endsWith(".css")) { //Check and see if the href and URL exist. if (linkElement.getAttributeNode("href").getNodeValue().contains("://")) { try { if (this.loadStyles( IOUtils.toString(new URL(linkElement.getAttributeNode("href").getNodeValue())), null) == false) { Utilities.showError("Error: invalid file in link tag pointing to " + linkElement.getAttributeNode("href").getTextContent()); return false; } if (linkElement.getAttributeNode("cache") != null) { try { FileUtils.writeStringToFile( new File(Globals .getCWD(linkElement.getAttributeNode("cache").getTextContent())), IOUtils.toString( new URL(linkElement.getAttributeNode("href").getNodeValue()))); } catch (Exception e2) { Utilities.showError("Error: unable to cache to file " + linkElement.getAttributeNode("cache").getTextContent()); } } } catch (Exception e) { Utilities.showError( "Unable to locate " + linkElement.getAttributeNode("href").getNodeValue()); //Attempt to load from the fallback file. (If tag and file exist) if (linkElement.getAttributeNode("fallback") != null) { if (this.loadStyles(null, Globals .getCWD(linkElement.getAttributeNode("fallback").getTextContent())) == false) { Utilities.showError("Error: invalid file in fallback tag pointing to " + linkElement.getAttributeNode("fallback").getTextContent()); return false; } ; } } //Load from file } else { if (this.loadStyles(null, Globals.getCWD(linkElement.getAttributeNode("href").getTextContent())) == false) { Utilities.showError("Error: invalid file in link tag pointing to " + linkElement.getAttributeNode("href").getTextContent()); return false; } ; } //If it links to a script } else if ((linkElement.getAttributeNode("rel") != null && linkElement.getAttributeNode("rel").getTextContent().equals("script")) || linkElement.getAttributeNode("href").getTextContent().endsWith(".js")) { //Check and see if the file exists if (linkElement.getAttributeNode("href").getNodeValue().contains("://")) { //Run script in file URL url; try { url = new URL(linkElement.getAttributeNode("href").getTextContent()); URLConnection connection = url.openConnection(); connection.setConnectTimeout(10000); connection.setReadTimeout(10000); engine.eval(new InputStreamReader(connection.getInputStream()), "Remote file: " + linkElement.getAttributeNode("href").getTextContent()); if (linkElement.getAttributeNode("cache") != null) { try { FileUtils.writeStringToFile( new File(Globals .getCWD(linkElement.getAttributeNode("cache").getTextContent())), IOUtils.toString( new URL(linkElement.getAttributeNode("href").getNodeValue()))); } catch (Exception e2) { Utilities.showError("Error: unable to cache to file " + linkElement.getAttributeNode("cache").getTextContent()); } } return true; } catch (Exception e) { //Attempt to load from the fallback file. (If tag and file exist) if (linkElement.getAttributeNode("fallback") != null) { try { engine.eval( new java.io.FileReader(Globals .getCWD(linkElement.getAttributeNode("fallback").getTextContent())), linkElement.getAttributeNode("fallback").getTextContent()); } catch (Exception e2) { Utilities.showError("Error: invalid file in fallback tag pointing to " + linkElement.getAttributeNode("fallback").getTextContent()); } } else { Utilities.showError( "Unable to load " + linkElement.getAttributeNode("href").getTextContent() + ". No fallback found."); } } } else { try { //Run script in file engine.eval( new java.io.FileReader( Globals.getCWD(linkElement.getAttributeNode("href").getTextContent())), linkElement.getAttributeNode("href").getTextContent()); return true; } catch (FileNotFoundException e) { Utilities.showError("Error: invalid file in link tag pointing to " + linkElement.getAttributeNode("href").getTextContent()); e.printStackTrace(); return false; } catch (DOMException e) { Utilities.showError("Error: Improperly formatted XML"); e.printStackTrace(); return false; } catch (Exception e) { Utilities.showError("Error: invalid script in file " + linkElement.getAttributeNode("href").getTextContent()); e.printStackTrace(); return false; } } } else { //Attempt to load as a .rsm file //If the file is a URL on the internet: if (linkElement.getAttributeNode("href").getNodeValue().contains("://")) { try { //Load the file from the internet. Main.loader.loadAll(Utilities.EscapeScriptTags( IOUtils.toString(new URL(linkElement.getAttributeNode("href").getNodeValue())))); if (linkElement.getAttributeNode("cache") != null) { try { FileUtils.writeStringToFile( new File(Globals .getCWD(linkElement.getAttributeNode("cache").getTextContent())), IOUtils.toString( new URL(linkElement.getAttributeNode("href").getNodeValue()))); } catch (Exception e2) { Utilities.showError("Error: unable to cache to file " + linkElement.getAttributeNode("cache").getTextContent()); } } } catch (Exception e) { if (linkElement.getAttributeNode("fallback") != null) { try { Main.loader.loadAll(Utilities.EscapeScriptTags(FileUtils.readFileToString(new File( Globals.getCWD(linkElement.getAttributeNode("fallback").getNodeValue()))))); } catch (Exception e2) { Utilities.showError("Error: invalid file in fallback tag pointing to " + linkElement.getAttributeNode("fallback").getTextContent()); } } } //Load the file from the Hard Drive } else { try { Main.loader.loadAll(Utilities.EscapeScriptTags(FileUtils.readFileToString( new File(Globals.getCWD(linkElement.getAttributeNode("href").getNodeValue()))))); } catch (DOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } else { System.out.println(linkElement.toString()); Utilities.showError( "Warning: <link> tags must contain a href attribute and a rel attribute. Skipping tag."); } return false; }
From source file:com.miz.apis.thetvdb.TheTVDbService.java
private ArrayList<TvShow> getListFromUrl(String serviceUrl) { ArrayList<TvShow> results = new ArrayList<TvShow>(); // Fail early if (TextUtils.isEmpty(serviceUrl)) return results; try {/* ww w . j a va 2 s.co m*/ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); URL url = new URL(serviceUrl); URLConnection con = url.openConnection(); con.setReadTimeout(60000); con.setConnectTimeout(60000); Document doc = db.parse(con.getInputStream()); doc.getDocumentElement().normalize(); // Check if there's an element with the "id" tag NodeList nodeList = doc.getElementsByTagName("Series"); for (int i = 0; i < nodeList.getLength(); i++) { if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) { TvShow show = new TvShow(); Element firstElement = (Element) nodeList.item(i); NodeList list; Element element; NodeList tag; try { list = firstElement.getElementsByTagName("SeriesName"); element = (Element) list.item(0); tag = element.getChildNodes(); show.setTitle(tag.item(0).getNodeValue()); } catch (Exception e) { show.setTitle(mContext.getString(R.string.stringNA)); } try { list = firstElement.getElementsByTagName("Overview"); element = (Element) list.item(0); tag = element.getChildNodes(); show.setDescription(tag.item(0).getNodeValue()); } catch (Exception e) { show.setDescription(mContext.getString(R.string.stringNA)); } try { list = firstElement.getElementsByTagName("id"); element = (Element) list.item(0); tag = element.getChildNodes(); show.setId(tag.item(0).getNodeValue()); } catch (Exception e) { show.setId(DbAdapterTvShows.UNIDENTIFIED_ID); } try { list = firstElement.getElementsByTagName("id"); element = (Element) list.item(0); tag = element.getChildNodes(); show.setCoverUrl( "http://thetvdb.com/banners/posters/" + tag.item(0).getNodeValue() + "-1.jpg"); } catch (Exception e) { show.setCoverUrl(""); } try { list = firstElement.getElementsByTagName("FirstAired"); element = (Element) list.item(0); tag = element.getChildNodes(); show.setFirstAired(tag.item(0).getNodeValue()); } catch (Exception e) { show.setFirstAired(mContext.getString(R.string.stringNA)); } results.add(show); } } } catch (Exception e) { } return results; }
From source file:io.hummer.util.ws.WebServiceClient.java
private InvocationResult doInvokeGET(String parameters, Map<String, String> httpHeaders, int retries, long connectTimeoutMS, long readTimeoutMS, boolean doUseCache) throws Exception { if (retries < 0) throw new Exception("Invocation to " + endpointURL + " failed: " + xmlUtil.toString(parameters)); String host = new URL(endpointURL).getHost(); if (!lastRequestedHosts.containsKey(host)) { lastRequestedHosts.put(host, new AtomicLong()); }//ww w . ja va 2 s . c o m Object lockForTargetHost = lastRequestedHosts.get(host); parameters = parameters.trim(); String urlString = endpointURL; if (!strUtil.isEmpty(parameters)) { String separator = endpointURL.contains("?") ? "&" : "?"; urlString = endpointURL + separator + parameters; } if (doUseCache) { /** retrieve result from document cache */ CacheEntry existing = cache.get(urlString); if (existing != null && !strUtil.isEmpty(existing.value)) { String valueShort = (String) existing.value; if (valueShort.length() > 200) valueShort = valueShort.substring(0, 200) + "..."; AtomicReference<Element> eRef = new AtomicReference<Element>(); Parallelization.warnIfNoResultAfter(eRef, "! Client could not convert element (" + existing.value.length() + " bytes) within 15 seconds: " + valueShort, 15 * 1000); Parallelization.warnIfNoResultAfter(eRef, "! Client could not convert element (" + existing.value.length() + " bytes) within 40 seconds: " + valueShort, 40 * 1000); Element e = xmlUtil.toElement((String) existing.value); eRef.set(e); logger.info("Result exists in cache for URL " + urlString + " - " + e + " - " + this.xmlUtil.toString().length()); return new InvocationResult(e); } } pauseToAvoidSpamming(); URL url = new URL(urlString); URLConnection c = url.openConnection(); c.setConnectTimeout((int) connectTimeoutMS); c.setReadTimeout((int) readTimeoutMS); logger.info("Retrieving data from service using GET: " + url); String tmpID = PerformanceInterceptor.event(EventType.START_HTTP_GET); for (String key : httpHeaders.keySet()) { c.setRequestProperty(key, httpHeaders.get(key)); } StringBuilder b = new StringBuilder(); synchronized (lockForTargetHost) { try { BufferedReader r = new BufferedReader(new InputStreamReader(c.getInputStream())); String temp; while ((temp = r.readLine()) != null) { b.append(temp); b.append("\n"); } } catch (Exception e) { logger.info("Could not GET page with regular URLConnection, trying HtmlUnit..: " + e); b = getPageUsingHtmlUnit(urlString, httpHeaders, readTimeoutMS, null); } } PerformanceInterceptor.event(EventType.FINISH_HTTP_GET, tmpID); String tmpID1 = PerformanceInterceptor.event(EventType.START_RESPONSE_TO_XML); String result = b.toString().trim(); if (!result.startsWith("<") || !result.endsWith(">")) { // wrap non-xml results (e.g., CSV files) StringBuilder sb = new StringBuilder("<doc><![CDATA["); sb.append(result); sb.append("]]></doc>"); result = sb.toString(); } String tmpID2 = PerformanceInterceptor.event(EventType.START_STRING_TO_XML); Element resultElement = xmlUtil.toElement(result); PerformanceInterceptor.event(EventType.FINISH_STRING_TO_XML, tmpID2); if (doUseCache) { /** put result element to document cache */ cache.putWithoutWaiting(urlString, xmlUtil.toString(resultElement, true)); } InvocationResult invResult = new InvocationResult(resultElement); PerformanceInterceptor.event(EventType.FINISH_RESPONSE_TO_XML, tmpID1); return invResult; }