/** * Sets extractor properties, controlling what properties or values are returned by {@link #getBlobsProperties} or * {@link #getBlobs}. * <p> * The properties have to be defined without prefix if there is no prefix in the schema definition. For blob * properties, the path must include the {@code /data} part. */ public void setExtractorProperties(Set<String> includedPaths, Set<String> excludedPaths, boolean allBlobs) { this.includedPaths = normalizePaths(includedPaths); this.excludedPaths = normalizePaths(excludedPaths); this.allBlobs = allBlobs; isDefaultConfiguration = includedPaths == null && excludedPaths == null && allBlobs; }
/** * Encodes a property name to use it in a url. * * @param propertyName like dc:title, file:content.3.filename (?) */ public static String encodePropertyName(String propertyName) { if (propertyName == null) { return null; } String[] items = propertyName.split(":"); return join(items, "/"); }
protected static String getFileName(Blob blob) { String entry = blob.getFilename(); if (entry == null) { entry = "Unknown_" + System.identityHashCode(blob); } return escapeEntryPath(entry); }
/** * Zip the given blob. * * @param blob the blob * @param filename if no filename is given, the blob's filename will be used * @return a zip containing the blob * @throws IOException */ public static Blob zip(Blob blob, String filename) throws IOException { if (filename == null || (filename = filename.trim()).length() == 0) { filename = blob.getFilename(); } File file = Framework.createTempFile("nxops-createzip-", ".tmp"); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file)); Framework.trackFile(file, file); try { zip(blob, out); } finally { out.finish(); out.close(); } return Blobs.createBlob(file, "application/zip", null, filename); }
/** * Gets the blob properties of the document. * * @param doc the document * @return the list of blob properties */ public List<Property> getBlobsProperties(DocumentModel doc) { List<Property> properties = new ArrayList<>(); for (String path : getBlobPaths(doc.getDocumentType())) { if (!isInterestingPath(path)) { continue; } List<String> split = Arrays.asList(path.split("/[*]/")); if (split.isEmpty()) { throw new IllegalStateException("Path detected not well-formed: " + path); } Property property = doc.getProperty(split.get(0)); List<String> subPath = split.subList(1, split.size()); findBlobsProperties(property, subPath, properties); } return properties; }
protected void findStrings(Property property, String path) { if (property instanceof StringProperty) { if (isInterestingPath(path)) { Serializable value = property.getValue(); if (value instanceof String) { if (isInterestingPath(path)) { Serializable value = property.getValue(); if (value instanceof Object[]) { for (Property p : ((ComplexProperty) property).getChildren()) { String pp = p.getField().getName().getPrefixedName(); findStrings(p, path + '/' + pp); findStrings(p, path + "/*");
List<Property> properties = new BlobsExtractor().getBlobsProperties(doc);
/** * The given propertyName should have 'schema_name:property_name' format. * * @return <code>null</code> if any error occurs. */ public static Object getPropertyValue(DocumentModel doc, String propertyName) { try { String schemaName = getSchemaName(propertyName); String fieldName = getFieldName(propertyName); return doc.getProperty(schemaName, fieldName); } catch (PropertyException e) { log.warn("Error trying to get property " + propertyName + ". " + e.getMessage()); if (log.isDebugEnabled()) { log.debug(e); } return null; } }
/** * Finds strings from the document for a given set of included and excluded paths. * <p> * Paths must be specified with a schema prefix in all cases (normalized). * * @param document the document * @param includedPaths the paths to include, or {@code null} for all paths * @param excludedPaths the paths to exclude, or {@code null} for none * @return a list of strings (each string is never {@code null}) */ public List<String> findStrings(DocumentModel document, Set<String> includedPaths, Set<String> excludedPaths) { this.document = document; this.includedPaths = includedPaths; this.excludedPaths = excludedPaths; strings = new ArrayList<>(); for (String schema : document.getSchemas()) { for (Property property : document.getPropertyObjects(schema)) { String path = property.getField().getName().getPrefixedName(); if (!path.contains(":")) { // add schema name as prefix if the schema doesn't have a prefix path = property.getSchema().getName() + ":" + path; } findStrings(property, path); } } return strings; }
public static String getFilename(DocumentModel doc, DocumentView docView) { String filename = docView.getParameter(FILENAME_KEY); if (filename == null) { // try to get it from document String propertyPath = docView.getParameter(FILE_PROPERTY_PATH_KEY); String propertyName = DocumentModelUtils.decodePropertyName(propertyPath); if (propertyName != null) { filename = (String) DocumentModelUtils.getPropertyValue(doc, propertyName + "/filename"); } } return filename; }
protected static void zip(Blob blob, ZipOutputStream out) throws IOException { String entry = getFileName(blob); InputStream in = blob.getStream(); try { ZipUtils._zip(entry, in, out); } finally { in.close(); } }
/** * Gets the blob paths of the document type. Extractor properties are ignored. * * @param documentType the document type * @return the list of blob paths * * @since 8.3 */ public List<String> getBlobPaths(DocumentType documentType) { String docType = documentType.getName(); List<String> paths = docBlobPaths.get(docType); if (paths == null) { paths = new ArrayList<>(); for (Schema schema : documentType.getSchemas()) { findBlobPaths(schema, null, schema, paths); } docBlobPaths.put(docType, paths); } return paths; }
/** * Gets the blobs of the document. * * @param doc the document * @return the list of blobs */ public List<Blob> getBlobs(DocumentModel doc) { List<Blob> blobs = new ArrayList<>(); for (Property property : getBlobsProperties(doc)) { blobs.add((Blob) property.getValue()); } return blobs; }
protected void findBlobsProperties(Property property, List<String> split, List<Property> properties) { if (split.isEmpty()) { if (property.getValue() != null) { properties.add(property); } } else { for (Property childProperty : property.getChildren()) { Property childSubProp = childProperty.get(split.get(0)); List<String> subPath = split.subList(1, split.size()); findBlobsProperties(childSubProp, subPath, properties); } } }
protected static String escapeEntryPath(String path) { String zipEntryEncoding = Framework.getProperty(ZIP_ENTRY_ENCODING_PROPERTY); if (zipEntryEncoding != null && zipEntryEncoding.equals(ZIP_ENTRY_ENCODING_OPTIONS.ascii.toString())) { return StringUtils.toAscii(path, true); } return path; }
/** * Zip a list of blob. * * @param blobs the blob list * @param fileName if no filename is given, the first blob's filename will be used * @return a zip containing the list of blob * @throws IOException */ public static Blob zip(List<Blob> blobs, String fileName) throws IOException { if (fileName == null || (fileName = fileName.trim()).length() == 0) { fileName = blobs.isEmpty() ? null : blobs.get(0).getFilename(); } File file = Framework.createTempFile("nxops-createzip-", ".tmp"); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file)); Framework.trackFile(file, file); try { zip(blobs, out); } finally { out.finish(); out.close(); } return Blobs.createBlob(file, "application/zip", null, fileName); }
/** * Decodes a property path given in a url. * * @param propertyPath like dc:title file/content/3/filename (?) */ public static String decodePropertyName(String propertyPath) { if (propertyPath == null) { return null; } String[] items = propertyPath.split("/"); return join(items, "."); }
protected static void zip(List<Blob> blobs, ZipOutputStream out) throws IOException { // use a set to avoid zipping entries with same names Collection<String> names = new HashSet<String>(); int cnt = 1; for (Blob blob : blobs) { String entry = getFileName(blob); if (!names.add(entry)) { entry = "renamed_" + (cnt++) + "_" + entry; } InputStream in = blob.getStream(); try { ZipUtils._zip(entry, in, out); } finally { in.close(); } } }
Type fieldType = ((ListType) type).getFieldType(); if (fieldType.isComplexType()) { findBlobPaths((ComplexType) fieldType, fieldPath + "/*", schema, paths); } else { continue; // not binary text findBlobPaths(ctype, fieldPath, schema, paths);
@OperationMethod public Blob run(Blob blob) throws IOException { return BlobUtils.zip(blob, fileName); }