001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.services;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newList;
018: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
019:
020: import java.util.Collections;
021: import java.util.Comparator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.apache.tapestry.TapestryConstants;
026: import org.apache.tapestry.services.ClasspathAssetAliasManager;
027: import org.apache.tapestry.services.Request;
028:
029: public class ClasspathAssetAliasManagerImpl implements
030: ClasspathAssetAliasManager {
031: private final Request _request;
032:
033: /** Map from alias to path. */
034: private final Map<String, String> _aliasToPathPrefix;
035:
036: /** Map from path to alias. */
037: private final Map<String, String> _pathPrefixToAlias = newMap();
038:
039: private final List<String> _sortedAliases;
040:
041: private final List<String> _sortedPathPrefixes;
042:
043: /**
044: * Configuration is a map of aliases (short names) to complete names. Keys and values should not
045: * start with a slash, but should end with one. Example: "tapestry/" --> "org/apache/tapestry/".
046: */
047: public ClasspathAssetAliasManagerImpl(Request request,
048:
049: final Map<String, String> configuration) {
050: _request = request;
051:
052: _aliasToPathPrefix = configuration;
053:
054: for (Map.Entry<String, String> e : _aliasToPathPrefix
055: .entrySet()) {
056: _pathPrefixToAlias.put(e.getValue(), e.getKey());
057: }
058:
059: Comparator<String> sortDescendingByLength = new Comparator<String>() {
060: public int compare(String o1, String o2) {
061: return o2.length() - o1.length();
062: }
063: };
064:
065: _sortedAliases = newList(_aliasToPathPrefix.keySet());
066: Collections.sort(_sortedAliases, sortDescendingByLength);
067:
068: _sortedPathPrefixes = newList(_aliasToPathPrefix.values());
069: Collections.sort(_sortedPathPrefixes, sortDescendingByLength);
070: }
071:
072: public String toClientURL(String resourcePath) {
073: StringBuilder builder = new StringBuilder(_request
074: .getContextPath());
075: builder.append(TapestryConstants.ASSET_PATH_PREFIX);
076:
077: for (String pathPrefix : _sortedPathPrefixes) {
078: if (resourcePath.startsWith(pathPrefix)) {
079: String alias = _pathPrefixToAlias.get(pathPrefix);
080: builder.append(alias);
081: builder.append(resourcePath.substring(pathPrefix
082: .length()));
083:
084: return builder.toString();
085: }
086: }
087:
088: // No alias available as a prefix (kind of unlikely, but whatever).
089:
090: builder.append(resourcePath);
091:
092: return builder.toString();
093: }
094:
095: public String toResourcePath(String clientURL) {
096: String basePath = clientURL
097: .substring(TapestryConstants.ASSET_PATH_PREFIX.length());
098:
099: for (String alias : _sortedAliases) {
100: if (basePath.startsWith(alias)) {
101: return _aliasToPathPrefix.get(alias)
102: + basePath.substring(alias.length());
103: }
104: }
105:
106: return basePath;
107: }
108:
109: }
|