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