luetzkendorf 2004/07/06 10:03:56
Added: webdavclient/clientlib/src/java/org/apache/webdav/lib/methods
SubscribeMethod.java UnsubscribeMethod.java
Log:
first methods for notification support added
Revision Changes Path
1.1
jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/SubscribeMethod.java
Index: SubscribeMethod.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/SubscribeMethod.java,v
1.1 2004/07/06 17:03:56 luetzkendorf Exp $
* $Revision: 1.1 $
* $Date: 2004/07/06 17:03:56 $
*
* ====================================================================
*
* Copyright 1999-2002 The Apache Software Foundation
*
* Licensed 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.apache.webdav.lib.methods;
import java.io.IOException;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpConnection;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpState;
/**
* Implements the SUBSCRIBE method.
*
* @author Stefan L�tzkendorf
* @see <a
href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/e2k3/e2k3/_webdav_subscribe.asp">Reference</a>
*/
public class SubscribeMethod extends XMLResponseMethodBase
implements DepthSupport
{
private static final String HEADER_SUBSCRIPTION_ID = "Subscription-Id";
private static final String HEADER_SUBSCRIPTION_LIFETIME =
"Subscription-Lifetime";
private static final String HEADER_NOTIFICATION_TYPE = "Notification-Type";
private static final String HEADER_NOTIFICATION_DELAY = "Notification-Delay";
private static final String HEADER_DEPTH = "Depth";
private static final String HEADER_CALL_BACK = "Call-Back";
public static final String TYPE_UPDATE = "update";
public static final String TYPE_UPDATE_NEW_MEMBER = "update/newmember";
public static final String TYPE_DELETE = "delete";
public static final String TYPE_MOVE = "move";
private String callback = null;
private String notificationType = null;
private int depth = -1;
private long subsciptionLifetime = -1;
private long subscriptionId = -1;
private long notificationDelay = -1;
private long responsedSubscriptionLifetime = -1;
private long responsedSubscriptionId = -1;
public String getCallback()
{
return callback;
}
/**
* Sets the URI tha's to be notified if the subscribed event does occur.
*/
public void setCallback(String callback)
{
this.callback = callback;
}
public String getNotificationType()
{
return notificationType;
}
/**
* Sets the notification type, i.e. determines the events that are
* subscribed.
* @see [EMAIL PROTECTED] #TYPE_DELETE}, [EMAIL PROTECTED] #TYPE_MOVE},
* [EMAIL PROTECTED] #TYPE_UPDATE}, [EMAIL PROTECTED]
#TYPE_UPDATE_NEW_MEMBER}
*/
public void setNotificationType(String notificationType)
{
this.notificationType = notificationType;
}
public long getSubsciptionLifetime()
{
return subsciptionLifetime;
}
/**
* Sets the duration of the subscription in seconds.
*/
public void setSubsciptionLifetime(long subsciptionLifetime)
{
this.subsciptionLifetime = subsciptionLifetime;
}
public long getSubscriptionId()
{
return subscriptionId;
}
/**
* Sets the ID of a subscription to be refreshed.
* @param subscriptionId
*/
public void setSubscriptionId(long subscriptionId)
{
this.subscriptionId = subscriptionId;
}
/**
* Sets the notification delay in seconds.
*/
public void setNotificationDelay(long delay) {
this.notificationDelay = delay;
}
public long getNotificationDelay() {
return this.notificationDelay;
}
public int getDepth()
{
return this.depth;
}
/**
* Sets the depth.
*/
public void setDepth(int depth)
{
switch(depth) {
case DEPTH_0:
case DEPTH_1:
case DEPTH_INFINITY:
this.depth = depth;
break;
default:
throw new IllegalArgumentException(
"Depth must be 0, 1 or "+DEPTH_INFINITY+".");
}
}
/**
* Returns the subscription ID responsed from the server.
* @return -1 if no subscription id was in the response
*/
public long getResponsedSubscriptionId() {
checkUsed();
return this.responsedSubscriptionId;
}
/**
* Returns the subscription lifetime responsed from the server.
* @return -1 if no subscription lifetime was given in the response
*/
public long getResponsedSubscriptionLifetime() {
checkUsed();
return this.responsedSubscriptionLifetime;
}
// --------------------------------------------------- WebdavMethod Methods
public String getName()
{
return "SUBSCRIBE";
}
public void recycle()
{
super.recycle();
this.callback = null;
this.depth = -1;
this.notificationDelay = -1;
this.notificationType = null;
this.responsedSubscriptionId = -1;
this.responsedSubscriptionLifetime = -1;
this.subsciptionLifetime = -1;
this.subscriptionId = -1;
}
protected void addRequestHeaders(HttpState state, HttpConnection conn)
throws IOException, HttpException
{
super.addRequestHeaders(state, conn);
if (this.callback != null) {
super.setRequestHeader(HEADER_CALL_BACK, this.callback);
}
if (this.depth > -1) {
super.setRequestHeader(HEADER_DEPTH, Integer.toString(this.depth));
}
if (this.notificationType != null) {
super.setRequestHeader(HEADER_NOTIFICATION_TYPE, this.notificationType);
}
if (this.subsciptionLifetime > -1) {
super.setRequestHeader(HEADER_SUBSCRIPTION_LIFETIME,
Long.toString(this.subsciptionLifetime));
}
if (this.subscriptionId > -1) {
super.setRequestHeader(HEADER_SUBSCRIPTION_ID, Long.toString(
this.subscriptionId));
}
if (this.notificationDelay > -1) {
super.setRequestHeader(HEADER_NOTIFICATION_DELAY, Long.toString(
this.notificationDelay));
}
}
/**
* Adds special checking of header values of the SUBSCRIBE method to
* the super class implementation.
*/
public void setRequestHeader(String headerName, String headerValue)
{
try {
if (headerName.equalsIgnoreCase(HEADER_DEPTH)){
setDepth(Integer.parseInt(headerValue));
}
else if(headerName.equals(HEADER_SUBSCRIPTION_ID)) {
setSubscriptionId(Long.parseLong(headerValue));
}
else if(headerName.equals(HEADER_SUBSCRIPTION_LIFETIME)) {
setSubscriptionId(Long.parseLong(headerValue));
}
else if(headerName.equals(HEADER_NOTIFICATION_DELAY)) {
setNotificationDelay(Long.parseLong(headerValue));
}
else {
super.setRequestHeader(headerName, headerValue);
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid header value '" +
headerValue + "' for header " + headerName + "!");
}
}
protected void processResponseHeaders(HttpState state, HttpConnection conn)
{
super.processResponseHeaders(state, conn);
Header header;
header = getResponseHeader(HEADER_SUBSCRIPTION_ID);
if (header != null) {
this.responsedSubscriptionId = Long.parseLong(header.getValue());
}
header = getRequestHeader(HEADER_SUBSCRIPTION_LIFETIME);
if (header != null) {
this.responsedSubscriptionLifetime = Long.parseLong(header.getValue());
}
}
}
1.1
jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/UnsubscribeMethod.java
Index: UnsubscribeMethod.java
===================================================================
// vi: set ts=3 sw=3:
package org.apache.webdav.lib.methods;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.apache.commons.httpclient.HttpConnection;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpState;
/**
* Implements the UNSUBSCRIBE method.
*
* @author Stefan L�tzkendorf
* @see <a
href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/e2k3/e2k3/_webdav_unsubscribe.asp">Reference</a>
*/
public class UnsubscribeMethod extends XMLResponseMethodBase
{
private static final String HEADER_SUBSCRIPTION_ID = "Subscription-Id";
private List subscriptionIds = new ArrayList();
/**
* Adds an ID for a subscription that is to be withdrawn.
*/
public void addSubscriptionId(long id) {
this.subscriptionIds.add(new Long(id));
}
// --------------------------------------------------- WebdavMethod Methods
public String getName()
{
return "UNSUBSCRIBE";
}
public void recycle()
{
this.subscriptionIds.clear();
}
protected void addRequestHeaders(HttpState state, HttpConnection conn)
throws IOException, HttpException
{
super.addRequestHeaders(state, conn);
if (this.subscriptionIds.size() > 0) {
StringBuffer b = new StringBuffer();
boolean first = true;
for (Iterator i = this.subscriptionIds.iterator(); i.hasNext();) {
if (first) first = false; else b.append(", ");
b.append(i.next());
}
super.addRequestHeader(HEADER_SUBSCRIPTION_ID, b.toString());
}
}
/**
* Adds special checking of header values of the UNSUBSCRIBE method to
* the super class implementation.
*/
public void setRequestHeader(String headerName, String headerValue)
{
if (headerName.equalsIgnoreCase(HEADER_SUBSCRIPTION_ID)){
StringTokenizer t = new StringTokenizer(headerValue, ", ");
try {
for(;t.hasMoreTokens();) {
addSubscriptionId(Long.parseLong(t.nextToken()));
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid header value '" +
headerValue + "' for header " + headerName + "!");
}
} else {
super.setRequestHeader(headerName, headerValue);
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]