001/*
002 * (C) Copyright 2006-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 *     Nuxeo - initial API and implementation
018 */
019package org.nuxeo.ecm.platform.url.codec;
020
021import java.util.ArrayList;
022import java.util.List;
023import java.util.Map;
024import java.util.regex.Matcher;
025import java.util.regex.Pattern;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.common.utils.URIUtils;
030import org.nuxeo.ecm.core.api.DocumentLocation;
031import org.nuxeo.ecm.core.api.DocumentRef;
032import org.nuxeo.ecm.core.api.PathRef;
033import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
034import org.nuxeo.ecm.platform.url.DocumentViewImpl;
035import org.nuxeo.ecm.platform.url.api.DocumentView;
036import org.nuxeo.ecm.platform.url.service.AbstractDocumentViewCodec;
037
038/**
039 * Codec handling a document repository, path, view and additional request parameters.
040 *
041 * @author Anahide Tchertchian
042 */
043public class DocumentPathCodec extends AbstractDocumentViewCodec {
044
045    // The maximum length of an url for Internet Explorer.
046    public static final int URL_MAX_LENGTH = 2000;
047
048    public static final String PREFIX = "nxpath";
049
050    // nxpath/server/path/to/doc@view_id?requestParams
051    public static final String URL_PATTERN = "/" // slash
052            + "([\\w\\.]+)" // server name (group 1)
053            + "(?:/(.*))?" // path (group 2) (optional)
054            + "@([\\w\\-\\.;=]+)" // view id (group 3)
055            + "/?" // final slash (optional)
056            + "(?:\\?(.*)?)?"; // query (group 4) (optional)
057
058    private static final Log log = LogFactory.getLog(DocumentPathCodec.class);
059
060    public DocumentPathCodec() {
061    }
062
063    public DocumentPathCodec(String prefix) {
064    }
065
066    @Override
067    public String getPrefix() {
068        if (prefix != null) {
069            return prefix;
070        }
071        return PREFIX;
072    }
073
074    @Override
075    public String getUrlFromDocumentView(DocumentView docView) {
076        // Use DocumentIdCodec if the document is a version
077        if ("true".equals(docView.getParameter("version"))) {
078            if (docView.getDocumentLocation().getIdRef() != null) {
079                DocumentIdCodec idCodec = new DocumentIdCodec();
080                return idCodec.getUrlFromDocumentView(docView);
081            }
082        }
083
084        DocumentLocation docLoc = docView.getDocumentLocation();
085        if (docLoc != null) {
086            List<String> items = new ArrayList<>();
087            items.add(getPrefix());
088            items.add(docLoc.getServerName());
089            PathRef docRef = docLoc.getPathRef();
090
091            if (docRef != null) {
092                // this is a path, get rid of leading slash
093                String path = docRef.toString();
094                if (path.startsWith("/")) {
095                    path = path.substring(1);
096                }
097                if (path.length() > 0) {
098                    items.add(URIUtils.quoteURIPathComponent(path, false));
099                }
100            }
101
102            String uri = String.join("/", items);
103            String viewId = docView.getViewId();
104            if (viewId != null) {
105                uri += "@" + viewId;
106            }
107
108            String uriWithParam = URIUtils.addParametersToURIQuery(uri, docView.getParameters());
109
110            // If the URL with the Path codec is to long, it use the URL with
111            // the Id Codec.
112            if (uriWithParam.length() > URL_MAX_LENGTH) {
113
114                // If the DocumentLocation did not contains the document Id, it
115                // use the Path Codec even if the Url is too long for IE.
116                if (null == docView.getDocumentLocation().getIdRef()) {
117                    log.error("The DocumentLocation did not contains the RefId.");
118                    return uriWithParam;
119                }
120
121                DocumentIdCodec idCodec = new DocumentIdCodec();
122                return idCodec.getUrlFromDocumentView(docView);
123
124            } else {
125                return uriWithParam;
126            }
127        }
128        return null;
129    }
130
131    /**
132     * Extracts document location from a Zope-like URL, eg: server/path_or_docId/view_id/tab_id .
133     */
134    @Override
135    public DocumentView getDocumentViewFromUrl(String url) {
136        final Pattern pattern = Pattern.compile(getPrefix() + URL_PATTERN);
137        Matcher m = pattern.matcher(url);
138        if (m.matches()) {
139
140            final String server = m.group(1);
141            String path = m.group(2);
142            if (path != null) {
143                // add leading slash to make it absolute if it's not the root
144                path = "/" + URIUtils.unquoteURIPathComponent(path);
145            }
146            final DocumentRef docRef = path != null ? new PathRef(path) : null;
147            String viewId = m.group(3);
148            int jsessionidIndex = viewId.indexOf(";jsessionid");
149            if (jsessionidIndex != -1) {
150                viewId = viewId.substring(0, jsessionidIndex);
151            }
152
153            // get other parameters
154            String query = m.group(4);
155            Map<String, String> params = URIUtils.getRequestParameters(query);
156
157            final DocumentLocation docLoc = new DocumentLocationImpl(server, docRef);
158
159            return new DocumentViewImpl(docLoc, viewId, params);
160        }
161
162        return null;
163    }
164
165}