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 <[email protected]> 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 * {@code 056 * { 057 * "entity-type":"document", 058 * ... 059 * "contextParameters": { 060 * "documentURL": "DOCUMENT_URL" 061 * } 062 * } 063 * </pre> 064 * </p> 065 * 066 * @since 7.2 067 */ 068@Setup(mode = SINGLETON, priority = REFERENCE) 069public class DocumentUrlJsonEnricher extends AbstractJsonEnricher<DocumentModel> { 070 071 public static final String NAME = "documentURL"; 072 073 public static final String NOTIFICATION_DOCUMENT_ID_CODEC_NAME = "notificationDocId"; 074 075 @Inject 076 private DocumentViewCodecManager viewCodecManager; 077 078 public DocumentUrlJsonEnricher() { 079 super(NAME); 080 } 081 082 @Override 083 public void write(JsonGenerator jg, DocumentModel document) throws IOException { 084 DocumentLocation docLoc = new DocumentLocationImpl(document); 085 String pCodecName = ctx.getParameter(CODEC_PARAMETER_NAME); 086 String codecName = isBlank(pCodecName) ? NOTIFICATION_DOCUMENT_ID_CODEC_NAME : pCodecName; 087 TypeInfo adapter = document.getAdapter(TypeInfo.class); 088 if (adapter == null) { 089 jg.writeNullField(NAME); 090 return; 091 } 092 DocumentView docView = new DocumentViewImpl(docLoc, adapter.getDefaultView()); 093 String url = viewCodecManager.getUrlFromDocumentView(codecName, docView, false, null); 094 if (url == null) { 095 jg.writeNullField(NAME); 096 return; 097 } 098 jg.writeStringField(NAME, ctx.getBaseUrl() + url); 099 } 100 101}