Index: addon/editor/frontend/src/main/java/org/hippoecm/frontend/editor/plugins/resource/ResourceUploadPlugin.java =================================================================== --- addon/editor/frontend/src/main/java/org/hippoecm/frontend/editor/plugins/resource/ResourceUploadPlugin.java (revision 25504) +++ addon/editor/frontend/src/main/java/org/hippoecm/frontend/editor/plugins/resource/ResourceUploadPlugin.java (revision ) @@ -15,18 +15,17 @@ */ package org.hippoecm.frontend.editor.plugins.resource; -import java.io.IOException; +import java.io.*; import java.util.Calendar; import javax.jcr.Node; import javax.jcr.RepositoryException; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; -import org.apache.wicket.ajax.AjaxRequestTarget; -import org.apache.wicket.ajax.markup.html.form.AjaxButton; +import org.apache.jackrabbit.extractor.PdfTextExtractor; import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.form.upload.FileUpload; -import org.apache.wicket.markup.html.form.upload.FileUploadField; import org.apache.wicket.model.StringResourceModel; import org.apache.wicket.util.value.IValueMap; import org.apache.wicket.util.value.ValueMap; @@ -40,9 +39,15 @@ import org.hippoecm.frontend.plugins.yui.upload.FileUploadWidget; import org.hippoecm.frontend.plugins.yui.upload.FileUploadWidgetSettings; import org.hippoecm.frontend.service.render.RenderPlugin; +import org.hippoecm.repository.api.HippoNodeType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * Plugin for uploading resources into the JCR repository. + * The plugin supports multi-file upload with instant or delayed upload. + * This plugin can be configured with specific types, so not all file types are allowed to be uploaded. + */ public class ResourceUploadPlugin extends RenderPlugin { @SuppressWarnings("unused") private final static String SVN_ID = "$Id: ResourceUploadPlugin.java 25504 2010-11-30 13:43:42Z abogaart $"; @@ -122,10 +127,16 @@ } else { JcrNodeModel nodeModel = (JcrNodeModel) ResourceUploadPlugin.this.getDefaultModel(); Node node = nodeModel.getNode(); + ByteArrayOutputStream output = new ByteArrayOutputStream(); try { - node.setProperty("jcr:mimeType", mimeType); - node.setProperty("jcr:data", upload.getInputStream()); - node.setProperty("jcr:lastModified", Calendar.getInstance()); + IOUtils.copy(upload.getInputStream(), output); + + setDefaultResourceProperties(node, mimeType, new ByteArrayInputStream(output.toByteArray())); + + if(extension.toLowerCase().equals("pdf")){ + handlePdfAndSetHippoTextProperty(node, new ByteArrayInputStream(output.toByteArray())); + } + ResourceHelper.validateResource(node, fileName); } catch (RepositoryException ex) { error(ex); @@ -136,7 +147,55 @@ } catch (GalleryException ex) { error(ex); log.error(ex.getMessage()); + } finally { + IOUtils.closeQuietly(output); } } } + + /** + * Set the default 'hippo:resource' properties: + * + * @param node the {@link Node} on which to set the properties + * @param mimeType the mime-type of the binary data (e.g. application/pdf, image/jpeg) + * @param inputStream the data + * @throws RepositoryException exception thrown when one of the properties or values could not be set + */ + private void setDefaultResourceProperties(Node node, String mimeType, ByteArrayInputStream inputStream) throws RepositoryException { + try{ + node.setProperty("jcr:mimeType", mimeType); + node.setProperty("jcr:data", inputStream); + node.setProperty("jcr:lastModified", Calendar.getInstance()); + } finally { + IOUtils.closeQuietly(inputStream); -} + } + } + + /** + * Handles the inputstream and extract text content from the PDF and sets it as a binary type property on the + * resource node. + * @param node the {@link Node} on which to set the '{@value HippoNodeType#HIPPO_TEXT}' property + * @param inputStream binary stream + */ + private void handlePdfAndSetHippoTextProperty(Node node, ByteArrayInputStream inputStream) { + PdfTextExtractor extractor = new PdfTextExtractor(); + ByteArrayInputStream byteInputStream = null; + try { + CharArrayReader charReader = (CharArrayReader) extractor.extractText(inputStream, "application/pdf", null); + byteInputStream = new ByteArrayInputStream(IOUtils.toByteArray(charReader)); + node.setProperty(HippoNodeType.HIPPO_TEXT, byteInputStream); + } catch (IOException e) { + log.warn("An exception has occurred while trying to create inputstream based on " + + "extracted text: {} ",e); + } catch (RepositoryException e) { + log.warn("An exception occurred while trying to set property with extracted text: {}: ",e); + } finally { + IOUtils.closeQuietly(byteInputStream); + IOUtils.closeQuietly(inputStream); + } + } +} Index: addon/editor/frontend/pom.xml =================================================================== --- addon/editor/frontend/pom.xml (revision 25515) +++ addon/editor/frontend/pom.xml (revision ) @@ -88,6 +88,22 @@ hippo-ecm-addon-yui 2.16.05-SNAPSHOT + + + org.apache.commons + commons-io + 1.3.2 + provided + + + + org.apache.jackrabbit + jackrabbit-text-extractors + 1.5.0 + provided + + +