001/* 002 * (C) Copyright 2015 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-2.1.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 * Nicolas Chapurlat <nchapurlat@nuxeo.com> 016 */ 017 018package org.nuxeo.ecm.platform.ui.web.io; 019 020import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON; 021import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE; 022 023import java.io.IOException; 024 025import org.codehaus.jackson.JsonGenerator; 026import org.nuxeo.ecm.core.api.Blob; 027import org.nuxeo.ecm.core.api.DocumentModel; 028import org.nuxeo.ecm.core.api.NuxeoException; 029import org.nuxeo.ecm.core.api.thumbnail.ThumbnailAdapter; 030import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher; 031import org.nuxeo.ecm.core.io.registry.context.RenderingContext.SessionWrapper; 032import org.nuxeo.ecm.core.io.registry.reflect.Setup; 033import org.nuxeo.ecm.platform.ui.web.tag.fn.DocumentModelFunctions; 034 035/** 036 * Enrich {@link DocumentModel} Json. 037 * <p> 038 * Add {@link DocumentModel}'s thumbnail url as json attachment. 039 * </p> 040 * <p> 041 * Enable if parameter enrichers.document=thumbnail is present. 042 * </p> 043 * <p> 044 * Format is: 045 * 046 * <pre> 047 * {@code 048 * { 049 * "entity-type":"document", 050 * ... 051 * "contextParameters": { 052 * "thumbnail": { 053 * "url": "THUMBNAIL_URL" 054 * }, 055 * } 056 * } 057 * </pre> 058 * 059 * </p> 060 * 061 * @since 7.2 062 */ 063@Setup(mode = SINGLETON, priority = REFERENCE) 064public class ThumbnailJsonEnricher extends AbstractJsonEnricher<DocumentModel> { 065 066 public static final String NAME = "thumbnail"; 067 068 public static final String THUMBNAIL_URL_LABEL = "url"; 069 070 public static final String THUMB_THUMBNAIL = "thumb:thumbnail"; 071 072 public static final String DOWNLOAD_THUMBNAIL = "downloadThumbnail"; 073 074 public ThumbnailJsonEnricher() { 075 super(NAME); 076 } 077 078 @Override 079 public void write(JsonGenerator jg, DocumentModel document) throws IOException { 080 ThumbnailAdapter thumbnailAdapter = document.getAdapter(ThumbnailAdapter.class); 081 jg.writeFieldName(NAME); 082 jg.writeStartObject(); 083 if (thumbnailAdapter != null) { 084 try { 085 Blob thumbnail = null; 086 try (SessionWrapper wrapper = ctx.getSession(document)) { 087 thumbnail = thumbnailAdapter.getThumbnail(wrapper.getSession()); 088 } 089 if (thumbnail != null) { 090 String url = DocumentModelFunctions.fileUrl(ctx.getBaseUrl().replaceAll("/$", ""), 091 DOWNLOAD_THUMBNAIL, document, THUMB_THUMBNAIL, thumbnail.getFilename()); 092 jg.writeStringField(THUMBNAIL_URL_LABEL, url); 093 } else { 094 jg.writeNullField(THUMBNAIL_URL_LABEL); 095 } 096 } catch (NuxeoException e) { 097 // no default thumbnail factory 098 jg.writeNullField(THUMBNAIL_URL_LABEL); 099 } 100 } else { 101 jg.writeNullField(THUMBNAIL_URL_LABEL); 102 } 103 jg.writeEndObject(); 104 } 105 106}