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