[MRM-206]

-added search + test cases in web services


git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@710189 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Maria Odea B. Ching 2008-11-03 22:23:23 +00:00
parent 5e04d6c1a2
commit 9d345b6c94
15 changed files with 996 additions and 43 deletions

View File

@ -0,0 +1,62 @@
package org.apache.archiva.indexer.util;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.commons.lang.StringUtils;
/**
* SearchUtil - utility class for search.
*
* @version
*/
public class SearchUtil
{
public static final String BYTECODE_KEYWORD = "bytecode:";
/**
* Determines whether the queryString has the bytecode keyword.
*
* @param queryString
* @return
*/
public static boolean isBytecodeSearch( String queryString )
{
if ( queryString.startsWith( BYTECODE_KEYWORD ) )
{
return true;
}
return false;
}
/**
* Removes the bytecode keyword from the query string.
*
* @param queryString
* @return
*/
public static String removeBytecodeKeyword( String queryString )
{
String qString = StringUtils.uncapitalize( queryString );
qString = StringUtils.remove( queryString, BYTECODE_KEYWORD );
return qString;
}
}

View File

@ -25,6 +25,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.archiva.indexer.util.SearchUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
@ -106,8 +107,6 @@ public class SearchAction
private static final String COMPLETE_QUERY_STRING_SEPARATOR = ";";
private static final String BYTECODE_KEYWORD = "bytecode:";
private List<String> managedRepositoryList;
private String groupId;
@ -232,9 +231,9 @@ public class SearchAction
return GlobalResults.ACCESS_TO_NO_REPOS;
}
if( isBytecodeSearch( q ) )
if( SearchUtil.isBytecodeSearch( q ) )
{
results = crossRepoSearch.searchForBytecode( getPrincipal(), selectedRepos, removeKeyword( q ), limits );
results = crossRepoSearch.searchForBytecode( getPrincipal(), selectedRepos, SearchUtil.removeBytecodeKeyword( q ), limits );
}
else
{
@ -439,24 +438,6 @@ public class SearchAction
this.completeQueryString = completeQueryString;
}
private boolean isBytecodeSearch( String queryString )
{
if ( queryString.startsWith( BYTECODE_KEYWORD ) )
{
return true;
}
return false;
}
private String removeKeyword( String queryString )
{
String qString = StringUtils.uncapitalize( queryString );
qString = StringUtils.remove( queryString, BYTECODE_KEYWORD );
return qString;
}
public ArchivaConfiguration getArchivaConfiguration()
{
return archivaConfiguration;

View File

@ -32,7 +32,25 @@
</bean>
<bean name="testXmlRpcService" lazy-init="true" scope="singleton" class="org.apache.archiva.web.xmlrpc.services.PingServiceImpl"/>
<bean name="xmlRpcUserRepositories" class="org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories">
<constructor-arg ref="userRepositories"/>
<constructor-arg ref="xmlRpcAuthenticator"/>
</bean>
<!-- Web Services : Search Service -->
<bean name="searchService" lazy-init="true" scope="singleton" class="org.apache.archiva.web.xmlrpc.services.SearchServiceImpl">
<!--
<constructor-arg ref="archivaConfiguration"/>
<constructor-arg ref="repositoryContentFactory"/>
-->
<constructor-arg ref="xmlRpcUserRepositories"/>
<constructor-arg ref="crossRepositorySearch"/>
<constructor-arg ref="archivaDAO#jdo"/>
<constructor-arg ref="repositoryBrowsing"/>
</bean>
<!-- Web Services : Administration Service -->
<bean name="administrationService" lazy-init="true" scope="singleton" class="org.apache.archiva.web.xmlrpc.services.AdministrationServiceImpl">
<constructor-arg ref="archivaConfiguration"/>
<constructor-arg ref="repositoryContentConsumers"/>
@ -46,12 +64,12 @@
<bean name="xmlrpcServicesList" lazy-init="true" scope="singleton" class="java.util.ArrayList">
<constructor-arg ref="administrationService"/>
<!-- <constructor-arg ref="searchService"/> -->
</bean>
<bean name="xmlRpcAuthenticator" class="org.apache.archiva.web.xmlrpc.security.XmlRpcAuthenticator">
<constructor-arg>
<ref bean="securitySystem"/>
</constructor-arg>
<constructor-arg ref="securitySystem"/>
<constructor-arg ref="userRepositories"/>
</bean>
<bean id="mailSession" class="org.springframework.jndi.JndiObjectFactoryBean">

View File

@ -32,6 +32,10 @@
<artifactId>archiva-xmlrpc-api</artifactId>
<name>Archiva Web :: XML-RPC API</name>
<dependencies>
<dependency>
<groupId>org.apache.archiva</groupId>
<artifactId>archiva-indexer</artifactId>
</dependency>
<dependency>
<groupId>com.atlassian.xmlrpc</groupId>
<artifactId>atlassian-xmlrpc-binder-annotations</artifactId>

View File

@ -19,10 +19,40 @@ package org.apache.archiva.web.xmlrpc.api;
* under the License.
*/
import java.util.Date;
import java.util.List;
import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
import com.atlassian.xmlrpc.ServiceObject;
@ServiceObject("Search")
public interface SearchService
{
/*
* quick/general text search which returns a list of artifacts
* query for an artifact based on a checksum
* query for all available versions of an artifact, sorted in version significance order
* query for all available versions of an artifact since a given date
* query for an artifact's direct dependencies
* query for an artifact's dependency tree (as with mvn dependency:tree - no duplicates should be included)
* query for all artifacts that depend on a given artifact
*/
public List<Artifact> quickSearch( String queryString )
throws Exception;
public List<Artifact> getArtifactByChecksum( String checksum) throws Exception;
public List<Artifact> getArtifactVersions( String groupId, String artifactId ) throws Exception;
public List<Artifact> getArtifactVersionsByDate( String groupId, String artifactId, String version, Date whenGathered )
throws Exception;
public List<Artifact> getDirectDependencies( String repositoryId, String groupId, String artifactId, String version )
throws Exception;
public List<Artifact> getDependencyTree( String groupId, String artifactId, String version ) throws Exception;
public List<Artifact> getDependees( String groupId, String artifactId, String version )
throws Exception;
}

View File

@ -0,0 +1,124 @@
package org.apache.archiva.web.xmlrpc.api.beans;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.Serializable;
import java.util.Date;
import com.atlassian.xmlrpc.ServiceBean;
import com.atlassian.xmlrpc.ServiceBeanField;
@ServiceBean
public class Artifact
implements Serializable
{
private String repositoryId;
private String groupId;
private String artifactId;
private String version;
private String type;
private Date whenGathered;
public Artifact()
{
}
public Artifact( String repositoryId, String groupId, String artifactId, String version,
String type, Date whenGathered )
{
this.repositoryId = repositoryId;
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
this.type = type;
this.whenGathered = whenGathered;
}
public String getGroupId()
{
return groupId;
}
public String getArtifactId()
{
return artifactId;
}
public String getVersion()
{
return version;
}
public String getType()
{
return type;
}
public Date getWhenGathered()
{
return whenGathered;
}
@ServiceBeanField( "groupId" )
public void setGroupId( String groupId )
{
this.groupId = groupId;
}
@ServiceBeanField( "artifactId" )
public void setArtifactId( String artifactId )
{
this.artifactId = artifactId;
}
@ServiceBeanField( "version" )
public void setVersion( String version )
{
this.version = version;
}
@ServiceBeanField( "type" )
public void setType( String type )
{
this.type = type;
}
@ServiceBeanField( "whenGathered" )
public void setWhenGathered( Date whenGathered )
{
this.whenGathered = whenGathered;
}
public String getRepositoryId()
{
return repositoryId;
}
public void setRepositoryId( String repositoryId )
{
this.repositoryId = repositoryId;
}
}

View File

@ -54,9 +54,9 @@
URL: ex. http://127.0.0.1:8080/archiva/xmlrpc
USERNAME & PASSWORD: Archiva credentials
-->
<argument>URL</argument>
<argument>USERNAME</argument>
<argument>PASSWORD</argument>
<argument>http://127.0.0.1:8080/archiva/xmlrpc</argument>
<argument>admin</argument>
<argument>admin1</argument>
</arguments>
</configuration>
</plugin>

View File

@ -1,6 +1,5 @@
package org.apache.archiva.web.xmlrpc.security;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@ -56,7 +55,18 @@ public class ServiceMethodsPermissionsMapping
public static final List<String> SERVICE_METHODS_FOR_OPERATION_ACCESS_REPORT = new ArrayList<String>();
public static final List<String> SERVICE_METHODS_FOR_OPERATION_REPOSITORY_ACCESS = new ArrayList<String>();
public static final List<String> SERVICE_METHODS_FOR_OPERATION_REPOSITORY_ACCESS = new ArrayList<String>()
{
{
add( "SearchService.quickSearch" );
add( "SearchService.getArtifactByChecksum" );
add( "SearchService.getArtifactVersions" );
add( "SearchService.queryArtifactVersionsByDate" );
add(" SearchService.getDirectDependencies" );
add(" SearchService.getDirectDependencyTree" );
add(" SearchService.getDependees" );
}
};
public static final List<String> SERVICE_METHODS_FOR_OPERATION_ADD_REPOSITORY = new ArrayList<String>();
@ -64,6 +74,6 @@ public class ServiceMethodsPermissionsMapping
public static final List<String> SERVICE_METHODS_FOR_OPERATION_EDIT_REPOSITORY = new ArrayList<String>();
public static final List<String> SERVICE_METHODS_FOR_OPERATION_REPOSITORY_UPLOAD = new ArrayList<String>();
public static final List<String> SERVICE_METHODS_FOR_OPERATION_REPOSITORY_UPLOAD = new ArrayList<String>();
}

