/** * Returns a hash value for the specified file. * * @param file The file to read * @param strong <code>true</code> to calculate a strong etag based on the file content; * <code>false</code> to compute it from the canonical path, date and length. * @return The MD5 checksum value as a string. * @throws IOException If the file does not exist or an error occurred while reading the file. * @throws UnsupportedOperationException If the MD5 algorithm is not available for that platform. */ public static String hash(File file, boolean strong) throws IOException, UnsupportedOperationException { if (strong) return hash(file); else return hash(file.getCanonicalPath()+'$'+file.length()+'%'+file.lastModified()); }
/** * Returns a hash value for the specified text. * * @param text The text value to hash. * * @return The Hash value for the specified test or <code>null</code> if an error occurred. * * @throws UnsupportedOperationException If the MD5 algorithm is not available for that platform. */ public static String hash(String text) throws UnsupportedOperationException { MessageDigest md = getAlgorithm(); md.update(text.getBytes(StandardCharsets.UTF_8), 0, text.length()); byte[] bytes = md.digest(); return toHex(bytes); }
/** * Returns an ID for this bundle. * * @param files the files included in the bundle. * @return An ID based on the hash value of the concatenation. */ public static String id(List<File> files) { StringBuilder id = new StringBuilder(); for (File f : files) { id.append(f.getAbsolutePath()); } return MD5.hash(id.toString()); }
/** * Returns a hash value for the specified file content. * * <p>Implementation note: this method loads the entire file using NIO. * * @param file The file to read * @return The MD5 checksum value as a string. * * @throws IOException If the file does not exist or an error occurred while reading the file. * @throws UnsupportedOperationException If the MD5 algorithm is not available for that platform. */ public static String hash(File file) throws IOException, UnsupportedOperationException { MessageDigest md = getAlgorithm(); FileInputStream fis = new FileInputStream(file); FileChannel in = fis.getChannel(); try { MappedByteBuffer buffer = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size()); md.update(buffer); byte[] bytes = md.digest(); return toHex(bytes); } finally { fis.close(); } }
private static String createAutoSuggestTempName(Collection<String> fields, boolean terms, int min, Collection<String> resultFields) { StringBuilder name = new StringBuilder(); if (fields != null) { for (String field : fields) name.append(field).append('%'); } name.append(terms).append('%'); name.append(min).append('%'); if (resultFields != null) { for (String field : resultFields) name.append(field).append('%'); } return MD5.hash(name.toString()).toLowerCase(); }
@Override public @Nullable String getETag(ContentRequest req) { File global = GlobalSettings.getPropertiesFile(); if (global == null) return null; return MD5.hash(global.length()+"x"+global.lastModified()); }
public String getETag(ContentRequest req) { String etag = buildIndexEtag(req); return etag == null ? null : MD5.hash(etag); }
@Override public String getETag(ContentRequest req) { StringBuilder etag = new StringBuilder(); // Get relevant parameters etag.append(req.getParameter("term", "keyword")).append('%'); etag.append(req.getParameter("field", "")).append('%'); etag.append(buildIndexEtag(req)); // MD5 of computed etag value return MD5.hash(etag.toString()); }
@Override public String getETag(ContentRequest req) { StringBuilder etag = new StringBuilder(); etag.append(req.getParameter("facets", "")).append('%'); etag.append(req.getParameter("max-number", "20")).append('%'); etag.append(buildIndexEtag(req)); return MD5.hash(etag.toString()); }
@Override public @NonNull String getETag(ContentRequest req) { StringBuilder etag = new StringBuilder(); for (File f : ServiceLoader.getInstance().listServiceFiles()) { etag.append('~').append(f.length()).append('!').append(f.lastModified()); } return MD5.hash(etag.toString()); }
@Override public String getETag(ContentRequest req) { StringBuilder etag = new StringBuilder(); // Get relevant parameters etag.append(req.getParameter("term", "keyword")).append('%'); etag.append(req.getParameter("field", "")).append('%'); etag.append(buildIndexEtag(req)); // MD5 of computed etag value return MD5.hash(etag.toString()); }
@Override public String getETag(ContentRequest req) { StringBuilder etag = new StringBuilder(); // Get relevant parameters etag.append(req.getParameter("term", "keyword")).append('%'); etag.append(req.getParameter("field", "")).append('%'); etag.append(buildIndexEtag(req)); // MD5 of computed etag value return MD5.hash(etag.toString()); }
@Override public String getETag(ContentRequest req) { StringBuilder etag = new StringBuilder(); etag.append(req.getParameter("base", "")).append('%'); etag.append(req.getParameter("facets", "")).append('%'); etag.append(req.getParameter("max-number", "20")).append('%'); etag.append(buildIndexEtag(req)); return MD5.hash(etag.toString()); }
@Override public String getETag(ContentRequest req) { StringBuilder etag = new StringBuilder(); etag.append(req.getParameter("relative-path", "")).append("%"); etag.append(XMLConfiguration.getXMLRootFolder(req)).append("%"); File file = new File(XMLConfiguration.getXMLRootFolder(req), addXMLExtension(req.getParameter("relative-path", ""))); if (file != null && file.exists()) { etag.append(file.length()).append("%"); etag.append(file.lastModified()).append("%"); } else { etag.append("not-found").append("%"); } return MD5.hash(etag.toString()); }
/** * Computes the etag for the templates. * @param templates The main file for the templates. * @return The corresponding etag. */ private static @Nullable String computeEtag(File templates, @Nullable URL fallback) { if (!templates.exists()) { if (fallback != null) return MD5.hash(fallback.toString()); else { LOGGER.error("Unable to find XSLT stylesheet '{}'.", templates.getName()); LOGGER.error("Create a stylesheet at the path below:"); LOGGER.error(templates.getPath()); return null; } } List<File> files = new ArrayList<>(); File parent = templates.getParentFile(); if (parent != null) { listTemplateFiles(parent, files); } StringBuilder b = new StringBuilder(); try { for (File f : files) { b.append(MD5.hash(f, false)); } } catch (IOException ex) { LOGGER.warn("Error thrown while trying to calculate template etag", ex); return null; } return MD5.hash(b.toString()); }
@Override public String getETag(ContentRequest req) { StringBuffer etag = new StringBuffer(); etag.append(req.getParameter("master-file", "/")); etag.append(req.getParameter("pswebsite-root", "/")); etag.append(req.getIntParameter("display-level", 2)); etag.append(req.getParameter("pswebsite-content", "")); try { // get the content as a etag etag.append(buildNavTree(req)); } catch (IOException ex) { etag.append("incorrect"); } return MD5.hash(etag.toString()); }
@Override public String getETag(ContentRequest req) { Environment env = req.getEnvironment(); File file = env.getPublicFile(req.getPathInfo()); if (file != null && file.exists()) return MD5.hash(req.getPathInfo() + "_" + file.length() + "x" + file.lastModified()); else return "not-exist"; }
/** * Returns an MD5 Value of the query string. * * {@inheritDoc} */ @Override public @NonNull String getETag(ContentRequest req) { StringBuilder hash = new StringBuilder("?"); // TODO Use parameter map Enumeration<String> names = req.getParameterNames(); while (names.hasMoreElements()) { String name = names.nextElement(); String[] values = req.getParameterValues(name); if (values != null) { for (String value : values) { hash.append(name).append('=').append(value).append('&'); } } } // Returns a hash of the query string return MD5.hash(hash.toString()); }
/** * Calculate the etag for the specified lists of files based on the absolute path, length * and last modified date. * * @param files the list of files. * @param imported the list of imported files (CSS only). * * @return an MD5 value. */ private static String calculateEtag(List<File> files, List<File> imported) { StringBuilder key = new StringBuilder(); for (File f : files) { appendKey(f, key); } // Also include files that are imported (@import rules in CSS) for (File f : imported) { appendKey(f, key); } return MD5.hash(key.toString()); }
public String getETag(ContentRequest req) { IndexDefinition def = FlintConfig.get().getIndexDefinition(req.getParameter("definition")); if (def == null) return null; StringBuilder etag = new StringBuilder(); for (File root : def.findContentRoots(GlobalSettings.getRepository())) { etag.append(root.getAbsolutePath()).append('%'); } return MD5.hash(etag.toString()); }