001/* 002 * (C) Copyright 2006-2008 Nuxeo SAS (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.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 * troger 016 * 017 * $Id$ 018 */ 019 020package org.nuxeo.ecm.platform.annotations.gwt.server.configuration; 021 022import java.io.IOException; 023import java.util.HashMap; 024import java.util.List; 025import java.util.Map; 026 027import javax.servlet.ServletException; 028import javax.servlet.http.HttpServletRequest; 029import javax.servlet.http.HttpServletResponse; 030 031import org.apache.commons.logging.Log; 032import org.apache.commons.logging.LogFactory; 033import org.nuxeo.ecm.core.api.CoreInstance; 034import org.nuxeo.ecm.core.api.CoreSession; 035import org.nuxeo.ecm.core.api.DocumentLocation; 036import org.nuxeo.ecm.core.api.DocumentModel; 037import org.nuxeo.ecm.core.api.DocumentNotFoundException; 038import org.nuxeo.ecm.core.api.NuxeoPrincipal; 039import org.nuxeo.ecm.platform.annotations.configuration.service.FilterDescriptor; 040import org.nuxeo.ecm.platform.annotations.configuration.service.WebAnnotationConfigurationService; 041import org.nuxeo.ecm.platform.annotations.configuration.service.WebAnnotationDefinitionDescriptor; 042import org.nuxeo.ecm.platform.annotations.configuration.service.WebAnnotationFieldDescriptor; 043import org.nuxeo.ecm.platform.annotations.gwt.client.configuration.AnnotationDefinition; 044import org.nuxeo.ecm.platform.annotations.gwt.client.configuration.WebConfiguration; 045import org.nuxeo.ecm.platform.annotations.gwt.client.configuration.WebConfigurationService; 046import org.nuxeo.ecm.platform.url.api.DocumentView; 047import org.nuxeo.ecm.platform.url.api.DocumentViewCodecManager; 048import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper; 049import org.nuxeo.runtime.api.Framework; 050 051import com.google.gwt.user.server.rpc.RemoteServiceServlet; 052 053/** 054 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 055 */ 056public class WebConfigurationServiceImpl extends RemoteServiceServlet implements WebConfigurationService { 057 058 private static final long serialVersionUID = 2389527283775608787L; 059 060 private static final Log log = LogFactory.getLog(WebConfigurationServiceImpl.class); 061 062 private WebAnnotationConfigurationService webAnnotationConfigurationService; 063 064 protected DocumentViewCodecManager documentViewCodecManager; 065 066 private NuxeoPrincipal currentUser; 067 068 protected WebAnnotationConfigurationService getConfig() { 069 if (webAnnotationConfigurationService == null) { 070 webAnnotationConfigurationService = Framework.getService(WebAnnotationConfigurationService.class); 071 } 072 return webAnnotationConfigurationService; 073 } 074 075 public WebConfiguration getWebConfiguration(String url) { 076 WebConfiguration conf = new WebConfiguration(); 077 078 List<WebAnnotationDefinitionDescriptor> types = getConfig().getEnabledWebAnnotationDefinitions(); 079 080 for (WebAnnotationDefinitionDescriptor type : types) { 081 Map<String, String[]> fields = new HashMap<String, String[]>(); 082 for (WebAnnotationFieldDescriptor field : type.getFields()) { 083 fields.put(field.getName(), field.getChoices()); 084 } 085 086 conf.addAnnotationDefinition(new AnnotationDefinition(type.getUri(), type.getName(), type.getIcon(), 087 type.getType(), type.getListIcon(), type.getCreateIcon(), type.isInMenu(), fields)); 088 } 089 090 UserInfoMapper userInfoMapper = getConfig().getUserInfoMapper(); 091 if (userInfoMapper != null) { 092 conf.setUserInfo(userInfoMapper.getUserInfo(currentUser)); 093 } 094 095 WebPermission webPermission = getConfig().getWebPermission(); 096 if (webPermission != null) { 097 conf.setCanAnnotate(canAnnotate(url, webPermission)); 098 } 099 100 Map<String, FilterDescriptor> filters = getConfig().getFilterDefinitions(); 101 for (FilterDescriptor filter : filters.values()) { 102 conf.addFilter(filter.getOrder(), filter.getName(), filter.getIcon(), filter.getType(), filter.getAuthor(), 103 filter.getFields()); 104 } 105 106 conf.setDisplayedFields(getConfig().getDisplayedFields()); 107 conf.setFieldLabels(getConfig().getFieldLabels()); 108 return conf; 109 } 110 111 @Override 112 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, 113 IOException { 114 currentUser = (NuxeoPrincipal) request.getUserPrincipal(); 115 super.service(request, response); 116 } 117 118 protected boolean canAnnotate(String url, WebPermission webPermission) { 119 DocumentViewCodecManager documentViewCodecManager = getDocumentViewCodecManager(); 120 DocumentView docView = documentViewCodecManager.getDocumentViewFromUrl(url, true, getBaseUrl(url)); 121 DocumentLocation docLocation = docView.getDocumentLocation(); 122 try (CoreSession coreSession = CoreInstance.openCoreSession(docLocation.getServerName())) { 123 DocumentModel docModel = coreSession.getDocument(docLocation.getDocRef()); 124 return webPermission.canAnnotate(docModel); 125 } catch (DocumentNotFoundException e) { 126 log.error("Unable to get Document: " + docLocation.getDocRef(), e); 127 } 128 return true; // if any error, default to authorize annotations 129 } 130 131 protected DocumentViewCodecManager getDocumentViewCodecManager() { 132 if (documentViewCodecManager == null) { 133 documentViewCodecManager = Framework.getService(DocumentViewCodecManager.class); 134 } 135 return documentViewCodecManager; 136 } 137 138 protected String getBaseUrl(String url) { 139 String nxUrl = VirtualHostHelper.getContextPathProperty() + "/"; 140 return url.substring(0, url.lastIndexOf(nxUrl) + nxUrl.length()); 141 } 142 143}