001/*
002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 */
019package org.nuxeo.ecm.platform.filemanager.core.listener;
020
021import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.ABOUT_TO_CREATE;
022import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.BEFORE_DOC_UPDATE;
023
024import java.io.IOException;
025import java.security.MessageDigest;
026import java.security.NoSuchAlgorithmException;
027import java.util.List;
028
029import org.apache.commons.codec.binary.Base64;
030import org.apache.commons.codec.digest.DigestUtils;
031import org.apache.commons.logging.Log;
032import org.apache.commons.logging.LogFactory;
033import org.nuxeo.ecm.core.api.Blob;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.core.api.PropertyException;
036import org.nuxeo.ecm.core.api.model.Property;
037import org.nuxeo.ecm.core.event.Event;
038import org.nuxeo.ecm.core.event.EventContext;
039import org.nuxeo.ecm.core.event.EventListener;
040import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
041import org.nuxeo.ecm.platform.filemanager.api.FileManager;
042import org.nuxeo.runtime.api.Framework;
043
044public class DigestComputer implements EventListener {
045
046    private static final Log log = LogFactory.getLog(DigestComputer.class);
047
048    private void addDigestToDocument(DocumentModel doc) {
049        FileManager fm = Framework.getService(FileManager.class);
050        List<String> xpathFields = fm.getFields();
051        String digestAlgo = fm.getDigestAlgorithm();
052        for (String xpathField : xpathFields) {
053            Property blobProp = null;
054            try {
055                blobProp = doc.getProperty(xpathField);
056            } catch (PropertyException e) {
057                log.debug("Property " + xpathField + " not found on doc, skipping");
058            }
059            if (blobProp != null && !blobProp.isPhantom() && blobProp.isDirty()) {
060                try {
061                    Blob blob = (Blob) blobProp.getValue();
062                    if (blob != null) {
063                        String digest = computeDigest(blob, digestAlgo);
064                        if (!digest.equals(blob.getDigest())) {
065                            blob.setDigest(digest);
066                        }
067                    }
068                } catch (PropertyException | IOException e) {
069                    log.error("Error while trying to save blob digest", e);
070                }
071            }
072        }
073    }
074
075    private String computeDigest(Blob blob, String digestAlgo) throws IOException {
076
077        MessageDigest md;
078        try {
079            md = MessageDigest.getInstance(digestAlgo);
080        } catch (NoSuchAlgorithmException e) {
081            throw new RuntimeException(e);
082        }
083        byte[] b = DigestUtils.updateDigest(md, blob.getStream()).digest();
084        return Base64.encodeBase64String(b);
085    }
086
087    @Override
088    public void handleEvent(Event event) {
089        FileManager fm = Framework.getService(FileManager.class);
090        if (!fm.isDigestComputingEnabled()) {
091            return;
092        }
093
094        EventContext ctx = event.getContext();
095        String evt = event.getName();
096        if (ABOUT_TO_CREATE.equals(evt) || BEFORE_DOC_UPDATE.equals(evt)) {
097
098            if (ctx instanceof DocumentEventContext) {
099                DocumentEventContext docCtx = (DocumentEventContext) ctx;
100                DocumentModel doc = docCtx.getSourceDocument();
101                if (doc == null || doc.isProxy()) {
102                    return;
103                }
104                addDigestToDocument(doc);
105            }
106        }
107    }
108
109}