List of usage examples for java.net URLConnection getLastModified
public long getLastModified()
From source file:org.jab.docsearch.utils.NetUtils.java
/** * Gets URL modified date as long// w ww . j a va 2 s . co m * * @param url URL to connect * @return date of URLs modification or 0 if an error occurs */ public long getURLModifiedDate(final String url) { try { URL tmpURL = new URL(url); URLConnection conn = tmpURL.openConnection(); // set connection parameter conn.setDoInput(true); conn.setDoOutput(false); conn.setUseCaches(false); conn.setRequestProperty("User-Agent", USER_AGENT); // connect conn.connect(); long modifiedDate = conn.getLastModified(); if (logger.isDebugEnabled()) { logger.debug("getURLModifiedDate() modified date=" + modifiedDate + " of URL='" + url + "'"); } return modifiedDate; } catch (IOException ioe) { logger.error("getURLModifiedDate() failed for URL='" + url + "'", ioe); return 0; } }
From source file:org.impalaframework.web.servlet.ResourceServlet.java
protected long getLastModified(HttpServletRequest request) { if (log.isDebugEnabled()) { log.debug("Checking last modified of resource: " + request.getPathInfo()); }//from w ww . j av a2s .c om URL[] resources; try { resources = getRequestResourceURLs(request); } catch (MalformedURLException e) { return -1; } if (resources == null || resources.length == 0) { return -1; } long lastModified = -1; for (int i = 0; i < resources.length; i++) { URLConnection resourceConn; try { resourceConn = resources[i].openConnection(); } catch (IOException e) { return -1; } if (resourceConn.getLastModified() > lastModified) { lastModified = resourceConn.getLastModified(); } } return lastModified; }
From source file:ch.entwine.weblounge.kernel.site.SiteServlet.java
/** * Tries to serve the request as a static resource from the bundle. * //from w w w .java 2 s .co m * @param request * the http servlet request * @param response * the http servlet response * @throws ServletException * if serving the request fails * @throws IOException * if writing the response back to the client fails */ protected void serviceResource(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { Http11ResponseType responseType = null; String requestPath = request.getPathInfo(); // There is also a special set of resources that we don't want to expose if (isProtected(requestPath)) { response.sendError(HttpServletResponse.SC_FORBIDDEN); return; } String bundlePath = UrlUtils.concat("/site", requestPath); // Does the resource exist? final URL url = bundle.getResource(bundlePath); if (url == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } // Load the resource from the bundle URLConnection connection = url.openConnection(); String contentEncoding = connection.getContentEncoding(); long contentLength = connection.getContentLength(); long lastModified = connection.getLastModified(); if (contentLength <= 0) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } // final Resource resource = Resource.newResource(url); // if (!resource.exists()) { // response.sendError(HttpServletResponse.SC_NOT_FOUND); // return; // } // We don't allow directory listings // if (resource.isDirectory()) { // response.sendError(HttpServletResponse.SC_FORBIDDEN); // return; // } String mimeType = tika.detect(bundlePath); // Try to get mime type and content encoding from resource if (mimeType == null) mimeType = connection.getContentType(); if (mimeType != null) { if (contentEncoding != null) mimeType += ";" + contentEncoding; response.setContentType(mimeType); } // Send the response back to the client InputStream is = connection.getInputStream(); // InputStream is = resource.getInputStream(); try { logger.debug("Serving {}", url); responseType = Http11ProtocolHandler.analyzeRequest(request, lastModified, Times.MS_PER_DAY + System.currentTimeMillis(), contentLength); if (!Http11ProtocolHandler.generateResponse(response, responseType, is)) { logger.warn("I/O error while generating content from {}", url); } } finally { IOUtils.closeQuietly(is); } }
From source file:org.jvnet.hudson.update_center.MavenRepositoryImpl.java
/** * Loads a remote repository index (.zip or .gz), convert it to Lucene index and return it. *///from ww w . j av a2 s. co m private File loadIndex(String id, URL url) throws IOException, UnsupportedExistingLuceneIndexException { File dir = new File(new File(System.getProperty("java.io.tmpdir")), "maven-index/" + id); File local = new File(dir, "index" + getExtension(url)); File expanded = new File(dir, "expanded"); URLConnection con = url.openConnection(); if (url.getUserInfo() != null) { con.setRequestProperty("Authorization", "Basic " + new sun.misc.BASE64Encoder().encode(url.getUserInfo().getBytes())); } if (!expanded.exists() || !local.exists() || (local.lastModified() != con.getLastModified() && !offlineIndex)) { System.out.println("Downloading " + url); // if the download fail in the middle, only leave a broken tmp file dir.mkdirs(); File tmp = new File(dir, "index_" + getExtension(url)); FileOutputStream o = new FileOutputStream(tmp); IOUtils.copy(con.getInputStream(), o); o.close(); if (expanded.exists()) FileUtils.deleteDirectory(expanded); expanded.mkdirs(); if (url.toExternalForm().endsWith(".gz")) { System.out.println("Reconstructing index from " + url); FSDirectory directory = FSDirectory.getDirectory(expanded); NexusIndexWriter w = new NexusIndexWriter(directory, new NexusAnalyzer(), true); FileInputStream in = new FileInputStream(tmp); try { IndexDataReader dr = new IndexDataReader(in); IndexDataReadResult result = dr.readIndex(w, new DefaultIndexingContext(id, id, null, expanded, null, null, NexusIndexer.DEFAULT_INDEX, true)); } finally { IndexUtils.close(w); IOUtils.closeQuietly(in); directory.close(); } } else if (url.toExternalForm().endsWith(".zip")) { Expand e = new Expand(); e.setSrc(tmp); e.setDest(expanded); e.execute(); } else { throw new UnsupportedOperationException("Unsupported index format: " + url); } // as a proof that the expansion was properly completed tmp.renameTo(local); local.setLastModified(con.getLastModified()); } else { System.out.println("Reusing the locally cached " + url + " at " + local); } return expanded; }
From source file:org.apache.cocoon.components.search.SimpleLuceneXMLIndexerImpl.java
/** * return a unique uid of a url connection * * @param urlConnection Description of Parameter * @return String unique uid of a urlConnection * @since//from w ww . ja va2 s .c o m */ private String uid(URLConnection urlConnection) { // Append path and date into a string in such a way that lexicographic // sorting gives the same results as a walk of the file hierarchy. Thus // null (\u0000) is used both to separate directory components and to // separate the path from the date. return urlConnection.toString().replace('/', '\u0000') + "\u0000" + DateField.timeToString(urlConnection.getLastModified()); }
From source file:nl.openweb.hippo.umd.ui.ResourceServlet.java
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String resourcePath = StringUtils.substringBefore(request.getPathInfo(), ";"); if (LOG.isDebugEnabled()) { LOG.debug("Processing request for resource {}.", resourcePath); }// w ww . j a v a 2 s .co m URL resource = getResourceURL(resourcePath); if (resource == null) { if (LOG.isDebugEnabled()) { LOG.debug("Resource not found: {}", resourcePath); } response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } long ifModifiedSince = request.getDateHeader("If-Modified-Since"); URLConnection conn = resource.openConnection(); long lastModified = conn.getLastModified(); if (ifModifiedSince >= lastModified) { if (LOG.isDebugEnabled()) { LOG.debug("Resource: {} Not Modified.", resourcePath); } response.setStatus(304); return; } int contentLength = conn.getContentLength(); prepareResponse(response, resource, lastModified, contentLength); OutputStream out = selectOutputStream(request, response); try { InputStream is = conn.getInputStream(); try { byte[] buffer = new byte[1024]; int bytesRead = -1; while ((bytesRead = is.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } } finally { is.close(); } } finally { out.close(); } }
From source file:org.jenkinsci.modules.optpluginhelper.PluginHelper.java
/** * List all the optional plugins (while populating the staging area with any new ones we discover). * * @return the list of optional plugins available from all the current defined {@link PluginSource} extensions. *//*from w w w. j ava 2 s. c om*/ private List<File> listPlugins() { // TODO figure out what to do if two sources provide different versions of the same plugin, currently undefined List<File> result = new ArrayList<File>(); final Jenkins jenkins = Jenkins.getInstance(); if (jenkins == null) { return result; } File baseDir = new File(jenkins.root, OPTIONAL_PLUGIN_DIR); if (baseDir.exists() && !baseDir.isDirectory()) { LOGGER.log(Level.SEVERE, "Optional plugin working directory {0} exists and is not a directory", baseDir); return result; } if (!baseDir.isDirectory()) { if (!baseDir.mkdirs()) { LOGGER.log(Level.SEVERE, "Could not create optional plugin working directory {0}", baseDir); return result; } } for (URL resource : PluginSource.allPlugins()) { try { final String externalForm = resource.toExternalForm(); ExtractedPluginMetadata metadata = extractedPluginMetadataMap.get(externalForm); if (metadata != null) { File archive = new File(baseDir, metadata.shortName + ".jpi"); if (archive.isFile() && archive.length() == metadata.length && Util.getDigestOf(archive).equals(metadata.digest)) { result.add(archive); continue; } } final URLConnection connection = resource.openConnection(); long lastModified = connection.getLastModified(); long size = connection.getContentLength(); String path = resource.getPath(); String fileName = FilenameUtils.getBaseName(path); boolean nameCheck = false; if (StringUtils.isBlank(fileName)) { nameCheck = true; fileName = Util.getDigestOf(resource.toString()); } File file = new File(baseDir, fileName + ".jpi"); if (file.isFile() && (file.lastModified() == lastModified || lastModified == 0) && file.length() == size) { final String fileDigest = Util.getDigestOf(file); final String resourceDigest; final InputStream stream = connection.getInputStream(); try { resourceDigest = Util.getDigestOf(stream); } finally { IOUtils.closeQuietly(stream); } if (fileDigest.equals(resourceDigest)) { result.add(file); extractedPluginMetadataMap.put(externalForm, new ExtractedPluginMetadata(file)); continue; } } FileUtils.copyURLToFile(resource, file); if (nameCheck) { final String shortName = jenkins.getPluginManager().getPluginStrategy().getShortName(file); if (!fileName.equals(shortName)) { File newFile = new File(baseDir, shortName + ".jpi"); if (!newFile.isFile() || !Util.getDigestOf(newFile).equals(Util.getDigestOf(file))) { FileUtils.moveFile(file, newFile); } file = newFile; } } if (lastModified != 0) { if (!file.setLastModified(lastModified)) { LOGGER.log(Level.FINE, "Couldn't set last modified on {0}", file); } } result.add(file); extractedPluginMetadataMap.put(externalForm, new ExtractedPluginMetadata(file)); } catch (IOException e) { LOGGER.log(Level.WARNING, String.format("Could not process optional plugin from %s", resource), e); } } LOGGER.log(Level.FINE, "List of plugins: " + result); return result; }
From source file:$.ResourceServlet.java
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String resourcePath = StringUtils.substringBefore(request.getPathInfo(), ";"); if (LOG.isDebugEnabled()) { LOG.debug("Processing request for resource {}.", resourcePath); }/* w ww. jav a 2s.c om*/ URL resource = getResourceURL(resourcePath); if (resource == null) { if (LOG.isDebugEnabled()) { LOG.debug("Resource not found: {}", resourcePath); } response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } long ifModifiedSince = request.getDateHeader("If-Modified-Since"); URLConnection conn = resource.openConnection(); long lastModified = conn.getLastModified(); if (ifModifiedSince >= lastModified) { if (LOG.isDebugEnabled()) { LOG.debug("Resource: {} Not Modified.", resourcePath); } response.setStatus(304); return; } int contentLength = conn.getContentLength(); prepareResponse(response, resource, lastModified, contentLength); OutputStream out = selectOutputStream(request, response); try { InputStream is = conn.getInputStream(); try { byte[] buffer = new byte[1024]; int bytesRead = -1; while ((bytesRead = is.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } } finally { is.close(); } } finally { out.close(); } }
From source file:org.atricore.idbus.bundles.apache.tiles.OsgiDefinitionsFactory.java
/** * Appends locale-specific {@link Definition} objects to an existing * {@link Definitions} set by reading locale-specific versions of * the applied sources.//from w ww . j av a 2s .c o m * * @param definitions The Definitions object to append to. * @param tilesContext The requested locale. * @throws DefinitionsFactoryException if an error occurs reading definitions. */ protected void addDefinitions(Definitions definitions, TilesRequestContext tilesContext) throws DefinitionsFactoryException { Locale locale = localeResolver.resolveLocale(tilesContext); if (logger.isDebugEnabled()) logger.debug("Adding definitios for locale " + locale); if (isContextProcessed(tilesContext)) { if (logger.isDebugEnabled()) logger.debug("isContextProcessed(tilesContext):true, returning"); return; } if (locale == null) { if (logger.isDebugEnabled()) logger.debug("locale == null, returning"); return; } processedLocales.add(locale); List<String> postfixes = calculatePostfixes(locale); if (logger.isDebugEnabled()) logger.debug("Processing postfixes:" + (postfixes != null ? postfixes.size() : "null")); Map<String, Definition> localeDefsMap = new HashMap<String, Definition>(); for (Object postfix : postfixes) { if (logger.isDebugEnabled()) logger.debug("Processing postfix [" + postfix + "] for sources:" + (sources != null ? sources.size() : "null")); // For each postfix, all the sources must be loaded. for (Object source : sources) { URL url = (URL) source; String path = url.toExternalForm(); String newPath = concatPostfix(path, (String) postfix); if (logger.isDebugEnabled()) logger.debug("Adding source definition : " + newPath); try { URL newUrl = new URL(newPath); URLConnection connection = newUrl.openConnection(); connection.connect(); if (logger.isDebugEnabled()) logger.debug("Loding definition from URL:" + newUrl.toExternalForm()); lastModifiedDates.put(newUrl.toExternalForm(), connection.getLastModified()); // Definition must be collected, starting from the base // source up to the last localized file. Map<String, Definition> defsMap = reader.read(connection.getInputStream()); if (defsMap != null) { localeDefsMap.putAll(defsMap); } } catch (FileNotFoundException e) { // File not found. continue. if (logger.isDebugEnabled()) { logger.debug("File " + newPath + " not found, continue"); } } catch (IOException e) { // Assume I/O Exception is a Not Found error ? /* throw new DefinitionsFactoryException( "I/O error processing configuration.", e); */ if (logger.isDebugEnabled()) logger.debug("I/O error processing configuration " + newPath + ":" + e.getMessage(), e); } } } // At the end of definitions loading, they can be assigned to // Definitions implementation, to allow inheritance resolution. definitions.addDefinitions(localeDefsMap, localeResolver.resolveLocale(tilesContext)); }
From source file:org.webical.dao.util.WebDavCalendarSynchronisation.java
/** * Retrieves last modification datetime of the specified calendar * * @param calendar - calendar to retrieve last modification datetime * @return date of last modification or null * * @throws DaoException//from ww w . j av a 2 s . c o m * @throws MalformedURLException * @throws IOException * @throws NoSuchAlgorithmException * @throws KeyManagementException */ public Date lastModificationCalendar(Calendar calendar) throws DaoException, MalformedURLException, IOException, NoSuchAlgorithmException, KeyManagementException { if (log.isDebugEnabled()) log.debug("lastModificationCalendar " + calendar.getName()); Date calLastMod = null; if (calendar == null || calendar.getUrl() == null) { throw new DaoException("calendar or url should not be null"); } // Set up authentication if needed if (calendar.getUsername() != null && calendar.getPassword() != null) { ConnectionUtil.setAuthentication(calendar.getUsername(), calendar.getPassword()); } // Get the correct input source URLConnection connection = null; URL url = new URL(calendar.getUrl()); if (url.getProtocol().equalsIgnoreCase(ConnectionUtil.HTTPS_PROTOCOL)) { //TODO get from User Settings connection = ConnectionUtil.getURLConnection(url, true); } else { connection = url.openConnection(); } log.debug(connection.getHeaderFields()); Date datum = new Date(connection.getLastModified()); // GMT date time if (datum.getTime() > 1000000000000L) calLastMod = datum; // > 09/09/2001 return calLastMod; }