001/* 002 * (C) Copyright 2012 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 * bjalon 018 */ 019package org.nuxeo.ecm.mobile.webengine.adapter; 020 021import java.io.IOException; 022import java.io.Serializable; 023import java.util.HashMap; 024import java.util.Map; 025 026import javax.ws.rs.GET; 027 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.nuxeo.ecm.core.api.Blobs; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.core.api.PropertyException; 033import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 034import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder; 035import org.nuxeo.ecm.core.convert.api.ConversionException; 036import org.nuxeo.ecm.core.convert.api.ConversionService; 037import org.nuxeo.ecm.mobile.webengine.document.MobileDocument; 038import org.nuxeo.ecm.platform.preview.helper.PreviewHelper; 039import org.nuxeo.ecm.webengine.WebException; 040import org.nuxeo.ecm.webengine.model.WebAdapter; 041import org.nuxeo.runtime.api.Framework; 042 043/** 044 * @author <a href="mailto:bjalon@nuxeo.com">Benjamin JALON</a> 045 * @since 5.5 046 */ 047@WebAdapter(name = "preview", type = "Preview", targetType = "MobileDocument") 048public class PreviewAdapter extends DefaultMobileAdapter { 049 private static final Log log = LogFactory.getLog(PreviewAdapter.class); 050 051 private String nuxeoContextPath; 052 053 @GET 054 public Object doGet() { 055 return getView("index"); 056 } 057 058 public String getPreviewURL() { 059 Object targetObject = ctx.getTargetObject(); 060 if (!(targetObject instanceof MobileDocument)) { 061 throw new WebException("Target Object must be MobileDocument"); 062 } 063 return getNuxeoContextPath() + "/" + PreviewHelper.getPreviewURL(((MobileDocument) targetObject).getDocument()); 064 } 065 066 public String getPreviewContent() throws PropertyException { 067 Object targetObject = ctx.getTargetObject(); 068 if (!(targetObject instanceof MobileDocument)) { 069 throw new WebException("Target Object must be MobileDocument"); 070 } 071 DocumentModel document = ((MobileDocument) targetObject).getDocument(); 072 if (!document.hasSchema("note")) { 073 throw new WebException("Can't produce preview for this document type"); 074 } 075 String content = (String) document.getPropertyValue("note:note"); 076 String mimetype = (String) document.getPropertyValue("note:mime_type"); 077 return convertToHtml(content, mimetype); 078 } 079 080 private String convertToHtml(String text, String mimeType) { 081 BlobHolder bh = new SimpleBlobHolder(Blobs.createBlob(text, mimeType, "UTF-8")); 082 Map<String, Serializable> parameters = new HashMap<String, Serializable>(); 083 parameters.put("bodyContentOnly", Boolean.TRUE); 084 try { 085 bh = Framework.getLocalService(ConversionService.class).convertToMimeType("text/html", bh, parameters); 086 text = bh.getBlob().getString(); 087 } catch (ConversionException | IOException e) { 088 log.error("Failed to convert to HTML.", e); 089 } 090 return text; 091 } 092 093 private String getNuxeoContextPath() { 094 if (nuxeoContextPath == null) { 095 nuxeoContextPath = Framework.getProperty("org.nuxeo.ecm.contextPath"); 096 } 097 return nuxeoContextPath; 098 } 099 100}