--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>\r
+<project xmlns="http://maven.apache.org/POM/4.0.0"\r
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">\r
+\r
+ <parent>\r
+ <artifactId>org.collectionspace.services.authentication</artifactId>\r
+ <groupId>org.collectionspace.services</groupId>\r
+ <version>1.0</version>\r
+ </parent>\r
+\r
+ <modelVersion>4.0.0</modelVersion>\r
+ <groupId>org.collectionspace.services</groupId>\r
+ <artifactId>org.collectionspace.services.authentication.client</artifactId>\r
+ <version>1.0</version>\r
+ <name>services.authentication.client</name>\r
+ \r
+ <dependencies>\r
+ <!-- keep slf4j dependencies on the top -->\r
+ <dependency>\r
+ <groupId>org.slf4j</groupId>\r
+ <artifactId>slf4j-api</artifactId>\r
+ <scope>test</scope>\r
+ </dependency>\r
+ <dependency>\r
+ <groupId>org.slf4j</groupId>\r
+ <artifactId>slf4j-log4j12</artifactId>\r
+ <scope>test</scope>\r
+ </dependency>\r
+ <dependency>\r
+ <groupId>org.collectionspace.services</groupId>\r
+ <artifactId>org.collectionspace.services.client</artifactId>\r
+ <version>1.0</version>\r
+ </dependency>\r
+ <dependency>\r
+ <groupId>org.collectionspace.services</groupId>\r
+ <artifactId>org.collectionspace.services.collectionobject.client</artifactId>\r
+ <version>1.0</version>\r
+ </dependency>\r
+ <dependency>\r
+ <groupId>org.testng</groupId>\r
+ <artifactId>testng</artifactId>\r
+ <version>5.6</version>\r
+ </dependency>\r
+ <dependency>\r
+ <groupId>org.jboss.resteasy</groupId>\r
+ <artifactId>resteasy-jaxrs</artifactId>\r
+ <version>1.0.2.GA</version>\r
+ <!-- filter out unwanted jars -->\r
+ <exclusions>\r
+ <exclusion>\r
+ <groupId>tjws</groupId>\r
+ <artifactId>webserver</artifactId>\r
+ </exclusion>\r
+ </exclusions>\r
+ </dependency>\r
+ <dependency>\r
+ <groupId>org.jboss.resteasy</groupId>\r
+ <artifactId>resteasy-jaxb-provider</artifactId>\r
+ <version>1.0.2.GA</version>\r
+ </dependency>\r
+ <dependency>\r
+ <groupId>org.jboss.resteasy</groupId>\r
+ <artifactId>resteasy-multipart-provider</artifactId>\r
+ <version>1.0.2.GA</version>\r
+ </dependency>\r
+ <dependency>\r
+ <groupId>commons-httpclient</groupId>\r
+ <artifactId>commons-httpclient</artifactId>\r
+ <version>3.1</version>\r
+ </dependency>\r
+ </dependencies>\r
+ \r
+ <build>\r
+ <finalName>cspace-services-collectionobject-client</finalName>\r
+ <plugins>\r
+ <plugin>\r
+ <groupId>org.apache.maven.plugins</groupId>\r
+ <artifactId>maven-surefire-plugin</artifactId>\r
+ <configuration>\r
+ <systemProperties>\r
+ <property>\r
+ <name>log4j.configuration</name>\r
+ <value>file:target/test-classes/log4j.properties</value>\r
+ </property>\r
+ </systemProperties>\r
+ </configuration>\r
+ </plugin>\r
+ <plugin>\r
+ <artifactId>maven-compiler-plugin</artifactId>\r
+ <version>2.0.2</version>\r
+ <configuration>\r
+ <source>1.6</source>\r
+ <target>1.6</target>\r
+ </configuration>\r
+ </plugin>\r
+ </plugins>\r
+ </build>\r
+</project>\r
+\r
--- /dev/null
+/**
+ * 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<Response> 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<Response> 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<Response> 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<Response> 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<Response> 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<Response> 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<Response> 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<Response> 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");
+ }
+}
--- /dev/null
+log4j.rootLogger=debug, stdout, R\r
+\r
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender\r
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout\r
+\r
+# Pattern to output the caller's file name and line number.\r
+log4j.appender.stdout.layout.ConversionPattern=%d %-5p [%t] [%c:%L] %m%n\r
+\r
+log4j.appender.R=org.apache.log4j.RollingFileAppender\r
+log4j.appender.R.File=target/test-client.log\r
+\r
+log4j.appender.R.MaxFileSize=100KB\r
+# Keep one backup file\r
+log4j.appender.R.MaxBackupIndex=1\r
+\r
+log4j.appender.R.layout=org.apache.log4j.PatternLayout\r
+log4j.appender.R.layout.ConversionPattern=%d %-5p [%t] [%c:%L] %m%n\r
+\r
+#packages\r
+log4j.logger.org.collectionspace=DEBUG\r
+log4j.logger.org.apache=INFO\r
+log4j.logger.httpclient=INFO\r
+log4j.logger.org.jboss.resteasy=WARN\r
collectionspace authentication service\r
</description>\r
<!-- set global properties for this build -->\r
- <property name="services.trunk" value="../.."/>\r
+ <property name="services.trunk" value="../../.."/>\r
<property file="${services.trunk}/build.properties" />\r
<property name="mvn.opts" value="" />\r
<property name="src" location="src"/>\r
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.collectionspace.services</groupId>
- <artifactId>org.collectionspace.services.authentication</artifactId>
+ <artifactId>org.collectionspace.services.authentication.service</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
- <name>services.authentication</name>
+ <name>services.authentication.service</name>
<properties>
<jboss.version>4.2.3.GA</jboss.version>
</properties>
<dependencies>
-
- <!-- utilities -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.1</version>
- <scope>test</scope>
- </dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
+ <!-- utilities -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.1</version>
+ <scope>test</scope>
+ </dependency>
<!-- javax -->
<dependency>
<build>
<finalName>cspace-services-authn</finalName>
<plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <systemProperties>
+ <property>
+ <name>log4j.configuration</name>
+ <value>file:target/test-classes/log4j.properties</value>
+ </property>
+ </systemProperties>
+ </configuration>
+ </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
--- /dev/null
+log4j.rootLogger=debug, stdout, R\r
+\r
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender\r
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout\r
+\r
+# Pattern to output the caller's file name and line number.\r
+log4j.appender.stdout.layout.ConversionPattern=%d %-5p [%t] [%c:%L] %m%n\r
+\r
+log4j.appender.R=org.apache.log4j.RollingFileAppender\r
+log4j.appender.R.File=${jboss.server.log.dir}/server.log\r
+\r
+log4j.appender.R.MaxFileSize=100KB\r
+# Keep one backup file\r
+log4j.appender.R.MaxBackupIndex=1\r
+\r
+log4j.appender.R.layout=org.apache.log4j.PatternLayout\r
+log4j.appender.R.layout.ConversionPattern=%d %-5p [%t] [%c:%L] %m%n\r
+\r
+#packages\r
+log4j.logger.org.nuxeo=WARN\r
+log4j.logger.org.collectionspace=DEBUG
\ No newline at end of file
--- /dev/null
+log4j.rootLogger=debug, stdout, R\r
+\r
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender\r
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout\r
+\r
+# Pattern to output the caller's file name and line number.\r
+log4j.appender.stdout.layout.ConversionPattern=%d %-5p [%t] [%c:%L] %m%n\r
+\r
+log4j.appender.R=org.apache.log4j.RollingFileAppender\r
+log4j.appender.R.File=target/test-client.log\r
+\r
+log4j.appender.R.MaxFileSize=100KB\r
+# Keep one backup file\r
+log4j.appender.R.MaxBackupIndex=1\r
+\r
+log4j.appender.R.layout=org.apache.log4j.PatternLayout\r
+log4j.appender.R.layout.ConversionPattern=%d %-5p [%t] [%c:%L] %m%n\r
+\r
+#packages\r
+log4j.logger.org.collectionspace=DEBUG\r
+log4j.logger.org.apache=INFO\r
+log4j.logger.httpclient=INFO\r
+log4j.logger.org.jboss.resteasy=INFO\r
--- /dev/null
+/**\r
+ * This document is a part of the source code and related artifacts\r
+ * for CollectionSpace, an open source collections management system\r
+ * for museums and related institutions:\r
+\r
+ * http://www.collectionspace.org\r
+ * http://wiki.collectionspace.org\r
+\r
+ * Copyright 2009 University of California at Berkeley\r
+\r
+ * Licensed under the Educational Community License (ECL), Version 2.0.\r
+ * You may not use this file except in compliance with this License.\r
+\r
+ * You may obtain a copy of the ECL 2.0 License at\r
+\r
+ * https://source.collectionspace.org/collection-space/LICENSE.txt\r
+\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.collectionspace.services.client;\r
+\r
+import java.io.InputStream;\r
+import java.net.URL;\r
+import java.util.Properties;\r
+import org.apache.commons.httpclient.HttpClient;\r
+import org.apache.commons.httpclient.UsernamePasswordCredentials;\r
+import org.apache.commons.httpclient.auth.AuthScope;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+/**\r
+ * BaseServiceClient is an abstract base client of all service clients\r
+ */\r
+public abstract class BaseServiceClient implements CollectionSpaceClient {\r
+\r
+ protected final Logger logger = LoggerFactory.getLogger(BaseServiceClient.class);\r
+ private Properties properties = new Properties();\r
+ private URL url;\r
+ private HttpClient httpClient;\r
+\r
+ protected BaseServiceClient() {\r
+ readProperties();\r
+ setupHttpClient();\r
+ }\r
+\r
+ @Override\r
+ public String getProperty(String propName) {\r
+ return properties.getProperty(propName);\r
+ }\r
+\r
+ @Override\r
+ public void setProperty(String propName, String value) {\r
+ properties.setProperty(propName, value);\r
+ }\r
+\r
+ @Override\r
+ public Object removeProperty(String propName) {\r
+ return properties.remove(propName);\r
+ }\r
+\r
+ public void printProperties() {\r
+ for(Object kobj : properties.keySet()){\r
+ String key = (String) kobj;\r
+ logger.trace("begin property name=" + key + " value=" + properties.get(key));\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public String getBaseURL() {\r
+ return properties.getProperty(URL_PROPERTY);\r
+ }\r
+\r
+ @Override\r
+ public HttpClient getHttpClient() {\r
+ return httpClient;\r
+ }\r
+\r
+ @Override\r
+ public boolean useAuth() {\r
+ String auth = properties.getProperty(AUTH_PROPERTY);\r
+ return Boolean.valueOf(auth);\r
+ }\r
+\r
+ @Override\r
+ public boolean useSSL() {\r
+ String ssl = properties.getProperty(SSL_PROPERTY);\r
+ return Boolean.valueOf(ssl);\r
+ }\r
+\r
+ /**\r
+ * readProperties reads properties from system class path as well\r
+ * as it overrides properties made available using command line\r
+ * @exception RuntimeException\r
+ */\r
+ private void readProperties() {\r
+\r
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();\r
+ InputStream is = null;\r
+ try{\r
+ is = cl.getResourceAsStream("collectionspace-client.properties");\r
+ properties.load(is);\r
+ if(logger.isDebugEnabled()){\r
+ printProperties();\r
+ }\r
+ String spec = System.getProperty(URL_PROPERTY);\r
+ if(spec != null && !"".equals(spec)){\r
+ properties.setProperty(URL_PROPERTY, spec);\r
+ }\r
+ spec = properties.getProperty(URL_PROPERTY);\r
+ url = new URL(spec);\r
+\r
+ String auth = System.getProperty(AUTH_PROPERTY);\r
+ if(auth != null && !"".equals(auth)){\r
+ properties.setProperty(AUTH_PROPERTY, auth);\r
+ }\r
+ String ssl = System.getProperty(SSL_PROPERTY);\r
+ if(ssl != null && !"".equals(ssl)){\r
+ properties.setProperty(AUTH_PROPERTY, ssl);\r
+ }\r
+ String user = System.getProperty(USER_PROPERTY);\r
+ if(user != null && !"".equals(user)){\r
+ properties.setProperty(USER_PROPERTY, user);\r
+ }\r
+ String password = System.getProperty(PASSWORD_PROPERTY);\r
+ if(password != null && !"".equals(password)){\r
+ properties.setProperty(PASSWORD_PROPERTY, password);\r
+ }\r
+ if(logger.isDebugEnabled()){\r
+ printProperties();\r
+ }\r
+ }catch(Exception e){\r
+ logger.debug("Caught exception while reading properties", e);\r
+ throw new RuntimeException(e);\r
+ }finally{\r
+ if(is != null){\r
+ try{\r
+ is.close();\r
+ }catch(Exception e){\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * setupHttpClient sets up HTTP client for the service client\r
+ * the setup process relies on the following properties\r
+ * URL_PROPERTY\r
+ * USER_PROPERTY\r
+ * PASSWORD_PROPERTY\r
+ * AUTH_PROPERTY\r
+ * SSL_PROPERTY\r
+ */\r
+ @Override\r
+ public void setupHttpClient() {\r
+ if(useAuth()){\r
+ this.httpClient = new HttpClient();\r
+ String user = properties.getProperty(USER_PROPERTY);\r
+ String password = properties.getProperty(PASSWORD_PROPERTY);\r
+ if(logger.isDebugEnabled()){\r
+ logger.debug("setupHttpClient() using url=" + url +\r
+ " user=" + user + " password=" + password);\r
+ }\r
+\r
+ httpClient.getState().setCredentials(\r
+ new AuthScope(url.getHost(), url.getPort(), AuthScope.ANY_REALM),\r
+ new UsernamePasswordCredentials(user, password));\r
+ //JAXRS client library requires HTTP preemptive authentication\r
+ httpClient.getParams().setAuthenticationPreemptive(true);\r
+ if(logger.isDebugEnabled()){\r
+ logger.debug("setupHttpClient: set preemptive authentication");\r
+ }\r
+ }\r
+ }\r
+}\r
-/**\r
- * This document is a part of the source code and related artifacts\r
- * for CollectionSpace, an open source collections management system\r
- * for museums and related institutions:\r
-\r
- * http://www.collectionspace.org\r
- * http://wiki.collectionspace.org\r
-\r
- * Copyright 2009 University of California at Berkeley\r
-\r
- * Licensed under the Educational Community License (ECL), Version 2.0.\r
- * You may not use this file except in compliance with this License.\r
-\r
- * You may obtain a copy of the ECL 2.0 License at\r
-\r
- * https://source.collectionspace.org/collection-space/LICENSE.txt\r
-\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-package org.collectionspace.services.client;\r
-\r
-import java.io.InputStream;\r
-import java.net.URL;\r
-import java.util.Properties;\r
-import org.apache.commons.httpclient.HttpClient;\r
-import org.apache.commons.httpclient.UsernamePasswordCredentials;\r
-import org.apache.commons.httpclient.auth.AuthScope;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-/**\r
- * CollectionSpaceClient is an abstract base client of all service clients\r
- */\r
-public abstract class CollectionSpaceClient {\r
-\r
- public static final String USER_PROPERTY = "cspace.user";\r
- public static final String PASSWORD_PROPERTY = "cspace.password";\r
- public static final String AUTH_PROPERTY = "cspace.auth";\r
- public static final String SSL_PROPERTY = "cspace.ssl";\r
- public static final String URL_PROPERTY = "cspace.url";\r
-\r
- \r
- private static final String PATH = "/cspace-services/";\r
-\r
- protected final Logger logger = LoggerFactory.getLogger(CollectionSpaceClient.class);\r
- private Properties properties = new Properties();\r
- private URL url;\r
- private HttpClient httpClient;\r
-\r
- protected CollectionSpaceClient() {\r
-\r
- readProperties();\r
-\r
- if(useAuth()){\r
- httpClient = new HttpClient();\r
- String user = properties.getProperty(USER_PROPERTY);\r
- String password = properties.getProperty(PASSWORD_PROPERTY);\r
- if(logger.isDebugEnabled()){\r
- logger.debug("using user=" + user + " password=" + password);\r
- }\r
- httpClient.getState().setCredentials(\r
- new AuthScope(url.getHost(), url.getPort(), AuthScope.ANY_REALM),\r
- new UsernamePasswordCredentials(user, password));\r
- httpClient.getParams().setAuthenticationPreemptive(true);\r
- if(logger.isDebugEnabled()){\r
- logger.debug("set up httpClient for authentication");\r
- }\r
- }\r
- }\r
-\r
- protected String getBaseURL() {\r
- return (String) properties.getProperty(URL_PROPERTY);\r
- }\r
-\r
- protected HttpClient getHttpClient() {\r
- return httpClient;\r
- }\r
-\r
- protected boolean useAuth() {\r
- String auth = properties.getProperty(AUTH_PROPERTY);\r
- return Boolean.valueOf(auth);\r
- }\r
-\r
- protected boolean useSSL() {\r
- String ssl = properties.getProperty(SSL_PROPERTY);\r
- return Boolean.valueOf(ssl);\r
- }\r
-\r
- /**\r
- * readProperties reads properties from system class path as well\r
- * as it overrides properties made available using command line\r
- * @exception RuntimeException\r
- */\r
- private void readProperties() {\r
-\r
- ClassLoader cl = Thread.currentThread().getContextClassLoader();\r
- InputStream is = null;\r
- try{\r
- is = cl.getResourceAsStream("collectionspace-client.properties");\r
- properties.load(is);\r
- if(logger.isDebugEnabled()){\r
- for(Object kobj : properties.keySet()){\r
- String key = (String) kobj;\r
- logger.debug("begin property name=" + key + " value=" + properties.get(key));\r
- }\r
- }\r
- String spec = System.getProperty(URL_PROPERTY);\r
- if(spec != null && !"".equals(spec)){\r
- properties.setProperty(URL_PROPERTY, spec);\r
- }\r
- spec = properties.getProperty(URL_PROPERTY);\r
- url = new URL(spec);\r
-\r
- String auth = System.getProperty(AUTH_PROPERTY);\r
- if(auth != null && !"".equals(auth)){\r
- properties.setProperty(AUTH_PROPERTY, auth);\r
- }\r
- String ssl = System.getProperty(SSL_PROPERTY);\r
- if(ssl != null && !"".equals(ssl)){\r
- properties.setProperty(AUTH_PROPERTY, ssl);\r
- }\r
- String user = System.getProperty(USER_PROPERTY);\r
- if(user != null && !"".equals(user)){\r
- properties.setProperty(USER_PROPERTY, user);\r
- }\r
- String password = System.getProperty(PASSWORD_PROPERTY);\r
- if(password != null && !"".equals(password)){\r
- properties.setProperty(PASSWORD_PROPERTY, password);\r
- }\r
- if(logger.isDebugEnabled()){\r
- for(Object kobj : properties.keySet()){\r
- String key = (String) kobj;\r
- logger.debug("end property name=" + key + " value=" + properties.get(key));\r
- }\r
- }\r
- }catch(Exception e){\r
- logger.debug("Caught exception while reading properties", e);\r
- throw new RuntimeException(e);\r
- }finally{\r
- if(is != null){\r
- try{\r
- is.close();\r
- }catch(Exception e){\r
- }\r
- }\r
- }\r
- }\r
-}\r
+/**
+ * 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();
+
+}
* @version $Revision:$
*/
-public class CollectionObjectClient extends CollectionSpaceClient {
+public class CollectionObjectClient extends BaseServiceClient {
- /**
- *
- */
- private static final CollectionObjectClient instance = new CollectionObjectClient();
/**
*
*/
* 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());
}
}
- /**
- * FIXME Comment this
- *
- * @return
- */
- public static CollectionObjectClient getInstance() {
- return instance;
- }
-
/**
* @return
* @see org.collectionspace.hello.client.CollectionObjectProxy#getCollectionObject()
*/
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);
* @version $Revision:$
*/
-public class IntakeClient extends CollectionSpaceClient {
+public class IntakeClient extends BaseServiceClient {
/**