From 424b3b33298003bc27c99c424b272af55889ffd8 Mon Sep 17 00:00:00 2001 From: Sanjay Dalal Date: Mon, 29 Jun 2009 19:04:53 +0000 Subject: [PATCH] CSPACE-227, CSPACE-228, CSPACE-229 configured service layer for authentication, tested end to end. commented out to not break tests. see wiki for enabling authentication (coming up) both for jaxrs layer as well as for jaxrs client-based consumer. --- services/JaxRsServiceProvider/pom.xml | 11 +- .../src/main/resources/jndi.properties | 3 - .../src/main/webapp/WEB-INF/jboss-web.xml | 5 + .../src/main/webapp/WEB-INF/web.xml | 26 ++++- .../client/CollectionSpaceClient.java | 100 +++++++++++++----- services/collectionobject/client/pom.xml | 14 ++- .../client/CollectionObjectClient.java | 9 +- .../test/CollectionObjectServiceTest.java | 70 ++++++------ .../client/src/test/resources/log4j.xml | 57 ++++++---- services/pom.xml | 1 + 10 files changed, 202 insertions(+), 94 deletions(-) delete mode 100644 services/JaxRsServiceProvider/src/main/resources/jndi.properties create mode 100644 services/JaxRsServiceProvider/src/main/webapp/WEB-INF/jboss-web.xml diff --git a/services/JaxRsServiceProvider/pom.xml b/services/JaxRsServiceProvider/pom.xml index 3e7546313..058a50954 100644 --- a/services/JaxRsServiceProvider/pom.xml +++ b/services/JaxRsServiceProvider/pom.xml @@ -131,12 +131,13 @@ - CollectionSpace + cspace-services org.codehaus.mojo jboss-maven-plugin + cspace ${jboss.dir} 8180 @@ -148,7 +149,9 @@ undeploy - ${basedir}/target/CollectionSpace.war + + ${project.build.directory}/${project.build.finalName}.war + @@ -158,7 +161,9 @@ deploy - ${basedir}/target/CollectionSpace.war + + ${project.build.directory}/${project.build.finalName}.war + diff --git a/services/JaxRsServiceProvider/src/main/resources/jndi.properties b/services/JaxRsServiceProvider/src/main/resources/jndi.properties deleted file mode 100644 index a3aa40712..000000000 --- a/services/JaxRsServiceProvider/src/main/resources/jndi.properties +++ /dev/null @@ -1,3 +0,0 @@ -java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory -java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces -java.naming.provider.url=jnp://localhost:1099 \ No newline at end of file diff --git a/services/JaxRsServiceProvider/src/main/webapp/WEB-INF/jboss-web.xml b/services/JaxRsServiceProvider/src/main/webapp/WEB-INF/jboss-web.xml new file mode 100644 index 000000000..c91efe084 --- /dev/null +++ b/services/JaxRsServiceProvider/src/main/webapp/WEB-INF/jboss-web.xml @@ -0,0 +1,5 @@ + + + + java:/jaas/cspace + \ No newline at end of file diff --git a/services/JaxRsServiceProvider/src/main/webapp/WEB-INF/web.xml b/services/JaxRsServiceProvider/src/main/webapp/WEB-INF/web.xml index 29379ffcb..812508a23 100644 --- a/services/JaxRsServiceProvider/src/main/webapp/WEB-INF/web.xml +++ b/services/JaxRsServiceProvider/src/main/webapp/WEB-INF/web.xml @@ -9,7 +9,7 @@ --> - CollectionSpace + CollectionSpace Services javax.ws.rs.Application @@ -18,7 +18,7 @@ resteasy.servlet.mapping.prefix - /nuxeo-rest + / @@ -43,7 +43,27 @@ Resteasy - /nuxeo-rest/* + /* + diff --git a/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java b/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java index 43d363504..f50212f96 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java +++ b/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java @@ -1,32 +1,76 @@ package org.collectionspace.services.client; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.UsernamePasswordCredentials; +import org.apache.commons.httpclient.auth.AuthScope; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public abstract class CollectionSpaceClient { - static final String URL_PROPERTY = "org.collectionspace.url"; - /* - static final String URL_PROPERTY_SCHEME = "org.collectionspace.url.schme"; - static final String URL_PROPERTY_HOST = "org.collectionspace.url.host"; - static final String URL_PROPERTY_PORT = "org.collectionspace.url.port"; - static final String URL_PROPERTY_CONTEXT = "org.collectionspace.url.context"; - */ - - private static final String SCHEME = "http"; - private static final String HOST = "localhost"; - private static final String PORT = "8180"; - private static final String URI = "/CollectionSpace/nuxeo-rest"; - private static final String URL = SCHEME + "://" + - HOST + ":" + - PORT + - URI; - private String collectionSpaceURL = null; - - - String getURL() { - String result = collectionSpaceURL; - - if (collectionSpaceURL == null) { - result = collectionSpaceURL = System.getProperty(URL_PROPERTY, URL); - } - - return result; - } + + static final String AUTH_PROPERTY = "org.collectionspace.auth"; + static final String SSL_PROPERTY = "org.collectionspace.ssl"; + static final String URL_PROPERTY = "org.collectionspace.url"; + /* + static final String URL_PROPERTY_SCHEME = "org.collectionspace.url.schme"; + static final String URL_PROPERTY_HOST = "org.collectionspace.url.host"; + static final String URL_PROPERTY_PORT = "org.collectionspace.url.port"; + static final String URL_PROPERTY_CONTEXT = "org.collectionspace.url.context"; + */ + private static final String HOST = "localhost"; + private static final int PORT = 8180; + private static final int SSL_PORT = 8543; + private static final String PATH = "/cspace-services/"; + private static final String DEFAULT_URL = "http://" + + HOST + ":" + PORT + PATH; + private static final String DEFAULT_SSL_URL = "https://" + + HOST + ":" + SSL_PORT + PATH; + private String baseURL = null; + private HttpClient httpClient; + private boolean useAuth; + private boolean useSSL; + final Logger logger = LoggerFactory.getLogger(CollectionSpaceClient.class); + + protected CollectionSpaceClient() { + + String url = System.getProperty(URL_PROPERTY, DEFAULT_URL); + if(url != null){ + baseURL = url; + } + useAuth = Boolean.getBoolean(AUTH_PROPERTY); + if(useAuth){ + httpClient = new HttpClient(); + httpClient.getState().setCredentials( + new AuthScope(HOST, PORT, AuthScope.ANY_REALM), + new UsernamePasswordCredentials("test", "test")); + httpClient.getParams().setAuthenticationPreemptive(true); + if(logger.isDebugEnabled()){ + logger.debug("set up httpClient for authentication"); + } + } + useSSL = Boolean.getBoolean(SSL_PROPERTY); + if(logger.isDebugEnabled()){ + logger.debug("useSSL=" + useSSL); + } + baseURL = useSSL ? DEFAULT_SSL_URL : DEFAULT_URL; + if(logger.isDebugEnabled()){ + logger.debug("using baseURL=" + baseURL); + } + } + + protected String getBaseURL() { + return baseURL; + } + + protected HttpClient getHttpClient() { + return httpClient; + } + + protected boolean useAuth() { + return useAuth; + } + + protected boolean useSSL() { + return useSSL; + } } diff --git a/services/collectionobject/client/pom.xml b/services/collectionobject/client/pom.xml index b2a6f1232..29bb3c079 100644 --- a/services/collectionobject/client/pom.xml +++ b/services/collectionobject/client/pom.xml @@ -72,8 +72,20 @@ - collectionspace-services-collectionobject-client + cspace-services-collectionobject-client + + org.apache.maven.plugins + maven-surefire-plugin + + + + log4j.configuration + log4j.xml + + + + maven-compiler-plugin 2.0.2 diff --git a/services/collectionobject/client/src/main/java/org/collectionspace/services/client/CollectionObjectClient.java b/services/collectionobject/client/src/main/java/org/collectionspace/services/client/CollectionObjectClient.java index 9bd95c175..0331788d2 100644 --- a/services/collectionobject/client/src/main/java/org/collectionspace/services/client/CollectionObjectClient.java +++ b/services/collectionobject/client/src/main/java/org/collectionspace/services/client/CollectionObjectClient.java @@ -17,7 +17,6 @@ import org.jboss.resteasy.spi.ResteasyProviderFactory; */ public class CollectionObjectClient extends CollectionSpaceClient { - /** * */ @@ -35,7 +34,13 @@ public class CollectionObjectClient extends CollectionSpaceClient { private CollectionObjectClient() { ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance(); RegisterBuiltin.register(factory); - collectionObjectProxy = ProxyFactory.create(CollectionObjectProxy.class, getURL()); + if(useAuth()){ + collectionObjectProxy = ProxyFactory.create(CollectionObjectProxy.class, + getBaseURL(), getHttpClient()); + }else{ + collectionObjectProxy = ProxyFactory.create(CollectionObjectProxy.class, + getBaseURL()); + } } /** diff --git a/services/collectionobject/client/src/test/java/org/collectionspace/services/client/test/CollectionObjectServiceTest.java b/services/collectionobject/client/src/test/java/org/collectionspace/services/client/test/CollectionObjectServiceTest.java index 97aee047a..093400cc8 100644 --- a/services/collectionobject/client/src/test/java/org/collectionspace/services/client/test/CollectionObjectServiceTest.java +++ b/services/collectionobject/client/src/test/java/org/collectionspace/services/client/test/CollectionObjectServiceTest.java @@ -13,6 +13,8 @@ import org.testng.annotations.Test; import org.collectionspace.services.collectionobject.CollectionObject; import org.collectionspace.services.collectionobject.CollectionObjectList; import org.collectionspace.services.client.CollectionObjectClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * A CollectionObjectNuxeoServiceTest. @@ -24,60 +26,61 @@ public class CollectionObjectServiceTest { private CollectionObjectClient collectionObjectClient = CollectionObjectClient.getInstance(); private String updateId = null; private String deleteId = null; + final Logger logger = LoggerFactory.getLogger(CollectionObjectServiceTest.class); @Test public void createCollectionObject() { - long identifier = this.createIdentifier(); - - CollectionObject collectionObject = createCollectionObject(identifier); + long identifier = this.createIdentifier(); + + CollectionObject collectionObject = createCollectionObject(identifier); ClientResponse res = collectionObjectClient.createCollectionObject(collectionObject); Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode()); - + //store updateId locally for "update" test - if (updateId == null) { - updateId = extractId(res); - } else { - deleteId = extractId(res); - System.out.println("Set deleteId: " + deleteId); + if(updateId == null){ + updateId = extractId(res); + }else{ + deleteId = extractId(res); + verbose("Set deleteId: " + deleteId); } } @Test(dependsOnMethods = {"createCollectionObject"}) public void updateCollectionObject() { - ClientResponse res = collectionObjectClient.getCollectionObject(updateId); + ClientResponse res = collectionObjectClient.getCollectionObject(updateId); CollectionObject collectionObject = res.getEntity(); verbose("Got CollectionObject to update with ID: " + updateId, - collectionObject, CollectionObject.class); - + collectionObject, CollectionObject.class); + //collectionObject.setCsid("updated-" + updateId); collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber()); collectionObject.setObjectName("updated-" + collectionObject.getObjectName()); - + // make call to update service res = collectionObjectClient.updateCollectionObject(updateId, collectionObject); - + // check the response - CollectionObject updatedCollectionObject = res.getEntity(); + CollectionObject updatedCollectionObject = res.getEntity(); Assert.assertEquals(updatedCollectionObject.getObjectName(), collectionObject.getObjectName()); verbose("updateCollectionObject: ", updatedCollectionObject, CollectionObject.class); - + return; } @Test(dependsOnMethods = {"createCollectionObject"}) public void createCollection() { - for (int i = 0; i < 3; i++) { - this.createCollectionObject(); - } + for(int i = 0; i < 3; i++){ + this.createCollectionObject(); + } } - + @Test(dependsOnMethods = {"createCollection"}) public void getCollectionObjectList() { //the resource method is expected to return at least an empty list CollectionObjectList coList = collectionObjectClient.getCollectionObjectList().getEntity(); List coItemList = coList.getCollectionObjectListItem(); int i = 0; - for(CollectionObjectList.CollectionObjectListItem pli : coItemList) { + for(CollectionObjectList.CollectionObjectListItem pli : coItemList){ verbose("getCollectionObjectList: list-item[" + i + "] csid=" + pli.getCsid()); verbose("getCollectionObjectList: list-item[" + i + "] objectNumber=" + pli.getObjectNumber()); verbose("getCollectionObjectList: list-item[" + i + "] URI=" + pli.getUri()); @@ -87,7 +90,7 @@ public class CollectionObjectServiceTest { @Test(dependsOnMethods = {"createCollection"}) public void deleteCollectionObject() { - System.out.println("Calling deleteCollectionObject:" + deleteId); + verbose("Calling deleteCollectionObject:" + deleteId); ClientResponse res = collectionObjectClient.deleteCollectionObject(deleteId); verbose("deleteCollectionObject: csid=" + deleteId); verbose("deleteCollectionObject: status = " + res.getStatus()); @@ -95,17 +98,17 @@ public class CollectionObjectServiceTest { } private CollectionObject createCollectionObject(long identifier) { - CollectionObject collectionObject = createCollectionObject("objectNumber-" + identifier, - "objectName-" + identifier); + CollectionObject collectionObject = createCollectionObject("objectNumber-" + identifier, + "objectName-" + identifier); return collectionObject; } private CollectionObject createCollectionObject(String objectNumber, String objectName) { - CollectionObject collectionObject = new CollectionObject(); - - collectionObject.setObjectNumber(objectNumber); - collectionObject.setObjectName(objectName); + CollectionObject collectionObject = new CollectionObject(); + + collectionObject.setObjectNumber(objectNumber); + collectionObject.setObjectName(objectName); return collectionObject; } @@ -120,7 +123,10 @@ public class CollectionObjectServiceTest { } private void verbose(String msg) { - System.out.println("CollectionObject Test: " + msg); +// if(logger.isInfoEnabled()){ +// logger.debug(msg); +// } + System.out.println(msg); } private void verbose(String msg, Object o, Class clazz) { @@ -142,9 +148,9 @@ public class CollectionObjectServiceTest { verbose(" name=" + mentry.getKey() + " value=" + mentry.getValue()); } } - + private long createIdentifier() { - long identifier = System.currentTimeMillis(); - return identifier; + long identifier = System.currentTimeMillis(); + return identifier; } } diff --git a/services/collectionobject/client/src/test/resources/log4j.xml b/services/collectionobject/client/src/test/resources/log4j.xml index 52121cb83..b26abaa9b 100644 --- a/services/collectionobject/client/src/test/resources/log4j.xml +++ b/services/collectionobject/client/src/test/resources/log4j.xml @@ -2,26 +2,25 @@ - - - - - - - - - + + + + + + + - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + diff --git a/services/pom.xml b/services/pom.xml index 96344dd9a..b847730b8 100644 --- a/services/pom.xml +++ b/services/pom.xml @@ -22,6 +22,7 @@ common + authentication client collectionobject JaxRsServiceProvider -- 2.47.3