Skip to content

Commit

Permalink
SA-50 switch to HttpClient 4.1 for CalDAV integration
Browse files Browse the repository at this point in the history
git-svn-id: https://source.jasig.org/sa/sched-assist/trunk@25908 f5dbab47-78f9-eb45-b975-e544023573eb
  • Loading branch information
nblair committed Feb 28, 2012
1 parent ec0b1c2 commit 9f7cab3
Show file tree
Hide file tree
Showing 23 changed files with 580 additions and 260 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<cglib.version>2.2.2</cglib.version>
<commons.codec.version>1.4</commons.codec.version>
<commons.fileupload.version>1.2.1</commons.fileupload.version>
<commons.httpclient.version>4.1.2</commons.httpclient.version>
<commons.io.version>1.4</commons.io.version>
<commons.lang.version>2.6</commons.lang.version>
<commons.logging.version>1.1.1</commons.logging.version>
Expand Down Expand Up @@ -130,6 +131,11 @@
<artifactId>commons-httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${commons.httpclient.version}</version>
</dependency>
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions sched-assist-spi-caldav/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import net.fortuna.ical4j.model.component.VEvent;

import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.http.HttpEntity;
import org.jasig.schedassist.model.ICalendarAccount;

/**
Expand Down Expand Up @@ -106,7 +106,7 @@ public interface CaldavDialect {
* @param event
* @return a {@link RequestEntity} used with the PUT request to store the specified {@link VEvent}
*/
RequestEntity generatePutAppointmentRequestEntity(VEvent event);
HttpEntity generatePutAppointmentRequestEntity(VEvent event);

/**
* Generate an appropriate {@link RequestEntity} body for retrieving Calendar data between
Expand All @@ -116,5 +116,5 @@ public interface CaldavDialect {
* @param endDate
* @return a {@link RequestEntity} used with the REPORT request to retrieve an account's Calendar data between the 2 {@link Date}s
*/
RequestEntity generateGetCalendarRequestEntity(Date startDate, Date endDate);
HttpEntity generateGetCalendarRequestEntity(Date startDate, Date endDate);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Jasig licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a
* copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.jasig.schedassist.impl.caldav;

import org.apache.http.client.CredentialsProvider;
import org.jasig.schedassist.model.ICalendarAccount;

