001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nicolas Chapurlat <nchapurlat@nuxeo.com> 016 */ 017 018package org.nuxeo.ecm.platform.url.io; 019 020import static org.apache.commons.lang.StringUtils.isBlank; 021import static org.nuxeo.ecm.automation.core.util.PaginableDocumentModelList.CODEC_PARAMETER_NAME; 022import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON; 023import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE; 024 025import java.io.IOException; 026 027import javax.inject.Inject; 028 029import org.codehaus.jackson.JsonGenerator; 030import org.nuxeo.ecm.core.api.DocumentLocation; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl; 033import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher; 034import org.nuxeo.ecm.core.io.registry.reflect.Setup; 035import org.nuxeo.ecm.platform.types.adapter.TypeInfo; 036import org.nuxeo.ecm.platform.url.DocumentViewImpl; 037import org.nuxeo.ecm.platform.url.api.DocumentView; 038import org.nuxeo.ecm.platform.url.api.DocumentViewCodecManager; 039 040/** 041 * Enrich {@link DocumentModel} Json. 042 * <p> 043 * Add {@link DocumentModel}'s document url as json attachment. 044 * </p> 045 * <p> 046 * Enable if parameter enrichers.document=documentURL is present. 047 * </p> 048 * <p> 049 * Format is: 050 * 051 * <pre> 052 * {@code 053 * { 054 * "entity-type":"document", 055 * ... 056 * "contextParameters": { 057 * "documentURL": "DOCUMENT_URL" 058 * } 059 * } 060 * </pre> 061 * 062 * </p> 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 @Inject 072 private DocumentViewCodecManager viewCodecManager; 073 074 public DocumentUrlJsonEnricher() { 075 super(NAME); 076 } 077 078 @Override 079 public void write(JsonGenerator jg, DocumentModel document) throws IOException { 080 DocumentLocation docLoc = new DocumentLocationImpl(document); 081 String pCodecName = ctx.getParameter(CODEC_PARAMETER_NAME); 082 String codecName = isBlank(pCodecName) ? viewCodecManager.getDefaultCodecName() : pCodecName; 083 TypeInfo adapter = document.getAdapter(TypeInfo.class); 084 if (adapter == null) { 085 jg.writeNullField(NAME); 086 return; 087 } 088 DocumentView docView = new DocumentViewImpl(docLoc, adapter.getDefaultView()); 089 String url = ctx.getBaseUrl() + viewCodecManager.getUrlFromDocumentView(codecName, docView, false, null); 090 jg.writeStringField(NAME, url); 091 } 092 093}