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.xwork2.config.ConfigurationManager;
23 import org.apache.struts2.dispatcher.mapper.ActionMapping;
24 import org.apache.struts2.dispatcher.mapper.DefaultActionMapper;
26 import org.apache.commons.lang.StringUtils;
28 import java.util.HashMap;
31 import javax.servlet.http.HttpServletRequest;
34 * Map alternate URLs to specific actions. Used for the repository browser and the proxy.
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 private static final String ACTION_EXTENSION = "action";
74 public ActionMapping getMapping( HttpServletRequest httpServletRequest, ConfigurationManager manager )
76 String path = httpServletRequest.getServletPath();
79 // if JEE 5 spec is correctly implemented, the "/*" pattern implies an empty string in servletpath
80 path = httpServletRequest.getPathInfo();
83 if ( path.startsWith( BROWSE_PREFIX ) )
85 path = path.substring( BROWSE_PREFIX.length() );
86 if ( StringUtils.isBlank( path ) ||
87 StringUtils.equals( path, "/" ) ||
88 StringUtils.equals( path, ".action" ) )
90 // Return "root" browse.
91 return createActionMapping( ACTION_BROWSE, "/", "", null );
95 Map<String, String> params = new HashMap<String, String>();
97 if ( path.charAt( 0 ) == '/' )
99 path = path.substring( 1 );
102 String[] parts = path.split( "/" );
103 switch ( parts.length )
106 params.put( PARAM_GROUP_ID, parts[0] );
107 return createActionMapping( ACTION_BROWSE_GROUP, "/", "", params );
110 params.put( PARAM_GROUP_ID, parts[0] );
111 params.put( PARAM_ARTIFACT_ID, parts[1] );
112 return createActionMapping( ACTION_BROWSE_ARTIFACT, "/", "", params );
115 params.put( PARAM_GROUP_ID, parts[0] );
116 params.put( PARAM_ARTIFACT_ID, parts[1] );
117 params.put( PARAM_VERSION, parts[2] );
118 return createActionMapping( ACTION_SHOW_ARTIFACT, "/", "", params );
121 params.put( PARAM_GROUP_ID, parts[0] );
122 params.put( PARAM_ARTIFACT_ID, parts[1] );
123 params.put( PARAM_VERSION, parts[2] );
125 if ( METHOD_DEPENDENCIES.equals( parts[3] ) )
127 return createActionMapping( ACTION_SHOW_ARTIFACT_DEPENDENCIES, "/", "", params );
129 else if ( METHOD_MAILING_LISTS.equals( parts[3] ) )
131 return createActionMapping( ACTION_SHOW_ARTIFACT_MAILING_LISTS, "/", "", params );
133 else if ( METHOD_USEDBY.equals( parts[3] ) )
135 return createActionMapping( ACTION_SHOW_ARTIFACT_DEPENDEES, "/", "", params );
137 else if ( METHOD_DEPENDENCY_TREE.equals( parts[3] ) )
139 return createActionMapping( ACTION_SHOW_ARTIFACT_DEPENDENCY_TREE, "/", "", params );
146 return super.getMapping( httpServletRequest, manager );
149 @SuppressWarnings("unchecked")
151 public String getUriFromActionMapping( ActionMapping actionMapping )
153 Map<String, Object> params = actionMapping.getParams();
154 if ( ACTION_BROWSE.equals( actionMapping.getName() ) )
156 return BROWSE_PREFIX;
158 else if ( ACTION_BROWSE_GROUP.equals( actionMapping.getName() ) )
160 return toUri( params, false, false, null );
162 else if ( ACTION_BROWSE_ARTIFACT.equals( actionMapping.getName() ) )
164 return toUri( params, true, false, null );
166 else if ( ACTION_SHOW_ARTIFACT.equals( actionMapping.getName() ) )
168 return toUri( params, true, true, null );
170 else if ( ACTION_SHOW_ARTIFACT_DEPENDENCIES.equals( actionMapping.getName() ) )
172 return toUri( params, true, true, METHOD_DEPENDENCIES );
174 else if ( ACTION_SHOW_ARTIFACT_MAILING_LISTS.equals( actionMapping.getName() ) )
176 return toUri( params, true, true, METHOD_MAILING_LISTS );
178 else if ( ACTION_SHOW_ARTIFACT_DEPENDEES.equals( actionMapping.getName() ) )
180 return toUri( params, true, true, METHOD_USEDBY );
182 else if ( ACTION_SHOW_ARTIFACT_DEPENDENCY_TREE.equals( actionMapping.getName() ) )
184 return toUri( params, true, true, METHOD_DEPENDENCY_TREE );
187 return super.getUriFromActionMapping( actionMapping );
190 private String toUri( Map<String, Object> params, boolean artifactId, boolean version, String method )
192 StringBuffer buf = new StringBuffer();
194 buf.append( BROWSE_PREFIX );
196 buf.append( params.remove( PARAM_GROUP_ID ) );
201 buf.append( params.remove( PARAM_ARTIFACT_ID ) );
206 buf.append( params.remove( PARAM_VERSION ) );
208 if ( StringUtils.isNotBlank( method ) )
211 buf.append( method );
216 return buf.toString();
219 private ActionMapping createActionMapping( String name, String namespace, String method, Map params )
221 ActionMapping mapping = new ActionMapping( name, namespace, method, params );
222 mapping.setExtension( ACTION_EXTENSION );