001/*
002 * (C) Copyright 2014-2016 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 *     <a href="mailto:glefevre@nuxeo.com">Gildas</a>
018 */
019package org.nuxeo.ecm.user.center;
020
021import java.io.UnsupportedEncodingException;
022import java.net.URLDecoder;
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027import java.util.regex.Matcher;
028import java.util.regex.Pattern;
029
030import org.nuxeo.common.utils.StringUtils;
031import org.nuxeo.common.utils.URIUtils;
032import org.nuxeo.ecm.core.api.DocumentLocation;
033import org.nuxeo.ecm.core.api.NuxeoException;
034import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
035import org.nuxeo.ecm.core.api.repository.RepositoryManager;
036import org.nuxeo.ecm.platform.url.DocumentViewImpl;
037import org.nuxeo.ecm.platform.url.api.DocumentView;
038import org.nuxeo.ecm.platform.url.service.AbstractDocumentViewCodec;
039import org.nuxeo.runtime.api.Framework;
040import org.nuxeo.runtime.services.config.ConfigurationService;
041
042/**
043 * Abstract class for the User and Group codec.
044 *
045 * @since 6.0
046 */
047public abstract class AbstractUserGroupCodec extends AbstractDocumentViewCodec {
048
049    public static final String DEFAULT_VIEW_ID = "view_home";
050
051    public static final String ALLOWED_CHARACTERS_REGEX = "nuxeo.codec.usergroup.allowedCharacters";
052
053    /**
054     * Get the DocumentView for a user or a group from a URL.
055     */
056    public DocumentView getDocumentViewFromUrl(String url, String defaultTab, String paramIdName, String paramShowName) {
057        ConfigurationService cs = Framework.getService(ConfigurationService.class);
058        String allowedCharsRegex = cs.getProperty(ALLOWED_CHARACTERS_REGEX);
059        String userGroupNameRegex = String.format("(%s)?", allowedCharsRegex);
060
061        // prefix/groupname/view_id?requestParams
062        String url_pattern = "/" // slash
063            + userGroupNameRegex // username/groupname (group 1)
064            + "(/([a-zA-Z_0-9\\-\\.]*))?" // view id (group 3) (optional)
065            + "/?" // final slash (optional)
066            + "(\\?((.*)?))?"; // query (group 5) (optional)
067
068        Pattern pattern = Pattern.compile(getPrefix() + url_pattern);
069        try {
070            url = URLDecoder.decode(url, "UTF-8");
071        } catch (UnsupportedEncodingException e) {
072            throw new NuxeoException("Unable to decode the requested url", e);
073        }
074        Matcher m = pattern.matcher(url);
075        if (m.matches()) {
076            if (m.groupCount() >= 1) {
077                String id = m.group(1);
078
079                String viewId = m.group(3);
080                if (viewId == null || "".equals(viewId)) {
081                    viewId = DEFAULT_VIEW_ID;
082                }
083
084                String query = m.group(5);
085                Map<String, String> params = URIUtils.getRequestParameters(query);
086                if (params == null) {
087                    params = new HashMap<>();
088                }
089
090                params.put(paramIdName, id);
091                params.put(paramShowName, "true");
092
093                if (!params.containsKey("tabIds")) {
094                    params.put("tabIds", defaultTab);
095                }
096
097                final DocumentLocation docLoc = new DocumentLocationImpl(getDefaultRepositoryName(), null);
098                return new DocumentViewImpl(docLoc, viewId, params);
099            }
100        }
101        return null;
102    }
103
104    /**
105     * Get the url from a DocumentView for a user or a group.
106     */
107    public String getUrlFromDocumentViewAndID(DocumentView docView, String paramName) {
108        String id = docView.getParameter(paramName);
109        if (id != null) {
110            docView.removeParameter(paramName);
111            List<String> items = new ArrayList<>();
112            items.add(getPrefix());
113            items.add(URIUtils.quoteURIPathComponent(id, true, false));
114            String viewId = docView.getViewId();
115            if (viewId != null) {
116                items.add(viewId);
117            }
118            String uri = String.join("/", items);
119            Map<String, String> parameters = docView.getParameters();
120            if (parameters == null) {
121                parameters = new HashMap<>();
122            }
123            return URIUtils.addParametersToURIQuery(uri, parameters);
124        }
125        return null;
126    }
127
128    protected String getDefaultRepositoryName() {
129        RepositoryManager repositoryManager = Framework.getService(RepositoryManager.class);
130        return repositoryManager == null ? null : repositoryManager.getDefaultRepositoryName();
131    }
132}