001/* 002 * (C) Copyright 2012 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 * Anahide Tchertchian 018 */ 019package org.nuxeo.ecm.styleguide; 020 021import java.util.ArrayList; 022import java.util.HashMap; 023import java.util.List; 024import java.util.Map; 025import java.util.regex.Matcher; 026import java.util.regex.Pattern; 027 028import org.apache.commons.lang.StringUtils; 029import org.nuxeo.common.utils.URIUtils; 030import org.nuxeo.ecm.core.api.DocumentLocation; 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 * Provides clean urls without conversation id to handle navigation 038 * 039 * @since 5.7 040 */ 041public class StyleGuideURLCodec extends AbstractDocumentViewCodec { 042 043 public static final String DEFAULT_VIEW_ID = "style_guide"; 044 045 // prefix/menu_id/(tab/subtab)?requestParams 046 public static final String GET_URL_PATTERN = "/" // slash 047 + "([a-zA-Z_0-9\\-\\.]*)?" // menu tab id (group 1) 048 + "(/([a-zA-Z_0-9\\-]*))?" // tab id (group 3) (optional) 049 + "(/([a-zA-Z_0-9\\-]*))?" // sub tab id (group 4) (optional) 050 + "(/)?" // final slash (optional) 051 + "(\\?(.*)?)?"; // query (group 8) (optional) 052 053 // prefix/outcome/in/several/parts/template.faces?requestParams 054 public static final String POST_URL_PATTERN = "/style_guide.faces(/)?(\\?(.*)?)?"; 055 056 public String getUrlFromDocumentView(DocumentView docView) { 057 DocumentLocation docLoc = docView.getDocumentLocation(); 058 if (docLoc != null) { 059 List<String> items = new ArrayList<String>(); 060 items.add(getPrefix()); 061 String viewId = docView.getViewId(); 062 if (viewId != null) { 063 items.add(viewId); 064 } else { 065 items.add(DEFAULT_VIEW_ID); 066 } 067 Map<String, String> docViewParams = docView.getParameters(); 068 Map<String, String> params = new HashMap<String, String>(); 069 if (docViewParams != null) { 070 params.putAll(docViewParams); 071 params.remove("conversationId"); 072 if (params.containsKey("tabId")) { 073 items.add(params.get("tabId")); 074 params.remove("tabId"); 075 if (params.containsKey("subTabId")) { 076 items.add(params.get("subTabId")); 077 params.remove("subTabId"); 078 } 079 } 080 } 081 String uri = StringUtils.join(items, "/"); 082 return URIUtils.addParametersToURIQuery(uri, params); 083 } 084 return null; 085 } 086 087 /** 088 * Extracts view id and parameters, for both get and post methods 089 */ 090 public DocumentView getDocumentViewFromUrl(String url) { 091 // strip url of any jsessionid param 092 int jsessionidIndex = url.indexOf(";jsessionid"); 093 if (jsessionidIndex != -1) { 094 url = url.substring(0, jsessionidIndex); 095 } 096 097 // try POST pattern -> FIXME this is not enough information to restore 098 // a tab 099 Pattern pattern = Pattern.compile(getPrefix() + POST_URL_PATTERN); 100 Matcher m = pattern.matcher(url); 101 if (m.matches()) { 102 if (m.groupCount() >= 1) { 103 104 // for debug 105 // for (int i = 1; i < m.groupCount() + 1; i++) { 106 // System.err.println(i + ": " + m.group(i)); 107 // } 108 109 // get other parameters 110 111 Map<String, String> params = null; 112 113 if (m.groupCount() > 4) { 114 String query = m.group(5); 115 params = URIUtils.getRequestParameters(query); 116 if (params != null) { 117 params.remove("conversationId"); 118 } 119 } 120 121 final DocumentLocation docLoc = new DocumentLocationImpl(null, null); 122 123 return new DocumentViewImpl(docLoc, null, params); 124 } 125 } 126 // try GET pattern 127 pattern = Pattern.compile(getPrefix() + GET_URL_PATTERN); 128 m = pattern.matcher(url); 129 if (m.matches()) { 130 if (m.groupCount() >= 1) { 131 132 // for debug 133 // for (int i = 1; i < m.groupCount() + 1; i++) { 134 // System.err.println(i + ": " + m.group(i)); 135 // } 136 137 Map<String, String> params = new HashMap<String, String>(); 138 String menuId = m.group(1); 139 if (menuId != null && !"".equals(menuId)) { 140 params.put("menuId", menuId); 141 } 142 143 String tabId = m.group(3); 144 if (tabId != null && !"".equals(tabId)) { 145 params.put("tabId", tabId); 146 } 147 148 String subTabId = m.group(5); 149 if (subTabId != null && !"".equals(subTabId)) { 150 params.put("subTabId", subTabId); 151 } 152 153 // get other parameters 154 155 if (m.groupCount() > 3) { 156 String query = m.group(8); 157 Map<String, String> queryParams = URIUtils.getRequestParameters(query); 158 if (queryParams != null) { 159 queryParams.remove("conversationId"); 160 params.putAll(queryParams); 161 } 162 } 163 164 final DocumentLocation docLoc = new DocumentLocationImpl(null, null); 165 166 return new DocumentViewImpl(docLoc, DEFAULT_VIEW_ID, params); 167 } 168 } 169 170 return null; 171 } 172 173}