List of usage examples for java.net URLConnection setConnectTimeout
public void setConnectTimeout(int timeout)
From source file:com.pursuer.reader.easyrss.WebpageItemViewCtrl.java
@SuppressLint("SimpleDateFormat") private void showMobilizedPage() { showMobilized = true;//from ww w . j a va 2s .c o m view.findViewById(R.id.Mobilized).setVisibility(View.VISIBLE); view.findViewById(R.id.OriginalContent).setVisibility(View.GONE); view.findViewById(R.id.BtnMobilzedPage).setEnabled(false); view.findViewById(R.id.BtnOriginalPage).setEnabled(true); view.findViewById(R.id.OriginalProgress).setVisibility(View.GONE); view.findViewById(R.id.MobilizedProgress).setVisibility(View.VISIBLE); view.findViewById(R.id.MobilizedContent).setVisibility(View.GONE); final TextView title = (TextView) view.findViewById(R.id.ItemTitle); title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize * 4 / 3); title.setText(item.getTitle()); final TextView info = (TextView) view.findViewById(R.id.ItemInfo); info.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize * 4 / 5); final StringBuilder infoText = new StringBuilder(); final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); sdf.setTimeZone(TimeZone.getDefault()); infoText.append(sdf.format(Utils.timestampToDate(item.getTimestamp()))); if (item.getAuthor() != null && item.getAuthor().length() > 0) { infoText.append(" | By "); infoText.append(item.getAuthor()); } if (item.getSourceTitle() != null && item.getSourceTitle().length() > 0) { infoText.append(" ("); infoText.append(item.getSourceTitle()); infoText.append(")"); } info.setText(infoText); if (thread == null) { thread = new Thread(new Runnable() { @SuppressWarnings("deprecation") @Override public void run() { try { final StringBuilder urlBuilder = new StringBuilder(); urlBuilder.append("http://easyrss.pursuer.me/parser?url="); urlBuilder.append(URLEncoder.encode(item.getHref())); urlBuilder.append("&email="); urlBuilder.append(URLEncoder.encode(dataMgr.getSettingByName(Setting.SETTING_USERNAME))); urlBuilder.append("&version="); urlBuilder.append(context.getString(R.string.Version)); final URLConnection connection = new URL(urlBuilder.toString()).openConnection(); connection.setConnectTimeout(30 * 1000); connection.setReadTimeout(20 * 1000); final InputStreamReader input = new InputStreamReader(connection.getInputStream()); final StringBuilder builder = new StringBuilder(); final char buff[] = new char[8192]; int len; while ((len = input.read(buff)) != -1) { builder.append(new String(buff, 0, len)); } builder.append(theme == SettingTheme.THEME_NORMAL ? DataUtils.DEFAULT_NORMAL_CSS : DataUtils.DEFAULT_DARK_CSS); pageContent = builder.toString(); handler.sendMessage(handler.obtainMessage(MSG_LOADING_FINISHED, WebpageItemViewCtrl.this)); } catch (final MalformedURLException exception) { exception.printStackTrace(); pageContent = genFailedToLoadContentPage(context, theme); handler.sendMessage(handler.obtainMessage(MSG_LOADING_FINISHED, WebpageItemViewCtrl.this)); } catch (final IOException exception) { exception.printStackTrace(); pageContent = genFailedToLoadContentPage(context, theme); handler.sendMessage(handler.obtainMessage(MSG_LOADING_FINISHED, WebpageItemViewCtrl.this)); } } }); thread.setPriority(Thread.MIN_PRIORITY); thread.start(); } else if (!thread.isAlive()) { handler.sendMessage(handler.obtainMessage(MSG_LOADING_FINISHED, WebpageItemViewCtrl.this)); } }
From source file:net.solarnetwork.node.price.delimited.DelimitedPriceDatumDataSource.java
private String readDataRow(URL theUrl) { BufferedReader resp = null;/*from w w w . jav a 2 s . com*/ if (log.isDebugEnabled()) { log.debug("Requesting price data from [" + theUrl + ']'); } try { URLConnection conn = theUrl.openConnection(); conn.setConnectTimeout(this.connectionTimeout); conn.setReadTimeout(this.connectionTimeout); conn.setRequestProperty("Accept", "text/*"); resp = new BufferedReader(new InputStreamReader(conn.getInputStream())); String str; int skipCount = this.skipLines; while ((str = resp.readLine()) != null) { if (skipCount > 0) { skipCount--; continue; } break; } if (log.isTraceEnabled()) { log.trace("Found price data: " + str); } return str; } catch (IOException e) { throw new RuntimeException(e); } finally { if (resp != null) { try { resp.close(); } catch (IOException e) { // ignore this log.debug("Exception closing URL stream", e); } } } }
From source file:net.solarnetwork.node.io.url.UrlDataCollector.java
@Override public void collectData() { String resolvedUrl = url;//from w ww. ja v a 2 s . c o m if (urlFactory != null) { resolvedUrl = urlFactory.getObject(); } URL dataUrl = null; try { dataUrl = new URL(resolvedUrl); } catch (MalformedURLException e) { throw new RuntimeException("Bad url configured: " + resolvedUrl); } if (log.isDebugEnabled()) { log.debug("Connecting to URL [" + resolvedUrl + ']'); } BufferedReader reader = null; String data = null; String enc = null; Pattern pat = Pattern.compile(matchExpression); try { URLConnection conn = dataUrl.openConnection(); conn.setConnectTimeout(connectionTimeout); conn.setReadTimeout(connectionTimeout); conn.setUseCaches(false); InputStream in = conn.getInputStream(); if (this.encoding == null) { enc = conn.getContentEncoding(); if (enc != null) { if (log.isTraceEnabled()) { log.trace("Using connection encoding [" + enc + ']'); } this.encoding = enc; } } if (enc == null) { enc = getEncodingToUse(); } reader = new BufferedReader(new InputStreamReader(in, enc)); String lastLine = null; boolean keepGoing = true; while (keepGoing) { String line = reader.readLine(); if (line == null) { keepGoing = false; if (skipToLastLine) { line = lastLine; } } Matcher m = pat.matcher(line); if (m.find()) { if (log.isDebugEnabled()) { log.debug("Found matching data line [" + line + ']'); } data = line; keepGoing = false; } else { lastLine = line; } } } catch (IOException e) { throw new RuntimeException(e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { if (log.isWarnEnabled()) { log.warn("IOException closing input stream: " + e); } } } } if (data == null) { log.info("Input stream finished without finding expected data"); } else { if (this.buffer == null) { this.buffer = new StringBuilder(data); } else { this.buffer.append(data); } } }
From source file:com.oakesville.mythling.util.HttpHelper.java
private void prepareConnection(URLConnection conn, Map<String, String> headers) throws IOException { conn.setConnectTimeout(getConnectTimeout()); conn.setReadTimeout(getReadTimeout()); for (String key : headers.keySet()) conn.setRequestProperty(key, headers.get(key)); if (method == Method.Post) { ((HttpURLConnection) conn).setRequestMethod("POST"); if (postContent != null) conn.setDoOutput(true);//from www . j a v a 2s.c om } }
From source file:org.drools.core.io.impl.UrlResource.java
private URLConnection openURLConnection(URL url) throws IOException { URLConnection con = url.openConnection(); con.setConnectTimeout(TIMEOUT); con.setReadTimeout(TIMEOUT);//from w w w. j ava 2 s . c o m return con; }
From source file:org.transitime.avl.PollUrlAvlModule.java
/** * Actually reads data from feed and processes it by opening up a URL * specified by getUrl() and then reading the contents. Calls the abstract * method processData() to actually process the input stream. * <p>/*w ww. j av a 2s . c o m*/ * This method needs to be overwritten if not real data from a URL * * @throws Exception * Throws a generic exception since the processing is done in * the abstract method processData() and it could throw any type * of exception since we don't really know how the AVL feed will * be processed. */ protected void getAndProcessData() throws Exception { // For logging IntervalTimer timer = new IntervalTimer(); // Get from the AVL feed subclass the URL to use for this feed String fullUrl = getUrl(); // Log what is happening logger.info("Getting data from feed using url=" + fullUrl); // Create the connection URL url = new URL(fullUrl); URLConnection con = url.openConnection(); // Set the timeout so don't wait forever int timeoutMsec = AvlConfig.getAvlFeedTimeoutInMSecs(); con.setConnectTimeout(timeoutMsec); con.setReadTimeout(timeoutMsec); // Request compressed data to reduce bandwidth used if (useCompression) con.setRequestProperty("Accept-Encoding", "gzip,deflate"); // If authentication being used then set user and password if (authenticationUser.getValue() != null && authenticationPassword.getValue() != null) { String authString = authenticationUser.getValue() + ":" + authenticationPassword.getValue(); byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); String authStringEnc = new String(authEncBytes); con.setRequestProperty("Authorization", "Basic " + authStringEnc); } // Set any additional AVL feed specific request headers setRequestHeaders(con); // Create appropriate input stream depending on whether content is // compressed or not InputStream in = con.getInputStream(); if ("gzip".equals(con.getContentEncoding())) { in = new GZIPInputStream(in); logger.debug("Returned data is compressed"); } else { logger.debug("Returned data is NOT compressed"); } // For debugging logger.debug("Time to access inputstream {} msec", timer.elapsedMsec()); // Call the abstract method to actually process the data timer.resetTimer(); Collection<AvlReport> avlReportsReadIn = processData(in); in.close(); logger.debug("Time to parse document {} msec", timer.elapsedMsec()); // Process all the reports read in if (shouldProcessAvl.getValue()) processAvlReports(avlReportsReadIn); }
From source file:org.eclipse.kura.core.deployment.download.impl.HttpDownloadCountingOutputStream.java
@Override public void startWork() throws KuraException { this.executor = Executors.newSingleThreadExecutor(); this.future = this.executor.submit(new Callable<Void>() { @Override//from w w w .ja v a 2 s.c o m public Void call() throws Exception { URL localUrl = null; boolean shouldAuthenticate = false; try { shouldAuthenticate = HttpDownloadCountingOutputStream.this.options.getUsername() != null && HttpDownloadCountingOutputStream.this.options.getPassword() != null && !(HttpDownloadCountingOutputStream.this.options.getUsername().trim().isEmpty() && !HttpDownloadCountingOutputStream.this.options.getPassword().trim() .isEmpty()); if (shouldAuthenticate) { Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( HttpDownloadCountingOutputStream.this.options.getUsername(), HttpDownloadCountingOutputStream.this.options.getPassword().toCharArray()); } }); } localUrl = new URL(HttpDownloadCountingOutputStream.this.m_downloadURL); URLConnection urlConnection = localUrl.openConnection(); int connectTimeout = getConnectTimeout(); int readTimeout = getPropReadTimeout(); urlConnection.setConnectTimeout(connectTimeout); urlConnection.setReadTimeout(readTimeout); testConnectionProtocol(urlConnection); HttpDownloadCountingOutputStream.this.is = localUrl.openStream(); String s = urlConnection.getHeaderField("Content-Length"); s_logger.info("Content-lenght: " + s); setTotalBytes(s != null ? Integer.parseInt(s) : -1); postProgressEvent(HttpDownloadCountingOutputStream.this.options.getClientId(), 0, HttpDownloadCountingOutputStream.this.totalBytes, DOWNLOAD_STATUS.IN_PROGRESS, null); int bufferSize = getBufferSize(); if (bufferSize == 0 && getTotalBytes() > 0) { int newSize = Math.round(HttpDownloadCountingOutputStream.this.totalBytes / 100 * 1); bufferSize = newSize; setBufferSize(newSize); } else if (bufferSize == 0) { int newSize = 1024 * 4; bufferSize = newSize; setBufferSize(newSize); } long numBytes = IOUtils.copyLarge(HttpDownloadCountingOutputStream.this.is, HttpDownloadCountingOutputStream.this, new byte[bufferSize]); postProgressEvent(HttpDownloadCountingOutputStream.this.options.getClientId(), numBytes, HttpDownloadCountingOutputStream.this.totalBytes, DOWNLOAD_STATUS.COMPLETED, null); } catch (IOException e) { postProgressEvent(HttpDownloadCountingOutputStream.this.options.getClientId(), getByteCount(), HttpDownloadCountingOutputStream.this.totalBytes, DOWNLOAD_STATUS.FAILED, e.getMessage()); throw new KuraConnectException(e); } finally { if (HttpDownloadCountingOutputStream.this.is != null) { try { HttpDownloadCountingOutputStream.this.is.close(); } catch (IOException e) { } } try { close(); } catch (IOException e) { } localUrl = null; if (shouldAuthenticate) { Authenticator.setDefault(null); } } return null; } }); try { this.future.get(); } catch (ExecutionException ex) { throw new KuraException(KuraErrorCode.INTERNAL_ERROR, ex); } catch (InterruptedException ex) { throw new KuraException(KuraErrorCode.INTERNAL_ERROR, ex); } }
From source file:com.greatmancode.tools.utils.Updater.java
private boolean read() { try {//from www . java 2 s. com 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) { caller.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")) { caller.getLogger() .warning("dev.bukkit.org rejected the API key provided in plugins/Updater/config.yml"); caller.getLogger().warning("Please double-check your configuration to ensure it is correct."); this.result = UpdateResult.FAIL_APIKEY; } else { caller.getLogger().warning("The updater could not contact dev.bukkit.org for updating."); caller.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:org.infoglue.igide.helper.http.HTTPTextDocumentListenerEngine.java
private void listen() { String errorMessage;/* w ww . ja v a2 s . com*/ boolean error; errorMessage = ""; error = false; try { Logger.logConsole("Starting listen thread"); URLConnection urlConn = url.openConnection(); urlConn.setConnectTimeout(3000); urlConn.setRequestProperty("Connection", "Keep-Alive"); urlConn.setReadTimeout(0); urlConn.setDoInput(true); urlConn.setDoOutput(true); urlConn.setUseCaches(false); urlConn.setAllowUserInteraction(false); if (urlConn.getHeaderFields().toString().indexOf("401 Unauthorized") > -1) { Logger.logConsole("User has no access to the CMS - closing connection"); throw new AccessControlException("User has no access to the CMS - closing connection"); } String boundary = urlConn.getHeaderField("boundary"); DataInputStream input = new DataInputStream(urlConn.getInputStream()); StringBuffer buf = new StringBuffer(); if (listener != null) listener.onConnection(url); String str = null; while ((str = input.readLine()) != null) { if (str.indexOf("XMLNotificationWriter.ping") == -1) { if (str.equals(boundary)) { String message = buf.toString(); // By checking there is more in the String than the XML declaration we assume the message is valid if (message != null && !message.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "").equals("")) { if (listener != null) listener.recieveDocument(buf.toString()); else Logger.logConsole((new StringBuilder("NEW DOCUMENT!!\r\n")).append(buf.toString()) .append("\r\n").toString()); } buf = new StringBuffer(); } else { buf.append(str); } } } input.close(); } catch (MalformedURLException me) { error = true; errorMessage = (new StringBuilder("Faulty CMS-url:")).append(url).toString(); if (listener != null) listener.onException(me); else System.err.println((new StringBuilder("MalformedURLException: ")).append(me).toString()); final String errorMessageFinal = errorMessage; Logger.logConsole( (new StringBuilder("The connection was shut. Was it an error:")).append(error).toString()); if (!error) { if (System.currentTimeMillis() - lastRetry > 20000L) { Logger.logConsole("Trying to restart the listener as it was a while since last..."); lastRetry = System.currentTimeMillis(); listen(); } } else { try { if (listener != null) listener.onEndConnection(url); } catch (Exception e) { Logger.logConsole( (new StringBuilder("Error ending connection:")).append(e.getMessage()).toString()); } Display.getDefault().asyncExec(new Runnable() { public void run() { MessageDialog.openError(viewer.getControl().getShell(), "Error", (new StringBuilder()).append(errorMessageFinal).toString()); } }); } } catch (IOException ioe) { error = true; errorMessage = "Got an I/O-Exception talking to the CMS. Check that it is started and in valid state."; Logger.logConsole((new StringBuilder("ioe: ")).append(ioe.getMessage()).toString()); if (listener != null) listener.onException(ioe); else Logger.logConsole((new StringBuilder("TextDocumentListener cannot connect to: ")) .append(url.toExternalForm()).toString()); final String errorMessageFinal = errorMessage; Logger.logConsole( (new StringBuilder("The connection was shut. Was it an error:")).append(error).toString()); if (!error) { if (System.currentTimeMillis() - lastRetry > 20000L) { Logger.logConsole("Trying to restart the listener as it was a while since last..."); lastRetry = System.currentTimeMillis(); listen(); } } else { try { if (listener != null) listener.onEndConnection(url); } catch (Exception e) { Logger.logConsole( (new StringBuilder("Error ending connection:")).append(e.getMessage()).toString()); } Display.getDefault().asyncExec(new Runnable() { public void run() { MessageDialog.openError(viewer.getControl().getShell(), "Error", (new StringBuilder()).append(errorMessageFinal).toString()); } }); } } catch (AccessControlException ace) { error = true; errorMessage = "The user you tried to connect with did not have the correct access rights. Check that he/she has roles etc enough to access the CMS"; Logger.logConsole((new StringBuilder("ioe: ")).append(ace.getMessage()).toString()); if (listener != null) listener.onException(ace); else Logger.logConsole((new StringBuilder()).append(ace.getMessage()).toString()); final String errorMessageFinal = errorMessage; Logger.logConsole( (new StringBuilder("The connection was shut. Was it an error:")).append(error).toString()); if (!error) { if (System.currentTimeMillis() - lastRetry > 20000L) { Logger.logConsole("Trying to restart the listener as it was a while since last..."); lastRetry = System.currentTimeMillis(); listen(); } } else { try { if (listener != null) listener.onEndConnection(url); } catch (Exception e) { Logger.logConsole( (new StringBuilder("Error ending connection:")).append(e.getMessage()).toString()); } Display.getDefault().asyncExec(new Runnable() { public void run() { MessageDialog.openError(viewer.getControl().getShell(), "Error", (new StringBuilder()).append(errorMessageFinal).toString()); } }); } } catch (Exception exception) { final String errorMessageFinal = errorMessage; Logger.logConsole( (new StringBuilder("The connection was shut. Was it an error:")).append(error).toString()); if (!error) { if (System.currentTimeMillis() - lastRetry > 20000L) { Logger.logConsole("Trying to restart the listener as it was a while since last..."); lastRetry = System.currentTimeMillis(); listen(); } } else { try { if (listener != null) listener.onEndConnection(url); } catch (Exception e) { Logger.logConsole( (new StringBuilder("Error ending connection:")).append(e.getMessage()).toString()); } Display.getDefault().asyncExec(new Runnable() { public void run() { MessageDialog.openError(viewer.getControl().getShell(), "Error", (new StringBuilder()).append(errorMessageFinal).toString()); } }); } } catch (Throwable e) { final String errorMessageFinal = errorMessage; Logger.logConsole( (new StringBuilder("The connection was shut. Was it an error:")).append(error).toString()); if (!error) { if (System.currentTimeMillis() - lastRetry > 20000L) { Logger.logConsole("Trying to restart the listener as it was a while since last..."); lastRetry = System.currentTimeMillis(); listen(); } } else { try { if (listener != null) listener.onEndConnection(url); } catch (Exception e2) { Logger.logConsole( (new StringBuilder("Error ending connection:")).append(e2.getMessage()).toString()); } Display.getDefault().asyncExec(new Runnable() { public void run() { MessageDialog.openError(viewer.getControl().getShell(), "Error", (new StringBuilder()).append(errorMessageFinal).toString()); } }); } } }
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 2 s. c om*/ 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) { } } } }