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 public ActionMapping getMapping( HttpServletRequest httpServletRequest, ConfigurationManager manager )
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, manager );
148 public String getUriFromActionMapping( ActionMapping actionMapping )
150 Map params = actionMapping.getParams();
151 if ( ACTION_BROWSE.equals( actionMapping.getName() ) )
153 return BROWSE_PREFIX;
155 else if ( ACTION_BROWSE_GROUP.equals( actionMapping.getName() ) )
157 return toUri( params, false, false, null );
159 else if ( ACTION_BROWSE_ARTIFACT.equals( actionMapping.getName() ) )
161 return toUri( params, true, false, null );
163 else if ( ACTION_SHOW_ARTIFACT.equals( actionMapping.getName() ) )
165 return toUri( params, true, true, null );
167 else if ( ACTION_SHOW_ARTIFACT_DEPENDENCIES.equals( actionMapping.getName() ) )
169 return toUri( params, true, true, METHOD_DEPENDENCIES );
171 else if ( ACTION_SHOW_ARTIFACT_MAILING_LISTS.equals( actionMapping.getName() ) )
173 return toUri( params, true, true, METHOD_MAILING_LISTS );
175 else if ( ACTION_SHOW_ARTIFACT_DEPENDEES.equals( actionMapping.getName() ) )
177 return toUri( params, true, true, METHOD_USEDBY );
179 else if ( ACTION_SHOW_ARTIFACT_DEPENDENCY_TREE.equals( actionMapping.getName() ) )
181 return toUri( params, true, true, METHOD_DEPENDENCY_TREE );
184 return super.getUriFromActionMapping( actionMapping );
187 private String toUri( Map params, boolean artifactId, boolean version, String method )
189 StringBuffer buf = new StringBuffer();
191 buf.append( BROWSE_PREFIX );
193 buf.append( params.remove( PARAM_GROUP_ID ) );
198 buf.append( params.remove( PARAM_ARTIFACT_ID ) );
203 buf.append( params.remove( PARAM_VERSION ) );
205 if ( StringUtils.isNotBlank( method ) )
208 buf.append( method );
213 return buf.toString();