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 org.apache.commons.collections.CollectionUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.maven.archiva.database.ArchivaDAO;
25 import org.apache.maven.archiva.database.Constraint;
26 import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
27 import org.apache.maven.archiva.indexer.RepositoryIndexException;
28 import org.apache.maven.archiva.indexer.RepositoryIndexSearchException;
29 import org.apache.maven.archiva.indexer.search.CrossRepositorySearch;
30 import org.apache.maven.archiva.indexer.search.SearchResultLimits;
31 import org.apache.maven.archiva.indexer.search.SearchResults;
32 import org.apache.maven.archiva.security.AccessDeniedException;
33 import org.apache.maven.archiva.security.ArchivaSecurityException;
34 import org.apache.maven.archiva.security.ArchivaUser;
35 import org.apache.maven.archiva.security.PrincipalNotFoundException;
36 import org.apache.maven.archiva.security.UserRepositories;
37 import org.codehaus.plexus.xwork.action.PlexusActionSupport;
39 import java.net.MalformedURLException;
40 import java.util.Collections;
41 import java.util.List;
44 * Search all indexed fields by the given criteria.
46 * @plexus.component role="com.opensymphony.xwork.Action" role-hint="searchAction"
48 public class SearchAction
49 extends PlexusActionSupport
57 * @plexus.requirement role-hint="jdo"
59 private ArchivaDAO dao;
64 private SearchResults results;
67 * @plexus.requirement role-hint="default"
69 private CrossRepositorySearch crossRepoSearch;
74 private UserRepositories userRepositories;
77 * @plexus.requirement role-hint="xwork"
79 private ArchivaUser archivaUser;
81 private static final String RESULTS = "results";
83 private static final String ARTIFACT = "artifact";
85 private List databaseResults;
87 public String quickSearch()
88 throws MalformedURLException, RepositoryIndexException, RepositoryIndexSearchException
90 /* TODO: give action message if indexing is in progress.
91 * This should be based off a count of 'unprocessed' artifacts.
92 * This (yet to be written) routine could tell the user that X (unprocessed) artifacts are not yet
93 * present in the full text search.
96 assert q != null && q.length() != 0;
98 SearchResultLimits limits = new SearchResultLimits( 0 );
100 List<String> selectedRepos = getObservableRepos();
101 if ( CollectionUtils.isEmpty( selectedRepos ) )
103 return GlobalResults.ACCESS_TO_NO_REPOS;
106 results = crossRepoSearch.searchForTerm( getPrincipal(), selectedRepos, q, limits );
108 if ( results.isEmpty() )
110 addActionError( "No results found" );
114 // TODO: filter / combine the artifacts by version? (is that even possible with non-artifact hits?)
116 /* I don't think that we should, as I expect us to utilize the 'score' system in lucene in
117 * the future to return relevant links better.
118 * I expect the lucene scoring system to take multiple hits on different areas of a single document
119 * to result in a higher score.
126 public String findArtifact()
129 // TODO: give action message if indexing is in progress
131 if ( StringUtils.isBlank( q ) )
133 addActionError( "Unable to search for a blank checksum" );
137 Constraint constraint = new ArtifactsByChecksumConstraint( q );
138 databaseResults = dao.getArtifactDAO().queryArtifacts( constraint );
140 if ( databaseResults.isEmpty() )
142 addActionError( "No results found" );
146 if ( databaseResults.size() == 1 )
148 // 1 hit? return it's information directly!
156 public String doInput()
161 private String getPrincipal()
163 return archivaUser.getActivePrincipal();
166 private List<String> getObservableRepos()
170 return userRepositories.getObservableRepositoryIds( getPrincipal() );
172 catch ( PrincipalNotFoundException e )
174 getLogger().warn( e.getMessage(), e );
176 catch ( AccessDeniedException e )
178 getLogger().warn( e.getMessage(), e );
179 // TODO: pass this onto the screen.
181 catch ( ArchivaSecurityException e )
183 getLogger().warn( e.getMessage(), e );
185 return Collections.emptyList();
193 public void setQ( String q )
198 public SearchResults getResults()
203 public List getDatabaseResults()
205 return databaseResults;