/**
* Factory interface for HTTP Client's {@link CredentialsProvider}s.
*
* @author Nicholas Blair
* @version $Id: CredentialsProviderProvider.java $
*/
public interface CredentialsProviderFactory {

/**
* Return a {@link CredentialsProvider} that can be used when performing caldav operations against
* the provided {@link ICalendarAccount}.
*
* Implementations may not all use the {@link ICalendarAccount}; some return the same set of
* credentials for all inputs.
*
* @param account
* @return a never null {@link CredentialsProvider} that the data dao can use to interact with the CalDAV server
*/
CredentialsProvider getCredentialsProvider(ICalendarAccount account);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.property.ProdId;

import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.lang.Validate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
import org.jasig.schedassist.model.ICalendarAccount;
import org.jasig.schedassist.model.IEventUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -121,12 +121,12 @@ public void setEventUtils(IEventUtils eventUtils) {
* (non-Javadoc)
* @see org.jasig.schedassist.impl.caldav.CaldavDialect#generateGetCalendarRequestEntity(java.util.Date, java.util.Date)
*/
public RequestEntity generateGetCalendarRequestEntity(Date startDate, Date endDate) {
public HttpEntity generateGetCalendarRequestEntity(Date startDate, Date endDate) {
final String content = generateGetCalendarRequestXML(startDate, endDate);

StringRequestEntity requestEntity;
StringEntity requestEntity;
try {
requestEntity = new StringRequestEntity(content, "text/xml", "UTF-8");
requestEntity = new StringEntity(content, "text/xml", "UTF-8");
return requestEntity;
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
Expand Down Expand Up @@ -164,10 +164,10 @@ protected String generateGetCalendarRequestXML(Date startDate, Date endDate) {
* (non-Javadoc)
* @see org.jasig.schedassist.impl.caldav.CaldavDialect#generateCreateAppointmentRequestEntity(net.fortuna.ical4j.model.component.VEvent)
*/
public RequestEntity generatePutAppointmentRequestEntity(VEvent event) {
public HttpEntity generatePutAppointmentRequestEntity(VEvent event) {
final String requestEntityBody = this.eventUtils.wrapEventInCalendar(event).toString();
try {
StringRequestEntity requestEntity = new StringRequestEntity(requestEntityBody, "text/calendar", "UTF-8");
StringEntity requestEntity = new StringEntity(requestEntityBody, "text/calendar", "UTF-8");
return requestEntity;
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@

package org.jasig.schedassist.impl.caldav;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.jasig.schedassist.model.ICalendarAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

/**
* Default {@link HttpCredentialsProvider} uses the exact same credentials for
* all accounts.
* Default {@link CredentialsProviderFactory} implementation, returns
* {@link DefaultCredentialsProviderImpl} instances.
*
* The intent with this implementation is that the administrative credentials you configure
* have full read/write/admin privileges for all {@link ICalendarAccount}s. in your calendar
* system.
*
* @author Nicholas Blair
* @version $Id: DefaultHttpCredentialsProviderImpl.java $
* @version $Id: DefaultCredentialsProviderProviderImpl.java $
*/
public class DefaultHttpCredentialsProviderImpl implements
HttpCredentialsProvider {
public class DefaultCredentialsProviderFactoryImpl implements
CredentialsProviderFactory {

private String caldavAdminUsername;
private String caldavAdminPassword;

private AuthScope authScope;
/**
* @param caldavAdminUsername the caldavAdminUsername to set
*/
Expand All @@ -67,13 +67,32 @@ protected String getCaldavAdminUsername() {
protected String getCaldavAdminPassword() {
return caldavAdminPassword;
}
/**
* @return the authScope
*/
public AuthScope getAuthScope() {
return authScope;
}
/**
* @param authScope the authScope to set
*/
@Autowired
public void setAuthScope(AuthScope authScope) {
this.authScope = authScope;
}
/**
*
* @return a {@link Credentials} made up of {@link #getCaldavAdminUsername()} and {@link #getCaldavAdminPassword()}.
*/
protected Credentials getAdminCredentials() {
return new UsernamePasswordCredentials(getCaldavAdminUsername(), getCaldavAdminPassword());
}
/* (non-Javadoc)
* @see org.jasig.schedassist.impl.caldav.HttpCredentialsProvider#getCredentials(org.jasig.schedassist.model.ICalendarAccount)
* @see org.jasig.schedassist.impl.caldav.CredentialsProviderProvider#getCredentialsProvider(org.jasig.schedassist.model.ICalendarAccount)
*/
@Override
public Credentials getCredentials(ICalendarAccount account) {
UsernamePasswordCredentials adminCredentials = new UsernamePasswordCredentials(caldavAdminUsername, caldavAdminPassword);
return adminCredentials;
public CredentialsProvider getCredentialsProvider(ICalendarAccount account) {
DefaultCredentialsProviderImpl provider = new DefaultCredentialsProviderImpl(getAdminCredentials(), authScope);
return provider;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Jasig licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a
* copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.jasig.schedassist.impl.caldav;

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

/**
* This {@link CredentialsProvider} implementation will return the
* supplied-at-construction-time {@link Credentials} if and only if
* the request matches the configured {@link AuthScope}.
*
* @author Nicholas Blair
* @version $Id: DefaultCredentialsProviderImpl.java $
*/
public class DefaultCredentialsProviderImpl implements CredentialsProvider {

private final Credentials adminCredentials;
private final AuthScope targetAuthScope;
/**
*
* @param adminCredentials
* @param targetAuthScope
*/
public DefaultCredentialsProviderImpl(Credentials adminCredentials,
AuthScope targetAuthScope) {
this.adminCredentials = adminCredentials;
this.targetAuthScope = targetAuthScope;
}
/**
* @return the adminCredentials
*/
public Credentials getAdminCredentials() {
return adminCredentials;
}
/**
* @return the targetAuthScope
*/
public AuthScope getTargetAuthScope() {
return targetAuthScope;
}
/*
* (non-Javadoc)
* @see org.apache.http.client.CredentialsProvider#clear()
*/
@Override
public void clear() {
throw new UnsupportedOperationException("clear not supported");
}
/*
* (non-Javadoc)
* @see org.apache.http.client.CredentialsProvider#getCredentials(org.apache.http.auth.AuthScope)
*/
@Override
public Credentials getCredentials(AuthScope authscope) {
if(!targetAuthScope.equals(authscope)) {
return null;
}
return adminCredentials;
}
/*
* (non-Javadoc)
* @see org.apache.http.client.CredentialsProvider#setCredentials(org.apache.http.auth.AuthScope, org.apache.http.auth.Credentials)
*/
@Override
public void setCredentials(AuthScope arg0, Credentials arg1) {
throw new UnsupportedOperationException("setCredentials not supported");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

package org.jasig.schedassist.impl.caldav;

import org.apache.commons.httpclient.Credentials;
import org.apache.http.auth.Credentials;
import org.apache.http.client.CredentialsProvider;
import org.jasig.schedassist.model.ICalendarAccount;

/**
Expand All @@ -40,5 +41,5 @@ public interface HttpCredentialsProvider {
* @param account
* @return a never null {@link Credentials} that the data dao can use to interact with the CalDAV server
*/
Credentials getCredentials(ICalendarAccount account);
CredentialsProvider getCredentials(ICalendarAccount account);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
*/
package org.jasig.schedassist.impl.caldav;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.http.HttpRequest;
import org.apache.http.client.HttpClient;
import org.jasig.schedassist.model.ICalendarAccount;

/**
* Different CalDAV servers may expect different information in the HTTP requests.
*
* This interface provides a mechanism for altering the {@link HttpMethod}s
* This interface provides a mechanism for altering the {@link HttpRequest}s
* created by the {@link CaldavCalendarDataDaoImpl} before they are sent to the CalDAV
* server.
*
Expand All @@ -39,14 +39,14 @@
public interface HttpMethodInterceptor {

/**
* Implementations will receive the {@link HttpMethod} just before the
* {@link HttpClient#executeMethod(HttpMethod)} is called on it.
* Implementations will receive the {@link HttpRequest} just before the
* {@link HttpClient#executeMethod(HttpRequest)} is called on it.
*
* Implementations are allowed to mutate the method as needed, but MUST never return null.
*
* @param method
* @param onBehalfOf the {@link ICalendarAccount} that matches the account the request will be on behalf of
* @return the CalDAV implementation-specific altered {@link HttpMethod}
* @return the CalDAV implementation-specific altered {@link HttpRequest}
*/
HttpMethod doWithMethod(HttpMethod method, ICalendarAccount onBehalfOf);
HttpRequest doWithMethod(HttpRequest method, ICalendarAccount onBehalfOf);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,23 @@
*/
package org.jasig.schedassist.impl.caldav;

import org.apache.commons.httpclient.HttpMethod;
import org.apache.http.HttpRequest;
import org.apache.http.message.AbstractHttpMessage;
import org.jasig.schedassist.model.ICalendarAccount;

/**
* Does nothing to the {@link HttpMethod}.
* Does nothing to the {@link AbstractHttpMessage}.
* @author Nicholas Blair
* @version $ Id: NoopHttpMethodInterceptorImpl.java $
*/
public class NoopHttpMethodInterceptorImpl implements HttpMethodInterceptor {
/* (non-Javadoc)
* @see org.jasig.schedassist.impl.caldav.HttpMethodInterceptor#doWithMethod(org.apache.commons.httpclient.HttpMethod, org.jasig.schedassist.model.ICalendarAccount)

/*
* (non-Javadoc)
* @see org.jasig.schedassist.impl.caldav.HttpMethodInterceptor#doWithMethod(org.apache.http.HttpRequest, org.jasig.schedassist.model.ICalendarAccount)
*/
@Override
public HttpMethod doWithMethod(HttpMethod method,
public HttpRequest doWithMethod(HttpRequest method,
ICalendarAccount onBehalfOf) {
return method;
}
Expand Down
Loading

0 comments on commit 9f7cab3

Please sign in to comment.