List of usage examples for java.net URLConnection setReadTimeout
public void setReadTimeout(int timeout)
From source file:org.kuali.mobility.maps.service.LocationServiceImpl.java
private String getPageAsStringFromUrl(String link) { String html = ""; BufferedReader in = null;//from www . j a v a 2 s . c o m try { URL url = new URL(link); URLConnection conn = url.openConnection(); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { html += inputLine + " \n"; } } catch (Exception e) { // LOG.error(e.getMessage(), e); } finally { if (in != null) { try { in.close(); } catch (Exception e) { } } } return html; }
From source file:org.wso2.iot.system.service.api.OTAServerManager.java
public void startDownloadUpgradePackage(final OTAServerManager serverManager) { if (asyncTask != null) { asyncTask.cancel(true);/* www . j a v a 2 s . c o m*/ } asyncTask = new AsyncTask<Void, Void, Void>() { protected Void doInBackground(Void... unused) { Preference.putString(context, context.getResources().getString(R.string.upgrade_download_status), Constants.Status.OTA_UPGRADE_ONGOING); File targetFile = new File(FileUtils.getUpgradePackageFilePath()); if (targetFile.exists()) { targetFile.delete(); } try { boolean fileStatus = targetFile.createNewFile(); if (!fileStatus) { Log.e(TAG, "Update package file creation failed."); } } catch (IOException e) { String message = "Update package file retrieval error."; Log.e(TAG, message + e); reportDownloadError(OTAStateChangeListener.ERROR_WRITE_FILE_ERROR); CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE, Constants.Code.FAILURE, Constants.Status.INTERNAL_ERROR, message); } try { wakeLock.acquire(); URL url = serverConfig.getPackageURL(); Log.d(TAG, "Start downloading package:" + url.toString()); URLConnection connection = url.openConnection(); connection.setConnectTimeout(Constants.FIRMWARE_UPGRADE_CONNECTIVITY_TIMEOUT); connection.setReadTimeout(Constants.FIRMWARE_UPGRADE_READ_TIMEOUT); lengthOfFile = connection.getContentLength(); downloadedLength = 0; InputStream input = new BufferedInputStream(url.openStream()); OutputStream output = new FileOutputStream(targetFile); Timer timeoutTimer = new Timer(); Log.d(TAG, "Update package file size:" + lengthOfFile); if (getFreeDiskSpace() < lengthOfFile) { String message = "Device does not have enough memory to download the OTA" + " update"; CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE, Constants.Code.FAILURE, Constants.Status.LOW_DISK_SPACE, message); CommonUtils.callAgentApp(context, Constants.Operation.FIRMWARE_UPGRADE_FAILURE, Preference.getInt(context, context.getResources().getString(R.string.operation_id)), message); Log.e(TAG, message); return null; } byte data[] = new byte[DEFAULT_BYTES]; long count; isProgressUpdateTerminated = false; executor = new DownloadProgressUpdateExecutor(); executor.execute(new Runnable() { @Override public void run() { while (lengthOfFile > downloadedLength && !isProgressUpdateTerminated) { Preference.putString(context, context.getResources().getString(R.string.upgrade_download_status), Constants.Status.OTA_UPGRADE_ONGOING); publishDownloadProgress(lengthOfFile, downloadedLength); try { Thread.sleep(1000); } catch (InterruptedException ignored) { } } } }); while ((count = input.read(data)) >= 0) { downloadedLength += count; output.write(data, DEFAULT_OFFSET, (int) count); timeoutTimer.cancel(); timeoutTimer = new Timer(); timeoutTimer.schedule(new Timeout(this), Constants.FIRMWARE_UPGRADE_READ_TIMEOUT); } publishDownloadProgress(lengthOfFile, downloadedLength); isProgressUpdateTerminated = true; timeoutTimer.cancel(); output.flush(); output.close(); input.close(); Preference.putString(context, context.getResources().getString(R.string.upgrade_download_status), context.getResources().getString(R.string.status_success)); if (serverManager.stateChangeListener != null) { serverManager.stateChangeListener.onStateOrProgress( OTAStateChangeListener.STATE_IN_DOWNLOADING, DEFAULT_STATE_ERROR_CODE, null, DEFAULT_STATE_INFO_CODE); } } catch (SocketTimeoutException e) { String message = "Connection failure (Socket timeout) when downloading update package."; Log.e(TAG, message + e); CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE, Constants.Code.FAILURE, Constants.Status.CONNECTION_FAILED, message); CommonUtils.callAgentApp(context, Constants.Operation.FAILED_FIRMWARE_UPGRADE_NOTIFICATION, Preference.getInt(context, context.getResources().getString(R.string.operation_id)), message); Preference.putString(context, context.getResources().getString(R.string.upgrade_download_status), Constants.Status.CONNECTION_FAILED); } catch (IOException e) { String message = "Unable to find firmware upgrade package " + serverConfig.getPackageURL().toString(); Log.e(TAG, message + e); CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE, Constants.Code.FAILURE, Constants.Status.FILE_NOT_FOUND, message); CommonUtils.callAgentApp(context, Constants.Operation.FAILED_FIRMWARE_UPGRADE_NOTIFICATION, Preference.getInt(context, context.getResources().getString(R.string.operation_id)), message); reportDownloadError(OTAStateChangeListener.ERROR_WRITE_FILE_ERROR); Preference.putString(context, context.getResources().getString(R.string.upgrade_download_status), Constants.Status.FILE_NOT_FOUND); } finally { wakeLock.release(); wakeLock.acquire(2); if (targetFile.exists() && lengthOfFile != downloadedLength) { targetFile.delete(); String status = Preference.getString(context, context.getResources().getString(R.string.upgrade_download_status)); if (!Constants.Status.OTA_UPGRADE_ONGOING.equals(status)) { Preference.putString(context, context.getResources().getString(R.string.upgrade_download_status), Constants.Status.OTA_DOWNLOAD_FAILED); } } } return null; } }.executeOnExecutor(threadPoolExecutor); }
From source file:org.xlrnet.metadict.engines.heinzelnisse.HeinzelnisseEngine.java
@NotNull private URLConnection buildUrlConnection(@NotNull URL url) throws IOException { URLConnection connection = url.openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("X-Requested-With", "XMLHttpRequest"); connection.setRequestProperty("Referrer", "http://www.heinzelnisse.info/app"); connection.setConnectTimeout(5000);/* www. j ava 2 s . co m*/ connection.setReadTimeout(5000); return connection; }
From source file:org.geoserver.wps.Execute.java
/** * Executes/* w w w . j a v a2 s . c o m*/ * * @param ref * @return */ Object executeRemoteRequest(InputReferenceType ref, ComplexPPIO ppio, String inputId) throws Exception { URL destination = new URL(ref.getHref()); HttpMethod method = null; GetMethod refMethod = null; InputStream input = null; InputStream refInput = null; // execute the request try { if ("http".equalsIgnoreCase(destination.getProtocol())) { // setup the client HttpClient client = new HttpClient(); // setting timeouts (30 seconds, TODO: make this configurable) HttpConnectionManagerParams params = new HttpConnectionManagerParams(); params.setSoTimeout(connectionTimeout); params.setConnectionTimeout(connectionTimeout); // TODO: make the http client a well behaved http client, no more than x connections // per server (x admin configurable maybe), persistent connections and so on HttpConnectionManager manager = new SimpleHttpConnectionManager(); manager.setParams(params); client.setHttpConnectionManager(manager); // prepare either a GET or a POST request if (ref.getMethod() == null || ref.getMethod() == MethodType.GET_LITERAL) { GetMethod get = new GetMethod(ref.getHref()); get.setFollowRedirects(true); method = get; } else { String encoding = ref.getEncoding(); if (encoding == null) { encoding = "UTF-8"; } PostMethod post = new PostMethod(ref.getHref()); Object body = ref.getBody(); if (body == null) { if (ref.getBodyReference() != null) { URL refDestination = new URL(ref.getBodyReference().getHref()); if ("http".equalsIgnoreCase(refDestination.getProtocol())) { // open with commons http client refMethod = new GetMethod(ref.getBodyReference().getHref()); refMethod.setFollowRedirects(true); client.executeMethod(refMethod); refInput = refMethod.getResponseBodyAsStream(); } else { // open with the built-in url management URLConnection conn = refDestination.openConnection(); conn.setConnectTimeout(connectionTimeout); conn.setReadTimeout(connectionTimeout); refInput = conn.getInputStream(); } post.setRequestEntity(new InputStreamRequestEntity(refInput, ppio.getMimeType())); } else { throw new WPSException("A POST request should contain a non empty body"); } } else if (body instanceof String) { post.setRequestEntity(new StringRequestEntity((String) body, ppio.getMimeType(), encoding)); } else { throw new WPSException("The request body should be contained in a CDATA section, " + "otherwise it will get parsed as XML instead of being preserved as is"); } method = post; } // add eventual extra headers if (ref.getHeader() != null) { for (Iterator it = ref.getHeader().iterator(); it.hasNext();) { HeaderType header = (HeaderType) it.next(); method.setRequestHeader(header.getKey(), header.getValue()); } } int code = client.executeMethod(method); if (code == 200) { input = method.getResponseBodyAsStream(); } else { throw new WPSException("Error getting remote resources from " + ref.getHref() + ", http error " + code + ": " + method.getStatusText()); } } else { // use the normal url connection methods then... URLConnection conn = destination.openConnection(); conn.setConnectTimeout(connectionTimeout); conn.setReadTimeout(connectionTimeout); input = conn.getInputStream(); } // actually parse teh data if (input != null) { return ppio.decode(input); } else { throw new WPSException("Could not find a mean to read input " + inputId); } } finally { // make sure to close the connection and streams no matter what if (input != null) { input.close(); } if (method != null) { method.releaseConnection(); } if (refMethod != null) { refMethod.releaseConnection(); } } }
From source file:org.gudy.azureus2.pluginsimpl.local.utils.xml.simpleparser.SimpleXMLParserDocumentImpl.java
private void createSupport(InputStream input_stream) throws SimpleXMLParserDocumentException { try {/*from ww w . j a v a2 s. co m*/ DocumentBuilderFactory dbf = getDBF(); // Step 2: create a DocumentBuilder that satisfies the constraints // specified by the DocumentBuilderFactory DocumentBuilder db = dbf.newDocumentBuilder(); // Set an ErrorHandler before parsing OutputStreamWriter errorWriter = new OutputStreamWriter(System.err); MyErrorHandler error_handler = new MyErrorHandler(new PrintWriter(errorWriter, true)); db.setErrorHandler(error_handler); db.setEntityResolver(new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) { // System.out.println( publicId + ", " + systemId ); // handle bad DTD external refs try { URL url = new URL(systemId); if (source_url != null) { String net = AENetworkClassifier.categoriseAddress(source_url.getHost()); if (net != AENetworkClassifier.AT_PUBLIC) { if (AENetworkClassifier.categoriseAddress(url.getHost()) != net) { return new InputSource(new ByteArrayInputStream( "<?xml version='1.0' encoding='UTF-8'?>".getBytes())); } } } String host = url.getHost(); InetAddress.getByName(host); // try connecting too as connection-refused will also bork XML parsing InputStream is = null; try { URLConnection con = url.openConnection(); con.setConnectTimeout(15 * 1000); con.setReadTimeout(15 * 1000); is = con.getInputStream(); byte[] buffer = new byte[32]; int pos = 0; while (pos < buffer.length) { int len = is.read(buffer, pos, buffer.length - pos); if (len <= 0) { break; } pos += len; } String str = new String(buffer, "UTF-8").trim().toLowerCase(Locale.US); if (!str.contains("<?xml")) { // not straightforward to check for naked DTDs, could be lots of <!-- commentry preamble which of course can occur // in HTML too buffer = new byte[32000]; pos = 0; while (pos < buffer.length) { int len = is.read(buffer, pos, buffer.length - pos); if (len <= 0) { break; } pos += len; } str += new String(buffer, "UTF-8").trim().toLowerCase(Locale.US); if (str.contains("<html") && str.contains("<head")) { throw (new Exception("Bad DTD")); } } } catch (Throwable e) { return new InputSource( new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes())); } finally { if (is != null) { try { is.close(); } catch (Throwable e) { } } } return (null); } catch (UnknownHostException e) { return new InputSource( new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes())); } catch (Throwable e) { return (null); } } }); // Step 3: parse the input file document = db.parse(input_stream); SimpleXMLParserDocumentNodeImpl[] root_nodes = parseNode(document, false); int root_node_count = 0; // remove any processing instructions such as <?xml-stylesheet for (int i = 0; i < root_nodes.length; i++) { SimpleXMLParserDocumentNodeImpl node = root_nodes[i]; if (node.getNode().getNodeType() != Node.PROCESSING_INSTRUCTION_NODE) { root_node = node; root_node_count++; } } if (root_node_count != 1) { throw (new SimpleXMLParserDocumentException( "invalid document - " + root_nodes.length + " root elements")); } } catch (Throwable e) { throw (new SimpleXMLParserDocumentException(e)); } }
From source file:edu.usf.cutr.gtfs_realtime.bullrunner.GtfsRealtimeProviderImpl.java
private Pair downloadVehicleDetails() throws IOException, JSONException { URLConnection connection = null; try {//from w ww. ja v a2s .c o m connection = _url.openConnection(); } catch (Exception ex) { _log.error("Error in opening feeds url", ex); } connection.setConnectTimeout(10000); // connectTimeout is time out in miliseconds connection.setReadTimeout(10000); java.io.InputStream in = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); StringBuilder builder = new StringBuilder(); String inputLine; JSONArray stopIDsArray; JSONArray vehicleArray; try { while ((inputLine = reader.readLine()) != null) builder.append(inputLine).append("\n"); JSONObject object = (JSONObject) new JSONTokener(builder.toString()).nextValue(); String data = object.getString("PredictionData"); JSONObject child2_obj = new JSONObject(data); responseTimeStamp = child2_obj.getString("TimeStamp"); stopIDsArray = child2_obj.getJSONArray("StopPredictions"); vehicleArray = child2_obj.getJSONArray("VehicleLocationData"); } catch (java.net.SocketTimeoutException ex) { _log.error("Error readline, server dosn't close the connection.", ex); stopIDsArray = null; vehicleArray = null; } return new Pair(stopIDsArray, vehicleArray); }
From source file:org.sakaiproject.lessonbuildertool.service.AjaxServer.java
public String getMimeType(String url) { Session s = SessionManager.getCurrentSession(); if (s == null || s.getUserId() == null) { // return ""; }// w ww. j a v a 2 s .co m if (SimplePageBean.getYoutubeKeyFromUrl(url) != null) return "application/youtube"; String mimeType = ""; URLConnection conn = null; try { conn = new URL(new URL(ServerConfigurationService.getServerUrl()), url).openConnection(); conn.setConnectTimeout(10000); conn.setReadTimeout(10000); conn.connect(); String t = conn.getContentType(); if (t != null && !t.equals("")) { int i = t.indexOf(";"); if (i >= 0) t = t.substring(0, i); t = t.trim(); mimeType = t; } } catch (Exception e) { } finally { if (conn != null) { try { conn.getInputStream().close(); } catch (Exception e) { // log.error("getTypeOfUrl unable to close " + e); } } } if (mimeType == null || mimeType.equals("")) { String name = url; // starts after last / int i = name.lastIndexOf("/"); if (i >= 0) name = name.substring(i + 1); String extension = null; i = name.lastIndexOf("."); if (i > 0) extension = name.substring(i + 1); if (extension == null) return ""; if (SimplePageBean.imageTypes.contains(extension)) { return "image/unknown"; } if (extension.equals("html") || extension.equals("htm")) { return "text/html"; } else if (extension.equals("xhtml") || extension.equals("xht")) { return "application/xhtml+xml"; } else { return ""; } } return mimeType; }
From source file:org.apache.hadoop.mapred.JobClient.java
private static void getTaskLogs(TaskAttemptID taskId, URL taskLogUrl, OutputStream out) { try {/*ww w .ja v a 2s . c om*/ URLConnection connection = taskLogUrl.openConnection(); connection.setReadTimeout(tasklogtimeout); connection.setConnectTimeout(tasklogtimeout); BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream())); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(out)); try { String logData = null; while ((logData = input.readLine()) != null) { if (logData.length() > 0) { output.write(taskId + ": " + logData + "\n"); output.flush(); } } } finally { input.close(); } } catch (IOException ioe) { LOG.warn("Error reading task output" + ioe.getMessage()); } }
From source file:org.apache.hadoop.mapred.task.reduce.Fetcher.java
/** * The crux of the matter...//w ww .j a v a 2 s . com * * @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); } } }
From source file:org.sakaiproject.profile2.logic.ProfileImageLogicImpl.java
/** * Get a URL resource as a byte[]//from w ww. j av a 2s . co m * @param url URL to fetch * @return */ private byte[] getUrlAsBytes(String url) { byte[] data = null; try { URL u = new URL(url); URLConnection uc = u.openConnection(); uc.setReadTimeout(5000); //5 sec timeout InputStream inputStream = uc.getInputStream(); data = IOUtils.toByteArray(inputStream); } catch (Exception e) { log.error("Failed to retrieve url bytes: " + e.getClass() + ": " + e.getMessage()); } return data; }