From 211bac101214da222f4097871791b28813556b4c Mon Sep 17 00:00:00 2001 From: Sanjay Dalal Date: Thu, 30 Jul 2009 22:58:16 +0000 Subject: [PATCH] CSPACE-323 changed directory structure of authentication service, refactored CoolectionSpaceClient to accommodate needs of AuthenticationServiceTest. added testng tests for authentication. authentication tests are enabled only if -Dcspace.server.secure=true property is used. A services/authentication/service A + services/authentication/service/src A services/authentication/service/src/test/resources A services/authentication/service/src/test/resources/log4j.properties A services/authentication/service/src/main/resources/log4j.properties A + services/authentication/service/pom.xml A + services/authentication/service/build.xml D services/authentication/src D services/authentication/pom.xml D services/authentication/build.xml A services/authentication/client A services/authentication/client/src A services/authentication/client/src/test A services/authentication/client/src/test/java A services/authentication/client/src/test/java/org A services/authentication/client/src/test/java/org/collectionspace A services/authentication/client/src/test/java/org/collectionspace/services A services/authentication/client/src/test/java/org/collectionspace/services/authentication A services/authentication/client/src/test/java/org/collectionspace/services/authentication/client A services/authentication/client/src/test/java/org/collectionspace/services/authentication/client/AuthenticationServiceTest.java A services/authentication/client/src/test/resources A services/authentication/client/src/test/resources/log4j.properties A services/authentication/client/src/main A services/authentication/client/src/main/java A services/authentication/client/src/main/java/org A services/authentication/client/src/main/java/org/collectionspace A services/authentication/client/src/main/java/org/collectionspace/services A services/authentication/client/src/main/java/org/collectionspace/services/authentication A services/authentication/client/src/main/java/org/collectionspace/services/authentication/client A services/authentication/client/src/main/resources A services/authentication/client/pom.xml M services/collectionobject/client/src/test/java/org/collectionspace/services/client/test/CollectionObjectServiceTest.java M services/collectionobject/client/src/main/java/org/collectionspace/services/client/CollectionObjectClient.java M services/intake/client/src/main/java/org/collectionspace/services/client/IntakeClient.java M services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java A + services/client/src/main/java/org/collectionspace/services/client/BaseServiceClient.java --- services/authentication/client/pom.xml | 100 ++++++ .../client/AuthenticationServiceTest.java | 284 ++++++++++++++++++ .../src/test/resources/log4j.properties | 23 ++ .../authentication/{ => service}/build.xml | 2 +- services/authentication/{ => service}/pom.xml | 31 +- .../authentication/CSpaceDBLoginModule.java | 0 .../resources/config/jboss-login-config.xml | 0 .../config/jboss-web-security-config.xml | 0 .../resources/config/web-security-config.xml | 0 .../resources/db/mysql/authentication.sql | 0 .../main/resources/db/mysql/test_authn.sql | 0 .../src/main/resources/log4j.properties | 21 ++ .../authentication/AppTest.java | 0 .../src/test/resources/log4j.properties | 23 ++ .../services/client/BaseServiceClient.java | 178 +++++++++++ .../client/CollectionSpaceClient.java | 214 ++++--------- .../client/CollectionObjectClient.java | 24 +- .../test/CollectionObjectServiceTest.java | 2 +- .../services/client/IntakeClient.java | 2 +- 19 files changed, 724 insertions(+), 180 deletions(-) create mode 100644 services/authentication/client/pom.xml create mode 100644 services/authentication/client/src/test/java/org/collectionspace/services/authentication/client/AuthenticationServiceTest.java create mode 100644 services/authentication/client/src/test/resources/log4j.properties rename services/authentication/{ => service}/build.xml (96%) rename services/authentication/{ => service}/pom.xml (76%) rename services/authentication/{ => service}/src/main/java/org/collectionspace/authentication/CSpaceDBLoginModule.java (100%) rename services/authentication/{ => service}/src/main/resources/config/jboss-login-config.xml (100%) rename services/authentication/{ => service}/src/main/resources/config/jboss-web-security-config.xml (100%) rename services/authentication/{ => service}/src/main/resources/config/web-security-config.xml (100%) rename services/authentication/{ => service}/src/main/resources/db/mysql/authentication.sql (100%) rename services/authentication/{ => service}/src/main/resources/db/mysql/test_authn.sql (100%) create mode 100644 services/authentication/service/src/main/resources/log4j.properties rename services/authentication/{ => service}/src/test/java/org/collectionspace/authentication/AppTest.java (100%) create mode 100644 services/authentication/service/src/test/resources/log4j.properties create mode 100644 services/client/src/main/java/org/collectionspace/services/client/BaseServiceClient.java diff --git a/services/authentication/client/pom.xml b/services/authentication/client/pom.xml new file mode 100644 index 000000000..8dbbefd1e --- /dev/null +++ b/services/authentication/client/pom.xml @@ -0,0 +1,100 @@ + + + + + org.collectionspace.services.authentication + org.collectionspace.services + 1.0 + + + 4.0.0 + org.collectionspace.services + org.collectionspace.services.authentication.client + 1.0 + services.authentication.client + + + + + org.slf4j + slf4j-api + test + + + org.slf4j + slf4j-log4j12 + test + + + org.collectionspace.services + org.collectionspace.services.client + 1.0 + + + org.collectionspace.services + org.collectionspace.services.collectionobject.client + 1.0 + + + org.testng + testng + 5.6 + + + org.jboss.resteasy + resteasy-jaxrs + 1.0.2.GA + + + + tjws + webserver + + + + + org.jboss.resteasy + resteasy-jaxb-provider + 1.0.2.GA + + + org.jboss.resteasy + resteasy-multipart-provider + 1.0.2.GA + + + commons-httpclient + commons-httpclient + 3.1 + + + + + cspace-services-collectionobject-client + + + org.apache.maven.plugins + maven-surefire-plugin + + + + log4j.configuration + file:target/test-classes/log4j.properties + + + + + + maven-compiler-plugin + 2.0.2 + + 1.6 + 1.6 + + + + + + diff --git a/services/authentication/client/src/test/java/org/collectionspace/services/authentication/client/AuthenticationServiceTest.java b/services/authentication/client/src/test/java/org/collectionspace/services/authentication/client/AuthenticationServiceTest.java new file mode 100644 index 000000000..bd49c3da8 --- /dev/null +++ b/services/authentication/client/src/test/java/org/collectionspace/services/authentication/client/AuthenticationServiceTest.java @@ -0,0 +1,284 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + * + * http://www.collectionspace.org + * http://wiki.collectionspace.org + * + * Copyright © 2009 Regents of the University of California + * + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + * + * You may obtain a copy of the ECL 2.0 License at + * https://source.collectionspace.org/collection-space/LICENSE.txt + * + * 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. + */ +package org.collectionspace.services.authentication.client; + +import java.util.ArrayList; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; +import org.jboss.resteasy.client.ClientResponse; +import org.testng.Assert; +import org.testng.annotations.Test; + +import org.collectionspace.services.collectionobject.CollectionObject; +import org.collectionspace.services.client.CollectionObjectClient; +import org.collectionspace.services.client.CollectionSpaceClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * AuthenticationServiceTest uses CollectionObject service to test authentication + * + * $LastChangedRevision: 434 $ + * $LastChangedDate: 2009-07-28 14:34:15 -0700 (Tue, 28 Jul 2009) $ + */ +public class AuthenticationServiceTest { + + private String knownCollectionObjectId = null; + final Logger logger = LoggerFactory.getLogger(AuthenticationServiceTest.class); + + @Test + public void auth_createCollectionObject() { + if(!isServerSecure()){ + return; + } + String identifier = this.createIdentifier(); + CollectionObject collectionObject = createCollectionObject(identifier); + CollectionObjectClient collectionObjectClient = new CollectionObjectClient(); + collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true"); + collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test"); + collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "test"); + try{ + collectionObjectClient.setupHttpClient(); + collectionObjectClient.setProxy(); + }catch(Exception e){ + logger.error("auth_createCollectionObject: caught " + e.getMessage()); + return; + } + ClientResponse res = collectionObjectClient.createCollectionObject(collectionObject); + verbose("auth_createCollectionObject: status = " + res.getStatus()); + Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode(), + "expected " + Response.Status.CREATED.getStatusCode()); + + // Store the ID returned from this create operation for additional tests below. + knownCollectionObjectId = extractId(res); + } + + @Test(dependsOnMethods = {"auth_createCollectionObject"}) + public void auth_createCollectionObjectWithoutUser() { + if(!isServerSecure()){ + return; + } + String identifier = this.createIdentifier(); + CollectionObject collectionObject = createCollectionObject(identifier); + CollectionObjectClient collectionObjectClient = new CollectionObjectClient(); + collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true"); + collectionObjectClient.removeProperty(CollectionSpaceClient.USER_PROPERTY); + collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "test"); + try{ + collectionObjectClient.setupHttpClient(); + collectionObjectClient.setProxy(); + }catch(Exception e){ + logger.error("auth_createCollectionObjectWithoutUser: caught " + e.getMessage()); + return; + } + ClientResponse res = collectionObjectClient.createCollectionObject(collectionObject); + verbose("auth_createCollectionObjectWithoutUser: status = " + res.getStatus()); + Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), + "expected " + Response.Status.UNAUTHORIZED.getStatusCode()); + } + + @Test(dependsOnMethods = {"auth_createCollectionObjectWithoutUser"}) + public void auth_createCollectionObjectWithoutPassword() { + if(!isServerSecure()){ + logger.warn("set -Dcspace.server.secure=true to run security tests"); + return; + } + String identifier = this.createIdentifier(); + CollectionObject collectionObject = createCollectionObject(identifier); + CollectionObjectClient collectionObjectClient = new CollectionObjectClient(); + collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true"); + collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test"); + collectionObjectClient.removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY); + try{ + collectionObjectClient.setupHttpClient(); + collectionObjectClient.setProxy(); + }catch(Exception e){ + logger.error("auth_createCollectionObjectWithoutPassword: caught " + e.getMessage()); + return; + } + ClientResponse res = collectionObjectClient.createCollectionObject(collectionObject); + verbose("auth_createCollectionObjectWithoutPassword: status = " + res.getStatus()); + Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), + "expected " + Response.Status.UNAUTHORIZED.getStatusCode()); + } + + @Test(dependsOnMethods = {"auth_createCollectionObjectWithoutPassword"}) + public void auth_createCollectionObjectWithIncorrectPassword() { + if(!isServerSecure()){ + logger.warn("set -Dcspace.server.secure=true to run security tests"); + return; + } + String identifier = this.createIdentifier(); + CollectionObject collectionObject = createCollectionObject(identifier); + CollectionObjectClient collectionObjectClient = new CollectionObjectClient(); + collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true"); + collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test"); + collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "bar"); + try{ + collectionObjectClient.setupHttpClient(); + collectionObjectClient.setProxy(); + }catch(Exception e){ + logger.error("auth_createCollectionObjectWithIncorrectPassword: caught " + e.getMessage()); + return; + } + ClientResponse res = collectionObjectClient.createCollectionObject(collectionObject); + verbose("auth_createCollectionObjectWithIncorrectPassword: status = " + res.getStatus()); + Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), + "expected " + Response.Status.UNAUTHORIZED.getStatusCode()); + } + + @Test(dependsOnMethods = {"auth_createCollectionObjectWithoutPassword"}) + public void auth_createCollectionObjectWithoutUserPassword() { + if(!isServerSecure()){ + logger.warn("set -Dcspace.server.secure=true to run security tests"); + return; + } + String identifier = this.createIdentifier(); + CollectionObject collectionObject = createCollectionObject(identifier); + CollectionObjectClient collectionObjectClient = new CollectionObjectClient(); + collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true"); + collectionObjectClient.removeProperty(CollectionSpaceClient.USER_PROPERTY); + collectionObjectClient.removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY); + try{ + collectionObjectClient.setupHttpClient(); + collectionObjectClient.setProxy(); + }catch(Exception e){ + logger.error("auth_createCollectionObjectWithoutUserPassword: caught " + e.getMessage()); + return; + } + ClientResponse res = collectionObjectClient.createCollectionObject(collectionObject); + verbose("auth_createCollectionObjectWithoutUserPassword: status = " + res.getStatus()); + Assert.assertEquals(res.getStatus(), Response.Status.FORBIDDEN.getStatusCode(), + "expected " + Response.Status.FORBIDDEN.getStatusCode()); + } + + @Test(dependsOnMethods = {"auth_createCollectionObjectWithoutPassword"}) + public void auth_createCollectionObjectWithIncorrectUserPassword() { + if(!isServerSecure()){ + logger.warn("set -Dcspace.server.secure=true to run security tests"); + return; + } + String identifier = this.createIdentifier(); + CollectionObject collectionObject = createCollectionObject(identifier); + CollectionObjectClient collectionObjectClient = new CollectionObjectClient(); + collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true"); + collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "foo"); + collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "bar"); + try{ + collectionObjectClient.setupHttpClient(); + collectionObjectClient.setProxy(); + }catch(Exception e){ + logger.error("auth_createCollectionObjectWithIncorrectUserPassword: caught " + e.getMessage()); + return; + } + ClientResponse res = collectionObjectClient.createCollectionObject(collectionObject); + verbose("auth_createCollectionObjectWithIncorrectUserPassword: status = " + res.getStatus()); + Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), + "expected " + Response.Status.UNAUTHORIZED.getStatusCode()); + } + + + @Test(dependsOnMethods = {"auth_createCollectionObjectWithIncorrectUserPassword"}) + public void auth_deleteCollectionObject() { + if(!isServerSecure()){ + logger.warn("set -Dcspace.server.secure=true to run security tests"); + return; + } + CollectionObjectClient collectionObjectClient = new CollectionObjectClient(); + collectionObjectClient = new CollectionObjectClient(); + collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true"); + collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test"); + collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "test"); + try{ + collectionObjectClient.setupHttpClient(); + collectionObjectClient.setProxy(); + }catch(Exception e){ + logger.error("auth_deleteCollectionObject: caught " + e.getMessage()); + return; + } + verbose("Calling deleteCollectionObject:" + knownCollectionObjectId); + ClientResponse res = collectionObjectClient.deleteCollectionObject(knownCollectionObjectId); + verbose("auth_deleteCollectionObject: status = " + res.getStatus()); + Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode(), + "expected " + Response.Status.OK.getStatusCode()); + } + + // --------------------------------------------------------------- + // Utility methods used by tests above + // --------------------------------------------------------------- + private CollectionObject createCollectionObject(String 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); + + return collectionObject; + } + + private String extractId(ClientResponse res) { + MultivaluedMap mvm = res.getMetadata(); + String uri = (String) ((ArrayList) mvm.get("Location")).get(0); + verbose("extractId:uri=" + uri); + String[] segments = uri.split("/"); + String id = segments[segments.length - 1]; + verbose("id=" + id); + return id; + } + + private void verbose(String msg) { + if(logger.isInfoEnabled()){ + logger.debug(msg); + } + } + + private void verbose(String msg, Object o, Class clazz) { + try{ + verbose(msg); + JAXBContext jc = JAXBContext.newInstance(clazz); + Marshaller m = jc.createMarshaller(); + m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, + Boolean.TRUE); + m.marshal(o, System.out); + }catch(Exception e){ + e.printStackTrace(); + } + } + + private String createIdentifier() { + long identifier = System.currentTimeMillis(); + return Long.toString(identifier); + } + + private boolean isServerSecure() { + return Boolean.getBoolean("cspace.server.secure"); + } +} diff --git a/services/authentication/client/src/test/resources/log4j.properties b/services/authentication/client/src/test/resources/log4j.properties new file mode 100644 index 000000000..5d288d87d --- /dev/null +++ b/services/authentication/client/src/test/resources/log4j.properties @@ -0,0 +1,23 @@ +log4j.rootLogger=debug, stdout, R + +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout + +# Pattern to output the caller's file name and line number. +log4j.appender.stdout.layout.ConversionPattern=%d %-5p [%t] [%c:%L] %m%n + +log4j.appender.R=org.apache.log4j.RollingFileAppender +log4j.appender.R.File=target/test-client.log + +log4j.appender.R.MaxFileSize=100KB +# Keep one backup file +log4j.appender.R.MaxBackupIndex=1 + +log4j.appender.R.layout=org.apache.log4j.PatternLayout +log4j.appender.R.layout.ConversionPattern=%d %-5p [%t] [%c:%L] %m%n + +#packages +log4j.logger.org.collectionspace=DEBUG +log4j.logger.org.apache=INFO +log4j.logger.httpclient=INFO +log4j.logger.org.jboss.resteasy=WARN diff --git a/services/authentication/build.xml b/services/authentication/service/build.xml similarity index 96% rename from services/authentication/build.xml rename to services/authentication/service/build.xml index 2972776a6..86bf45265 100644 --- a/services/authentication/build.xml +++ b/services/authentication/service/build.xml @@ -4,7 +4,7 @@ collectionspace authentication service - + diff --git a/services/authentication/pom.xml b/services/authentication/service/pom.xml similarity index 76% rename from services/authentication/pom.xml rename to services/authentication/service/pom.xml index 4c0f9a475..79b335f65 100644 --- a/services/authentication/pom.xml +++ b/services/authentication/service/pom.xml @@ -7,10 +7,10 @@ 4.0.0 org.collectionspace.services - org.collectionspace.services.authentication + org.collectionspace.services.authentication.service jar 1.0 - services.authentication + services.authentication.service 4.2.3.GA @@ -19,14 +19,6 @@ - - - - junit - junit - 4.1 - test - org.slf4j slf4j-api @@ -35,6 +27,13 @@ org.slf4j slf4j-log4j12 + + + junit + junit + 4.1 + test + @@ -53,6 +52,18 @@ cspace-services-authn + + org.apache.maven.plugins + maven-surefire-plugin + + + + log4j.configuration + file:target/test-classes/log4j.properties + + + + org.apache.maven.plugins maven-compiler-plugin diff --git a/services/authentication/src/main/java/org/collectionspace/authentication/CSpaceDBLoginModule.java b/services/authentication/service/src/main/java/org/collectionspace/authentication/CSpaceDBLoginModule.java similarity index 100% rename from services/authentication/src/main/java/org/collectionspace/authentication/CSpaceDBLoginModule.java rename to services/authentication/service/src/main/java/org/collectionspace/authentication/CSpaceDBLoginModule.java diff --git a/services/authentication/src/main/resources/config/jboss-login-config.xml b/services/authentication/service/src/main/resources/config/jboss-login-config.xml similarity index 100% rename from services/authentication/src/main/resources/config/jboss-login-config.xml rename to services/authentication/service/src/main/resources/config/jboss-login-config.xml diff --git a/services/authentication/src/main/resources/config/jboss-web-security-config.xml b/services/authentication/service/src/main/resources/config/jboss-web-security-config.xml similarity index 100% rename from services/authentication/src/main/resources/config/jboss-web-security-config.xml rename to services/authentication/service/src/main/resources/config/jboss-web-security-config.xml diff --git a/services/authentication/src/main/resources/config/web-security-config.xml b/services/authentication/service/src/main/resources/config/web-security-config.xml similarity index 100% rename from services/authentication/src/main/resources/config/web-security-config.xml rename to services/authentication/service/src/main/resources/config/web-security-config.xml diff --git a/services/authentication/src/main/resources/db/mysql/authentication.sql b/services/authentication/service/src/main/resources/db/mysql/authentication.sql similarity index 100% rename from services/authentication/src/main/resources/db/mysql/authentication.sql rename to services/authentication/service/src/main/resources/db/mysql/authentication.sql diff --git a/services/authentication/src/main/resources/db/mysql/test_authn.sql b/services/authentication/service/src/main/resources/db/mysql/test_authn.sql similarity index 100% rename from services/authentication/src/main/resources/db/mysql/test_authn.sql rename to services/authentication/service/src/main/resources/db/mysql/test_authn.sql diff --git a/services/authentication/service/src/main/resources/log4j.properties b/services/authentication/service/src/main/resources/log4j.properties new file mode 100644 index 000000000..01d5b6faf --- /dev/null +++ b/services/authentication/service/src/main/resources/log4j.properties @@ -0,0 +1,21 @@ +log4j.rootLogger=debug, stdout, R + +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout + +# Pattern to output the caller's file name and line number. +log4j.appender.stdout.layout.ConversionPattern=%d %-5p [%t] [%c:%L] %m%n + +log4j.appender.R=org.apache.log4j.RollingFileAppender +log4j.appender.R.File=${jboss.server.log.dir}/server.log + +log4j.appender.R.MaxFileSize=100KB +# Keep one backup file +log4j.appender.R.MaxBackupIndex=1 + +log4j.appender.R.layout=org.apache.log4j.PatternLayout +log4j.appender.R.layout.ConversionPattern=%d %-5p [%t] [%c:%L] %m%n + +#packages +log4j.logger.org.nuxeo=WARN +log4j.logger.org.collectionspace=DEBUG \ No newline at end of file diff --git a/services/authentication/src/test/java/org/collectionspace/authentication/AppTest.java b/services/authentication/service/src/test/java/org/collectionspace/authentication/AppTest.java similarity index 100% rename from services/authentication/src/test/java/org/collectionspace/authentication/AppTest.java rename to services/authentication/service/src/test/java/org/collectionspace/authentication/AppTest.java diff --git a/services/authentication/service/src/test/resources/log4j.properties b/services/authentication/service/src/test/resources/log4j.properties new file mode 100644 index 000000000..18c510350 --- /dev/null +++ b/services/authentication/service/src/test/resources/log4j.properties @@ -0,0 +1,23 @@ +log4j.rootLogger=debug, stdout, R + +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout + +# Pattern to output the caller's file name and line number. +log4j.appender.stdout.layout.ConversionPattern=%d %-5p [%t] [%c:%L] %m%n + +log4j.appender.R=org.apache.log4j.RollingFileAppender +log4j.appender.R.File=target/test-client.log + +log4j.appender.R.MaxFileSize=100KB +# Keep one backup file +log4j.appender.R.MaxBackupIndex=1 + +log4j.appender.R.layout=org.apache.log4j.PatternLayout +log4j.appender.R.layout.ConversionPattern=%d %-5p [%t] [%c:%L] %m%n + +#packages +log4j.logger.org.collectionspace=DEBUG +log4j.logger.org.apache=INFO +log4j.logger.httpclient=INFO +log4j.logger.org.jboss.resteasy=INFO diff --git a/services/client/src/main/java/org/collectionspace/services/client/BaseServiceClient.java b/services/client/src/main/java/org/collectionspace/services/client/BaseServiceClient.java new file mode 100644 index 000000000..91aaee852 --- /dev/null +++ b/services/client/src/main/java/org/collectionspace/services/client/BaseServiceClient.java @@ -0,0 +1,178 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + + * http://www.collectionspace.org + * http://wiki.collectionspace.org + + * Copyright 2009 University of California at Berkeley + + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + + * You may obtain a copy of the ECL 2.0 License at + + * https://source.collectionspace.org/collection-space/LICENSE.txt + + * 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. + */ +package org.collectionspace.services.client; + +import java.io.InputStream; +import java.net.URL; +import java.util.Properties; +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; + +/** + * BaseServiceClient is an abstract base client of all service clients + */ +public abstract class BaseServiceClient implements CollectionSpaceClient { + + protected final Logger logger = LoggerFactory.getLogger(BaseServiceClient.class); + private Properties properties = new Properties(); + private URL url; + private HttpClient httpClient; + + protected BaseServiceClient() { + readProperties(); + setupHttpClient(); + } + + @Override + public String getProperty(String propName) { + return properties.getProperty(propName); + } + + @Override + public void setProperty(String propName, String value) { + properties.setProperty(propName, value); + } + + @Override + public Object removeProperty(String propName) { + return properties.remove(propName); + } + + public void printProperties() { + for(Object kobj : properties.keySet()){ + String key = (String) kobj; + logger.trace("begin property name=" + key + " value=" + properties.get(key)); + } + } + + @Override + public String getBaseURL() { + return properties.getProperty(URL_PROPERTY); + } + + @Override + public HttpClient getHttpClient() { + return httpClient; + } + + @Override + public boolean useAuth() { + String auth = properties.getProperty(AUTH_PROPERTY); + return Boolean.valueOf(auth); + } + + @Override + public boolean useSSL() { + String ssl = properties.getProperty(SSL_PROPERTY); + return Boolean.valueOf(ssl); + } + + /** + * readProperties reads properties from system class path as well + * as it overrides properties made available using command line + * @exception RuntimeException + */ + private void readProperties() { + + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + InputStream is = null; + try{ + is = cl.getResourceAsStream("collectionspace-client.properties"); + properties.load(is); + if(logger.isDebugEnabled()){ + printProperties(); + } + String spec = System.getProperty(URL_PROPERTY); + if(spec != null && !"".equals(spec)){ + properties.setProperty(URL_PROPERTY, spec); + } + spec = properties.getProperty(URL_PROPERTY); + url = new URL(spec); + + String auth = System.getProperty(AUTH_PROPERTY); + if(auth != null && !"".equals(auth)){ + properties.setProperty(AUTH_PROPERTY, auth); + } + String ssl = System.getProperty(SSL_PROPERTY); + if(ssl != null && !"".equals(ssl)){ + properties.setProperty(AUTH_PROPERTY, ssl); + } + String user = System.getProperty(USER_PROPERTY); + if(user != null && !"".equals(user)){ + properties.setProperty(USER_PROPERTY, user); + } + String password = System.getProperty(PASSWORD_PROPERTY); + if(password != null && !"".equals(password)){ + properties.setProperty(PASSWORD_PROPERTY, password); + } + if(logger.isDebugEnabled()){ + printProperties(); + } + }catch(Exception e){ + logger.debug("Caught exception while reading properties", e); + throw new RuntimeException(e); + }finally{ + if(is != null){ + try{ + is.close(); + }catch(Exception e){ + } + } + } + } + + /** + * setupHttpClient sets up HTTP client for the service client + * the setup process relies on the following properties + * URL_PROPERTY + * USER_PROPERTY + * PASSWORD_PROPERTY + * AUTH_PROPERTY + * SSL_PROPERTY + */ + @Override + public void setupHttpClient() { + if(useAuth()){ + this.httpClient = new HttpClient(); + String user = properties.getProperty(USER_PROPERTY); + String password = properties.getProperty(PASSWORD_PROPERTY); + if(logger.isDebugEnabled()){ + logger.debug("setupHttpClient() using url=" + url + + " user=" + user + " password=" + password); + } + + httpClient.getState().setCredentials( + new AuthScope(url.getHost(), url.getPort(), AuthScope.ANY_REALM), + new UsernamePasswordCredentials(user, password)); + //JAXRS client library requires HTTP preemptive authentication + httpClient.getParams().setAuthenticationPreemptive(true); + if(logger.isDebugEnabled()){ + logger.debug("setupHttpClient: set preemptive authentication"); + } + } + } +} 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 365976603..371803ff6 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,152 +1,62 @@ -/** - * This document is a part of the source code and related artifacts - * for CollectionSpace, an open source collections management system - * for museums and related institutions: - - * http://www.collectionspace.org - * http://wiki.collectionspace.org - - * Copyright 2009 University of California at Berkeley - - * Licensed under the Educational Community License (ECL), Version 2.0. - * You may not use this file except in compliance with this License. - - * You may obtain a copy of the ECL 2.0 License at - - * https://source.collectionspace.org/collection-space/LICENSE.txt - - * 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. - */ -package org.collectionspace.services.client; - -import java.io.InputStream; -import java.net.URL; -import java.util.Properties; -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; - -/** - * CollectionSpaceClient is an abstract base client of all service clients - */ -public abstract class CollectionSpaceClient { - - public static final String USER_PROPERTY = "cspace.user"; - public static final String PASSWORD_PROPERTY = "cspace.password"; - public static final String AUTH_PROPERTY = "cspace.auth"; - public static final String SSL_PROPERTY = "cspace.ssl"; - public static final String URL_PROPERTY = "cspace.url"; - - - private static final String PATH = "/cspace-services/"; - - protected final Logger logger = LoggerFactory.getLogger(CollectionSpaceClient.class); - private Properties properties = new Properties(); - private URL url; - private HttpClient httpClient; - - protected CollectionSpaceClient() { - - readProperties(); - - if(useAuth()){ - httpClient = new HttpClient(); - String user = properties.getProperty(USER_PROPERTY); - String password = properties.getProperty(PASSWORD_PROPERTY); - if(logger.isDebugEnabled()){ - logger.debug("using user=" + user + " password=" + password); - } - httpClient.getState().setCredentials( - new AuthScope(url.getHost(), url.getPort(), AuthScope.ANY_REALM), - new UsernamePasswordCredentials(user, password)); - httpClient.getParams().setAuthenticationPreemptive(true); - if(logger.isDebugEnabled()){ - logger.debug("set up httpClient for authentication"); - } - } - } - - protected String getBaseURL() { - return (String) properties.getProperty(URL_PROPERTY); - } - - protected HttpClient getHttpClient() { - return httpClient; - } - - protected boolean useAuth() { - String auth = properties.getProperty(AUTH_PROPERTY); - return Boolean.valueOf(auth); - } - - protected boolean useSSL() { - String ssl = properties.getProperty(SSL_PROPERTY); - return Boolean.valueOf(ssl); - } - - /** - * readProperties reads properties from system class path as well - * as it overrides properties made available using command line - * @exception RuntimeException - */ - private void readProperties() { - - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - InputStream is = null; - try{ - is = cl.getResourceAsStream("collectionspace-client.properties"); - properties.load(is); - if(logger.isDebugEnabled()){ - for(Object kobj : properties.keySet()){ - String key = (String) kobj; - logger.debug("begin property name=" + key + " value=" + properties.get(key)); - } - } - String spec = System.getProperty(URL_PROPERTY); - if(spec != null && !"".equals(spec)){ - properties.setProperty(URL_PROPERTY, spec); - } - spec = properties.getProperty(URL_PROPERTY); - url = new URL(spec); - - String auth = System.getProperty(AUTH_PROPERTY); - if(auth != null && !"".equals(auth)){ - properties.setProperty(AUTH_PROPERTY, auth); - } - String ssl = System.getProperty(SSL_PROPERTY); - if(ssl != null && !"".equals(ssl)){ - properties.setProperty(AUTH_PROPERTY, ssl); - } - String user = System.getProperty(USER_PROPERTY); - if(user != null && !"".equals(user)){ - properties.setProperty(USER_PROPERTY, user); - } - String password = System.getProperty(PASSWORD_PROPERTY); - if(password != null && !"".equals(password)){ - properties.setProperty(PASSWORD_PROPERTY, password); - } - if(logger.isDebugEnabled()){ - for(Object kobj : properties.keySet()){ - String key = (String) kobj; - logger.debug("end property name=" + key + " value=" + properties.get(key)); - } - } - }catch(Exception e){ - logger.debug("Caught exception while reading properties", e); - throw new RuntimeException(e); - }finally{ - if(is != null){ - try{ - is.close(); - }catch(Exception e){ - } - } - } - } -} +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + + * http://www.collectionspace.org + * http://wiki.collectionspace.org + + * Copyright 2009 University of California at Berkeley + + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + + * You may obtain a copy of the ECL 2.0 License at + + * https://source.collectionspace.org/collection-space/LICENSE.txt + + * 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. + */ +package org.collectionspace.services.client; +import org.apache.commons.httpclient.HttpClient; + +/** + * + */ +public interface CollectionSpaceClient { + String AUTH_PROPERTY = "cspace.auth"; + String PASSWORD_PROPERTY = "cspace.password"; + String SSL_PROPERTY = "cspace.ssl"; + String URL_PROPERTY = "cspace.url"; + String USER_PROPERTY = "cspace.user"; + + String getBaseURL(); + + HttpClient getHttpClient(); + + String getProperty(String propName); + + Object removeProperty(String propName); + + void setProperty(String propName, String value); + + /** + * setupHttpClient sets up HTTP client for the service client + * the setup process relies on the following properties + * URL_PROPERTY + * USER_PROPERTY + * PASSWORD_PROPERTY + * AUTH_PROPERTY + * SSL_PROPERTY + */ + void setupHttpClient(); + + boolean useAuth(); + + boolean useSSL(); + +} 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 0331788d2..b9070c37f 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 @@ -15,12 +15,8 @@ import org.jboss.resteasy.spi.ResteasyProviderFactory; * @version $Revision:$ */ -public class CollectionObjectClient extends CollectionSpaceClient { +public class CollectionObjectClient extends BaseServiceClient { - /** - * - */ - private static final CollectionObjectClient instance = new CollectionObjectClient(); /** * */ @@ -31,9 +27,16 @@ public class CollectionObjectClient extends CollectionSpaceClient { * Default constructor for CollectionObjectClient class. * */ - private CollectionObjectClient() { + public CollectionObjectClient() { ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance(); RegisterBuiltin.register(factory); + setProxy(); + } + + /** + * allow to reset proxy as per security needs + */ + public void setProxy() { if(useAuth()){ collectionObjectProxy = ProxyFactory.create(CollectionObjectProxy.class, getBaseURL(), getHttpClient()); @@ -43,15 +46,6 @@ public class CollectionObjectClient extends CollectionSpaceClient { } } - /** - * FIXME Comment this - * - * @return - */ - public static CollectionObjectClient getInstance() { - return instance; - } - /** * @return * @see org.collectionspace.hello.client.CollectionObjectProxy#getCollectionObject() 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 7de255f64..619ba790f 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 @@ -48,7 +48,7 @@ import org.slf4j.LoggerFactory; */ public class CollectionObjectServiceTest { - private CollectionObjectClient collectionObjectClient = CollectionObjectClient.getInstance(); + private CollectionObjectClient collectionObjectClient = new CollectionObjectClient(); private String knownCollectionObjectId = null; private final String NON_EXISTENT_ID = createNonExistentIdentifier(); final Logger logger = LoggerFactory.getLogger(CollectionObjectServiceTest.class); diff --git a/services/intake/client/src/main/java/org/collectionspace/services/client/IntakeClient.java b/services/intake/client/src/main/java/org/collectionspace/services/client/IntakeClient.java index cdc28e903..167c44d1f 100644 --- a/services/intake/client/src/main/java/org/collectionspace/services/client/IntakeClient.java +++ b/services/intake/client/src/main/java/org/collectionspace/services/client/IntakeClient.java @@ -15,7 +15,7 @@ import org.jboss.resteasy.spi.ResteasyProviderFactory; * @version $Revision:$ */ -public class IntakeClient extends CollectionSpaceClient { +public class IntakeClient extends BaseServiceClient { /** -- 2.47.3