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