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