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 hasPreviewByType.put(docType, false); 063 return false; 064 } else { 065 hasPreviewByType.put(docType, true); 066 return true; 067 } 068 } 069 } 070 071 /** 072 * @param document 073 * @throws PreviewException 074 * @since 5.7.3 075 */ 076 public static boolean docHasBlobToPreview(DocumentModel document) throws PreviewException { 077 HtmlPreviewAdapter adapter = document.getAdapter(HtmlPreviewAdapter.class); 078 return adapter != null && adapter.hasBlobToPreview(); 079 } 080 081 /** 082 * @since 8.2 083 */ 084 public static boolean blobSupportsPreview(DocumentModel doc, String xpath) { 085 if (isBlobHolder(doc, xpath)) { 086 xpath = null; 087 } 088 HtmlPreviewAdapter adapter = getBlobPreviewAdapter(doc); 089 return adapter != null && adapter.hasPreview(xpath); 090 } 091 092 /** 093 * @since 8.2 094 */ 095 public static HtmlPreviewAdapter getBlobPreviewAdapter(DocumentModel doc) { 096 ConverterBasedHtmlPreviewAdapter adapter = new ConverterBasedHtmlPreviewAdapter(); 097 adapter.setAdaptedDocument(doc); 098 return adapter; 099 } 100 101 private static boolean isBlobHolder(DocumentModel doc, String xpath) { 102 DocumentBlobHolder bh = (DocumentBlobHolder) doc.getAdapter(BlobHolder.class); 103 return bh != null && bh.getXpath().equals(xpath); 104 } 105}