List of usage examples for java.net URLConnection getLastModified
public long getLastModified()
From source file:org.kchine.rpf.PoolUtils.java
public static String cacheJar(URL url, String location, int logInfo, boolean forced) throws Exception { final String jarName = url.toString().substring(url.toString().lastIndexOf("/") + 1); if (!location.endsWith("/") && !location.endsWith("\\")) location += "/"; String fileName = location + jarName; new File(location).mkdirs(); final JTextArea area = ((logInfo & LOG_PRGRESS_TO_DIALOG) != 0) ? new JTextArea() : null; final JProgressBar jpb = ((logInfo & LOG_PRGRESS_TO_DIALOG) != 0) ? new JProgressBar(0, 100) : null; final JFrame f = ((logInfo & LOG_PRGRESS_TO_DIALOG) != 0) ? new JFrame("copying " + jarName + " ...") : null;/*from www .jav a 2 s . c om*/ try { ResponseCache.setDefault(null); URLConnection urlC = null; Exception connectionException = null; for (int i = 0; i < RECONNECTION_RETRIAL_NBR; ++i) { try { urlC = url.openConnection(); connectionException = null; break; } catch (Exception e) { connectionException = e; } } if (connectionException != null) throw connectionException; InputStream is = url.openStream(); File file = new File(fileName); long urlLastModified = urlC.getLastModified(); if (!forced) { boolean somethingToDo = !file.exists() || file.lastModified() < urlLastModified || (file.length() != urlC.getContentLength() && !isValidJar(fileName)); if (!somethingToDo) return fileName; } if ((logInfo & LOG_PRGRESS_TO_DIALOG) != 0) { Runnable runnable = new Runnable() { public void run() { try { f.setUndecorated(true); f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); area.setEditable(false); area.setForeground(Color.white); area.setBackground(new Color(0x00, 0x80, 0x80)); jpb.setIndeterminate(true); jpb.setForeground(Color.white); jpb.setBackground(new Color(0x00, 0x80, 0x80)); JPanel p = new JPanel(new BorderLayout()); p.setBorder(BorderFactory.createLineBorder(Color.black, 3)); p.setBackground(new Color(0x00, 0x80, 0x80)); p.add(jpb, BorderLayout.SOUTH); p.add(area, BorderLayout.CENTER); f.add(p); f.pack(); f.setSize(300, 80); locateInScreenCenter(f); f.setVisible(true); System.out.println("here"); } catch (Exception e) { e.printStackTrace(); } } }; if (SwingUtilities.isEventDispatchThread()) runnable.run(); else { SwingUtilities.invokeLater(runnable); } } if ((logInfo & LOG_PRGRESS_TO_SYSTEM_OUT) != 0) { System.out.println("Downloading " + jarName + ":"); System.out.print("expected:==================================================\ndone :"); } if ((logInfo & LOG_PRGRESS_TO_LOGGER) != 0) { log.info("Downloading " + jarName + ":"); } int jarSize = urlC.getContentLength(); int currentPercentage = 0; FileOutputStream fos = null; fos = new FileOutputStream(fileName); int count = 0; int printcounter = 0; byte data[] = new byte[BUFFER_SIZE]; int co = 0; while ((co = is.read(data, 0, BUFFER_SIZE)) != -1) { fos.write(data, 0, co); count = count + co; int expected = (50 * count / jarSize); while (printcounter < expected) { if ((logInfo & LOG_PRGRESS_TO_SYSTEM_OUT) != 0) { System.out.print("="); } if ((logInfo & LOG_PRGRESS_TO_LOGGER) != 0) { log.info((int) (100 * count / jarSize) + "% done."); } ++printcounter; } if ((logInfo & LOG_PRGRESS_TO_DIALOG) != 0) { final int p = (int) (100 * count / jarSize); if (p > currentPercentage) { currentPercentage = p; final JTextArea fa = area; final JProgressBar fjpb = jpb; SwingUtilities.invokeLater(new Runnable() { public void run() { fjpb.setIndeterminate(false); fjpb.setValue(p); fa.setText("Copying " + jarName + " ..." + "\n" + p + "%" + " Done. "); } }); SwingUtilities.invokeLater(new Runnable() { public void run() { fa.setCaretPosition(fa.getText().length()); fa.repaint(); fjpb.repaint(); } }); } } } /* * while ((oneChar = is.read()) != -1) { fos.write(oneChar); * count++; * * final int p = (int) (100 * count / jarSize); if (p > * currentPercentage) { System.out.print(p+" % "); currentPercentage = * p; if (showProgress) { final JTextArea fa = area; final * JProgressBar fjpb = jpb; SwingUtilities.invokeLater(new * Runnable() { public void run() { fjpb.setIndeterminate(false); * fjpb.setValue(p); fa.setText("\n" + p + "%" + " Done "); } }); * * SwingUtilities.invokeLater(new Runnable() { public void run() { * fa.setCaretPosition(fa.getText().length()); fa.repaint(); * fjpb.repaint(); } }); } else { if (p%2==0) System.out.print("="); } } * } * */ is.close(); fos.close(); } catch (MalformedURLException e) { System.err.println(e.toString()); throw e; } catch (IOException e) { System.err.println(e.toString()); } finally { if ((logInfo & LOG_PRGRESS_TO_DIALOG) != 0) { f.dispose(); } if ((logInfo & LOG_PRGRESS_TO_SYSTEM_OUT) != 0) { System.out.println("\n 100% of " + jarName + " has been downloaded \n"); } if ((logInfo & LOG_PRGRESS_TO_LOGGER) != 0) { log.info(" 100% of " + jarName + " has been downloaded"); } } return fileName; }
From source file:com.apache.ivy.BasicURLHandler.java
public void download(URL src, File dest, CopyProgressListener l) throws IOException { // Install the IvyAuthenticator if ("http".equals(src.getProtocol()) || "https".equals(src.getProtocol())) { IvyAuthenticator.install();// w w w .j a v a 2s. co m } URLConnection srcConn = null; try { src = normalizeToURL(src); srcConn = src.openConnection(); srcConn.setRequestProperty("User-Agent", "Apache Ivy/1.0");// + Ivy.getIvyVersion()); srcConn.setRequestProperty("Accept-Encoding", "gzip,deflate"); if (srcConn instanceof HttpURLConnection) { HttpURLConnection httpCon = (HttpURLConnection) srcConn; if (!checkStatusCode(src, httpCon)) { throw new IOException("The HTTP response code for " + src + " did not indicate a success." + " See log for more detail."); } } // do the download InputStream inStream = getDecodingInputStream(srcConn.getContentEncoding(), srcConn.getInputStream()); FileUtil.copy(inStream, dest, l); // check content length only if content was not encoded if (srcConn.getContentEncoding() == null) { int contentLength = srcConn.getContentLength(); if (contentLength != -1 && dest.length() != contentLength) { dest.delete(); throw new IOException("Downloaded file size doesn't match expected Content Length for " + src + ". Please retry."); } } // update modification date long lastModified = srcConn.getLastModified(); if (lastModified > 0) { dest.setLastModified(lastModified); } } finally { disconnect(srcConn); } }
From source file:org.adaway.service.ApplyService.java
/** * Downloads files from hosts sources//from w w w . j a v a2 s . c o m * * @return return code */ private int download() { Cursor enabledHostsSourcesCursor; byte data[]; int count; long currentLastModifiedOnline; int returnCode = StatusCodes.SUCCESS; // default return code if (Utils.isAndroidOnline(mService)) { showApplyNotification(mService, mService.getString(R.string.download_dialog), mService.getString(R.string.download_dialog), mService.getString(R.string.download_dialog)); // output to write into FileOutputStream out = null; try { out = mService.openFileOutput(Constants.DOWNLOADED_HOSTS_FILENAME, Context.MODE_PRIVATE); mNumberOfFailedDownloads = 0; mNumberOfDownloads = 0; // get cursor over all enabled hosts source enabledHostsSourcesCursor = ProviderHelper.getEnabledHostsSourcesCursor(mService); // iterate over all hosts sources in db with cursor if (enabledHostsSourcesCursor.moveToFirst()) { do { mNumberOfDownloads++; InputStream is = null; BufferedInputStream bis = null; String currentUrl = enabledHostsSourcesCursor .getString(enabledHostsSourcesCursor.getColumnIndex("url")); try { Log.v(Constants.TAG, "Downloading hosts file: " + currentUrl); /* change URL in download dialog */ updateApplyNotification(mService, mService.getString(R.string.download_dialog), currentUrl); /* build connection */ URL mURL = new URL(currentUrl); URLConnection connection = mURL.openConnection(); connection.setConnectTimeout(15000); connection.setReadTimeout(30000); /* connect */ connection.connect(); is = connection.getInputStream(); bis = new BufferedInputStream(is); if (is == null) { Log.e(Constants.TAG, "Stream is null"); } /* download with progress */ data = new byte[1024]; count = 0; // run while only when thread is not cancelled while ((count = bis.read(data)) != -1) { out.write(data, 0, count); } // add line seperator to add files together in one file out.write(Constants.LINE_SEPERATOR.getBytes()); // save last modified online for later use currentLastModifiedOnline = connection.getLastModified(); ProviderHelper.updateHostsSourceLastModifiedOnline(mService, enabledHostsSourcesCursor .getInt(enabledHostsSourcesCursor.getColumnIndex(HostsSources._ID)), currentLastModifiedOnline); } catch (IOException e) { Log.e(Constants.TAG, "Exception while downloading from " + currentUrl, e); mNumberOfFailedDownloads++; // set last_modified_online of failed download to 0 (not available) ProviderHelper.updateHostsSourceLastModifiedOnline(mService, enabledHostsSourcesCursor .getInt(enabledHostsSourcesCursor.getColumnIndex(HostsSources._ID)), 0); } finally { // flush and close streams try { if (out != null) { out.flush(); } if (bis != null) { bis.close(); } if (is != null) { is.close(); } } catch (Exception e) { Log.e(Constants.TAG, "Exception on flush and closing streams.", e); } } } while (enabledHostsSourcesCursor.moveToNext()); } // close cursor in the end if (enabledHostsSourcesCursor != null && !enabledHostsSourcesCursor.isClosed()) { enabledHostsSourcesCursor.close(); } // if all downloads failed return download_fail error if (mNumberOfDownloads == mNumberOfFailedDownloads && mNumberOfDownloads != 0) { returnCode = StatusCodes.DOWNLOAD_FAIL; } } catch (Exception e) { Log.e(Constants.TAG, "Private File can not be created, Exception: " + e); returnCode = StatusCodes.PRIVATE_FILE_FAIL; } finally { try { if (out != null) { out.close(); } } catch (Exception e) { Log.e(Constants.TAG, "Exception on close of out.", e); } } } else { returnCode = StatusCodes.NO_CONNECTION; } return returnCode; }
From source file:com.blackducksoftware.integration.hub.cli.CLIDownloadService.java
public void customInstall(HubProxyInfo hubProxyInfo, CLILocation cliLocation, CIEnvironmentVariables ciEnvironmentVariables, final URL archive, String hubVersion, final String localHostName) throws IOException, InterruptedException, HubIntegrationException, IllegalArgumentException, EncryptionException { boolean cliMismatch = true; try {//from w w w. j a va 2 s.c o m final File hubVersionFile = cliLocation.createHubVersionFile(); if (hubVersionFile.exists()) { final String storedHubVersion = IOUtils.toString(new FileReader(hubVersionFile)); if (hubVersion.equals(storedHubVersion)) { cliMismatch = false; } else { hubVersionFile.delete(); hubVersionFile.createNewFile(); } } final File cliInstallDirectory = cliLocation.getCLIInstallDir(); if (!cliInstallDirectory.exists()) { cliMismatch = true; } if (cliMismatch) { logger.debug("Attempting to download the Hub CLI."); final FileWriter writer = new FileWriter(hubVersionFile); writer.write(hubVersion); writer.close(); hubVersionFile.setLastModified(0L); } final long cliTimestamp = hubVersionFile.lastModified(); URLConnection connection = null; try { Proxy proxy = null; if (hubProxyInfo != null) { String proxyHost = hubProxyInfo.getHost(); int proxyPort = hubProxyInfo.getPort(); String proxyUsername = hubProxyInfo.getUsername(); String proxyPassword = hubProxyInfo.getDecryptedPassword(); if (StringUtils.isNotBlank(proxyHost) && proxyPort > 0) { proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); } if (proxy != null) { if (StringUtils.isNotBlank(proxyUsername) && StringUtils.isNotBlank(proxyPassword)) { AuthenticatorUtil.setAuthenticator(proxyUsername, proxyPassword); } else { AuthenticatorUtil.resetAuthenticator(); } } } if (proxy != null) { connection = archive.openConnection(proxy); } else { connection = archive.openConnection(); } connection.setIfModifiedSince(cliTimestamp); connection.connect(); } catch (final IOException ioe) { logger.error("Skipping installation of " + archive + " to " + cliLocation.getCanonicalPath() + ": " + ioe.toString()); return; } if (connection instanceof HttpURLConnection && ((HttpURLConnection) connection).getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { // CLI has not been modified return; } final long sourceTimestamp = connection.getLastModified(); if (cliInstallDirectory.exists() && cliInstallDirectory.listFiles().length > 0) { if (!cliMismatch && sourceTimestamp == cliTimestamp) { logger.debug("The current Hub CLI is up to date."); return; } for (final File file : cliInstallDirectory.listFiles()) { FileUtils.deleteDirectory(file); } } else { cliInstallDirectory.mkdir(); } logger.debug("Updating the Hub CLI."); hubVersionFile.setLastModified(sourceTimestamp); logger.info("Unpacking " + archive.toString() + " to " + cliInstallDirectory.getCanonicalPath() + " on " + localHostName); final CountingInputStream cis = new CountingInputStream(connection.getInputStream()); try { unzip(cliInstallDirectory, cis, logger); updateJreSecurity(logger, cliLocation, ciEnvironmentVariables); } catch (final IOException e) { throw new IOException(String.format("Failed to unpack %s (%d bytes read of total %d)", archive, cis.getByteCount(), connection.getContentLength()), e); } } catch (final IOException e) { throw new IOException("Failed to install " + archive + " to " + cliLocation.getCanonicalPath(), e); } }
From source file:gr.iti.mklab.bubing.parser.ITIHTMLParser.java
public void processImageURL(URI pageUri, URI base, String imageUri, String altText) throws MalformedURLException, IOException { URI url = BURL.parse(imageUri); if (url != null) { URI resolved = base.resolve(url); String resolvedStr = resolved.toString(); //avoid trying to index the same image multiple times if (!ItiAgent.UNIQUE_IMAGE_URLS.mightContain(resolvedStr)) { // Put it in the bloom filter even if it is not saved eventually // to avoid doing the same checks for the same image a second time ItiAgent.UNIQUE_IMAGE_URLS.put(resolvedStr); final URLConnection con = resolved.toURL().openConnection(); if (Utils.checkContentHeaders(con.getContentLength(), con.getContentType())) { InputStream is = con.getInputStream(); BufferedImage image = null; try { image = ImageIO.read(is); } catch (IllegalArgumentException e) { // this exception is probably thrown because of a greyscale jpeg image System.out.println("Exception: " + e.getMessage() + " | Image: " + imageUri); image = ImageIOGreyScale.read(is); // retry with the modified class } catch (MalformedURLException e) { System.out.println("Malformed url exception. Url: " + imageUri); }//from w ww . j a v a 2 s . com if (Utils.checkImage(image)) { Image item = new Image(); item.setUrl(resolvedStr); item.setTitle(altText); item.setWidth(image.getWidth()); item.setHeight(image.getHeight()); item.setWebPageUrl(pageUri.toString()); item.setLastModifiedDate(new Date(con.getLastModified())); item.setObjectId(new ObjectId()); try { VisualIndexer.getInstance().indexAndStore(image, item); } catch (Exception e) { System.out.println("HTMLImageParser parse exeption: " + e); } } } } } }
From source file:org.jab.docsearch.spider.LinkFinder.java
/** * Get all links from page/*from w w w. j a v a2 s . c o m*/ */ public void getAllLinks() { // writes links from a page out to a file String urlStr = pageName; String shortUrl = ""; numUnChanged = 0; numSkips = 0; int numSuccesses = 0; int numFailed = 0; int numNoRobots = 0; addLink(urlStr); domainUrl = Utils.getDomainURL(urlStr); if (logger.isDebugEnabled()) { logger.debug("getAllLinks() domain url='" + domainUrl + "'"); } SpiderUrl curl = new SpiderUrl(urlStr); baseUrlFolder = Utils.getBaseURLFolder(urlStr); int curLinkNo = 0; boolean completedSpider = false; boolean isDead = false; int curPread = 0; if (ds != null) { ds.setIsWorking(true); ds.setProgressMax(maxLinksToFind); ds.setCurProgressMSG("Spidering Files..."); } int numSpidered = 0; int curSuccessNo = 0; // start spider while (curLinkNo != -1) { BufferedInputStream urlStream = null; FileOutputStream fileOutStream = null; try { completedSpider = false; isDead = false; if (ds != null) { ds.setCurProgress(curPread); if (!ds.getIsWorking()) { break; } } curLinkNo = getNextUrlNo(); if (curLinkNo == -1) { logger.debug("getAllLinks() end of links reached."); break; } else { urlStr = getLinkNameByNo(curLinkNo); logger.info("getAllLinks() analyzing page='" + urlStr + "'"); curl = getSpiderUrl(curLinkNo); } shortUrl = Utils.concatEnd(urlStr, 33); setStatus(I18n.getString("connecting_to") + " " + shortUrl); // open url URL url = new URL(urlStr); URLConnection conn = url.openConnection(); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestProperty("User-Agent", "DocSearcher " + I18n.getString("ds.version")); conn.connect(); urlStream = new BufferedInputStream(conn.getInputStream()); // filesize int fileSize = conn.getContentLength(); if (fileSize > maxFileSizeToGet) { String ex = I18n.getString("skipping_file_too_big") + " (" + fileSize + " > " + maxFileSizeToGet + ") " + shortUrl; setStatus(ex); throw new Exception(ex); } setStatus(I18n.getString("downloading_uc") + "... " + shortUrl + " " + fileSize + " " + I18n.getString("bytes")); curl.setSize(fileSize); // last modified long curModified = conn.getLastModified(); // was .getDate(); curl.setLastModified(curModified); // content type String curContentType = netUtils.getContentType(conn); curl.setContentType(curContentType); // build the value for downloadFile String dnldTmpName = getDownloadFileName(curl.getContentType(), urlStr.toLowerCase()); String downloadFile = FileUtils.addFolder(downloadFileDir, dnldTmpName); // TODO it is better to use content type! boolean curIsWebPage = isHtml(urlStr.toLowerCase()) || (curContentType.toLowerCase().indexOf("html") != -1); logger.debug("getAllLinks() saving to " + downloadFile); fileOutStream = new FileOutputStream(downloadFile); int curSize = 0; int curI; int lastPercent = 0; StringBuilder tag = new StringBuilder(); String link = null; boolean inTag = false; boolean getFileSizeFromStream = false; if (fileSize == -1) { getFileSizeFromStream = true; } while ((curI = urlStream.read()) != -1) { fileOutStream.write(curI); curSize++; if (ds != null) { if (!ds.getIsWorking()) { break; } } // fix problem if filesize not in content length if (getFileSizeFromStream) { fileSize = curSize + urlStream.available(); } // notify of download progress if (curSize > 0 && (curSize % 10) == 0) { int curPercent = (curSize * 100) / fileSize; if (curPercent != lastPercent) { lastPercent = curPercent; setStatus(I18n.getString("downloading_uc") + "... : (" + shortUrl + ") --> " + curPercent + " %" + " ( " + (numSuccesses + numFailed + numNoRobots) + "/" + getNumLinksFound() + ")"); } } // end for percent updates else if (curSize % 40 == 0) { setStatus(I18n.getString("downloading_uc") + "... : (" + shortUrl + ") --> " + curSize + " " + I18n.getString("bytes")); } // handle links if (curIsWebPage) { char c = (char) curI; // LOOK AT THE TAGS // start tag if (c == '<') { inTag = true; tag = new StringBuilder(); } // end tag else if (c == '>') { inTag = false; tag.append(c); String realTag = tag.toString(); String lowerTag = realTag.toLowerCase(); // TODO fix problem with spaces before = // link if (lowerTag.startsWith("<a ")) { link = Utils.getTagString("href=", realTag); link = Utils.getNormalUrl(link); doPossibleAdd(urlStr, link); } // area else if (lowerTag.startsWith("<area")) { link = Utils.getTagString("href=", realTag); link = Utils.getNormalUrl(link); doPossibleAdd(urlStr, link); } // TODO is in param realy a link? else if (lowerTag.startsWith("<param")) { String appletParam = Utils.getTagString("name=", realTag); if (appletParam.toLowerCase().equals("url")) { link = Utils.getTagString("value=", realTag); link = Utils.getNormalUrl(link); doPossibleAdd(urlStr, link); } } } // in tag if (inTag) { tag.append(c); } } // filesize ok if (getFileSizeFromStream && fileSize > maxFileSizeToGet) { break; } } // end while downloading curPread++; fileOutStream.close(); urlStream.close(); curl.setMd5(FileUtils.getMD5Sum(downloadFile)); // now add out document if (ds != null) { curSuccessNo = ds.idx.addDocToIndex(downloadFile, iw, dsi, false, curl); switch (curSuccessNo) { case 0: // good numSuccesses++; break; case 1: // bad numFailed++; break; case 2: // meta robots - no index numNoRobots++; break; } } // delete temp file if (!FileUtils.deleteFile(downloadFile)) { logger.warn("getAllLinks() can't delete file '" + downloadFile + "'"); } numSpidered++; completedSpider = true; // max links found if (numSpidered > maxLinksToFind) { break; } } catch (Exception e) { logger.fatal("getAllLinks() failed", e); setStatus(I18n.getString("error") + " : " + e.toString()); isDead = true; } finally { // close resources IOUtils.closeQuietly(urlStream); IOUtils.closeQuietly(fileOutStream); curl.setSpidered(completedSpider); curl.setIsDeadLink(isDead); setStatus(I18n.getString("download_complete") + " " + shortUrl); } } // end for iterating over links if (ds != null) { ds.resetProgress(); } saveAllLinks(); logger.info("getAllLinks() " + numSpidered + " total web pages spidered for links."); showMessage(I18n.getString("spidering_complete") + " (" + Utils.concatStrToEnd(pageName, 28) + ") ", numSpidered + " " + I18n.getString("documents_indexed") + " " + getNumLinksFound() + " " + I18n.getString("links_found") + "\n\n" + numSuccesses + " " + I18n.getString("documents_spidered_successful") + "\n\n" + numFailed + " " + I18n.getString("documents_spidered_failed") + "\n\n" + numNoRobots + " " + I18n.getString("documents_not_spidered")); }
From source file:hudson.FilePath.java
/** * Given a tgz/zip file, extracts it to the given target directory, if necessary. * * <p>// ww w . j a va 2 s . c o m * This method is a convenience method designed for installing a binary package to a location * that supports upgrade and downgrade. Specifically, * * <ul> * <li>If the target directory doesn't exist {@linkplain #mkdirs() it'll be created}. * <li>The timestamp of the .tgz file is left in the installation directory upon extraction. * <li>If the timestamp left in the directory doesn't match with the timestamp of the current archive file, * the directory contents will be discarded and the archive file will be re-extracted. * <li>If the connection is refused but the target directory already exists, it is left alone. * </ul> * * @param archive * The resource that represents the tgz/zip file. This URL must support the "Last-Modified" header. * (Most common usage is to get this from {@link ClassLoader#getResource(String)}) * @param listener * If non-null, a message will be printed to this listener once this method decides to * extract an archive. * @return * true if the archive was extracted. false if the extraction was skipped because the target directory * was considered up to date. * @since 1.299 */ public boolean installIfNecessaryFrom(URL archive, TaskListener listener, String message) throws IOException, InterruptedException { URLConnection con; try { con = archive.openConnection(); con.connect(); } catch (IOException x) { if (this.exists()) { // Cannot connect now, so assume whatever was last unpacked is still OK. if (listener != null) { listener.getLogger() .println("Skipping installation of " + archive + " to " + remote + ": " + x); } return false; } else { throw x; } } long sourceTimestamp = con.getLastModified(); FilePath timestamp = this.child(".timestamp"); if (this.exists()) { if (timestamp.exists() && sourceTimestamp == timestamp.lastModified()) return false; // already up to date this.deleteContents(); } if (listener != null) listener.getLogger().println(message); CountingInputStream cis = new CountingInputStream(con.getInputStream()); try { if (archive.toExternalForm().endsWith(".zip")) unzipFrom(cis); else untarFrom(cis, GZIP); } catch (IOException e) { throw new IOException2(String.format("Failed to unpack %s (%d bytes read of total %d)", archive, cis.getByteCount(), con.getContentLength()), e); } timestamp.touch(sourceTimestamp); return true; }
From source file:com.icesoft.faces.application.D2DViewHandler.java
protected void renderResponse(FacesContext facesContext) throws IOException { BridgeFacesContext context = (BridgeFacesContext) facesContext; UIViewRoot root = context.getViewRoot(); String viewId = root.getViewId(); if (log.isTraceEnabled()) { log.trace("Rendering " + root + " with " + root.getChildCount() + " children"); }// w ww.j av a 2 s .c o m ResponseWriter responseWriter = context.createAndSetResponseWriter(); boolean reloadView = false; URLConnection viewConnection = null; URL viewURL = null; if ((root.getChildCount() == 0) || (reloadInterval > -1)) { // We have not parsed the page yet; // Need an input stream for the page; if (viewId.startsWith("/faces")) { viewId = viewId.substring(6); } if (viewId.endsWith(".jpg") || viewId.endsWith(".gif") || viewId.endsWith(".png")) { context.getExternalContext().dispatch(viewId); return; } try { viewURL = context.getExternalContext().getResource(viewId); if (null == viewURL) { if (viewId.endsWith(".faces")) { viewId = truncate(".faces", viewId); } else if (viewId.endsWith(".jsf")) { viewId = truncate(".jsf", viewId); } else if (viewId.endsWith(".iface")) { viewId = truncate(".iface", viewId); } else if (viewId.endsWith(".jsp")) { //MyFaces thinks everything is a .jsp viewId = truncate(".jsp", viewId); } viewId = viewId + ".jspx"; viewURL = context.getExternalContext().getResource(viewId); } if (null == viewURL) { if (viewId.endsWith(".jspx")) { viewId = truncate(".jspx", viewId) + ".jsp"; } viewURL = context.getExternalContext().getResource(viewId); } root.setViewId(viewId); long currentTime = System.currentTimeMillis(); long lastLoaded = getTimeAttribute(root, LAST_LOADED_KEY); long lastChecked = getTimeAttribute(root, LAST_CHECKED_KEY); long lastModified = 0; //newly instantiated viewRoot will have lastChecked of 0 //and lastLoaded of 0 if (currentTime > lastChecked + reloadInterval) { viewConnection = viewURL.openConnection(); lastModified = viewConnection.getLastModified(); root.getAttributes().put(LAST_CHECKED_KEY, new Long(currentTime)); if (lastModified > lastLoaded) { reloadView = true; if (log.isDebugEnabled()) { log.debug("View is modified, reloading " + String.valueOf(viewURL)); } } } } catch (Exception e) { throw new FacesException("Can't find stream for " + viewId, e); } } if (reloadView) { Reader viewInput = null; try { viewInput = new InputStreamReader(viewConnection.getInputStream(), CHAR_ENCODING); if (viewId.endsWith(".jsp")) { if (log.isDebugEnabled()) { log.debug("JspPageToDocument transforming JSP page " + String.valueOf(viewURL)); } viewInput = JspPageToDocument.transform(viewInput); } else if (viewId.endsWith(".jspx")) { if (log.isDebugEnabled()) { log.debug("JspPageToDocument preprocessing JSP doc " + String.valueOf(viewURL)); } viewInput = JspPageToDocument.preprocessJspDocument(viewInput); } } catch (Throwable e) { throw new FacesException("Can't read stream for " + viewId, e); } // Parse the page; try { //TODO: pass viewInput as an InputStream in order to give to the XML parser a chance to //TODO: read the encoding type declared in the xml processing instruction (<?xml version="1.0" charset="..."?>) if (log.isDebugEnabled()) { log.debug("Parsing " + String.valueOf(viewURL)); } parser.parse(viewInput, context); root.getAttributes().put(LAST_LOADED_KEY, new Long(System.currentTimeMillis())); } catch (Throwable e) { throw new FacesException("Can't parse stream for " + viewId + " " + e.getMessage(), e); } if (ImplementationUtil.isJSF12()) { if (log.isDebugEnabled()) { log.debug("Rendering outside ViewTag for JSF 1.2"); } //JSF 1.2 ViewTag does not invoke rendering responseWriter.startDocument(); renderResponse(context, root); // make state saving changes to DOM before ending document invokeStateSaving(context); responseWriter.endDocument(); tracePrintComponentTree(context); } } else { responseWriter.startDocument(); renderResponse(context, root); // make state saving changes to DOM before ending document invokeStateSaving(context); responseWriter.endDocument(); tracePrintComponentTree(context); } }
From source file:JNLPAppletLauncher.java
private void validateCache(URLConnection conn, File nativeFile, File indexFile) throws IOException { // Lock the cache directory final String lckFileName = "cache.lck"; File lckFile = new File(cacheDir, lckFileName); lckFile.createNewFile();// ww w. ja v a 2s .co m final FileOutputStream lckOut = new FileOutputStream(lckFile); final FileChannel lckChannel = lckOut.getChannel(); final FileLock lckLock = lckChannel.lock(); try { // Check to see whether the cached jar file exists and is valid boolean valid = false; long cachedTimeStamp = readTimeStamp(indexFile); long urlTimeStamp = conn.getLastModified(); if (nativeFile.exists() && urlTimeStamp > 0 && urlTimeStamp == readTimeStamp(indexFile)) { valid = true; } // Validate the cache, download the jar if needed if (!valid) { if (VERBOSE) { System.err.println("processNativeJar: downloading " + nativeFile.getAbsolutePath()); } indexFile.delete(); nativeFile.delete(); // Copy from URL to File int len = conn.getContentLength(); if (VERBOSE) { System.err.println("Content length = " + len + " bytes"); } int totalNumBytes = copyURLToFile(conn, nativeFile); if (DEBUG) { System.err.println("processNativeJar: " + conn.getURL().toString() + " --> " + nativeFile.getAbsolutePath() + " : " + totalNumBytes + " bytes written"); } // Write timestamp to index file. writeTimeStamp(indexFile, urlTimeStamp); } else { if (DEBUG) { System.err .println("processNativeJar: using previously cached: " + nativeFile.getAbsolutePath()); } } } finally { // Unlock the cache directory lckLock.release(); } }
From source file:focusedCrawler.util.parser.PaginaURL.java
public PaginaURL(URL url, URLConnection conexao, int max, StopList sl) throws IOException { this(url, conexao.getDate(), conexao.getLastModified(), conexao.getContentLength(), conexao.getInputStream(), max, sl); // System.out.println("CONEXAO: RESPONSE CODE = " + ((HttpURLConnection) conexao).getResponseCode()); URL url_final = ((HttpURLConnection) conexao).getURL(); // System.out.println("CONEXAO: GET URL = " + url_final); // if (!url_final.equals(url)) { // System.out.println("A URL '" + url + "' foi redirecionada para '" + url_final + "'"); // } else { // System.out.println("URL OK"); // }/*from ww w. ja va2s . c o m*/ ((HttpURLConnection) conexao).disconnect(); }