001/* 002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id: DocumentIdCodec.java 22535 2007-07-13 14:57:58Z atchertchian $ 018 */ 019 020package org.nuxeo.ecm.platform.url.codec; 021 022import java.util.ArrayList; 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.DocumentRef; 032import org.nuxeo.ecm.core.api.IdRef; 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, id, view and additional request parameters. 040 * 041 * @author Anahide Tchertchian 042 */ 043public class DocumentIdCodec extends AbstractDocumentViewCodec { 044 045 public static final String PREFIX = "nxdoc"; 046 047 // nxdoc/server/docId/view_id/?requestParams 048 public static final String URLPattern = "/(\\w+)/([a-zA-Z_0-9\\-]+)(/([a-zA-Z_0-9\\-\\.;=]*))?(/)?(\\?(.*)?)?"; 049 050 public DocumentIdCodec() { 051 } 052 053 public DocumentIdCodec(String prefix) { 054 } 055 056 @Override 057 public String getPrefix() { 058 if (prefix != null) { 059 return prefix; 060 } 061 return PREFIX; 062 } 063 064 public String getUrlFromDocumentView(DocumentView docView) { 065 DocumentLocation docLoc = docView.getDocumentLocation(); 066 if (docLoc != null) { 067 List<String> items = new ArrayList<String>(); 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 = StringUtils.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 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}