001/*
002 * (C) Copyright 2006-2007 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 */
017
018package org.nuxeo.ecm.platform.preview.helper;
019
020import java.io.UnsupportedEncodingException;
021import java.net.URLEncoder;
022import java.util.Map;
023import java.util.StringJoiner;
024import java.util.concurrent.ConcurrentHashMap;
025
026import org.apache.commons.lang3.StringUtils;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
030import org.nuxeo.ecm.core.api.blobholder.DocumentBlobHolder;
031import org.nuxeo.ecm.platform.preview.adapter.base.ConverterBasedHtmlPreviewAdapter;
032import org.nuxeo.ecm.platform.preview.api.HtmlPreviewAdapter;
033import org.nuxeo.ecm.platform.preview.api.PreviewException;
034
035public class PreviewHelper {
036
037    public static final String REST_API_PREFIX = "site/api/v1";
038
039    protected static final Map<String, Boolean> hasPreviewByType = new ConcurrentHashMap<>();
040
041    private PreviewHelper() {
042    }
043
044    public static String getPreviewURL(DocumentModel doc) {
045        return getPreviewURL(doc, null);
046    }
047
048    public static String getPreviewURL(DocumentModel doc, String xpath) {
049        StringJoiner sj = new StringJoiner("/", "", "/") // add trailing slash
050            .add(REST_API_PREFIX)
051            .add("repo").add(doc.getRepositoryName())
052            .add("id").add(doc.getId());
053        if (xpath != null) {
054            sj.add("@blob").add(xpath);
055        }
056        String result = sj.add("@preview").toString();
057        String ct = doc.getChangeToken();
058        if (StringUtils.isNotBlank(ct)) {
059            try {
060                result += "?" + CoreSession.CHANGE_TOKEN + "=" + URLEncoder.encode(ct, "UTF-8");
061            } catch (UnsupportedEncodingException e) {
062                throw new PreviewException(e);
063            }
064        }
065        return result;
066    }
067
068    public static boolean typeSupportsPreview(DocumentModel doc) {
069        String docType = doc.getType();
070        if (hasPreviewByType.containsKey(docType)) {
071            return hasPreviewByType.get(docType);
072        } else {
073            HtmlPreviewAdapter adapter = doc.getAdapter(HtmlPreviewAdapter.class);
074            if (adapter == null) {
075                hasPreviewByType.put(docType, false);
076                return false;
077            } else {
078                hasPreviewByType.put(docType, true);
079                return true;
080            }
081        }
082    }
083
084    /**
085     * @since 5.7.3
086     */
087    public static boolean docHasBlobToPreview(DocumentModel document) throws PreviewException {
088        HtmlPreviewAdapter adapter = document.getAdapter(HtmlPreviewAdapter.class);
089        return adapter != null && adapter.hasBlobToPreview();
090    }
091
092    /**
093     * @since 8.2
094     */
095    public static boolean blobSupportsPreview(DocumentModel doc, String xpath) {
096        if (isBlobHolder(doc, xpath)) {
097            xpath = null;
098        }
099        HtmlPreviewAdapter adapter = getBlobPreviewAdapter(doc);
100        return adapter != null && adapter.hasPreview(xpath);
101    }
102
103    /**
104     * @since 8.2
105     */
106    public static HtmlPreviewAdapter getBlobPreviewAdapter(DocumentModel doc) {
107        ConverterBasedHtmlPreviewAdapter adapter = new ConverterBasedHtmlPreviewAdapter();
108        adapter.setAdaptedDocument(doc);
109        return adapter;
110    }
111
112    private static boolean isBlobHolder(DocumentModel doc, String xpath) {
113        DocumentBlobHolder bh = (DocumentBlobHolder) doc.getAdapter(BlobHolder.class);
114        return bh != null && bh.getXpath().equals(xpath);
115    }
116
117    /**
118     * @since 11.1
119     */
120    public static String makeHtmlPage(String body) {
121        return "<!doctype html><html><body>" + body + "</body></html>";
122    }
123
124}