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 */ 019 020package org.nuxeo.ecm.platform.publisher.remoting.marshaling; 021 022import java.io.BufferedReader; 023import java.io.File; 024import java.io.FileReader; 025import java.io.IOException; 026 027import org.dom4j.DocumentException; 028 029import org.nuxeo.ecm.core.api.CoreSession; 030import org.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.ecm.core.api.NuxeoException; 032import org.nuxeo.ecm.core.io.DocumentPipe; 033import org.nuxeo.ecm.core.io.DocumentReader; 034import org.nuxeo.ecm.core.io.DocumentWriter; 035import org.nuxeo.ecm.core.io.impl.DocumentPipeImpl; 036import org.nuxeo.ecm.core.io.impl.plugins.XMLDocumentWriter; 037import org.nuxeo.ecm.platform.publisher.remoting.marshaling.interfaces.DocumentModelMarshaler; 038import org.nuxeo.ecm.platform.publisher.remoting.marshaling.io.SingleDocumentReaderWithInLineBlobs; 039import org.nuxeo.ecm.platform.publisher.remoting.marshaling.io.SingleShadowDocumentWriter; 040import org.nuxeo.ecm.platform.publisher.remoting.marshaling.io.SingleXMlDocumentReader; 041import org.nuxeo.runtime.api.Framework; 042 043/** 044 * DocumentModel marshaler using Core IO services. 045 * 046 * @author tiry 047 */ 048public class CoreIODocumentModelMarshaler implements DocumentModelMarshaler { 049 050 protected String originatingServer; 051 052 @Override 053 public String marshalDocument(DocumentModel doc) { 054 055 // load the datamodel 056 if (originatingServer != null) { 057 String source = new ExtendedDocumentLocation(originatingServer, doc).toString(); 058 doc.setProperty("dublincore", "source", source); 059 } 060 061 CoreSession coreSession = doc.getCoreSession(); 062 DocumentReader reader = new SingleDocumentReaderWithInLineBlobs(coreSession, doc); 063 064 File tmpFile = null; 065 try { 066 tmpFile = Framework.createTempFile("io-marshaling-", "xml"); 067 DocumentWriter writer = new XMLDocumentWriter(tmpFile); 068 DocumentPipe pipe = new DocumentPipeImpl(); 069 pipe.setReader(reader); 070 pipe.setWriter(writer); 071 pipe.run(); 072 FileReader freader = new FileReader(tmpFile); 073 074 BufferedReader br = new BufferedReader(freader); 075 StringBuilder sb = new StringBuilder(); 076 String line; 077 while ((line = br.readLine()) != null) { 078 sb.append(line).append("\n"); 079 } 080 reader.close(); 081 br.close(); 082 return sb.toString(); 083 } catch (IOException e) { 084 throw new NuxeoException("Unable to marshal DocumentModel", e); 085 } finally { 086 if (tmpFile != null) { 087 tmpFile.delete(); 088 } 089 } 090 } 091 092 @Override 093 public DocumentModel unMarshalDocument(String data, CoreSession coreSession) { 094 try { 095 DocumentReader reader = new SingleXMlDocumentReader(data); 096 DocumentWriter writer = new SingleShadowDocumentWriter(coreSession, null); 097 DocumentPipe pipe = new DocumentPipeImpl(); 098 pipe.setReader(reader); 099 pipe.setWriter(writer); 100 pipe.run(); 101 return ((SingleShadowDocumentWriter) writer).getShadowDocument(); 102 } catch (IOException | DocumentException e) { 103 throw new NuxeoException("Unable to unmarshal DocumentModel", e); 104 } 105 } 106 107 @Override 108 public void setOriginatingServer(String serverName) { 109 originatingServer = serverName; 110 } 111 112}