List of usage examples for java.net URLConnection getLastModified
public long getLastModified()
From source file:org.massyframework.assembly.base.web.HttpResourceProcessorManagement.java
/** * ???Http?//from w w w. j a va 2 s .c o m * @param req http * @param resp http? * @param resourcePath ? * @param resourceURL ?URL * @throws IOException ?IO */ private void writeResource(final HttpServletRequest req, final HttpServletResponse resp, final String pathInfo, final URL resourceURL) throws IOException { URLConnection connection = resourceURL.openConnection(); long lastModified = connection.getLastModified(); int contentLength = connection.getContentLength(); String etag = null; if (lastModified != -1 && contentLength != -1) etag = "W/\"" + contentLength + "-" + lastModified + "\""; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ // Check for cache revalidation. // We should prefer ETag validation as the guarantees are stronger and all HTTP 1.1 clients should be using it String ifNoneMatch = req.getHeader(IF_NONE_MATCH); if (ifNoneMatch != null && etag != null && ifNoneMatch.indexOf(etag) != -1) { resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); } long ifModifiedSince = req.getDateHeader(IF_MODIFIED_SINCE); // for purposes of comparison we add 999 to ifModifiedSince since the fidelity // of the IMS header generally doesn't include milli-seconds if (ifModifiedSince > -1 && lastModified > 0 && lastModified <= (ifModifiedSince + 999)) { resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); } // return the full contents regularly if (contentLength != -1) resp.setContentLength(contentLength); String contentType = getServletContext().getMimeType(pathInfo); if (contentType != null) resp.setContentType(contentType); if (lastModified > 0) resp.setDateHeader(LAST_MODIFIED, lastModified); if (etag != null) resp.setHeader(ETAG, etag); if (contentLength != 0) { // open the input stream InputStream is = null; try { is = connection.getInputStream(); // write the resource try { OutputStream os = resp.getOutputStream(); int writtenContentLength = writeResourceToOutputStream(is, os); if (contentLength == -1 || contentLength != writtenContentLength) resp.setContentLength(writtenContentLength); } catch (IllegalStateException e) { // can occur if the response output is already open as a Writer Writer writer = resp.getWriter(); writeResourceToWriter(is, writer); // Since ContentLength is a measure of the number of bytes contained in the body // of a message when we use a Writer we lose control of the exact byte count and // defer the problem to the Servlet Engine's Writer implementation. } } catch (FileNotFoundException e) { // FileNotFoundException may indicate the following scenarios // - url is a directory // - url is not accessible sendError(resp, HttpServletResponse.SC_FORBIDDEN); } catch (SecurityException e) { // SecurityException may indicate the following scenarios // - url is not accessible sendError(resp, HttpServletResponse.SC_FORBIDDEN); } finally { if (is != null) try { is.close(); } catch (IOException e) { // ignore } } } }
From source file:wicket.util.resource.UrlResourceStream.java
/** * Private constructor to force use of static factory methods. * //w w w. j a v a 2 s . c om * @param url * URL of resource */ public UrlResourceStream(final URL url) { // Save URL this.url = url; URLConnection connection = null; try { connection = url.openConnection(); contentLength = connection.getContentLength(); contentType = connection.getContentType(); lastModified = connection.getLastModified(); try { file = new File(new URI(url.toExternalForm())); } catch (Exception ex) { log.debug("cannot convert url: " + url + " to file (" + ex.getMessage() + "), falling back to the inputstream for polling"); } if (file != null && !file.exists()) { file = null; } } catch (IOException ex) { // It should be impossible to get here or the original URL // couldn't have been constructed. But we re-throw with details // anyway. final IllegalArgumentException illegalArgumentException = new IllegalArgumentException( "Invalid URL parameter " + url); illegalArgumentException.initCause(ex); throw illegalArgumentException; } finally { // if applicable, disconnect if (connection != null) { if (connection instanceof HttpURLConnection) { ((HttpURLConnection) connection).disconnect(); } else { try { connection.getInputStream().close(); } catch (Exception ex) { // ignore } } } } }
From source file:net.sf.eclipsecs.core.config.configtypes.ConfigurationType.java
/** * {@inheritDoc}/*from w w w .j a v a 2 s .co m*/ */ public CheckstyleConfigurationFile getCheckstyleConfiguration(ICheckConfiguration checkConfiguration) throws CheckstylePluginException { CheckstyleConfigurationFile data = new CheckstyleConfigurationFile(); try { // resolve the true configuration file URL data.setResolvedConfigFileURL(resolveLocation(checkConfiguration)); URLConnection connection = data.getResolvedConfigFileURL().openConnection(); connection.connect(); // get last modification timestamp data.setModificationStamp(connection.getLastModified()); // get the configuration file data byte[] configurationFileData = getBytesFromURLConnection(connection); data.setCheckConfigFileBytes(configurationFileData); // get the properties bundle byte[] additionalPropertiesBytes = getAdditionPropertiesBundleBytes(data.getResolvedConfigFileURL()); data.setAdditionalPropertyBundleBytes(additionalPropertiesBytes); // get the property resolver PropertyResolver resolver = getPropertyResolver(checkConfiguration, data); data.setPropertyResolver(resolver); } catch (IOException e) { CheckstylePluginException.rethrow(e); } return data; }
From source file:org.springframework.js.resource.ResourceServlet.java
private void prepareResponse(HttpServletResponse response, URL[] resources, String rawResourcePath) throws IOException { long lastModified = -1; int contentLength = 0; String mimeType = null;// ww w.j a va 2 s .c o m for (URL resource : resources) { URLConnection resourceConn = resource.openConnection(); if (resourceConn.getLastModified() > lastModified) { lastModified = resourceConn.getLastModified(); } String currentMimeType = getServletContext().getMimeType(resource.getPath()); if (currentMimeType == null) { String extension = resource.getPath().substring(resource.getPath().lastIndexOf('.')); currentMimeType = defaultMimeTypes.get(extension); } if (mimeType == null) { mimeType = currentMimeType; } else if (!mimeType.equals(currentMimeType)) { 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:org.shredzone.commons.gravatar.impl.GravatarServiceImpl.java
/** * Fetches a Gravatar icon from the server and stores it in the given {@link File}. * * @param url//from ww w .j a v a 2s . co m * Gravatar URL to fetch * @param file * {@link File} to store the icon to */ private void fetchGravatar(URL url, File file) throws IOException { limitUpstreamRequests(); URLConnection conn = url.openConnection(); conn.setConnectTimeout(TIMEOUT); conn.setReadTimeout(TIMEOUT); if (file.exists()) { conn.setIfModifiedSince(file.lastModified()); } conn.connect(); long lastModified = conn.getLastModified(); if (lastModified > 0L && lastModified <= file.lastModified()) { // Cache file exists and is unchanged if (log.isDebugEnabled()) { log.debug("Cached Gravatar is still good: {}", url); } file.setLastModified(System.currentTimeMillis()); // touch return; } try (InputStream in = conn.getInputStream(); OutputStream out = new FileOutputStream(file)) { byte[] buffer = new byte[8192]; int total = 0; int len; while ((len = in.read(buffer)) >= 0) { out.write(buffer, 0, len); total += len; if (total > MAX_GRAVATAR_SIZE) { log.warn("Gravatar exceeded maximum size: {}", url); break; } } out.flush(); if (log.isDebugEnabled()) { log.debug("Downloaded Gravatar: {}", url); } } }
From source file:com.recomdata.datasetexplorer.proxy.XmlHttpProxyServlet.java
/** * Check to see if the configuration file has been updated so that it may be reloaded. *///from w w w . j av a2s. c om private boolean configUpdated() { try { URL url = ctx.getResource(resourcesDir + XHP_CONFIG); URLConnection con; if (url == null) return false; con = url.openConnection(); long lastModified = con.getLastModified(); long XHP_LAST_MODIFIEDModified = 0; if (ctx.getAttribute(XHP_LAST_MODIFIED) != null) { XHP_LAST_MODIFIEDModified = ((Long) ctx.getAttribute(XHP_LAST_MODIFIED)).longValue(); } else { ctx.setAttribute(XHP_LAST_MODIFIED, new Long(lastModified)); return false; } if (XHP_LAST_MODIFIEDModified < lastModified) { ctx.setAttribute(XHP_LAST_MODIFIED, new Long(lastModified)); return true; } } catch (Exception ex) { getLogger().severe("XmlHttpProxyServlet error checking configuration: " + ex); } return false; }
From source file:org.impalaframework.web.servlet.ResourceServlet.java
private void prepareResponse(HttpServletResponse response, URL[] resources, String rawResourcePath) throws IOException { long lastModified = -1; int contentLength = 0; String mimeType = null;//from w ww.j a v a2 s . c o m for (int i = 0; i < resources.length; i++) { URLConnection resourceConn = resources[i].openConnection(); if (resourceConn.getLastModified() > lastModified) { lastModified = resourceConn.getLastModified(); } String currentMimeType = getServletContext().getMimeType(resources[i].getPath()); if (currentMimeType == null) { String extension = resources[i].getPath().substring(resources[i].getPath().lastIndexOf('.')); currentMimeType = (String) defaultMimeTypes.get(extension); } if (mimeType == null) { mimeType = currentMimeType; } else if (!mimeType.equals(currentMimeType)) { 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:org.geoserver.ows.AbstractURLPublisher.java
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { URL url = getUrl(request);/*w w w. j a v a2 s. com*/ // if not found return a 404 if (url == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return null; } File file = DataUtilities.urlToFile(url); if (file != null && file.exists() && file.isDirectory()) { String uri = request.getRequestURI().toString(); uri += uri.endsWith("/") ? "index.html" : "/index.html"; response.addHeader("Location", uri); response.sendError(HttpServletResponse.SC_MOVED_TEMPORARILY); return null; } // set the mime if known by the servlet container, set nothing otherwise // (Tomcat behaves like this when it does not recognize the file format) String mime = getServletContext().getMimeType(new File(url.getFile()).getName()); if (mime != null) { response.setContentType(mime); } // set the content length and content type URLConnection connection = null; InputStream input = null; try { connection = url.openConnection(); long length = connection.getContentLength(); if (length > 0 && length <= Integer.MAX_VALUE) { response.setContentLength((int) length); } long lastModified = connection.getLastModified(); if (lastModified > 0) { SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss", Locale.ENGLISH); format.setTimeZone(TimeZone.getTimeZone("GMT")); String formatted = format.format(new Date(lastModified)) + " GMT"; response.setHeader("Last-Modified", formatted); } // Guessing the charset (and closing the stream) EncodingInfo encInfo = null; OutputStream output = null; final byte[] b4 = new byte[4]; int count = 0; // open the output input = connection.getInputStream(); // Read the first four bytes, and determine charset encoding count = input.read(b4); encInfo = XmlCharsetDetector.getEncodingName(b4, count); response.setCharacterEncoding(encInfo.getEncoding() != null ? encInfo.getEncoding() : "UTF-8"); // send out the first four bytes read output = response.getOutputStream(); output.write(b4, 0, count); // copy the content to the output byte[] buffer = new byte[8192]; int n = -1; while ((n = input.read(buffer)) != -1) { output.write(buffer, 0, n); } } finally { if (input != null) input.close(); } return null; }
From source file:org.n52.wps.server.feed.AlgorithmFeed.java
private long testFeed() throws IOException { long lastFeedUpdate = 0; try {/*from www .j a v a 2s . c om*/ URL url = new URL(feedURL); URLConnection conn = url.openConnection(); // TODO implement checks on MimeType String contentType = conn.getContentType(); if (!contentType.equalsIgnoreCase(ZIP_MIME_TYPE)) { LOGGER.warn("Uncommon MimeType found at Feed URL: " + contentType); } lastFeedUpdate = conn.getLastModified(); } catch (MalformedURLException e) { LOGGER.error("Invalid feedURL: " + feedURL); throw new IOException(); } catch (IOException e) { LOGGER.error("Error connecting to feedURL: " + feedURL); throw new IOException(); } return lastFeedUpdate; }
From source file:org.adaway.service.UpdateService.java
/** * Check for updates of hosts sources/* w w w . ja v a 2 s. co m*/ * * @return return code */ private int checkForUpdates() { Cursor enabledHostsSourcesCursor; long currentLastModifiedLocal; long currentLastModifiedOnline; boolean updateAvailable = false; int returnCode = StatusCodes.ENABLED; // default return code if (Utils.isAndroidOnline(mService)) { 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 != null && enabledHostsSourcesCursor.moveToFirst()) { do { mNumberOfDownloads++; // get url and lastModified from db String currentUrl = enabledHostsSourcesCursor .getString(enabledHostsSourcesCursor.getColumnIndex("url")); currentLastModifiedLocal = enabledHostsSourcesCursor .getLong(enabledHostsSourcesCursor.getColumnIndex("last_modified_local")); try { Log.v(Constants.TAG, "Checking hosts file: " + currentUrl); /* build connection */ URL mURL = new URL(currentUrl); URLConnection connection = mURL.openConnection(); connection.setConnectTimeout(15000); connection.setReadTimeout(30000); currentLastModifiedOnline = connection.getLastModified(); Log.d(Constants.TAG, "mConnectionLastModified: " + currentLastModifiedOnline + " (" + DateUtils.longToDateString(mService, currentLastModifiedOnline) + ")"); Log.d(Constants.TAG, "mCurrentLastModified: " + currentLastModifiedLocal + " (" + DateUtils.longToDateString(mService, currentLastModifiedLocal) + ")"); // check if file is available connection.connect(); connection.getInputStream(); // check if update available for this hosts file if (currentLastModifiedOnline > currentLastModifiedLocal) { updateAvailable = true; } // save last modified online for later viewing in list ProviderHelper.updateHostsSourceLastModifiedOnline(mService, enabledHostsSourcesCursor .getInt(enabledHostsSourcesCursor.getColumnIndex(HostsSources._ID)), currentLastModifiedOnline); } catch (Exception 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); } } 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; } } else { // only report no connection when not in background if (!mBackgroundExecution) { returnCode = StatusCodes.NO_CONNECTION; } else { Log.e(Constants.TAG, "Should not happen! In background execution is no connection available!"); } } // set return code if update is available if (updateAvailable) { returnCode = StatusCodes.UPDATE_AVAILABLE; } // check if hosts file is applied if (!ApplyUtils.isHostsFileCorrect(mService, Constants.ANDROID_SYSTEM_ETC_HOSTS)) { returnCode = StatusCodes.DISABLED; } return returnCode; }