View File

@ -19,7 +19,11 @@ package org.apache.archiva.web.xmlrpc.security;
* under the License.
*/
import java.util.List;
import org.apache.maven.archiva.security.ArchivaRoleConstants;
import org.apache.maven.archiva.security.ArchivaSecurityException;
import org.apache.maven.archiva.security.UserRepositories;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.XmlRpcRequest;
import org.apache.xmlrpc.common.XmlRpcHttpRequestConfigImpl;
@ -44,23 +48,32 @@ public class XmlRpcAuthenticator
implements AuthenticationHandler
{
private final SecuritySystem securitySystem;
public XmlRpcAuthenticator( SecuritySystem securitySystem )
private UserRepositories userRepositories;
private String username;
public XmlRpcAuthenticator( SecuritySystem securitySystem, UserRepositories userRepositories )
{
this.securitySystem = securitySystem;
this.userRepositories = userRepositories;
}
public boolean isAuthorized( XmlRpcRequest pRequest )
throws XmlRpcException
{
System.out.println( "authenticator is called for request '" + pRequest.getMethodName() + "'" );
if ( pRequest.getConfig() instanceof XmlRpcHttpRequestConfigImpl )
{
XmlRpcHttpRequestConfigImpl config = (XmlRpcHttpRequestConfigImpl) pRequest.getConfig();
username = config.getBasicUserName();
SecuritySession session =
authenticate( new PasswordBasedAuthenticationDataSource( config.getBasicUserName(),
authenticate( new PasswordBasedAuthenticationDataSource( username,
config.getBasicPassword() ) );
String method = pRequest.getMethodName();
AuthorizationResult result = authorize( session, method );
AuthorizationResult result = authorize( session, method, username );
return result.isAuthorized();
}
@ -89,13 +102,12 @@ public class XmlRpcAuthenticator
}
}
private AuthorizationResult authorize( SecuritySession session, String methodName )
private AuthorizationResult authorize( SecuritySession session, String methodName, String username )
throws XmlRpcException
{
try
{
{
// sample attempt at simplifying authorization checking of requested service method
// TODO test with a sample client to see if this would work!
if ( ServiceMethodsPermissionsMapping.SERVICE_METHODS_FOR_OPERATION_MANAGE_CONFIGURATION.contains( methodName ) )
{
return securitySystem.authorize( session, ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION );
@ -104,6 +116,25 @@ public class XmlRpcAuthenticator
{
return securitySystem.authorize( session, ArchivaRoleConstants.OPERATION_RUN_INDEXER );
}
else if ( ServiceMethodsPermissionsMapping.SERVICE_METHODS_FOR_OPERATION_REPOSITORY_ACCESS.contains( methodName ) )
{
try
{
List<String> observableRepos = userRepositories.getObservableRepositoryIds( username );
if( observableRepos != null && observableRepos.size() > 1 )
{
return new AuthorizationResult( true, username, null );
}
else
{
return new AuthorizationResult( false, username, null );
}
}
catch ( ArchivaSecurityException e )
{
throw new XmlRpcException( 401, e.getMessage() );
}
}
else
{
return securitySystem.authorize( session, ArchivaRoleConstants.GLOBAL_REPOSITORY_MANAGER_ROLE );
@ -114,4 +145,9 @@ public class XmlRpcAuthenticator
throw new XmlRpcException( 401, e.getMessage(), e );
}
}
public String getActiveUser()
{
return username;
}
}

View File

@ -0,0 +1,48 @@
package org.apache.archiva.web.xmlrpc.security;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.util.List;
import org.apache.maven.archiva.security.ArchivaSecurityException;
import org.apache.maven.archiva.security.PrincipalNotFoundException;
import org.apache.maven.archiva.security.UserRepositories;
import org.apache.xmlrpc.server.AbstractReflectiveHandlerMapping.AuthenticationHandler;
public class XmlRpcUserRepositories
{
private UserRepositories userRepositories;
private AuthenticationHandler authnHandler;
public XmlRpcUserRepositories( UserRepositories userRepositories, AuthenticationHandler authnHandler )
{
this.userRepositories = userRepositories;
this.authnHandler = authnHandler;
}
public List<String> getObservableRepositories()
throws PrincipalNotFoundException, ArchivaSecurityException
{
XmlRpcAuthenticator xmlRpcAuthn = (XmlRpcAuthenticator) authnHandler;
return userRepositories.getObservableRepositoryIds( xmlRpcAuthn.getActiveUser() );
}
}

View File

@ -89,7 +89,7 @@ public class XmlRpcAuthenticatorTest
xmlRpcRequestControl = MockControl.createControl( XmlRpcRequest.class );
xmlRpcRequest = ( XmlRpcRequest ) xmlRpcRequestControl.getMock();
authenticator = new XmlRpcAuthenticator( securitySystem );
authenticator = new XmlRpcAuthenticator( securitySystem, null );
}
private User createUser( String principal, String fullname, String password )
@ -131,7 +131,7 @@ public class XmlRpcAuthenticatorTest
configControl.expectAndReturn( config.getBasicPassword(), PASSWORD );
xmlRpcRequestControl.expectAndReturn( xmlRpcRequest.getMethodName(),
"AdministrationService.getAllManagedRepositories" );
"AdministrationService.getAllManagedRepositories", 2 );
xmlRpcRequestControl.replay();
configControl.replay();
@ -171,7 +171,7 @@ public class XmlRpcAuthenticatorTest
configControl.expectAndReturn( config.getBasicPassword(), PASSWORD );
xmlRpcRequestControl.expectAndReturn( xmlRpcRequest.getMethodName(),
"AdministrationService.getAllManagedRepositories" );
"AdministrationService.getAllManagedRepositories", 2 );
xmlRpcRequestControl.replay();
configControl.replay();
@ -205,7 +205,7 @@ public class XmlRpcAuthenticatorTest
configControl.expectAndReturn( config.getBasicPassword(), PASSWORD );
xmlRpcRequestControl.expectAndReturn( xmlRpcRequest.getMethodName(),
"AdministrationService.getAllManagedRepositories" );
"AdministrationService.getAllManagedRepositories", 2 );
xmlRpcRequestControl.replay();
configControl.replay();

