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.DocumentModel;
025import org.nuxeo.ecm.platform.preview.helper.PreviewHelper;
026import org.nuxeo.ecm.platform.preview.io.PreviewJsonEnricher;
027import org.nuxeo.runtime.api.Framework;
028
029/**
030 * This content enricher adds a document Preview URL.
031 *
032 * @since 6.0
033 * @deprecated This enricher was migrated to {@link PreviewJsonEnricher}. The content enricher service doesn't work
034 *             anymore.
035 */
036@Deprecated
037public class PreviewContentEnricher extends AbstractContentEnricher {
038
039    public static final String PREVIEW_URL_LABEL = "url";
040
041    public static final String PREVIEW_CONTENT_ID = "preview";
042
043    @Override
044    public void enrich(JsonGenerator jg, RestEvaluationContext ec) throws IOException {
045        DocumentModel doc = ec.getDocumentModel();
046        String relativeUrl = PreviewHelper.getPreviewURL(doc);
047        jg.writeStartObject();
048        if (relativeUrl != null && !relativeUrl.isEmpty()) {
049            String url = Framework.getProperty("nuxeo.url") + "/" + PreviewHelper.getPreviewURL(doc);
050            jg.writeStringField(PREVIEW_URL_LABEL, url);
051        } else {
052            writeEmptyURL(jg);
053        }
054        jg.writeEndObject();
055        jg.flush();
056    }
057
058    private void writeEmptyURL(JsonGenerator jg) throws IOException {
059        jg.writeStringField(PREVIEW_URL_LABEL, null);
060    }
061
062}