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