001/* 002 * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo 016 */ 017 018package org.nuxeo.ecm.platform.publisher.remoting.marshaling; 019 020import java.io.BufferedReader; 021import java.io.File; 022import java.io.FileReader; 023import java.io.IOException; 024 025import org.dom4j.DocumentException; 026import org.nuxeo.ecm.core.api.CoreSession; 027import org.nuxeo.ecm.core.api.DocumentModel; 028import org.nuxeo.ecm.core.api.NuxeoException; 029import org.nuxeo.ecm.core.io.DocumentPipe; 030import org.nuxeo.ecm.core.io.DocumentReader; 031import org.nuxeo.ecm.core.io.DocumentWriter; 032import org.nuxeo.ecm.core.io.impl.DocumentPipeImpl; 033import org.nuxeo.ecm.core.io.impl.plugins.XMLDocumentWriter; 034import org.nuxeo.ecm.platform.publisher.remoting.marshaling.interfaces.DocumentModelMarshaler; 035import org.nuxeo.ecm.platform.publisher.remoting.marshaling.io.SingleDocumentReaderWithInLineBlobs; 036import org.nuxeo.ecm.platform.publisher.remoting.marshaling.io.SingleShadowDocumentWriter; 037import org.nuxeo.ecm.platform.publisher.remoting.marshaling.io.SingleXMlDocumentReader; 038 039/** 040 * DocumentModel marshaler using Core IO services. 041 * 042 * @author tiry 043 */ 044public class CoreIODocumentModelMarshaler implements DocumentModelMarshaler { 045 046 protected String originatingServer; 047 048 @Override 049 public String marshalDocument(DocumentModel doc) { 050 051 // load the datamodel 052 if (originatingServer != null) { 053 String source = new ExtendedDocumentLocation(originatingServer, doc).toString(); 054 doc.setProperty("dublincore", "source", source); 055 } 056 057 CoreSession coreSession = doc.getCoreSession(); 058 DocumentReader reader = new SingleDocumentReaderWithInLineBlobs(coreSession, doc); 059 060 File tmpFile = null; 061 try { 062 tmpFile = File.createTempFile("io-marshaling-", "xml"); 063 DocumentWriter writer = new XMLDocumentWriter(tmpFile); 064 DocumentPipe pipe = new DocumentPipeImpl(); 065 pipe.setReader(reader); 066 pipe.setWriter(writer); 067 pipe.run(); 068 FileReader freader = new FileReader(tmpFile); 069 070 BufferedReader br = new BufferedReader(freader); 071 StringBuilder sb = new StringBuilder(); 072 String line; 073 while ((line = br.readLine()) != null) { 074 sb.append(line).append("\n"); 075 } 076 reader.close(); 077 br.close(); 078 return sb.toString(); 079 } catch (IOException e) { 080 throw new NuxeoException("Unable to marshal DocumentModel", e); 081 } finally { 082 if (tmpFile != null) { 083 tmpFile.delete(); 084 } 085 } 086 } 087 088 @Override 089 public DocumentModel unMarshalDocument(String data, CoreSession coreSession) { 090 try { 091 DocumentReader reader = new SingleXMlDocumentReader(data); 092 DocumentWriter writer = new SingleShadowDocumentWriter(coreSession, null); 093 DocumentPipe pipe = new DocumentPipeImpl(); 094 pipe.setReader(reader); 095 pipe.setWriter(writer); 096 pipe.run(); 097 return ((SingleShadowDocumentWriter) writer).getShadowDocument(); 098 } catch (IOException | DocumentException e) { 099 throw new NuxeoException("Unable to unmarshal DocumentModel", e); 100 } 101 } 102 103 @Override 104 public void setOriginatingServer(String serverName) { 105 this.originatingServer = serverName; 106 } 107 108}