1 package org.apache.maven.archiva.web.mapper;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.opensymphony.webwork.dispatcher.mapper.ActionMapping;
23 import com.opensymphony.webwork.dispatcher.mapper.DefaultActionMapper;
25 import org.apache.commons.lang.StringUtils;
27 import java.util.HashMap;
30 import javax.servlet.http.HttpServletRequest;
33 * Map alternate URLs to specific actions. Used for the repository browser and the proxy.
35 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
37 public class RepositoryActionMapper
38 extends DefaultActionMapper
40 private static final String ACTION_BROWSE = "browse";
42 private static final String ACTION_BROWSE_ARTIFACT = "browseArtifact";
44 private static final String ACTION_BROWSE_GROUP = "browseGroup";
46 private static final String ACTION_SHOW_ARTIFACT = "showArtifact";
48 private static final String ACTION_SHOW_ARTIFACT_DEPENDEES = "showArtifactDependees";
50 private static final String ACTION_SHOW_ARTIFACT_DEPENDENCIES = "showArtifactDependencies";
52 private static final String ACTION_SHOW_ARTIFACT_DEPENDENCY_TREE = "showArtifactDependencyTree";
54 private static final String ACTION_SHOW_ARTIFACT_MAILING_LISTS = "showArtifactMailingLists";
56 private static final String BROWSE_PREFIX = "/browse";
58 private static final String METHOD_DEPENDENCIES = "dependencies";
60 private static final String METHOD_DEPENDENCY_TREE = "dependencyTree";
62 private static final String METHOD_MAILING_LISTS = "mailingLists";
64 private static final String METHOD_USEDBY = "usedby";
66 private static final String PARAM_ARTIFACT_ID = "artifactId";
68 private static final String PARAM_GROUP_ID = "groupId";
70 private static final String PARAM_VERSION = "version";
72 public ActionMapping getMapping( HttpServletRequest httpServletRequest )
74 String path = httpServletRequest.getServletPath();
77 // if JEE 5 spec is correctly implemented, the "/*" pattern implies an empty string in servletpath
78 path = httpServletRequest.getPathInfo();
81 if ( path.startsWith( BROWSE_PREFIX ) )
83 path = path.substring( BROWSE_PREFIX.length() );
84 if ( StringUtils.isBlank( path ) ||
85 StringUtils.equals( path, "/" ) ||
86 StringUtils.equals( path, ".action" ) )
88 // Return "root" browse.
89 return new ActionMapping( ACTION_BROWSE, "/", "", null );
93 Map params = new HashMap();
95 if ( path.charAt( 0 ) == '/' )
97 path = path.substring( 1 );
100 String[] parts = path.split( "/" );
101 switch ( parts.length )
104 params.put( PARAM_GROUP_ID, parts[0] );
105 return new ActionMapping( ACTION_BROWSE_GROUP, "/", "", params );
108 params.put( PARAM_GROUP_ID, parts[0] );
109 params.put( PARAM_ARTIFACT_ID, parts[1] );
110 return new ActionMapping( ACTION_BROWSE_ARTIFACT, "/", "", params );
113 params.put( PARAM_GROUP_ID, parts[0] );
114 params.put( PARAM_ARTIFACT_ID, parts[1] );
115 params.put( PARAM_VERSION, parts[2] );
116 return new ActionMapping( ACTION_SHOW_ARTIFACT, "/", "", params );
119 params.put( PARAM_GROUP_ID, parts[0] );
120 params.put( PARAM_ARTIFACT_ID, parts[1] );
121 params.put( PARAM_VERSION, parts[2] );
123 if ( METHOD_DEPENDENCIES.equals( parts[3] ) )
125 return new ActionMapping( ACTION_SHOW_ARTIFACT_DEPENDENCIES, "/", "", params );
127 else if ( METHOD_MAILING_LISTS.equals( parts[3] ) )
129 return new ActionMapping( ACTION_SHOW_ARTIFACT_MAILING_LISTS, "/", "", params );
131 else if ( METHOD_USEDBY.equals( parts[3] ) )
133 return new ActionMapping( ACTION_SHOW_ARTIFACT_DEPENDEES, "/", "", params );
135 else if ( METHOD_DEPENDENCY_TREE.equals( parts[3] ) )
137 return new ActionMapping( ACTION_SHOW_ARTIFACT_DEPENDENCY_TREE, "/", "", params );
144 return super.getMapping( httpServletRequest );
147 public String getUriFromActionMapping( ActionMapping actionMapping )
149 Map params = actionMapping.getParams();
150 if ( ACTION_BROWSE.equals( actionMapping.getName() ) )
152 return BROWSE_PREFIX;
154 else if ( ACTION_BROWSE_GROUP.equals( actionMapping.getName() ) )
156 return toUri( params, false, false, null );
158 else if ( ACTION_BROWSE_ARTIFACT.equals( actionMapping.getName() ) )
160 return toUri( params, true, false, null );
162 else if ( ACTION_SHOW_ARTIFACT.equals( actionMapping.getName() ) )
164 return toUri( params, true, true, null );
166 else if ( ACTION_SHOW_ARTIFACT_DEPENDENCIES.equals( actionMapping.getName() ) )
168 return toUri( params, true, true, METHOD_DEPENDENCIES );
170 else if ( ACTION_SHOW_ARTIFACT_MAILING_LISTS.equals( actionMapping.getName() ) )
172 return toUri( params, true, true, METHOD_MAILING_LISTS );
174 else if ( ACTION_SHOW_ARTIFACT_DEPENDEES.equals( actionMapping.getName() ) )
176 return toUri( params, true, true, METHOD_USEDBY );
178 else if ( ACTION_SHOW_ARTIFACT_DEPENDENCY_TREE.equals( actionMapping.getName() ) )
180 return toUri( params, true, true, METHOD_DEPENDENCY_TREE );
183 return super.getUriFromActionMapping( actionMapping );
186 private String toUri( Map params, boolean artifactId, boolean version, String method )
188 StringBuffer buf = new StringBuffer();
190 buf.append( BROWSE_PREFIX );
192 buf.append( params.remove( PARAM_GROUP_ID ) );
197 buf.append( params.remove( PARAM_ARTIFACT_ID ) );
202 buf.append( params.remove( PARAM_VERSION ) );
204 if ( StringUtils.isNotBlank( method ) )
207 buf.append( method );
212 return buf.toString();