001/*
002 * (C) Copyright 2014 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     * @param url
057     * @param defaultTab
058     * @param paramIdName
059     * @param paramShowName
060     * @return
061     */
062    public DocumentView getDocumentViewFromUrl(String url, String defaultTab, String paramIdName, String paramShowName) {
063        ConfigurationService cs = Framework.getService(ConfigurationService.class);
064        String allowedCharsRegex = cs.getProperty(ALLOWED_CHARACTERS_REGEX);
065        String userGroupNameRegex = String.format("(%s)?", allowedCharsRegex);
066
067        // prefix/groupname/view_id?requestParams
068        String url_pattern = "/" // slash
069            + userGroupNameRegex // username/groupname (group 1)
070            + "(/([a-zA-Z_0-9\\-\\.]*))?" // view id (group 3) (optional)
071            + "/?" // final slash (optional)
072            + "(\\?((.*)?))?"; // query (group 5) (optional)
073
074        Pattern pattern = Pattern.compile(getPrefix() + url_pattern);
075        try {
076            url = URLDecoder.decode(url, "UTF-8");
077        } catch (UnsupportedEncodingException e) {
078            throw new NuxeoException("Unable to decode the requested url", e);
079        }
080        Matcher m = pattern.matcher(url);
081        if (m.matches()) {
082            if (m.groupCount() >= 1) {
083                String id = m.group(1);
084
085                String viewId = m.group(3);
086                if (viewId == null || "".equals(viewId)) {
087                    viewId = DEFAULT_VIEW_ID;
088                }
089
090                String query = m.group(5);
091                Map<String, String> params = URIUtils.getRequestParameters(query);
092                if (params == null) {
093                    params = new HashMap<String, String>();
094                }
095
096                params.put(paramIdName, id);
097                params.put(paramShowName, "true");
098
099                if (!params.containsKey("tabIds")) {
100                    params.put("tabIds", defaultTab);
101                }
102
103                final DocumentLocation docLoc = new DocumentLocationImpl(getDefaultRepositoryName(), null);
104                return new DocumentViewImpl(docLoc, viewId, params);
105            }
106        }
107        return null;
108    }
109
110    /**
111     * Get the url from a DocumentView for a user or a group.
112     *
113     * @param docView
114     * @param id
115     * @return
116     */
117    public String getUrlFromDocumentViewAndID(DocumentView docView, String paramName) {
118        String id = docView.getParameter(paramName);
119        if (id != null) {
120            docView.removeParameter(paramName);
121            List<String> items = new ArrayList<String>();
122            items.add(getPrefix());
123            items.add(URIUtils.quoteURIPathComponent(id, true, false));
124            String viewId = docView.getViewId();
125            if (viewId != null) {
126                items.add(viewId);
127            }
128            String uri = StringUtils.join(items, "/");
129            Map<String, String> parameters = docView.getParameters();
130            if (parameters == null) {
131                parameters = new HashMap<String, String>();
132            }
133            return URIUtils.addParametersToURIQuery(uri, parameters);
134        }
135        return null;
136    }
137
138    protected String getDefaultRepositoryName() {
139        RepositoryManager repositoryManager = Framework.getService(RepositoryManager.class);
140        return repositoryManager == null ? null : repositoryManager.getDefaultRepositoryName();
141    }
142}