List of usage examples for java.net URLConnection getContentLength
public int getContentLength()
From source file:org.jab.docsearch.spider.LinkFinder.java
/** * Get all links from page/* w w w . ja v a 2s . com*/ */ 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:org.opencms.main.CmsStaticResourceHandler.java
/** * Writes the contents of the given resourceUrl in the response. Can be * overridden to add/modify response headers and similar.<p> * * @param request the request for the resource * @param response the response/*from w w w . j a v a 2 s .c o m*/ * @param resourceUrl the url to send * * @throws IOException in case writing the response fails */ protected void writeStaticResourceResponse(HttpServletRequest request, HttpServletResponse response, URL resourceUrl) throws IOException { URLConnection connection = null; InputStream is = null; String urlStr = resourceUrl.toExternalForm(); try { if (allowServePrecompressedResource(request, urlStr)) { // try to serve a precompressed version if available try { connection = new URL(urlStr + ".gz").openConnection(); is = connection.getInputStream(); // set gzip headers response.setHeader("Content-Encoding", "gzip"); } catch (Exception e) { LOG.debug("Unexpected exception looking for gzipped version of resource " + urlStr, e); } } if (is == null) { // precompressed resource not available, get non compressed connection = resourceUrl.openConnection(); try { is = connection.getInputStream(); } catch (FileNotFoundException e) { LOG.debug(e.getMessage(), e); response.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } } try { @SuppressWarnings("null") int length = connection.getContentLength(); if (length >= 0) { response.setContentLength(length); } } catch (Throwable e) { LOG.debug(e.getMessage(), e); // This can be ignored, content length header is not required. // Need to close the input stream because of // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4257700 to // prevent it from hanging, but that is done below. } streamContent(response, is); } finally { if (is != null) { is.close(); } } }
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 w ww . j av a 2 s. c o m*/ 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:gov.nih.nci.ncicb.tcga.dcc.common.web.StaticContentServlet.java
private void prepareResponse(HttpServletResponse response, URL[] resources, String rawResourcePath) throws IOException { long lastModified = -1; int contentLength = 0; String mimeType = null;//from w w w . j a va 2 s . co m for (int i = 0; i < resources.length; i++) { final URLConnection resourceConn = resources[i].openConnection(); //Setting the last modified http for the requested resource if (resourceConn.getLastModified() > lastModified) { lastModified = resourceConn.getLastModified(); } //Setting the mime type of the resource requested String currentMimeType = getServletContext().getMimeType(resources[i].getPath()); if (log.isDebugEnabled()) { log.debug("Current MimeType: " + currentMimeType); } if (currentMimeType == null) { int index = resources[i].getPath().lastIndexOf('.'); if (index > 0) { String extension = resources[i].getPath().substring(resources[i].getPath().lastIndexOf('.')); currentMimeType = (String) defaultMimeTypes.get(extension); } else { currentMimeType = "text/html"; //html will be the default mime. } } if (mimeType == null) { mimeType = currentMimeType; } else if (!mimeType.equals(currentMimeType)) { //This does not apply to us yet since we don't use combined resource url but maybe in the future ... throw new MalformedURLException("Combined resource path: " + rawResourcePath + " is invalid. All resources in a combined resource path must be of the same mime type."); } contentLength += resourceConn.getContentLength(); } response.setContentType(mimeType); response.setHeader(HTTP_CONTENT_LENGTH_HEADER, Long.toString(contentLength)); response.setDateHeader(HTTP_LAST_MODIFIED_HEADER, lastModified); if (cacheTimeout > 0) { configureCaching(response, cacheTimeout); } }
From source file:com.adito.extensions.store.ExtensionStore.java
/** * Install an extension directly from the Adito Extension Store given its id. * This method will also attempt to install all of the bundles dependencies. * // w w w.ja va2 s. co m * @param id extension bundle ID * @param version * @param request request object * @throws ExtensionException extension errors * @throws IOException io errors */ public void installExtensionFromStore(String id, String version, HttpServletRequest request) throws IOException, ExtensionException { URLConnection connection = downloadExtension(id, version); InputStream inputStream = connection.getInputStream(); installExtensionFromStore(id, inputStream, request, connection.getContentLength()); }
From source file:org.ramadda.util.Utils.java
/** * _more_/*from w w w. j a v a 2s . co m*/ * * @param from _more_ * @param file _more_ * * @return _more_ * * @throws IOException _more_ */ public static long writeTo(URL from, File file) throws IOException { URLConnection connection = from.openConnection(); InputStream is = IOUtil.getInputStream(from.toString(), Utils.class); int length = connection.getContentLength(); long numBytes = -1; FileOutputStream fos = new FileOutputStream(file); try { long result = IOUtil.writeTo(is, fos); numBytes = result; } finally { IOUtil.close(fos); IOUtil.close(is); if (numBytes <= 0) { try { file.delete(); } catch (Exception exc) { } } } return numBytes; }
From source file:org.xchain.framework.filter.UrlTranslationFilter.java
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // Perform Url Translation if enabled. if (this.enabled) { HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse = (HttpServletResponse) response; URL url = null;//w w w . ja v a 2 s . c o m URLConnection connection = null; InputStream in = null; OutputStream out = null; String path = httpRequest.getServletPath(); boolean matchFound = false; try { // Check the incoming path against the registered patterns. for (Pattern pattern : translationMap.keySet()) { Matcher match = pattern.matcher(path); if (match.matches()) { // Match found. Get the URI the pattern translates to. String uri = translationMap.get(pattern); // Perform group substitution. for (int group = 1; group <= match.groupCount(); group++) { uri = uri.replaceAll("\\$[{]" + group + "[}]", match.group(group)); } // Create a new URL from the translated uri. url = UrlFactory.getInstance().newUrl(uri); matchFound = true; break; } } if (!matchFound) { // No match found. Let the request go through. chain.doFilter(request, response); return; } if (log.isDebugEnabled()) { log.debug("doFilter: redirecting " + path + " to " + url); } // get a connection to the url. connection = url.openConnection(); // set the headers. httpResponse.setContentLength(connection.getContentLength()); // try to set the content type header defined in the container. String servletContentType = servletContext.getMimeType(path); if (servletContentType != null) { httpResponse.setContentType(servletContentType); } // get the streams. in = connection.getInputStream(); out = response.getOutputStream(); // create buffer and length for coping. byte[] buffer = new byte[1024]; int length = 0; // transfer the bytes. while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } } catch (MalformedURLException mue) { throw new ServletException(mue); } catch (UnknownServiceException use) { throw new ServletException("The protocol '" + url.getProtocol() + "' does not support input.", use); } finally { // close the streams. if (in != null) { try { in.close(); } catch (IOException ioe) { } } if (out != null) { try { out.close(); } catch (IOException ioe) { } } } } else chain.doFilter(request, response); }
From source file:org.matrix.androidsdk.db.MXMediaDownloadWorkerTask.java
@Override protected Void doInBackground(Integer... params) { try {/* w ww . ja v a 2 s . com*/ URL url = new URL(mUrl); Log.d(LOG_TAG, "MXMediaDownloadWorkerTask " + this + " starts"); mDownloadStats = new IMXMediaDownloadListener.DownloadStats(); // don't known yet mDownloadStats.mEstimatedRemainingTime = -1; InputStream stream = null; int filelen = -1; URLConnection connection = null; try { connection = url.openConnection(); if (mHsConfig != null && connection instanceof HttpsURLConnection) { // Add SSL Socket factory. HttpsURLConnection sslConn = (HttpsURLConnection) connection; try { Pair<SSLSocketFactory, X509TrustManager> pair = CertUtil .newPinnedSSLSocketFactory(mHsConfig); sslConn.setSSLSocketFactory(pair.first); sslConn.setHostnameVerifier(CertUtil.newHostnameVerifier(mHsConfig)); } catch (Exception e) { Log.e(LOG_TAG, "doInBackground SSL exception " + e.getLocalizedMessage()); } } // add a timeout to avoid infinite loading display. connection.setReadTimeout(DOWNLOAD_TIME_OUT); filelen = connection.getContentLength(); stream = connection.getInputStream(); } catch (Exception e) { Log.e(LOG_TAG, "bitmapForURL : fail to open the connection " + e.getMessage()); InputStream errorStream = ((HttpsURLConnection) connection).getErrorStream(); if (null != errorStream) { try { BufferedReader streamReader = new BufferedReader( new InputStreamReader(errorStream, "UTF-8")); StringBuilder responseStrBuilder = new StringBuilder(); String inputStr; while ((inputStr = streamReader.readLine()) != null) { responseStrBuilder.append(inputStr); } mErrorAsJsonElement = new JsonParser().parse(responseStrBuilder.toString()); } catch (Exception ee) { Log.e(LOG_TAG, "bitmapForURL : Error parsing error " + ee.getLocalizedMessage()); } } // privacy //Log.d(LOG_TAG, "MediaWorkerTask " + mUrl + " does not exist"); Log.d(LOG_TAG, "MediaWorkerTask an url does not exist"); // if some medias are not found // do not try to reload them until the next application launch. synchronized (mUnreachableUrls) { mUnreachableUrls.add(mUrl); } } dispatchDownloadStart(); // test if the download has not been cancelled if (!isDownloadCancelled() && (null == mErrorAsJsonElement)) { final long startDownloadTime = System.currentTimeMillis(); String filename = MXMediaDownloadWorkerTask.buildFileName(mUrl, mMimeType) + ".tmp"; FileOutputStream fos = new FileOutputStream(new File(mDirectoryFile, filename)); mDownloadStats.mDownloadId = mUrl; mDownloadStats.mProgress = 0; mDownloadStats.mDownloadedSize = 0; mDownloadStats.mFileSize = filelen; mDownloadStats.mElapsedTime = 0; mDownloadStats.mEstimatedRemainingTime = -1; mDownloadStats.mBitRate = 0; final android.os.Handler uiHandler = new android.os.Handler(Looper.getMainLooper()); final Timer refreshTimer = new Timer(); uiHandler.post(new Runnable() { @Override public void run() { refreshTimer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { uiHandler.post(new Runnable() { @Override public void run() { if (!mIsDone) { publishProgress(startDownloadTime); } } }); } }, new java.util.Date(), 100); } }); try { byte[] buf = new byte[DOWNLOAD_BUFFER_READ_SIZE]; int len; while (!isDownloadCancelled() && (len = stream.read(buf)) != -1) { fos.write(buf, 0, len); mDownloadStats.mDownloadedSize += len; } if (!isDownloadCancelled()) { mDownloadStats.mProgress = 100; } } catch (OutOfMemoryError outOfMemoryError) { Log.e(LOG_TAG, "doInBackground: out of memory"); } catch (Exception e) { Log.e(LOG_TAG, "doInBackground fail to read image " + e.getMessage()); } mIsDone = true; close(stream); fos.flush(); fos.close(); if (null != mEncryptedFileInfo) { File file = new File(mDirectoryFile, filename); FileInputStream fis = new FileInputStream(file); InputStream is = MXEncryptedAttachments.decryptAttachment(fis, mEncryptedFileInfo); fis.close(); // if the decryption succeeds, replace the encrypted file content by the unencrypted one if (null != is) { mApplicationContext.deleteFile(filename); fos = new FileOutputStream(file); byte[] buf = new byte[DOWNLOAD_BUFFER_READ_SIZE]; int len; while ((len = is.read(buf)) != -1) { fos.write(buf, 0, len); } } else { mDownloadStats.mProgress = 0; } } uiHandler.post(new Runnable() { @Override public void run() { refreshTimer.cancel(); } }); if ((null != connection) && (connection instanceof HttpsURLConnection)) { ((HttpsURLConnection) connection).disconnect(); } // the file has been successfully downloaded if (mDownloadStats.mProgress == 100) { try { File originalFile = new File(mDirectoryFile, filename); String newFileName = MXMediaDownloadWorkerTask.buildFileName(mUrl, mMimeType); File newFile = new File(mDirectoryFile, newFileName); if (newFile.exists()) { // Or you could throw here. mApplicationContext.deleteFile(newFileName); } originalFile.renameTo(newFile); } catch (Exception e) { Log.e(LOG_TAG, "doInBackground : renaming error " + e.getLocalizedMessage()); } } } if (mDownloadStats.mProgress == 100) { Log.d(LOG_TAG, "The download " + this + " is done."); } else { if (null != mErrorAsJsonElement) { Log.d(LOG_TAG, "The download " + this + " failed : mErrorAsJsonElement " + mErrorAsJsonElement.toString()); } else { Log.d(LOG_TAG, "The download " + this + " failed."); } } } catch (Exception e) { Log.e(LOG_TAG, "Unable to download media " + this); } // remove the image from the loading one synchronized (mPendingDownloadByUrl) { mPendingDownloadByUrl.remove(mUrl); } return null; }
From source file:org.apache.felix.webconsole.AbstractWebConsolePlugin.java
/** * If the request addresses a resource which may be served by the * <code>getResource</code> method of the * {@link #getResourceProvider() resource provider}, this method serves it * and returns <code>true</code>. Otherwise <code>false</code> is returned. * <code>false</code> is also returned if the resource provider has no * <code>getResource</code> method. * <p>//from w ww. j ava2s . com * If <code>true</code> is returned, the request is considered complete and * request processing terminates. Otherwise request processing continues * with normal plugin rendering. * * @param request The request object * @param response The response object * @return <code>true</code> if the request causes a resource to be sent back. * * @throws IOException If an error occurs accessing or spooling the resource. */ private final boolean spoolResource(HttpServletRequest request, HttpServletResponse response) throws IOException { // no resource if no resource accessor Method getResourceMethod = getGetResourceMethod(); if (getResourceMethod == null) { return false; } String pi = request.getPathInfo(); InputStream ins = null; try { // check for a resource, fail if none URL url = (URL) getResourceMethod.invoke(getResourceProvider(), new Object[] { pi }); if (url == null) { return false; } // open the connection and the stream (we use the stream to be able // to at least hint to close the connection because there is no // method to explicitly close the conneciton, unfortunately) URLConnection connection = url.openConnection(); ins = connection.getInputStream(); // FELIX-2017 Equinox may return an URL for a non-existing // resource but then (instead of throwing) return null on // getInputStream. We should account for this situation and // just assume a non-existing resource in this case. if (ins == null) { return false; } // check whether we may return 304/UNMODIFIED long lastModified = connection.getLastModified(); if (lastModified > 0) { long ifModifiedSince = request.getDateHeader("If-Modified-Since"); if (ifModifiedSince >= (lastModified / 1000 * 1000)) { // Round down to the nearest second for a proper compare // A ifModifiedSince of -1 will always be less response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return true; } // have to send, so set the last modified header now response.setDateHeader("Last-Modified", lastModified); } // describe the contents response.setContentType(getServletContext().getMimeType(pi)); response.setIntHeader("Content-Length", connection.getContentLength()); // spool the actual contents OutputStream out = response.getOutputStream(); byte[] buf = new byte[2048]; int rd; while ((rd = ins.read(buf)) >= 0) { out.write(buf, 0, rd); } // over and out ... return true; } catch (IllegalAccessException iae) { // log or throw ??? } catch (InvocationTargetException ite) { // log or throw ??? // Throwable cause = ite.getTargetException(); } finally { IOUtils.closeQuietly(ins); } return false; }
From source file:com.google.gwt.dev.shell.GWTShellServlet.java
/** * Fetch a file and return it as the HTTP response, setting the cache-related * headers according to the name of the file (see * {@link #getCacheTime(String)}). This function honors If-Modified-Since to * minimize the impact of limiting caching of files for development. * //from w w w .j av a 2 s. c o m * @param request the HTTP request * @param response the HTTP response * @param logger a TreeLogger to use for debug output * @param partialPath the path within the module * @param moduleName the name of the module * @throws IOException */ @SuppressWarnings("deprecation") private void doGetPublicFile(HttpServletRequest request, HttpServletResponse response, TreeLogger logger, String partialPath, String moduleName) throws IOException { // Create a logger branch for this request. logger = logger.branch(TreeLogger.TRACE, "The development shell servlet received a request for '" + partialPath + "' in module '" + moduleName + ".gwt.xml' ", null); // Handle auto-generation of resources. if (shouldAutoGenerateResources()) { if (autoGenerateResources(request, response, logger, partialPath, moduleName)) { return; } } URL foundResource = null; try { // Look for the requested file on the public path. // ModuleDef moduleDef = getModuleDef(logger, moduleName); if (shouldAutoGenerateResources()) { Resource publicResource = moduleDef.findPublicFile(partialPath); if (publicResource != null) { foundResource = publicResource.getURL(); } if (foundResource == null) { // Look for public generated files File shellDir = getShellWorkDirs().getShellPublicGenDir(moduleDef); File requestedFile = new File(shellDir, partialPath); if (requestedFile.exists()) { try { foundResource = requestedFile.toURI().toURL(); } catch (MalformedURLException e) { // ignore since it was speculative anyway } } } } /* * If the user is coming from compiled web-mode, check the linker output * directory for the real bootstrap file. */ if (foundResource == null) { File moduleDir = getShellWorkDirs().getCompilerOutputDir(moduleDef); File requestedFile = new File(moduleDir, partialPath); if (requestedFile.exists()) { try { foundResource = requestedFile.toURI().toURL(); } catch (MalformedURLException e) { // ignore since it was speculative anyway } } } if (foundResource == null) { String msg; if ("gwt.js".equals(partialPath)) { msg = "Loading the old 'gwt.js' bootstrap script is no longer supported; please load '" + moduleName + ".nocache.js' directly"; } else { msg = "Resource not found: " + partialPath + "; " + "(could a file be missing from the public path or a <servlet> " + "tag misconfigured in module " + moduleName + ".gwt.xml ?)"; } logger.log(TreeLogger.WARN, msg, null); throw new UnableToCompleteException(); } } catch (UnableToCompleteException e) { sendErrorResponse(response, HttpServletResponse.SC_NOT_FOUND, "Cannot find resource '" + partialPath + "' in the public path of module '" + moduleName + "'"); return; } // Get the MIME type. String path = foundResource.toExternalForm(); String mimeType = null; try { mimeType = getServletContext().getMimeType(path); } catch (UnsupportedOperationException e) { // Certain minimalist servlet containers throw this. // Fall through to guess the type. } if (mimeType == null) { mimeType = guessMimeType(path); if (logger.isLoggable(TreeLogger.TRACE)) { logger.log(TreeLogger.TRACE, "Guessed MIME type '" + mimeType + "'", null); } } maybeIssueXhtmlWarning(logger, mimeType, partialPath); long cacheSeconds = getCacheTime(path); InputStream is = null; try { // Check for up-to-datedness. URLConnection conn = foundResource.openConnection(); long lastModified = conn.getLastModified(); if (isNotModified(request, lastModified)) { response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); setResponseCacheHeaders(response, cacheSeconds); return; } // Set up headers to really send it. response.setStatus(HttpServletResponse.SC_OK); long now = new Date().getTime(); response.setHeader(HttpHeaders.DATE, HttpHeaders.toInternetDateFormat(now)); response.setContentType(mimeType); String lastModifiedStr = HttpHeaders.toInternetDateFormat(lastModified); response.setHeader(HttpHeaders.LAST_MODIFIED, lastModifiedStr); // Expiration header. Either immediately stale (requiring an // "If-Modified-Since") or infinitely cacheable (not requiring even a // freshness check). setResponseCacheHeaders(response, cacheSeconds); // Content length. int contentLength = conn.getContentLength(); if (contentLength >= 0) { response.setHeader(HttpHeaders.CONTENT_LENGTH, Integer.toString(contentLength)); } // Send the bytes. is = conn.getInputStream(); streamOut(is, response.getOutputStream(), 1024 * 8); } finally { Utility.close(is); } }