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 org.nuxeo.ecm.core.api.DocumentModel;
021import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
022import org.nuxeo.ecm.core.api.blobholder.DocumentBlobHolder;
023import org.nuxeo.ecm.platform.preview.adapter.base.ConverterBasedHtmlPreviewAdapter;
024import org.nuxeo.ecm.platform.preview.api.HtmlPreviewAdapter;
025import org.nuxeo.ecm.platform.preview.api.PreviewException;
026
027import java.util.Map;
028import java.util.StringJoiner;
029import java.util.concurrent.ConcurrentHashMap;
030
031public class PreviewHelper {
032
033    public static final String REST_API_PREFIX = "site/api/v1";
034
035    protected static final Map<String, Boolean> hasPreviewByType = new ConcurrentHashMap<>();
036
037    private PreviewHelper() {
038    }
039
040    public static String getPreviewURL(DocumentModel doc) {
041        return getPreviewURL(doc, null);
042    }
043
044    public static String getPreviewURL(DocumentModel doc, String xpath) {
045        StringJoiner sj = new StringJoiner("/", "", "/") // add trailing slash
046            .add(REST_API_PREFIX)
047            .add("repo").add(doc.getRepositoryName())
048            .add("id").add(doc.getId());
049        if (xpath != null) {
050            sj.add("@blob").add(xpath);
051        }
052        return sj.add("@preview").toString();
053    }
054
055    public static boolean typeSupportsPreview(DocumentModel doc) {
056        String docType = doc.getType();
057        if (hasPreviewByType.containsKey(docType)) {
058            return hasPreviewByType.get(docType);
059        } else {
060            HtmlPreviewAdapter adapter = doc.getAdapter(HtmlPreviewAdapter.class);
061            if (adapter == null) {
062                synchronized (hasPreviewByType) {
063                    hasPreviewByType.put(docType, false);
064                    return false;
065                }
066            } else {
067                synchronized (hasPreviewByType) {
068                    hasPreviewByType.put(docType, true);
069                    return true;
070                }
071            }
072        }
073    }
074
075    /**
076     * @param document
077     * @throws PreviewException
078     * @since 5.7.3
079     */
080    public static boolean docHasBlobToPreview(DocumentModel document) throws PreviewException {
081        HtmlPreviewAdapter adapter = document.getAdapter(HtmlPreviewAdapter.class);
082        return adapter != null && adapter.hasBlobToPreview();
083    }
084
085    /**
086     * @since 8.2
087     */
088    public static boolean blobSupportsPreview(DocumentModel doc, String xpath) {
089        if (isBlobHolder(doc, xpath)) {
090            xpath = null;
091        }
092        HtmlPreviewAdapter adapter = getBlobPreviewAdapter(doc);
093        return adapter != null && adapter.hasPreview(xpath);
094    }
095
096    /**
097     * @since 8.2
098     */
099    public static HtmlPreviewAdapter getBlobPreviewAdapter(DocumentModel doc) {
100        ConverterBasedHtmlPreviewAdapter adapter = new ConverterBasedHtmlPreviewAdapter();
101        adapter.setAdaptedDocument(doc);
102        return adapter;
103    }
104
105    private static boolean isBlobHolder(DocumentModel doc, String xpath) {
106        DocumentBlobHolder bh = (DocumentBlobHolder) doc.getAdapter(BlobHolder.class);
107        return bh != null && bh.getXpath().equals(xpath);
108    }
109}