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();
75 if ( path.startsWith( BROWSE_PREFIX ) )
77 path = path.substring( BROWSE_PREFIX.length() );
78 if ( StringUtils.isBlank( path ) ||
79 StringUtils.equals( path, "/" ) ||
80 StringUtils.equals( path, ".action" ) )
82 // Return "root" browse.
83 return new ActionMapping( ACTION_BROWSE, "/", "", null );
87 Map params = new HashMap();
89 if ( path.charAt( 0 ) == '/' )
91 path = path.substring( 1 );
94 String[] parts = path.split( "/" );
95 switch ( parts.length )
98 params.put( PARAM_GROUP_ID, parts[0] );
99 return new ActionMapping( ACTION_BROWSE_GROUP, "/", "", params );
102 params.put( PARAM_GROUP_ID, parts[0] );
103 params.put( PARAM_ARTIFACT_ID, parts[1] );
104 return new ActionMapping( ACTION_BROWSE_ARTIFACT, "/", "", params );
107 params.put( PARAM_GROUP_ID, parts[0] );
108 params.put( PARAM_ARTIFACT_ID, parts[1] );
109 params.put( PARAM_VERSION, parts[2] );
110 return new ActionMapping( ACTION_SHOW_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] );
117 if ( METHOD_DEPENDENCIES.equals( parts[3] ) )
119 return new ActionMapping( ACTION_SHOW_ARTIFACT_DEPENDENCIES, "/", "", params );
121 else if ( METHOD_MAILING_LISTS.equals( parts[3] ) )
123 return new ActionMapping( ACTION_SHOW_ARTIFACT_MAILING_LISTS, "/", "", params );
125 else if ( METHOD_USEDBY.equals( parts[3] ) )
127 return new ActionMapping( ACTION_SHOW_ARTIFACT_DEPENDEES, "/", "", params );
129 else if ( METHOD_DEPENDENCY_TREE.equals( parts[3] ) )
131 return new ActionMapping( ACTION_SHOW_ARTIFACT_DEPENDENCY_TREE, "/", "", params );
138 return super.getMapping( httpServletRequest );
141 public String getUriFromActionMapping( ActionMapping actionMapping )
143 Map params = actionMapping.getParams();
144 if ( ACTION_BROWSE.equals( actionMapping.getName() ) )
146 return BROWSE_PREFIX;
148 else if ( ACTION_BROWSE_GROUP.equals( actionMapping.getName() ) )
150 return toUri( params, false, false, null );
152 else if ( ACTION_BROWSE_ARTIFACT.equals( actionMapping.getName() ) )
154 return toUri( params, true, false, null );
156 else if ( ACTION_SHOW_ARTIFACT.equals( actionMapping.getName() ) )
158 return toUri( params, true, true, null );
160 else if ( ACTION_SHOW_ARTIFACT_DEPENDENCIES.equals( actionMapping.getName() ) )
162 return toUri( params, true, true, METHOD_DEPENDENCIES );
164 else if ( ACTION_SHOW_ARTIFACT_MAILING_LISTS.equals( actionMapping.getName() ) )
166 return toUri( params, true, true, METHOD_MAILING_LISTS );
168 else if ( ACTION_SHOW_ARTIFACT_DEPENDEES.equals( actionMapping.getName() ) )
170 return toUri( params, true, true, METHOD_USEDBY );
172 else if ( ACTION_SHOW_ARTIFACT_DEPENDENCY_TREE.equals( actionMapping.getName() ) )
174 return toUri( params, true, true, METHOD_DEPENDENCY_TREE );
177 return super.getUriFromActionMapping( actionMapping );
180 private String toUri( Map params, boolean artifactId, boolean version, String method )
182 StringBuffer buf = new StringBuffer();
184 buf.append( BROWSE_PREFIX );
186 buf.append( params.remove( PARAM_GROUP_ID ) );
191 buf.append( params.remove( PARAM_ARTIFACT_ID ) );
196 buf.append( params.remove( PARAM_VERSION ) );
198 if ( StringUtils.isNotBlank( method ) )
201 buf.append( method );
206 return buf.toString();