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 * 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 @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) ? viewCodecManager.getDefaultCodecName() : 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 = ctx.getBaseUrl() + viewCodecManager.getUrlFromDocumentView(codecName, docView, false, null); 092 jg.writeStringField(NAME, url); 093 } 094 095}