1 package org.apache.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 (StringUtils.isEmpty( path ))
85 // try RequestURI in last at least for StrutsTestCase
86 path = httpServletRequest.getRequestURI();
89 if ( path.startsWith( BROWSE_PREFIX ) )
91 path = path.substring( BROWSE_PREFIX.length() );
92 if ( StringUtils.isBlank( path ) ||
93 StringUtils.equals( path, "/" ) ||
94 StringUtils.equals( path, ".action" ) )
96 // Return "root" browse.
97 return createActionMapping( ACTION_BROWSE, "/", "", null );
101 Map<String, String> params = new HashMap<String, String>();
103 if ( path.charAt( 0 ) == '/' )
105 path = path.substring( 1 );
108 String[] parts = path.split( "/" );
109 switch ( parts.length )
112 params.put( PARAM_GROUP_ID, parts[0] );
113 return createActionMapping( ACTION_BROWSE_GROUP, "/", "", params );
116 params.put( PARAM_GROUP_ID, parts[0] );
117 params.put( PARAM_ARTIFACT_ID, parts[1] );
118 return createActionMapping( ACTION_BROWSE_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] );
124 return createActionMapping( ACTION_SHOW_ARTIFACT, "/", "", params );
127 params.put( PARAM_GROUP_ID, parts[0] );
128 params.put( PARAM_ARTIFACT_ID, parts[1] );
129 params.put( PARAM_VERSION, parts[2] );
131 if ( METHOD_DEPENDENCIES.equals( parts[3] ) )
133 return createActionMapping( ACTION_SHOW_ARTIFACT_DEPENDENCIES, "/", "", params );
135 else if ( METHOD_MAILING_LISTS.equals( parts[3] ) )
137 return createActionMapping( ACTION_SHOW_ARTIFACT_MAILING_LISTS, "/", "", params );
139 else if ( METHOD_USEDBY.equals( parts[3] ) )
141 return createActionMapping( ACTION_SHOW_ARTIFACT_DEPENDEES, "/", "", params );
143 else if ( METHOD_DEPENDENCY_TREE.equals( parts[3] ) )
145 return createActionMapping( ACTION_SHOW_ARTIFACT_DEPENDENCY_TREE, "/", "", params );
152 return super.getMapping( httpServletRequest, manager );
155 @SuppressWarnings("unchecked")
157 public String getUriFromActionMapping( ActionMapping actionMapping )
159 Map<String, Object> params = actionMapping.getParams();
160 if ( ACTION_BROWSE.equals( actionMapping.getName() ) )
162 return BROWSE_PREFIX;
164 else if ( ACTION_BROWSE_GROUP.equals( actionMapping.getName() ) )
166 return toUri( params, false, false, null );
168 else if ( ACTION_BROWSE_ARTIFACT.equals( actionMapping.getName() ) )
170 return toUri( params, true, false, null );
172 else if ( ACTION_SHOW_ARTIFACT.equals( actionMapping.getName() ) )
174 return toUri( params, true, true, null );
176 else if ( ACTION_SHOW_ARTIFACT_DEPENDENCIES.equals( actionMapping.getName() ) )
178 return toUri( params, true, true, METHOD_DEPENDENCIES );
180 else if ( ACTION_SHOW_ARTIFACT_MAILING_LISTS.equals( actionMapping.getName() ) )
182 return toUri( params, true, true, METHOD_MAILING_LISTS );
184 else if ( ACTION_SHOW_ARTIFACT_DEPENDEES.equals( actionMapping.getName() ) )
186 return toUri( params, true, true, METHOD_USEDBY );
188 else if ( ACTION_SHOW_ARTIFACT_DEPENDENCY_TREE.equals( actionMapping.getName() ) )
190 return toUri( params, true, true, METHOD_DEPENDENCY_TREE );
193 return super.getUriFromActionMapping( actionMapping );
196 private String toUri( Map<String, Object> params, boolean artifactId, boolean version, String method )
198 StringBuilder buf = new StringBuilder();
200 buf.append( BROWSE_PREFIX );
202 buf.append( params.remove( PARAM_GROUP_ID ) );
207 buf.append( params.remove( PARAM_ARTIFACT_ID ) );
212 buf.append( params.remove( PARAM_VERSION ) );
214 if ( StringUtils.isNotBlank( method ) )
217 buf.append( method );
222 return buf.toString();
225 private ActionMapping createActionMapping( String name, String namespace, String method, Map params )
227 ActionMapping mapping = new ActionMapping( name, namespace, method, params );
228 mapping.setExtension( ACTION_EXTENSION );