View File

@ -32,6 +32,36 @@
<artifactId>archiva-xmlrpc-services</artifactId>
<name>Archiva Web :: XML-RPC Services</name>
<dependencies>
<dependency>
<groupId>org.apache.xmlrpc</groupId>
<artifactId>xmlrpc-server</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.archiva</groupId>
<artifactId>archiva-xmlrpc-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.archiva</groupId>
<artifactId>archiva-xmlrpc-security</artifactId>
</dependency>
<dependency>
<groupId>org.apache.archiva</groupId>
<artifactId>archiva-model</artifactId>
</dependency>
<dependency>
<groupId>org.apache.archiva</groupId>
<artifactId>archiva-security</artifactId>
</dependency>
<dependency>
<groupId>org.apache.archiva</groupId>
<artifactId>archiva-indexer</artifactId>
</dependency>
<dependency>
<groupId>org.apache.archiva</groupId>
<artifactId>archiva-xmlrpc-api</artifactId>

View File

@ -0,0 +1,205 @@
package org.apache.archiva.web.xmlrpc.services;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.archiva.indexer.util.SearchUtil;
import org.apache.archiva.web.xmlrpc.api.SearchService;
import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories;
import org.apache.maven.archiva.database.ArchivaDAO;
import org.apache.maven.archiva.database.ArtifactDAO;
import org.apache.maven.archiva.database.browsing.BrowsingResults;
import org.apache.maven.archiva.database.browsing.RepositoryBrowsing;
import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
import org.apache.maven.archiva.indexer.search.CrossRepositorySearch;
import org.apache.maven.archiva.indexer.search.SearchResultHit;
import org.apache.maven.archiva.indexer.search.SearchResultLimits;
import org.apache.maven.archiva.indexer.search.SearchResults;
import org.apache.maven.archiva.model.ArchivaArtifact;
/**
* SearchServiceImpl
*
* @version $Id: SearchServiceImpl.java
*/
public class SearchServiceImpl
implements SearchService
{
private CrossRepositorySearch crossRepoSearch;
private XmlRpcUserRepositories xmlRpcUserRepositories;
private ArchivaDAO archivaDAO;
private RepositoryBrowsing repoBrowsing;
public SearchServiceImpl( XmlRpcUserRepositories xmlRpcUserRepositories, CrossRepositorySearch crossRepoSearch,
ArchivaDAO archivaDAO, RepositoryBrowsing repoBrowsing )
{
this.xmlRpcUserRepositories = xmlRpcUserRepositories;
this.crossRepoSearch = crossRepoSearch;
this.archivaDAO = archivaDAO;
this.repoBrowsing = repoBrowsing;
}
/*
* quick/general text search which returns a list of artifacts
* query for an artifact based on a checksum
* query for all available versions of an artifact, sorted in version significance order
* query for all available versions of an artifact since a given date
* query for an artifact's direct dependencies
* query for an artifact's dependency tree (as with mvn dependency:tree - no duplicates should be included)
* query for all artifacts that depend on a given artifact
*/
public List<Artifact> quickSearch( String queryString )
throws Exception
{
// 1. check whether bytecode search or ordinary search
// 2. get observable repos
// 3. convert results to a list of Artifact objects
List<Artifact> artifacts = new ArrayList<Artifact>();
List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
SearchResults results = null;
if( SearchUtil.isBytecodeSearch( queryString ) )
{
results = crossRepoSearch.searchForBytecode( "", observableRepos, SearchUtil.removeBytecodeKeyword( queryString ), limits );
}
else
{
results = crossRepoSearch.searchForTerm( "", observableRepos, queryString, limits );
}
List<SearchResultHit> hits = results.getHits();
for( SearchResultHit hit : hits )
{
ArtifactDAO artifactDAO = archivaDAO.getArtifactDAO();
ArchivaArtifact pomArtifact = artifactDAO.getArtifact(
hit.getGroupId(), hit.getArtifactId(), hit.getVersion(), "", "pom" );
if( pomArtifact != null )
{
Artifact artifact = new Artifact( pomArtifact.getModel().getRepositoryId(), pomArtifact.getGroupId(), pomArtifact.getArtifactId(), pomArtifact.getVersion(),
pomArtifact.getType(), pomArtifact.getModel().getWhenGathered() );
artifacts.add( artifact );
}
else
{
continue;
}
}
return artifacts;
}
public List<Artifact> getArtifactByChecksum( String checksum )
throws Exception
{
// 1. get ArtifactDAO from ArchivaDAO
// 2. create ArtifactsByChecksumConstraint( "queryTerm" )
// 3. query artifacts using constraint
// 4. convert results to list of Artifact objects
List<Artifact> results = new ArrayList<Artifact>();
ArtifactDAO artifactDAO = archivaDAO.getArtifactDAO();
ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( checksum );
List<ArchivaArtifact> artifacts = artifactDAO.queryArtifacts( constraint );
for( ArchivaArtifact archivaArtifact : artifacts )
{
Artifact artifact = new Artifact( archivaArtifact.getModel().getRepositoryId(), archivaArtifact.getModel().getGroupId(),
archivaArtifact.getModel().getArtifactId(), archivaArtifact.getModel().getVersion(), archivaArtifact.getType(),
archivaArtifact.getModel().getWhenGathered() );
results.add( artifact );
}
return results;
}
public List<Artifact> getArtifactVersions( String groupId, String artifactId )
throws Exception
{
List<Artifact> artifacts = new ArrayList<Artifact>();
List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
BrowsingResults results = repoBrowsing.selectArtifactId( "", observableRepos, groupId, artifactId );
ArtifactDAO artifactDAO = archivaDAO.getArtifactDAO();
for( String version : results.getVersions() )
{
ArchivaArtifact pomArtifact = artifactDAO.getArtifact( groupId, artifactId, version, "", "pom" );
Artifact artifact = new Artifact( "", groupId, artifactId, version, pomArtifact.getType(),
pomArtifact.getModel().getWhenGathered() );
artifacts.add( artifact );
}
// 1. get observable repositories
// 2. use RepositoryBrowsing method to query uniqueVersions?
return artifacts;
}
public List<Artifact> getArtifactVersionsByDate( String groupId, String artifactId, String version, Date since )
throws Exception
{
List<Artifact> artifacts = new ArrayList<Artifact>();
// 1. get observable repositories
// 2. use RepositoryBrowsing method to query uniqueVersions? (but with date)
return artifacts;
}
public List<Artifact> getDirectDependencies( String repositoryId, String groupId, String artifactId, String version )
throws Exception
{
List<Artifact> artifacts = new ArrayList<Artifact>();
return artifacts;
}
public List<Artifact> getDependencyTree( String groupId, String artifactId, String version )
throws Exception
{
List<Artifact> a = new ArrayList<Artifact>();
return a;
}
//get artifacts that depend on a given artifact
public List<Artifact> getDependees( String groupId, String artifactId, String version )
throws Exception
{
List<Artifact> artifacts = new ArrayList<Artifact>();
// repo browsing usedBy?
return artifacts;
}
}

