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