001/*
002 * (C) Copyright 2006-2011 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 *     Florent Guillaume
018 */
019
020package org.nuxeo.ecm.core.blob.binary;
021
022import static javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING;
023
024import java.io.File;
025import java.io.FileOutputStream;
026import java.io.IOException;
027
028import javax.xml.parsers.DocumentBuilder;
029import javax.xml.parsers.DocumentBuilderFactory;
030import javax.xml.parsers.ParserConfigurationException;
031import javax.xml.transform.OutputKeys;
032import javax.xml.transform.Result;
033import javax.xml.transform.Transformer;
034import javax.xml.transform.TransformerException;
035import javax.xml.transform.TransformerFactory;
036import javax.xml.transform.dom.DOMSource;
037import javax.xml.transform.stream.StreamResult;
038
039import org.nuxeo.common.xmap.annotation.XNode;
040import org.nuxeo.common.xmap.annotation.XObject;
041import org.w3c.dom.Document;
042import org.w3c.dom.Element;
043
044/**
045 * Descriptor for the configuration of an on-disk binaries storage.
046 */
047@XObject(value = BinaryManagerRootDescriptor.BINARY_STORE)
048public class BinaryManagerRootDescriptor {
049
050    public static final String BINARY_STORE = "binary-store";
051
052    public static final String DIGEST = "digest";
053
054    public static final String DEPTH = "depth";
055
056    /**
057     * The digest, for instance {@code MD5} or {@code SHA-256}.
058     */
059    @XNode(DIGEST)
060    public String digest;
061
062    /**
063     * The number of intermediate sub-directories to use.
064     */
065    @XNode(DEPTH)
066    public int depth;
067
068    /**
069     * Writes the descriptor to an XML file.
070     *
071     * @param out the output file to use
072     * @throws IOException
073     */
074    public void write(File out) throws IOException {
075        DocumentBuilder parser;
076        try {
077            parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
078        } catch (ParserConfigurationException e) {
079            throw (IOException) new IOException().initCause(e);
080        }
081        Document doc = parser.newDocument();
082
083        Element root = doc.createElement(BINARY_STORE);
084        doc.appendChild(root);
085        root.appendChild(doc.createElement(DIGEST)).appendChild(doc.createTextNode(digest));
086        root.appendChild(doc.createElement(DEPTH)).appendChild(doc.createTextNode(String.valueOf(depth)));
087
088        try {
089            TransformerFactory factory = TransformerFactory.newInstance();
090            factory.setFeature(FEATURE_SECURE_PROCESSING, true);
091            Transformer trans = factory.newTransformer();
092            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
093            trans.setOutputProperty(OutputKeys.INDENT, "yes");
094            // don't use StreamResult(out) as it fails on paths with spaces
095            Result outputTarget = new StreamResult(new FileOutputStream(out));
096            trans.transform(new DOMSource(doc), outputTarget);
097        } catch (TransformerException e) {
098            throw (IOException) new IOException().initCause(e);
099        }
100    }
101
102}