001/* 002 * (C) Copyright 2006-2016 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 * Nuxeo 018 */ 019package org.nuxeo.ecm.platform.publisher.impl.localfs; 020 021import java.io.File; 022import java.io.IOException; 023import java.io.StringReader; 024 025import org.apache.commons.io.FileUtils; 026import org.dom4j.Document; 027import org.dom4j.DocumentException; 028import org.dom4j.DocumentFactory; 029import org.dom4j.Element; 030import org.dom4j.Namespace; 031import org.dom4j.QName; 032import org.dom4j.io.SAXReader; 033import org.nuxeo.ecm.core.api.DocumentModel; 034import org.nuxeo.ecm.core.api.DocumentRef; 035import org.nuxeo.ecm.core.api.IdRef; 036import org.nuxeo.ecm.platform.publisher.api.PublishedDocument; 037import org.nuxeo.ecm.platform.publisher.helper.VersioningHelper; 038import org.nuxeo.ecm.platform.publisher.remoting.marshaling.CoreIODocumentModelMarshaler; 039 040public class FSPublishedDocument implements PublishedDocument { 041 042 private static final long serialVersionUID = 1L; 043 044 public static final Namespace nxfspub = new Namespace("nxfspub", "http://www.nuxeo.org/publisher/filesystem"); 045 046 public static final QName pubInfoQN = DocumentFactory.getInstance().createQName("publicationInfo", nxfspub); 047 048 public static final QName sourceDocRefQN = DocumentFactory.getInstance().createQName("sourceDocumentRef", nxfspub); 049 050 public static final QName sourceRepositoryNameQN = DocumentFactory.getInstance().createQName( 051 "sourceRepositoryName", nxfspub); 052 053 public static final QName sourceServerQN = DocumentFactory.getInstance().createQName("sourceServer", nxfspub); 054 055 public static final QName sourceVersionQN = DocumentFactory.getInstance().createQName("sourceVersion", nxfspub); 056 057 public static final QName isPendingQN = DocumentFactory.getInstance().createQName("isPending", nxfspub); 058 059 protected DocumentRef sourceDocumentRef; 060 061 protected String sourceRepositoryName; 062 063 protected String sourceServer; 064 065 protected String sourceVersion; 066 067 protected String persistPath; 068 069 protected String parentPath; 070 071 protected boolean isPending; 072 073 protected String xmlRepresentation; 074 075 public FSPublishedDocument(File file) throws NotFSPublishedDocumentException { 076 parseXML(file); 077 persistPath = file.getAbsolutePath(); 078 parentPath = file.getParent(); 079 } 080 081 protected void parseXML(File file) throws NotFSPublishedDocumentException { 082 SAXReader xmlReader = new SAXReader(); 083 try { 084 Document doc = xmlReader.read(file); 085 Element info = doc.getRootElement().element(pubInfoQN); 086 if (info == null) { 087 // valid xml file, but not a published document 088 throw new NotFSPublishedDocumentException(); 089 } 090 sourceDocumentRef = new IdRef(info.element(sourceDocRefQN).getTextTrim()); 091 sourceRepositoryName = info.element(sourceRepositoryNameQN).getTextTrim(); 092 sourceServer = info.element(sourceServerQN).getTextTrim(); 093 sourceVersion = info.element(sourceVersionQN).getTextTrim(); 094 isPending = Boolean.parseBoolean(info.element(isPendingQN).getTextTrim()); 095 } catch (DocumentException e) { 096 throw new NotFSPublishedDocumentException(e); 097 } 098 } 099 100 public void persist(String containerPath) throws IOException { 101 File output = new File(containerPath, sourceDocumentRef.toString()); 102 FileUtils.writeStringToFile(output, xmlRepresentation); 103 persistPath = output.getAbsolutePath(); 104 } 105 106 public FSPublishedDocument(String server, DocumentModel doc) throws DocumentException { 107 this(server, doc, false); 108 } 109 110 public FSPublishedDocument(String server, DocumentModel doc, boolean isPending) throws DocumentException { 111 112 sourceRepositoryName = doc.getRepositoryName(); 113 sourceDocumentRef = doc.getRef(); 114 sourceVersion = VersioningHelper.getVersionLabelFor(doc); 115 sourceServer = server; 116 this.isPending = isPending; 117 118 CoreIODocumentModelMarshaler marshaler = new CoreIODocumentModelMarshaler(); 119 String xmlDoc = marshaler.marshalDocument(doc); 120 121 SAXReader xmlReader = new SAXReader(); 122 Document xml = xmlReader.read(new StringReader(xmlDoc)); 123 124 xml.getRootElement().add(nxfspub); 125 Element info = xml.getRootElement().addElement(pubInfoQN); 126 127 info.addElement(sourceDocRefQN).setText(sourceDocumentRef.toString()); 128 info.addElement(sourceRepositoryNameQN).setText(sourceRepositoryName); 129 if (sourceServer != null) { 130 info.addElement(sourceServerQN).setText(sourceServer); 131 } else { 132 info.addElement(sourceServerQN); 133 } 134 if (sourceVersion != null) { 135 info.addElement(sourceVersionQN).setText(sourceVersion); 136 } else { 137 info.addElement(sourceVersionQN); 138 } 139 info.addElement(isPendingQN).setText(String.valueOf(isPending)); 140 xmlRepresentation = xml.asXML(); 141 } 142 143 @Override 144 public DocumentRef getSourceDocumentRef() { 145 return sourceDocumentRef; 146 } 147 148 @Override 149 public String getSourceRepositoryName() { 150 return sourceRepositoryName; 151 } 152 153 @Override 154 public String getSourceServer() { 155 return sourceServer; 156 } 157 158 @Override 159 public String getSourceVersionLabel() { 160 return sourceVersion; 161 } 162 163 public String getPersistPath() { 164 return persistPath; 165 } 166 167 @Override 168 public String getPath() { 169 return getPersistPath(); 170 } 171 172 @Override 173 public String getParentPath() { 174 return parentPath; 175 } 176 177 @Override 178 public boolean isPending() { 179 return isPending; 180 } 181 182 @Override 183 public Type getType() { 184 return Type.FILE_SYSTEM; 185 } 186 187}