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.url.io;
021
022import static org.apache.commons.lang.StringUtils.isBlank;
023import static org.nuxeo.ecm.automation.core.util.PaginableDocumentModelList.CODEC_PARAMETER_NAME;
024import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
025import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
026
027import java.io.IOException;
028
029import javax.inject.Inject;
030
031import org.codehaus.jackson.JsonGenerator;
032import org.nuxeo.ecm.core.api.DocumentLocation;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
035import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher;
036import org.nuxeo.ecm.core.io.registry.reflect.Setup;
037import org.nuxeo.ecm.platform.types.adapter.TypeInfo;
038import org.nuxeo.ecm.platform.url.DocumentViewImpl;
039import org.nuxeo.ecm.platform.url.api.DocumentView;
040import org.nuxeo.ecm.platform.url.api.DocumentViewCodecManager;
041
042/**
043 * Enrich {@link DocumentModel} Json.
044 * <p>
045 * Add {@link DocumentModel}'s document url as json attachment.
046 * </p>
047 * <p>
048 * Enable if parameter enrichers-document=documentURL is present.
049 * </p>
050 * <p>
051 * Format is:
052 *
053 * <pre>
054 * {@code
055 * {
056 *   "entity-type":"document",
057 *   ...
058 *   "contextParameters": {
059 *     "documentURL": "DOCUMENT_URL"
060 *   }
061 * }
062 * </pre>
063 * </p>
064 *
065 * @since 7.2
066 */
067@Setup(mode = SINGLETON, priority = REFERENCE)
068public class DocumentUrlJsonEnricher extends AbstractJsonEnricher<DocumentModel> {
069
070    public static final String NAME = "documentURL";
071
072    public static final String NOTIFICATION_DOCUMENT_ID_CODEC_NAME = "notificationDocId";
073
074    @Inject
075    private DocumentViewCodecManager viewCodecManager;
076
077    public DocumentUrlJsonEnricher() {
078        super(NAME);
079    }
080
081    @Override
082    public void write(JsonGenerator jg, DocumentModel document) throws IOException {
083        DocumentLocation docLoc = new DocumentLocationImpl(document);
084        String pCodecName = ctx.getParameter(CODEC_PARAMETER_NAME);
085        String codecName = isBlank(pCodecName) ? NOTIFICATION_DOCUMENT_ID_CODEC_NAME : pCodecName;
086        TypeInfo adapter = document.getAdapter(TypeInfo.class);
087        if (adapter == null) {
088            jg.writeNullField(NAME);
089            return;
090        }
091        DocumentView docView = new DocumentViewImpl(docLoc, adapter.getDefaultView());
092        String url = viewCodecManager.getUrlFromDocumentView(codecName, docView, false, null);
093        if (url == null) {
094            jg.writeNullField(NAME);
095            return;
096        }
097        jg.writeStringField(NAME, ctx.getBaseUrl() + url);
098    }
099
100}