001/*
002 * (C) Copyright 2014 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 *     Vladimir Pasquier <vpasquier@nuxeo.com>
016 */
017package org.nuxeo.ecm.automation.io.services.enricher;
018
019import java.io.IOException;
020
021import org.codehaus.jackson.JsonGenerator;
022import org.nuxeo.ecm.core.api.Blob;
023import org.nuxeo.ecm.core.api.DocumentModel;
024import org.nuxeo.ecm.core.api.thumbnail.ThumbnailAdapter;
025import org.nuxeo.ecm.platform.ui.web.io.ThumbnailJsonEnricher;
026import org.nuxeo.ecm.platform.ui.web.tag.fn.DocumentModelFunctions;
027import org.nuxeo.runtime.api.Framework;
028
029/**
030 * This contributor adds a document Thumbnail Download URL
031 *
032 * @since 5.9.5
033 * @deprecated This enricher was migrated to {@link ThumbnailJsonEnricher}. The content enricher service doesn't work
034 *             anymore.
035 */
036@Deprecated
037public class ThumbnailContentEnricher extends AbstractContentEnricher {
038
039    public static final String THUMBNAIL_URL_LABEL = "url";
040
041    public static final String THUMBNAIL_CONTENT_ID = "thumbnail";
042
043    public static final String THUMB_THUMBNAIL = "thumb:thumbnail";
044
045    public static final String DOWNLOAD_THUMBNAIL = "downloadThumbnail";
046
047    @Override
048    public void enrich(JsonGenerator jg, RestEvaluationContext ec) throws IOException {
049        DocumentModel doc = ec.getDocumentModel();
050        ThumbnailAdapter thumbnailAdapter = doc.getAdapter(ThumbnailAdapter.class);
051        jg.writeStartObject();
052        if (thumbnailAdapter != null) {
053            Blob thumbnail = thumbnailAdapter.getThumbnail(doc.getCoreSession());
054            if (thumbnail != null) {
055                String url = DocumentModelFunctions.fileUrl(Framework.getProperty("nuxeo.url"), DOWNLOAD_THUMBNAIL, doc,
056                        THUMB_THUMBNAIL, thumbnail.getFilename());
057                jg.writeStringField(THUMBNAIL_URL_LABEL, url);
058            } else {
059                writeEmptyThumbnail(jg);
060            }
061        } else {
062            writeEmptyThumbnail(jg);
063        }
064        jg.writeEndObject();
065        jg.flush();
066    }
067
068    private void writeEmptyThumbnail(JsonGenerator jg) throws IOException {
069        jg.writeStringField(THUMBNAIL_URL_LABEL, null);
070    }
071
072}