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.ByteArrayInputStream;
019import java.io.ByteArrayOutputStream;
020import java.io.IOException;
021
022import javax.jcr.Binary;
023import javax.jcr.Node;
024import javax.jcr.RepositoryException;
025
026class JCROutputStream extends ByteArrayOutputStream
027{
028    private Node _node;
029
030    JCROutputStream(Node node)
031    {
032        _node = node;
033    }
034
035    @Override
036    public void close() throws IOException
037    {
038        super.close();
039
040        try
041        {
042            Binary binary = _node.getSession().getValueFactory().createBinary(new ByteArrayInputStream(toByteArray()));
043            _node.setProperty("jcr:data", binary);
044        }
045        catch (RepositoryException e)
046        {
047            throw new IOException("Cannot save primary content", e);
048        }
049    }
050}