001    package org.apache.archiva.rest.services;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import net.sf.beanlib.provider.replicator.BeanReplicator;
023    import org.apache.archiva.indexer.search.RepositorySearch;
024    import org.apache.archiva.indexer.search.RepositorySearchException;
025    import org.apache.archiva.indexer.search.SearchFields;
026    import org.apache.archiva.indexer.search.SearchResultHit;
027    import org.apache.archiva.indexer.search.SearchResultLimits;
028    import org.apache.archiva.indexer.search.SearchResults;
029    import org.apache.archiva.maven2.model.Artifact;
030    import org.apache.archiva.rest.api.model.Dependency;
031    import org.apache.archiva.rest.api.model.GroupIdList;
032    import org.apache.archiva.rest.api.model.SearchRequest;
033    import org.apache.archiva.rest.api.model.StringList;
034    import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
035    import org.apache.archiva.rest.api.services.SearchService;
036    import org.apache.commons.collections.ListUtils;
037    import org.apache.commons.lang.StringUtils;
038    import org.springframework.stereotype.Service;
039    
040    import javax.inject.Inject;
041    import java.util.ArrayList;
042    import java.util.Collections;
043    import java.util.List;
044    
045    /**
046     * @author Olivier Lamy
047     */
048    @Service( "searchService#rest" )
049    public class DefaultSearchService
050        extends AbstractRestService
051        implements SearchService
052    {
053    
054        @Inject
055        private RepositorySearch repositorySearch;
056    
057        public List<Artifact> quickSearch( String queryString )
058            throws ArchivaRestServiceException
059        {
060            if ( StringUtils.isBlank( queryString ) )
061            {
062                return Collections.emptyList();
063            }
064    
065            SearchResultLimits limits = new SearchResultLimits( 0 );
066            try
067            {
068                SearchResults searchResults =
069                    repositorySearch.search( getPrincipal(), getObservableRepos(), queryString, limits,
070                                             Collections.<String>emptyList() );
071                return getArtifacts( searchResults );
072    
073            }
074            catch ( RepositorySearchException e )
075            {
076                log.error( e.getMessage(), e );
077                throw new ArchivaRestServiceException( e.getMessage(), e );
078            }
079        }
080    
081        public List<Artifact> quickSearchWithRepositories( SearchRequest searchRequest )
082            throws ArchivaRestServiceException
083        {
084            String queryString = searchRequest.getQueryTerms();
085            if ( StringUtils.isBlank( queryString ) )
086            {
087                return Collections.emptyList();
088            }
089            List<String> repositories = searchRequest.getRepositories();
090            if ( repositories == null || repositories.isEmpty() )
091            {
092                repositories = getObservableRepos();
093            }
094            SearchResultLimits limits =
095                new SearchResultLimits( searchRequest.getPageSize(), searchRequest.getSelectedPage() );
096            try
097            {
098                SearchResults searchResults = repositorySearch.search( getPrincipal(), repositories, queryString, limits,
099                                                                       Collections.<String>emptyList() );
100                return getArtifacts( searchResults );
101    
102            }
103            catch ( RepositorySearchException e )
104            {
105                log.error( e.getMessage(), e );
106                throw new ArchivaRestServiceException( e.getMessage(), e );
107            }
108        }
109    
110        public List<Artifact> getArtifactVersions( String groupId, String artifactId, String packaging )
111            throws ArchivaRestServiceException
112        {
113            if ( StringUtils.isBlank( groupId ) || StringUtils.isBlank( artifactId ) )
114            {
115                return Collections.emptyList();
116            }
117            SearchFields searchField = new SearchFields();
118            searchField.setGroupId( groupId );
119            searchField.setArtifactId( artifactId );
120            searchField.setPackaging( StringUtils.isBlank( packaging ) ? "jar" : packaging );
121            searchField.setRepositories( getObservableRepos() );
122    
123            try
124            {
125                SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, null );
126                return getArtifacts( searchResults );
127            }
128            catch ( RepositorySearchException e )
129            {
130                log.error( e.getMessage(), e );
131                throw new ArchivaRestServiceException( e.getMessage(), e );
132            }
133        }
134    
135        public List<Artifact> searchArtifacts( SearchRequest searchRequest )
136            throws ArchivaRestServiceException
137        {
138            if ( searchRequest == null )
139            {
140                return Collections.emptyList();
141            }
142            SearchFields searchField = new BeanReplicator().replicateBean( searchRequest, SearchFields.class );
143            SearchResultLimits limits = new SearchResultLimits( 0 );
144    
145            // if no repos set we use ones available for the user
146            if ( searchField.getRepositories() == null || searchField.getRepositories().isEmpty() )
147            {
148                searchField.setRepositories( getObservableRepos() );
149            }
150    
151            try
152            {
153                SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, limits );
154                return getArtifacts( searchResults );
155            }
156            catch ( RepositorySearchException e )
157            {
158                log.error( e.getMessage(), e );
159                throw new ArchivaRestServiceException( e.getMessage(), e );
160            }
161        }
162    
163        public GroupIdList getAllGroupIds( List<String> selectedRepos )
164            throws ArchivaRestServiceException
165        {
166            List<String> observableRepos = getObservableRepos();
167            List<String> repos = ListUtils.intersection( observableRepos, selectedRepos );
168            if ( repos == null || repos.isEmpty() )
169            {
170                return new GroupIdList( Collections.<String>emptyList() );
171            }
172            try
173            {
174                return new GroupIdList( new ArrayList<String>( repositorySearch.getAllGroupIds( getPrincipal(), repos ) ) );
175            }
176            catch ( RepositorySearchException e )
177            {
178                log.error( e.getMessage(), e );
179                throw new ArchivaRestServiceException( e.getMessage(), e );
180            }
181    
182        }
183    
184        public List<Dependency> getDependencies( String groupId, String artifactId, String version )
185            throws ArchivaRestServiceException
186        {
187            return null;  //To change body of implemented methods use File | Settings | File Templates.
188        }
189    
190        public List<Artifact> getArtifactByChecksum( String checksum )
191            throws ArchivaRestServiceException
192        {
193            return null;  //To change body of implemented methods use File | Settings | File Templates.
194        }
195    
196        public StringList getObservablesRepoIds()
197            throws ArchivaRestServiceException
198        {
199            return new StringList( getObservableRepos() );
200        }
201    
202        //-------------------------------------
203        // internal
204        //-------------------------------------
205        protected List<Artifact> getArtifacts( SearchResults searchResults )
206            throws ArchivaRestServiceException
207        {
208    
209            if ( searchResults == null || searchResults.isEmpty() )
210            {
211                return Collections.emptyList();
212            }
213            List<Artifact> artifacts = new ArrayList<Artifact>( searchResults.getReturnedHitsCount() );
214            for ( SearchResultHit hit : searchResults.getHits() )
215            {
216                // duplicate Artifact one per available version
217                if ( hit.getVersions().size() > 0 )
218                {
219                    for ( String version : hit.getVersions() )
220                    {
221    
222                        Artifact versionned = new BeanReplicator().replicateBean( hit, Artifact.class );
223    
224                        if ( StringUtils.isNotBlank( version ) )
225                        {
226                            versionned.setVersion( version );
227                            versionned.setUrl( getArtifactUrl( versionned ) );
228    
229                            artifacts.add( versionned );
230    
231                        }
232                    }
233                }
234            }
235            return artifacts;
236        }
237    
238    
239    }