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 * @param document 086 * @throws PreviewException 087 * @since 5.7.3 088 */ 089 public static boolean docHasBlobToPreview(DocumentModel document) throws PreviewException { 090 HtmlPreviewAdapter adapter = document.getAdapter(HtmlPreviewAdapter.class); 091 return adapter != null && adapter.hasBlobToPreview(); 092 } 093 094 /** 095 * @since 8.2 096 */ 097 public static boolean blobSupportsPreview(DocumentModel doc, String xpath) { 098 if (isBlobHolder(doc, xpath)) { 099 xpath = null; 100 } 101 HtmlPreviewAdapter adapter = getBlobPreviewAdapter(doc); 102 return adapter != null && adapter.hasPreview(xpath); 103 } 104 105 /** 106 * @since 8.2 107 */ 108 public static HtmlPreviewAdapter getBlobPreviewAdapter(DocumentModel doc) { 109 ConverterBasedHtmlPreviewAdapter adapter = new ConverterBasedHtmlPreviewAdapter(); 110 adapter.setAdaptedDocument(doc); 111 return adapter; 112 } 113 114 private static boolean isBlobHolder(DocumentModel doc, String xpath) { 115 DocumentBlobHolder bh = (DocumentBlobHolder) doc.getAdapter(BlobHolder.class); 116 return bh != null && bh.getXpath().equals(xpath); 117 } 118}