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.nuxeo.common.utils.URIUtils; 028import org.nuxeo.ecm.core.api.DocumentLocation; 029import org.nuxeo.ecm.core.api.DocumentRef; 030import org.nuxeo.ecm.core.api.IdRef; 031import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl; 032import org.nuxeo.ecm.platform.url.DocumentViewImpl; 033import org.nuxeo.ecm.platform.url.api.DocumentView; 034import org.nuxeo.ecm.platform.url.service.AbstractDocumentViewCodec; 035 036/** 037 * Codec handling a document repository, id, view and additional request parameters. 038 * 039 * @author Anahide Tchertchian 040 */ 041public class DocumentIdCodec extends AbstractDocumentViewCodec { 042 043 public static final String PREFIX = "nxdoc"; 044 045 // nxdoc/server/docId/view_id/?requestParams 046 public static final String URLPattern = "/(\\w+)/([a-zA-Z_0-9\\-]+)(/([a-zA-Z_0-9\\-\\.;=]*))?(/)?(\\?(.*)?)?"; 047 048 public DocumentIdCodec() { 049 } 050 051 public DocumentIdCodec(String prefix) { 052 } 053 054 @Override 055 public String getPrefix() { 056 if (prefix != null) { 057 return prefix; 058 } 059 return PREFIX; 060 } 061 062 @Override 063 public String getUrlFromDocumentView(DocumentView docView) { 064 DocumentLocation docLoc = docView.getDocumentLocation(); 065 if (docLoc != null) { 066 List<String> items = new ArrayList<>(); 067 items.add(getPrefix()); 068 items.add(docLoc.getServerName()); 069 IdRef docRef = docLoc.getIdRef(); 070 if (docRef == null) { 071 return null; 072 } 073 items.add(docRef.toString()); 074 String viewId = docView.getViewId(); 075 if (viewId != null) { 076 items.add(viewId); 077 } 078 String uri = String.join("/", items); 079 return URIUtils.addParametersToURIQuery(uri, docView.getParameters()); 080 } 081 return null; 082 } 083 084 /** 085 * Extracts document location from a Zope-like URL ie: server/path_or_docId/view_id/tab_id . 086 */ 087 @Override 088 public DocumentView getDocumentViewFromUrl(String url) { 089 final Pattern pattern = Pattern.compile(getPrefix() + URLPattern); 090 Matcher m = pattern.matcher(url); 091 if (m.matches()) { 092 if (m.groupCount() >= 4) { 093 094 // for debug 095 // for (int i = 1; i < m.groupCount() + 1; i++) { 096 // System.err.println(i + ": " + m.group(i)); 097 // } 098 099 final String server = m.group(1); 100 String uuid = m.group(2); 101 final DocumentRef docRef = new IdRef(uuid); 102 String viewId = m.group(4); 103 if (viewId != null) { 104 int jsessionidIndex = viewId.indexOf(";jsessionid"); 105 if (jsessionidIndex != -1) { 106 viewId = viewId.substring(0, jsessionidIndex); 107 } 108 } 109 110 // get other parameters 111 112 Map<String, String> params = null; 113 if (m.groupCount() > 6) { 114 String query = m.group(7); 115 params = URIUtils.getRequestParameters(query); 116 } 117 118 final DocumentLocation docLoc = new DocumentLocationImpl(server, docRef); 119 120 return new DocumentViewImpl(docLoc, viewId, params); 121 } 122 } 123 124 return null; 125 } 126 127}