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 */ 073 public void write(File out) throws IOException { 074 DocumentBuilder parser; 075 try { 076 parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 077 } catch (ParserConfigurationException e) { 078 throw new IOException(e); 079 } 080 Document doc = parser.newDocument(); 081 082 Element root = doc.createElement(BINARY_STORE); 083 doc.appendChild(root); 084 root.appendChild(doc.createElement(DIGEST)).appendChild(doc.createTextNode(digest)); 085 root.appendChild(doc.createElement(DEPTH)).appendChild(doc.createTextNode(String.valueOf(depth))); 086 087 try { 088 TransformerFactory factory = TransformerFactory.newInstance(); 089 factory.setFeature(FEATURE_SECURE_PROCESSING, true); 090 Transformer trans = factory.newTransformer(); 091 trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); 092 trans.setOutputProperty(OutputKeys.INDENT, "yes"); 093 // don't use StreamResult(out) as it fails on paths with spaces 094 try (FileOutputStream stream = new FileOutputStream(out)) { 095 Result outputTarget = new StreamResult(stream); 096 trans.transform(new DOMSource(doc), outputTarget); 097 } 098 } catch (TransformerException e) { 099 throw new IOException(e); 100 } 101 } 102 103}