Skip to content

Commit

Permalink
Adding factories to handle https and credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-at-rsmart committed Jun 18, 2012
1 parent 7683132 commit 5a8e5a2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
14 changes: 12 additions & 2 deletions pom.xml
Expand Up @@ -37,7 +37,7 @@
-->
<modelVersion>4.0.0</modelVersion>
<artifactId>couchdb-intro</artifactId>
<packaging>war</packaging>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<groupId>com.clearboxmedia</groupId>
<description></description>
Expand Down Expand Up @@ -103,6 +103,16 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<excludes>
<exclude>**/it/*Test.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
Expand All @@ -122,7 +132,7 @@
<configuration>
<skip>false</skip>
<systemPropertyVariables>
<couchdb.username>${couchdb.url}</couchdb.username>
<couchdb.url>${couchdb.url}</couchdb.url>
<couchdb.username>${couchdb.username}</couchdb.username>
<couchdb.password>${couchdb.password}</couchdb.password>
</systemPropertyVariables>
Expand Down
Expand Up @@ -28,26 +28,41 @@
*/
package com.clearboxmedia.couchspring.couch;

import java.net.MalformedURLException;
import java.net.URL;

import org.jcouchdb.db.ServerImpl;

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;

import java.util.logging.Logger;

/**
* Overrides {@link org.jcouchdb.db.ServerImpl} to force AuthScope.ANY for credentials
*
*
* @author Leo Przybylski (leo [at] clearboxmedia.com)
*/
public class ServerImpl extends org.jcouchdb.db.ServerImpl {
public class CouchDbServerFactory {
public static final Logger LOG = Logger.getLogger(CouchDbServerFactory.class.getSimpleName());

public ServerImpl(final String hostname) {
super(hostname);
public ServerImpl createCouchDbServerInstance(final String url) throws Exception {
return this.createCouchDbServerInstance(url, null);
}

public ServerImpl(final String hostname, int port) {
super(hostname, port);
public ServerImpl createCouchDbServerInstance(final String url, final Credentials credentials) throws Exception {
return this.createCouchDbServerInstance(new URL(url), null);
}

public void setCredentials(Credentials credentials) {
super.setCredentials(AuthScope.ANY, credentials);
public ServerImpl createCouchDbServerInstance(final URL url, final Credentials credentials) {
final ServerImpl retval = new ServerImpl(url.getHost(), url.getPort(), url.getProtocol().equals("https"));
if (credentials != null) {
retval.setCredentials(AuthScope.ANY, credentials);
}

LOG.warning("Creating Couch DB Server instance with port: " + url.getPort());

return retval;
}
}
19 changes: 11 additions & 8 deletions src/test/resources/couchdb-config.xml
Expand Up @@ -21,9 +21,17 @@

<bean id="jsonConfig" factory-bean="jsonConfigFactory" factory-method="createJsonConfig"/>

<bean id="couchDBServer" class="com.clearboxmedia.couchspring.couch.ServerImpl">
<constructor-arg value="${couchdb.url}"/>
<property name="credentials" ref="couchPrincipal" />
<!-- If my db requires username/password, I will need to set up a Principal -->
<bean id="couchPrincipal" class="org.apache.http.auth.UsernamePasswordCredentials">
<constructor-arg value="${couchdb.username}" />
<constructor-arg value="${couchdb.password}" />
</bean>

<bean id="serverFactory" class="com.clearboxmedia.couchspring.couch.CouchDbServerFactory" />

<bean id="couchDBServer" factory-bean="serverFactory" factory-method="createCouchDbServerInstance">
<constructor-arg value="${couchdb.url}"/>
<constructor-arg name="credentials" ref="couchPrincipal" />
</bean>

<bean id="systemDatabase" class="org.jcouchdb.db.Database">
Expand All @@ -32,9 +40,4 @@
<property name="jsonConfig" ref="jsonConfig"/>
</bean>

<bean id="couchPrincipal" class="org.apache.http.auth.UsernamePasswordCredentials">
<constructor-arg value="${couchdb.username}" />
<constructor-arg value="${couchdb.userpassword}" />
</bean>

</beans>

0 comments on commit 5a8e5a2

Please sign in to comment.