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