View File

@ -0,0 +1,387 @@
package org.apache.archiva.web.xmlrpc.services;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.archiva.web.xmlrpc.api.SearchService;
import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
import org.apache.archiva.web.xmlrpc.security.XmlRpcAuthenticator;
import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.FileTypes;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.apache.maven.archiva.database.ArchivaDAO;
import org.apache.maven.archiva.database.ArtifactDAO;
import org.apache.maven.archiva.database.browsing.BrowsingResults;
import org.apache.maven.archiva.database.browsing.RepositoryBrowsing;
import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
import org.apache.maven.archiva.indexer.filecontent.FileContentRecord;
import org.apache.maven.archiva.indexer.lucene.LuceneRepositoryContentRecord;
import org.apache.maven.archiva.indexer.search.CrossRepositorySearch;
import org.apache.maven.archiva.indexer.search.SearchResultLimits;
import org.apache.maven.archiva.indexer.search.SearchResults;
import org.apache.maven.archiva.model.ArchivaArtifact;
import org.apache.maven.archiva.model.ArchivaArtifactModel;
import org.apache.maven.archiva.model.ArtifactReference;
import org.apache.maven.archiva.repository.ManagedRepositoryContent;
import org.apache.maven.archiva.repository.RepositoryContentFactory;
import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
import org.apache.maven.archiva.repository.content.PathParser;
import org.apache.maven.archiva.security.ArchivaRoleConstants;
import org.apache.xmlrpc.XmlRpcRequest;
import org.apache.xmlrpc.common.XmlRpcHttpRequestConfigImpl;
import org.codehaus.plexus.redback.role.RoleManager;
import org.codehaus.plexus.redback.system.SecuritySystem;
import org.codehaus.plexus.redback.users.User;
import org.codehaus.plexus.redback.users.UserManager;
import org.codehaus.plexus.redback.users.UserNotFoundException;
import org.codehaus.plexus.spring.PlexusInSpringTestCase;
import org.easymock.ArgumentsMatcher;
import org.easymock.MockControl;
import org.easymock.classextension.MockClassControl;
import sun.security.action.GetLongAction;
/**
* SearchServiceImplTest
*
* @version $Id: SearchServiceImplTest.java
*/
public class SearchServiceImplTest
extends PlexusInSpringTestCase
{
private SearchService searchService;
private MockControl userReposControl;
private XmlRpcUserRepositories userRepos;
private MockControl crossRepoSearchControl;
private CrossRepositorySearch crossRepoSearch;
private MockControl archivaDAOControl;
private ArchivaDAO archivaDAO;
private MockControl artifactDAOControl;
private ArtifactDAO artifactDAO;
private MockControl repoBrowsingControl;
private RepositoryBrowsing repoBrowsing;
public void setUp()
throws Exception
{
userReposControl = MockClassControl.createControl( XmlRpcUserRepositories.class );
userRepos = ( XmlRpcUserRepositories ) userReposControl.getMock();
crossRepoSearchControl = MockControl.createControl( CrossRepositorySearch.class );
crossRepoSearch = ( CrossRepositorySearch ) crossRepoSearchControl.getMock();
archivaDAOControl = MockControl.createControl( ArchivaDAO.class );
archivaDAO = ( ArchivaDAO ) archivaDAOControl.getMock();
repoBrowsingControl = MockControl.createControl( RepositoryBrowsing.class );
repoBrowsing = ( RepositoryBrowsing ) repoBrowsingControl.getMock();
searchService = new SearchServiceImpl( userRepos, crossRepoSearch, archivaDAO, repoBrowsing );
artifactDAOControl = MockControl.createControl( ArtifactDAO.class );
artifactDAO = ( ArtifactDAO ) artifactDAOControl.getMock();
}
/*
* quick/general text search which returns a list of artifacts
* query for an artifact based on a checksum
* query for all available versions of an artifact, sorted in version significance order
* query for all available versions of an artifact since a given date
* query for an artifact's direct dependencies
* query for an artifact's dependency tree (as with mvn dependency:tree - no duplicates should be included)
* query for all artifacts that depend on a given artifact
*/
/* quick search */
public void testQuickSearchArtifactBytecodeSearch()
throws Exception
{
// 1. check whether bytecode search or ordinary search
// 2. get observable repos
// 3. convert results to a list of Artifact objects
List<String> observableRepoIds = new ArrayList<String>();
observableRepoIds.add( "repo1.mirror" );
observableRepoIds.add( "public.releases" );
userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
Date whenGathered = new Date();
SearchResults results = new SearchResults();
ArchivaArtifact artifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "jar" );
artifact.getModel().setWhenGathered( whenGathered );
FileContentRecord record = new FileContentRecord();
record.setRepositoryId( "repo1.mirror" );
record.setArtifact( artifact );
record.setContents( "org.apache.archiva:archiva-test:1.0:jar org.apache.archiva.test.MyClassName" );
record.setFilename( "archiva-test-1.0.jar" );
results.addHit( record );
SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
crossRepoSearchControl.expectAndDefaultReturn(
crossRepoSearch.searchForBytecode( "", observableRepoIds, "MyClassName", limits ), results );
archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
artifactDAOControl.expectAndReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "pom" ), artifact );
userReposControl.replay();
crossRepoSearchControl.replay();
archivaDAOControl.replay();
artifactDAOControl.replay();
List<Artifact> artifacts = searchService.quickSearch( "bytecode:MyClassName" );
userReposControl.verify();
crossRepoSearchControl.verify();
archivaDAOControl.verify();
artifactDAOControl.verify();
assertNotNull( artifacts );
assertEquals( 1, artifacts.size() );
}
public void testQuickSearchArtifactRegularSearch()
throws Exception
{
List<String> observableRepoIds = new ArrayList<String>();
observableRepoIds.add( "repo1.mirror" );
observableRepoIds.add( "public.releases" );
userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
Date whenGathered = new Date();
SearchResults results = new SearchResults();
ArchivaArtifact artifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "jar" );
artifact.getModel().setWhenGathered( whenGathered );
FileContentRecord record = new FileContentRecord();
record.setRepositoryId( "repo1.mirror" );
record.setArtifact( artifact );
record.setContents( "org.apache.archiva:archiva-test:1.0:jar" );
record.setFilename( "archiva-test-1.0.jar" );
results.addHit( record );
SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
crossRepoSearchControl.expectAndDefaultReturn(
crossRepoSearch.searchForTerm( "", observableRepoIds, "archiva", limits ), results );
archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
artifactDAOControl.expectAndReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "pom" ), artifact );
userReposControl.replay();
crossRepoSearchControl.replay();
archivaDAOControl.replay();
artifactDAOControl.replay();
List<Artifact> artifacts = searchService.quickSearch( "archiva" );
userReposControl.verify();
crossRepoSearchControl.verify();
archivaDAOControl.verify();
artifactDAOControl.verify();
assertNotNull( artifacts );
assertEquals( 1, artifacts.size() );
}
/* query artifact by checksum */
public void testGetArtifactByChecksum()
throws Exception
{
Date whenGathered = new Date();
ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( "a1b2c3aksjhdasfkdasasd" );
List<ArchivaArtifact> artifacts = new ArrayList<ArchivaArtifact>();
ArchivaArtifact artifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "jar" );
artifact.getModel().setWhenGathered( whenGathered );
artifacts.add( artifact );
archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
artifactDAO.queryArtifacts( constraint );
artifactDAOControl.setMatcher( MockControl.ALWAYS_MATCHER );
artifactDAOControl.setReturnValue( artifacts );
archivaDAOControl.replay();
artifactDAOControl.replay();
List<Artifact> results = searchService.getArtifactByChecksum( "a1b2c3aksjhdasfkdasasd" );
archivaDAOControl.verify();
artifactDAOControl.verify();
assertNotNull( results );
assertEquals( 1, results.size() );
}
/* query artifact versions */
public void testGetArtifactVersionsArtifactExists()
throws Exception
{
Date whenGathered = new Date();
List<String> observableRepoIds = new ArrayList<String>();
observableRepoIds.add( "repo1.mirror" );
observableRepoIds.add( "public.releases" );
List<String> versions = new ArrayList<String>();
versions.add( "1.0" );
versions.add( "1.1-beta-1" );
versions.add( "1.1-beta-2" );
versions.add( "1.1" );
versions.add( "1.2" );
versions.add( "1.2.1-SNAPSHOT" );
BrowsingResults results = new BrowsingResults( "org.apache.archiva", "archiva-test" );
results.setSelectedRepositoryIds( observableRepoIds );
results.setVersions( versions );
List<ArchivaArtifact> archivaArtifacts = new ArrayList<ArchivaArtifact>();
ArchivaArtifact archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 0 ), "", "pom" );
archivaArtifact.getModel().setWhenGathered( whenGathered );
archivaArtifacts.add( archivaArtifact );
archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 1 ), "", "pom" );
archivaArtifact.getModel().setWhenGathered( whenGathered );
archivaArtifacts.add( archivaArtifact );
archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 2 ), "", "pom" );
archivaArtifact.getModel().setWhenGathered( whenGathered );
archivaArtifacts.add( archivaArtifact );
archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 3 ), "", "pom" );
archivaArtifact.getModel().setWhenGathered( whenGathered );
archivaArtifacts.add( archivaArtifact );
archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 4 ), "", "pom" );
archivaArtifact.getModel().setWhenGathered( whenGathered );
archivaArtifacts.add( archivaArtifact );
archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 5 ), "", "pom" );
archivaArtifact.getModel().setWhenGathered( whenGathered );
archivaArtifacts.add( archivaArtifact );
userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
repoBrowsingControl.expectAndReturn( repoBrowsing.selectArtifactId( "", observableRepoIds, "org.apache.archiva", "archiva-test" ), results );
archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 0 ), "", "pom" ), archivaArtifacts.get( 0 ) );
artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 1 ), "", "pom" ), archivaArtifacts.get( 1 ) );
artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 2 ), "", "pom" ), archivaArtifacts.get( 2 ) );
artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 3 ), "", "pom" ), archivaArtifacts.get( 3 ) );
artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 4 ), "", "pom" ), archivaArtifacts.get( 4 ) );
artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 5 ), "", "pom" ), archivaArtifacts.get( 5 ) );
userReposControl.replay();
repoBrowsingControl.replay();
archivaDAOControl.replay();
artifactDAOControl.replay();
List<Artifact> artifacts = searchService.getArtifactVersions( "org.apache.archiva", "archiva-test" );
userReposControl.verify();
repoBrowsingControl.verify();
archivaDAOControl.verify();
artifactDAOControl.verify();
assertNotNull( artifacts );
assertEquals( 6, artifacts.size() );
}
/* query artifact versions since a given date */
public void testGetArtifactVersionsByDateArtifactExists()
throws Exception
{
}
public void testGetArtifactVersionsByDateArtifactDoesNotExist()
throws Exception
{
}
/* query artifact dependencies */
public void testGetDependenciesArtifactExists()
throws Exception
{
}
public void testGetDependenciesArtifactDoesNotExist()
throws Exception
{
}
/* get dependency tree */
public void testGetDependencyTreeArtifactExists()
throws Exception
{
}
public void testGetDependencyTreeArtifactDoesNotExist()
throws Exception
{
}
/* get dependees */
public void testGetDependeneesArtifactExists()
throws Exception
{
}
public void testGetDependeneesArtifactDoesNotExist()
throws Exception
{
}
}

View File

@ -0,0 +1,18 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.archiva</groupId>
<artifactId>archiva-test</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>