1 package org.apache.maven.archiva.web.action;
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 java.util.Collections;
23 import java.util.List;
25 import com.opensymphony.xwork2.ActionContext;
26 import org.apache.commons.collections.CollectionUtils;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.maven.archiva.database.ArchivaDatabaseException;
29 import org.apache.maven.archiva.database.ObjectNotFoundException;
30 import org.apache.maven.archiva.database.browsing.BrowsingResults;
31 import org.apache.maven.archiva.database.browsing.RepositoryBrowsing;
32 import org.apache.maven.archiva.model.ArchivaProjectModel;
33 import org.apache.maven.archiva.security.*;
36 * Browse the repository.
38 * @todo cache browsing results.
39 * @todo implement repository selectors (all or specific repository)
40 * @todo implement security around browse (based on repository id at first)
41 * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="browseAction" instantiation-strategy="per-lookup"
43 public class BrowseAction
44 extends PlexusActionSupport
47 * @plexus.requirement role-hint="default"
49 private RepositoryBrowsing repoBrowsing;
54 private UserRepositories userRepositories;
59 private ArchivaXworkUser archivaXworkUser;
61 private BrowsingResults results;
63 private String groupId;
65 private String artifactId;
67 private String repositoryId;
69 private ArchivaProjectModel sharedModel;
71 public String browse()
73 List<String> selectedRepos = getObservableRepos();
74 if ( CollectionUtils.isEmpty( selectedRepos ) )
76 return GlobalResults.ACCESS_TO_NO_REPOS;
79 this.results = repoBrowsing.getRoot( getPrincipal(), selectedRepos );
83 public String browseGroup()
85 if ( StringUtils.isEmpty( groupId ) )
88 addActionError( "You must specify a group ID to browse" );
92 List<String> selectedRepos = getObservableRepos();
93 if ( CollectionUtils.isEmpty( selectedRepos ) )
95 return GlobalResults.ACCESS_TO_NO_REPOS;
99 this.results = repoBrowsing.selectGroupId( getPrincipal(), selectedRepos, groupId );
103 public String browseArtifact()
105 if ( StringUtils.isEmpty( groupId ) )
108 addActionError( "You must specify a group ID to browse" );
112 if ( StringUtils.isEmpty( artifactId ) )
115 addActionError( "You must specify a artifact ID to browse" );
119 List<String> selectedRepos = getObservableRepos();
120 if ( CollectionUtils.isEmpty( selectedRepos ) )
122 return GlobalResults.ACCESS_TO_NO_REPOS;
125 this.results = repoBrowsing.selectArtifactId( getPrincipal(), selectedRepos, groupId, artifactId );
127 populateSharedModel();
132 private void populateSharedModel()
134 sharedModel = new ArchivaProjectModel();
135 sharedModel.setGroupId( groupId );
136 sharedModel.setArtifactId( artifactId );
137 boolean isFirstVersion = true;
139 for( String version : this.results.getVersions() )
143 ArchivaProjectModel model =
144 repoBrowsing.selectVersion( getPrincipal(), getObservableRepos(), groupId, artifactId, version );
157 if ( sharedModel.getPackaging() != null &&
158 !StringUtils.equalsIgnoreCase( sharedModel.getPackaging(), model.getPackaging() ) )
160 sharedModel.setPackaging( null );
163 if ( sharedModel.getName() != null &&
164 !StringUtils.equalsIgnoreCase( sharedModel.getName(), model.getName() ) )
166 sharedModel.setName( "" );
169 if ( sharedModel.getDescription() != null &&
170 !StringUtils.equalsIgnoreCase( sharedModel.getDescription(), model.getDescription() ) )
172 sharedModel.setDescription( null );
175 if ( sharedModel.getIssueManagement() != null && model.getIssueManagement() != null &&
176 !StringUtils.equalsIgnoreCase( sharedModel.getIssueManagement().getUrl(), model.getIssueManagement().getUrl() ) )
178 sharedModel.setIssueManagement( null );
181 if ( sharedModel.getCiManagement() != null && model.getCiManagement() != null &&
182 !StringUtils.equalsIgnoreCase( sharedModel.getCiManagement().getUrl(), model.getCiManagement().getUrl() ) )
184 sharedModel.setCiManagement( null );
187 if ( sharedModel.getOrganization() != null && model.getOrganization() != null &&
188 !StringUtils.equalsIgnoreCase( sharedModel.getOrganization().getName(), model.getOrganization().getName() ) )
190 sharedModel.setOrganization( null );
193 if ( sharedModel.getUrl() != null && !StringUtils.equalsIgnoreCase( sharedModel.getUrl(), model.getUrl() ) )
195 sharedModel.setUrl( null );
199 isFirstVersion = false;
201 catch ( ObjectNotFoundException e )
203 log.debug( e.getMessage(), e );
205 catch ( ArchivaDatabaseException e )
207 log.debug( e.getMessage(), e );
212 @SuppressWarnings("unchecked")
213 private String getPrincipal()
215 return archivaXworkUser.getActivePrincipal( ActionContext.getContext().getSession() );
218 private List<String> getObservableRepos()
222 return userRepositories.getObservableRepositoryIds( getPrincipal() );
224 catch ( PrincipalNotFoundException e )
226 log.warn( e.getMessage(), e );
228 catch ( AccessDeniedException e )
230 log.warn( e.getMessage(), e );
231 // TODO: pass this onto the screen.
233 catch ( ArchivaSecurityException e )
235 log.warn( e.getMessage(), e );
237 return Collections.emptyList();
240 public String getGroupId()
245 public void setGroupId( String groupId )
247 this.groupId = groupId;
250 public String getArtifactId()
255 public void setArtifactId( String artifactId )
257 this.artifactId = artifactId;
260 public BrowsingResults getResults()
265 public String getRepositoryId(){
270 public void setRepositoryId(String repositoryId){
272 this.repositoryId = repositoryId;
275 public ArchivaProjectModel getSharedModel()
280 public void setSharedModel( ArchivaProjectModel sharedModel )
282 this.sharedModel = sharedModel;