You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package org.apache.maven.archiva.web.action;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.commons.collections.CollectionUtils;
  21. import org.apache.commons.lang.StringUtils;
  22. import org.apache.maven.archiva.database.ArchivaDAO;
  23. import org.apache.maven.archiva.database.Constraint;
  24. import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
  25. import org.apache.maven.archiva.indexer.RepositoryIndexException;
  26. import org.apache.maven.archiva.indexer.RepositoryIndexSearchException;
  27. import org.apache.maven.archiva.indexer.search.CrossRepositorySearch;
  28. import org.apache.maven.archiva.indexer.search.SearchResultLimits;
  29. import org.apache.maven.archiva.indexer.search.SearchResults;
  30. import org.apache.maven.archiva.security.AccessDeniedException;
  31. import org.apache.maven.archiva.security.ArchivaSecurityException;
  32. import org.apache.maven.archiva.security.ArchivaUser;
  33. import org.apache.maven.archiva.security.PrincipalNotFoundException;
  34. import org.apache.maven.archiva.security.UserRepositories;
  35. import org.codehaus.plexus.xwork.action.PlexusActionSupport;
  36. import java.net.MalformedURLException;
  37. import java.util.Collections;
  38. import java.util.List;
  39. /**
  40. * Search all indexed fields by the given criteria.
  41. *
  42. * @plexus.component role="com.opensymphony.xwork.Action" role-hint="searchAction"
  43. */
  44. public class SearchAction
  45. extends PlexusActionSupport
  46. {
  47. /**
  48. * Query string.
  49. */
  50. private String q;
  51. /**
  52. * @plexus.requirement role-hint="jdo"
  53. */
  54. private ArchivaDAO dao;
  55. /**
  56. * The Search Results.
  57. */
  58. private SearchResults results;
  59. /**
  60. * @plexus.requirement role-hint="default"
  61. */
  62. private CrossRepositorySearch crossRepoSearch;
  63. /**
  64. * @plexus.requirement
  65. */
  66. private UserRepositories userRepositories;
  67. /**
  68. * @plexus.requirement role-hint="xwork"
  69. */
  70. private ArchivaUser archivaUser;
  71. private static final String RESULTS = "results";
  72. private static final String ARTIFACT = "artifact";
  73. private List databaseResults;
  74. public String quickSearch()
  75. throws MalformedURLException, RepositoryIndexException, RepositoryIndexSearchException
  76. {
  77. /* TODO: give action message if indexing is in progress.
  78. * This should be based off a count of 'unprocessed' artifacts.
  79. * This (yet to be written) routine could tell the user that X (unprocessed) artifacts are not yet
  80. * present in the full text search.
  81. */
  82. assert q != null && q.length() != 0;
  83. SearchResultLimits limits = new SearchResultLimits( 0 );
  84. List<String> selectedRepos = getObservableRepos();
  85. if ( CollectionUtils.isEmpty( selectedRepos ) )
  86. {
  87. return GlobalResults.ACCESS_TO_NO_REPOS;
  88. }
  89. results = crossRepoSearch.searchForTerm( getPrincipal(), selectedRepos, q, limits );
  90. if ( results.isEmpty() )
  91. {
  92. addActionError( "No results found" );
  93. return INPUT;
  94. }
  95. // TODO: filter / combine the artifacts by version? (is that even possible with non-artifact hits?)
  96. /* I don't think that we should, as I expect us to utilize the 'score' system in lucene in
  97. * the future to return relevant links better.
  98. * I expect the lucene scoring system to take multiple hits on different areas of a single document
  99. * to result in a higher score.
  100. * - Joakim
  101. */
  102. return SUCCESS;
  103. }
  104. public String findArtifact()
  105. throws Exception
  106. {
  107. // TODO: give action message if indexing is in progress
  108. if ( StringUtils.isBlank( q ) )
  109. {
  110. addActionError( "Unable to search for a blank checksum" );
  111. return INPUT;
  112. }
  113. Constraint constraint = new ArtifactsByChecksumConstraint( q );
  114. databaseResults = dao.getArtifactDAO().queryArtifacts( constraint );
  115. if ( databaseResults.isEmpty() )
  116. {
  117. addActionError( "No results found" );
  118. return INPUT;
  119. }
  120. if ( databaseResults.size() == 1 )
  121. {
  122. // 1 hit? return it's information directly!
  123. return ARTIFACT;
  124. }
  125. return RESULTS;
  126. }
  127. @Override
  128. public String doInput()
  129. {
  130. return INPUT;
  131. }
  132. private String getPrincipal()
  133. {
  134. return archivaUser.getActivePrincipal();
  135. }
  136. private List<String> getObservableRepos()
  137. {
  138. try
  139. {
  140. return userRepositories.getObservableRepositoryIds( getPrincipal() );
  141. }
  142. catch ( PrincipalNotFoundException e )
  143. {
  144. getLogger().warn( e.getMessage(), e );
  145. }
  146. catch ( AccessDeniedException e )
  147. {
  148. getLogger().warn( e.getMessage(), e );
  149. // TODO: pass this onto the screen.
  150. }
  151. catch ( ArchivaSecurityException e )
  152. {
  153. getLogger().warn( e.getMessage(), e );
  154. }
  155. return Collections.emptyList();
  156. }
  157. public String getQ()
  158. {
  159. return q;
  160. }
  161. public void setQ( String q )
  162. {
  163. this.q = q;
  164. }
  165. public SearchResults getResults()
  166. {
  167. return results;
  168. }
  169. public List getDatabaseResults()
  170. {
  171. return databaseResults;
  172. }
  173. }