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