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