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