List of usage examples for java.net URLConnection getLastModified
public long getLastModified()
From source file:ru.appliedtech.template.velocity.VelocityResourceLoader.java
@Override public long getLastModified(Resource resource) { IResourcesService resources = AC.getInsecureService(IResourcesService.class); try {/*from w ww .ja va 2 s . c o m*/ URLConnection connection = resources.getURL(resource.getName()).openConnection(); return connection.getLastModified(); } catch (AppException | IOException e) { throw new ResourceNotFoundException(e); } }
From source file:com.github.jknack.handlebars.io.URLTemplateSource.java
/** * Read the last modified date from a resource. * * @param resource The resource./*from w w w .j a v a2s. com*/ * @return The last modified date from a resource. * @throws IOException */ private long lastModified(final URL resource) { URLConnection uc = null; try { uc = resource.openConnection(); return uc.getLastModified(); } catch (IOException ex) { Handlebars.warn("Can't get last modified date of: %s", resource); return -1; } finally { try { if (uc != null) { // http://stackoverflow.com/questions/2057351/how-do-i-get-the-last-modification-time-of // -a-java-resource InputStream is = uc.getInputStream(); if (is != null) { is.close(); } } } catch (IOException e) { Handlebars.warn("Can't close: %s", resource); } } }
From source file:com.datos.vfs.provider.url.UrlFileObject.java
/** * Returns the last modified time of this file. *///w w w. j a v a 2 s . c o m @Override protected long doGetLastModifiedTime() throws Exception { final URLConnection conn = url.openConnection(); final InputStream in = conn.getInputStream(); try { return conn.getLastModified(); } finally { in.close(); } }
From source file:org.josso.tooling.gshell.install.provider.maven2.MavenFileObject.java
/** * Returns the last modified time of this file. *//*from ww w.j a va2 s . co m*/ protected long doGetLastModifiedTime() throws Exception { final URLConnection conn = uri.toURL().openConnection(); final InputStream in = conn.getInputStream(); try { return conn.getLastModified(); } finally { in.close(); } }
From source file:annis.gui.servlets.ResourceServlet.java
@Override @SuppressWarnings("unchecked") public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { OutputStream outStream = response.getOutputStream(); String completePath = request.getPathInfo(); if (completePath == null) { response.sendError(404, "must provide a valid and existing path with a vistype"); return;/*from w w w . ja v a 2 s. c om*/ } // remove trailing / completePath = completePath.substring(1); String[] pathComponents = completePath.split("/"); String vistype = pathComponents[0]; if (pathComponents.length < 2) { response.sendError(404, "must provide a valid and existing path"); return; } String path = StringUtils.join(Arrays.copyOfRange(pathComponents, 1, pathComponents.length), "/"); // get the visualizer for this vistype ResourcePlugin vis = resourceRegistry.get(vistype); if (vis == null) { response.sendError(500, "There is no resource with the short name " + vistype); } else if (path.endsWith(".class")) { response.sendError(403, "illegal class path access"); } else { URL resource = vis.getClass().getResource(path); if (resource == null) { response.sendError(404, path + " not found"); } else { // check if it is new URLConnection resourceConnection = resource.openConnection(); long resourceLastModified = resourceConnection.getLastModified(); long requestLastModified = request.getDateHeader("If-Modified-Since"); if (requestLastModified != -1 && resourceLastModified <= requestLastModified) { response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); } else { response.addDateHeader("Last-Modified", resourceLastModified); if ("localhost".equals(request.getServerName())) { // does always expire right now response.addDateHeader("Expires", new Date().getTime()); } else { // expires in one minute per default response.addDateHeader("Expires", new Date().getTime() + 60000); } // not in cache, stream out String mimeType = getServletContext().getMimeType(path); response.setContentType(mimeType); if (mimeType.startsWith("text/")) { response.setCharacterEncoding("UTF-8"); } OutputStream bufferedOut = new BufferedOutputStream(outStream); InputStream resourceInStream = new BufferedInputStream(resource.openStream()); try { int v = -1; while ((v = resourceInStream.read()) != -1) { bufferedOut.write(v); } } finally { resourceInStream.close(); bufferedOut.flush(); outStream.flush(); } } } } }
From source file:bboss.org.apache.velocity.runtime.resource.loader.URLResourceLoader.java
/** * Checks to see when a resource was last modified * * @param resource Resource the resource to check * @return long The time when the resource was last modified or 0 if the file can't be reached *///w ww . ja va2s.co m public long getLastModified(Resource resource) { // get the previously used root String name = resource.getName(); String root = (String) templateRoots.get(name); try { // get a connection to the URL URL u = new URL(root + name); URLConnection conn = u.openConnection(); tryToSetTimeout(conn); return conn.getLastModified(); } catch (IOException ioe) { // the file is not reachable at its previous address String msg = "URLResourceLoader: '" + name + "' is no longer reachable at '" + root + "'"; log.error(msg, ioe); throw new ResourceNotFoundException(msg, ioe); } }
From source file:org.apache.velocity.runtime.resource.loader.URLResourceLoader.java
/** * Checks to see when a resource was last modified * * @param resource Resource the resource to check * @return long The time when the resource was last modified or 0 if the file can't be reached *///ww w.j a v a 2s . c o m public long getLastModified(Resource resource) { // get the previously used root String name = resource.getName(); String root = (String) templateRoots.get(name); try { // get a connection to the URL URL u = new URL(root + name); URLConnection conn = u.openConnection(); tryToSetTimeout(conn); return conn.getLastModified(); } catch (IOException ioe) { // the file is not reachable at its previous address String msg = "URLResourceLoader: '" + name + "' is no longer reachable at '" + root + "'"; Logger.error(this, msg, ioe); throw new ResourceNotFoundException(msg, ioe); } }
From source file:org.mycore.common.content.MCRURLContent.java
@Override public String getETag() throws IOException { URLConnection openConnection = url.openConnection(); openConnection.connect();//from w ww . ja va2 s . com String eTag = openConnection.getHeaderField("ETag"); if (eTag != null) { return eTag; } long lastModified = openConnection.getLastModified(); long length = openConnection.getContentLengthLong(); eTag = getSimpleWeakETag(url.toString(), length, lastModified); return eTag == null ? null : eTag.substring(2); }
From source file:com.haulmont.cuba.web.sys.CubaWebJarsHandler.java
@Override public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException { String path = request.getPathInfo(); if (StringUtils.isEmpty(path) || StringUtils.isNotEmpty(path) && !path.startsWith(VAADIN_WEBJARS_PREFIX)) { return false; }/*from www . j a va2s .c o m*/ log.trace("WebJar resource requested: {}", path.replace(VAADIN_WEBJARS_PREFIX, "")); String errorMessage = checkResourcePath(path); if (StringUtils.isNotEmpty(errorMessage)) { log.warn(errorMessage); response.sendError(HttpServletResponse.SC_FORBIDDEN, errorMessage); return false; } URL resourceUrl = getStaticResourceUrl(path); if (resourceUrl == null) { resourceUrl = getClassPathResourceUrl(path); } if (resourceUrl == null) { String msg = String.format("Requested WebJar resource is not found: %s", path); response.sendError(HttpServletResponse.SC_NOT_FOUND, msg); log.warn(msg); return false; } String resourceName = getResourceName(path); String mimeType = servletContext.getMimeType(resourceName); response.setContentType(mimeType != null ? mimeType : FileTypesHelper.DEFAULT_MIME_TYPE); String cacheControl = "public, max-age=0, must-revalidate"; int resourceCacheTime = getCacheTime(resourceName); if (resourceCacheTime > 0) { cacheControl = "max-age=" + String.valueOf(resourceCacheTime); } response.setHeader("Cache-Control", cacheControl); response.setDateHeader("Expires", System.currentTimeMillis() + (resourceCacheTime * 1000)); InputStream inputStream = null; try { URLConnection connection = resourceUrl.openConnection(); long lastModifiedTime = connection.getLastModified(); // Remove milliseconds to avoid comparison problems (milliseconds // are not returned by the browser in the "If-Modified-Since" // header). lastModifiedTime = lastModifiedTime - lastModifiedTime % 1000; response.setDateHeader("Last-Modified", lastModifiedTime); if (browserHasNewestVersion(request, lastModifiedTime)) { response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return true; } inputStream = connection.getInputStream(); copy(inputStream, response.getOutputStream()); return true; } finally { if (inputStream != null) { inputStream.close(); } } }
From source file:org.voyanttools.trombone.input.source.UriInputSource.java
/** * Create a new instance with the specified URI. * /*ww w .j a v a2 s. c om*/ * @param uri the URI associated with this input source * @throws IOException * thrown when there's a problem creating or accessing header * information for the URI * @throws MalformedURLException * thrown if the URI is malformed */ public UriInputSource(URI uri) throws IOException { this.uri = uri; this.metadata = new DocumentMetadata(); this.metadata.setLocation(uri.toString()); this.metadata.setSource(Source.URI); String path = uri.getPath(); if (path.isEmpty() || path.equals("/")) { // no path, use host metadata.setTitle(uri.getHost()); } else if (path.endsWith("/")) { // ends in slash, use full path metadata.setTitle(path); } else { // try to use file part of URI metadata.setTitle(new File(path).getName()); } StringBuilder idBuilder = new StringBuilder(uri.toString()); // establish connection to find other and default metadata URLConnection c = null; try { c = getURLConnection(uri, 15000, 10000); // last modified of file long modified = c.getLastModified(); this.metadata.setModified(modified); idBuilder.append(modified); // try and get length for id int length = c.getContentLength(); idBuilder.append(length); String format = c.getContentType(); if (format != null && format.isEmpty() == false) { idBuilder.append(format); DocumentFormat docFormat = DocumentFormat.fromContentType(format); if (docFormat != DocumentFormat.UNKNOWN) { this.metadata.setDefaultFormat(docFormat); } } } finally { if (c != null && c instanceof HttpURLConnection) { ((HttpURLConnection) c).disconnect(); } } this.id = DigestUtils.md5Hex(idBuilder.toString()); }