001/* 002 * (C) Copyright 2013 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 * dmetzler 016 */ 017package org.nuxeo.ecm.automation.io.services.enricher; 018 019import java.io.IOException; 020import java.util.ArrayList; 021import java.util.Arrays; 022import java.util.List; 023import java.util.Map; 024import java.util.Map.Entry; 025import java.util.concurrent.ConcurrentHashMap; 026 027import javax.ws.rs.core.HttpHeaders; 028 029import org.apache.commons.logging.Log; 030import org.apache.commons.logging.LogFactory; 031import org.codehaus.jackson.JsonGenerationException; 032import org.codehaus.jackson.JsonGenerator; 033import org.jboss.el.ExpressionFactoryImpl; 034import org.nuxeo.common.utils.StringUtils; 035import org.nuxeo.ecm.core.api.CoreSession; 036import org.nuxeo.ecm.core.api.NuxeoPrincipal; 037import org.nuxeo.ecm.platform.actions.ActionContext; 038import org.nuxeo.ecm.platform.actions.ELActionContext; 039import org.nuxeo.ecm.platform.actions.ejb.ActionManager; 040import org.nuxeo.ecm.platform.el.ExpressionContext; 041import org.nuxeo.runtime.api.Framework; 042import org.nuxeo.runtime.model.ComponentInstance; 043import org.nuxeo.runtime.model.DefaultComponent; 044 045/** 046 * @since 5.7.3 047 * @deprecated The JSON marshalling was migrated to nuxeo-core-io. An enricher system is also available. See 048 * org.nuxeo.ecm.core.io.marshallers.json.enrichers.BreadcrumbJsonEnricher for an example. To migrate an 049 * existing enricher, keep the marshalling code and use it in class implementing 050 * AbstractJsonEnricher<DocumentModel> (the use of contextual parameters is a bit different but 051 * compatible / you have to manage the enricher's parameters yourself). Don't forget to contribute to 052 * service org.nuxeo.ecm.core.io.registry.MarshallerRegistry to register your enricher. 053 */ 054@Deprecated 055public class ContentEnricherServiceImpl extends DefaultComponent implements ContentEnricherService { 056 057 /** 058 * 059 */ 060 public static final String NXCONTENT_CATEGORY_HEADER = "X-NXContext-Category"; 061 062 protected static final Log log = LogFactory.getLog(ContentEnricherServiceImpl.class); 063 064 public static final String ENRICHER = "enricher"; 065 066 private Map<String, ContentEnricherDescriptor> descriptorRegistry = new ConcurrentHashMap<>(); 067 068 @Override 069 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 070 071 if (ENRICHER.equals(extensionPoint)) { 072 ContentEnricherDescriptor cd = (ContentEnricherDescriptor) contribution; 073 descriptorRegistry.put(cd.name, cd); 074 } 075 076 } 077 078 @Override 079 public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 080 if (ENRICHER.equals(extensionPoint)) { 081 ContentEnricherDescriptor cd = (ContentEnricherDescriptor) contribution; 082 if (descriptorRegistry.containsKey(cd.name)) { 083 descriptorRegistry.remove(cd.name); 084 } 085 } 086 } 087 088 @Override 089 public List<ContentEnricher> getEnrichers(String category, RestEvaluationContext context) { 090 List<ContentEnricher> result = new ArrayList<>(); 091 for (ContentEnricherDescriptor descriptor : getEnricherDescriptors(category, context)) { 092 093 ContentEnricher contentEnricher = descriptor.getContentEnricher(); 094 result.add(contentEnricher); 095 } 096 097 return result; 098 } 099 100 private List<ContentEnricherDescriptor> getEnricherDescriptors(String category, RestEvaluationContext context) { 101 List<ContentEnricherDescriptor> result = new ArrayList<>(); 102 for (Entry<String, ContentEnricherDescriptor> entry : descriptorRegistry.entrySet()) { 103 ContentEnricherDescriptor descriptor = entry.getValue(); 104 if (descriptor.categories.contains(category)) { 105 result.add(descriptor); 106 } 107 } 108 return result; 109 } 110 111 @Override 112 public void writeContext(JsonGenerator jg, RestEvaluationContext ec) throws JsonGenerationException, IOException { 113 114 for (String category : getCategoriesToActivate(ec)) { 115 for (ContentEnricherDescriptor descriptor : getEnricherDescriptors(category, ec)) { 116 if (evaluateFilter(ec, descriptor)) { 117 ContentEnricher enricher = descriptor.getContentEnricher(); 118 if (enricher != null) { 119 jg.writeFieldName(descriptor.name); 120 enricher.enrich(jg, ec); 121 } 122 } 123 } 124 } 125 126 } 127 128 /** 129 * @param ec 130 * @param descriptor 131 * @return 132 */ 133 private boolean evaluateFilter(RestEvaluationContext ec, ContentEnricherDescriptor descriptor) { 134 for (String filterId : descriptor.filterIds) { 135 ActionManager as = Framework.getLocalService(ActionManager.class); 136 if (!as.checkFilter(filterId, createActionContext(ec))) { 137 return false; 138 } 139 } 140 return true; 141 } 142 143 /** 144 * Creates an ActionService compatible ActionContext to evaluate filters 145 * 146 * @param ec 147 * @return 148 */ 149 private ActionContext createActionContext(RestEvaluationContext ec) { 150 ActionContext actionContext = new ELActionContext(new ExpressionContext(), new ExpressionFactoryImpl()); 151 CoreSession session = ec.getDocumentModel().getCoreSession(); 152 actionContext.setDocumentManager(session); 153 actionContext.setCurrentPrincipal((NuxeoPrincipal) session.getPrincipal()); 154 155 actionContext.setCurrentDocument(ec.getDocumentModel()); 156 157 return actionContext; 158 } 159 160 private List<String> getCategoriesToActivate(RestEvaluationContext ec) { 161 HttpHeaders headers = ec.getHeaders(); 162 if (headers != null) { 163 List<String> requestHeader = headers.getRequestHeader(NXCONTENT_CATEGORY_HEADER); 164 if (requestHeader != null && !requestHeader.isEmpty()) { 165 return Arrays.asList(StringUtils.split(requestHeader.get(0), ',', true)); 166 } else { 167 return new ArrayList<>(0); 168 } 169 } 170 return new ArrayList<>(0); 171 } 172 173}