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.CloseableCoreSession;
036import org.nuxeo.ecm.core.api.CoreInstance;
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 = 1L;
061
062    private static final Log log = LogFactory.getLog(WebConfigurationServiceImpl.class);
063
064    private NuxeoPrincipal currentUser;
065
066    @Override
067    public WebConfiguration getWebConfiguration(String url) {
068        WebAnnotationConfigurationService config = Framework.getService(WebAnnotationConfigurationService.class);
069        WebConfiguration conf = new WebConfiguration();
070
071        List<WebAnnotationDefinitionDescriptor> types = config.getEnabledWebAnnotationDefinitions();
072
073        for (WebAnnotationDefinitionDescriptor type : types) {
074            Map<String, String[]> fields = new HashMap<String, String[]>();
075            for (WebAnnotationFieldDescriptor field : type.getFields()) {
076                fields.put(field.getName(), field.getChoices());
077            }
078
079            conf.addAnnotationDefinition(new AnnotationDefinition(type.getUri(), type.getName(), type.getIcon(),
080                    type.getType(), type.getListIcon(), type.getCreateIcon(), type.isInMenu(), fields));
081        }
082
083        UserInfoMapper userInfoMapper = config.getUserInfoMapper();
084        if (userInfoMapper != null) {
085            conf.setUserInfo(userInfoMapper.getUserInfo(currentUser));
086        }
087
088        WebPermission webPermission = config.getWebPermission();
089        if (webPermission != null) {
090            conf.setCanAnnotate(canAnnotate(url, webPermission));
091        }
092
093        Map<String, FilterDescriptor> filters = config.getFilterDefinitions();
094        for (FilterDescriptor filter : filters.values()) {
095            conf.addFilter(filter.getOrder(), filter.getName(), filter.getIcon(), filter.getType(), filter.getAuthor(),
096                    filter.getFields());
097        }
098
099        conf.setDisplayedFields(config.getDisplayedFields());
100        conf.setFieldLabels(config.getFieldLabels());
101        return conf;
102    }
103
104    @Override
105    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,
106            IOException {
107        currentUser = (NuxeoPrincipal) request.getUserPrincipal();
108        super.service(request, response);
109    }
110
111    protected boolean canAnnotate(String url, WebPermission webPermission) {
112        DocumentViewCodecManager documentViewCodecManager = Framework.getService(DocumentViewCodecManager.class);
113        DocumentView docView = documentViewCodecManager.getDocumentViewFromUrl(url, true, getBaseUrl(url));
114        DocumentLocation docLocation = docView.getDocumentLocation();
115        try (CloseableCoreSession coreSession = CoreInstance.openCoreSession(docLocation.getServerName())) {
116            DocumentModel docModel = coreSession.getDocument(docLocation.getDocRef());
117            return webPermission.canAnnotate(docModel);
118        } catch (DocumentNotFoundException e) {
119            log.error("Unable to get Document: " + docLocation.getDocRef(), e);
120        }
121        return true; // if any error, default to authorize annotations
122    }
123
124    protected String getBaseUrl(String url) {
125        String nxUrl = VirtualHostHelper.getContextPathProperty() + "/";
126        return url.substring(0, url.lastIndexOf(nxUrl) + nxUrl.length());
127    }
128
129}