001/*
002 *  Copyright 2010 Anyware Services
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 */
016package org.ametys.plugins.repository.metadata.jcr;
017
018import java.io.IOException;
019import java.io.InputStream;
020import java.io.OutputStream;
021
022import javax.jcr.Binary;
023import javax.jcr.Node;
024import javax.jcr.RepositoryException;
025
026import org.apache.commons.codec.digest.DigestUtils;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030import org.ametys.plugins.repository.AmetysRepositoryException;
031import org.ametys.plugins.repository.RepositoryConstants;
032import org.ametys.plugins.repository.metadata.BinaryMetadata;
033import org.ametys.plugins.repository.metadata.ModifiableBinaryMetadata;
034
035/**
036 * JCR based implementation of a {@link BinaryMetadata} based on the ametys:binaryMetadata nodetype
037 * @deprecated Use org.ametys.cms.data.Binary instead
038 */
039@Deprecated
040public class JCRBinaryMetadata extends JCRResource implements ModifiableBinaryMetadata
041{
042    private static Logger __logger = LoggerFactory.getLogger(JCRBinaryMetadata.class);
043    
044    /**
045     * Creates a Node-based binary metadata
046     * @param node the support Node
047     */
048    public JCRBinaryMetadata(Node node)
049    {
050        super(node);
051    }
052
053    public String getFilename()
054    {
055        try
056        {
057            return getNode().getProperty("ametys:filename").getString();
058        }
059        catch (RepositoryException e)
060        {
061            throw new AmetysRepositoryException(e);
062        }
063    }
064
065    public void setFilename(String filename)
066    {
067        try
068        {
069            getNode().setProperty("ametys:filename", filename);
070        }
071        catch (RepositoryException e)
072        {
073            throw new AmetysRepositoryException(e);
074        }
075    }
076    
077    public String getHash()
078    {
079        try
080        {
081            return getNode().getProperty(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":hash").getString();
082        }
083        catch (RepositoryException e)
084        {
085            return null;
086        }
087    }
088    
089    @Override
090    public void setInputStream(InputStream stream) throws AmetysRepositoryException
091    {
092        try
093        {
094            _checkLock();
095            Binary binary = getNode().getSession().getValueFactory().createBinary(stream);
096            getNode().setProperty("jcr:data", binary);
097            
098            try (InputStream binaryStream = binary.getStream())
099            {
100                String sha1Hex = DigestUtils.sha1Hex(binaryStream);
101                getNode().setProperty(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":hash" , sha1Hex);
102            }
103            catch (IOException e)
104            {
105                __logger.warn("An error occurred setting the hash of binary", e);
106            }
107        }
108        catch (RepositoryException e)
109        {
110            throw new AmetysRepositoryException(e);
111        }
112    }
113    
114    @Override
115    public OutputStream getOutputStream() throws AmetysRepositoryException
116    {
117        return new JCROutputStream(getNode(), true);
118    }
119}