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