Example usage for org.apache.commons.httpclient HttpConnection setConnectionTimeout

List of usage examples for org.apache.commons.httpclient HttpConnection setConnectionTimeout

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpConnection setConnectionTimeout.

Prototype

@Deprecated
public void setConnectionTimeout(int timeout) 

Source Link

Document

Sets the connection timeout.

Usage

From source file:org.apache.webdav.lib.NotificationListener.java

/**
 * Registers a Subscriber with the remote server. 
 * // www.  ja va2  s.  c  o  m
 * @param method the "notification type", determines for what events do you
 *               want do subscribe. one of  "Update", "Update/newmember",
 *               "Delete", "Move".
 * @param uri the resource for that you subscribe
 * @param depth the depth of the collection tree that you want to observe
 * @param lifetime the duration for that you want to observe (in seconds)
 * @param notificationDelay the time the server waits before it sends a notify
 *                          message to the host provided in the constructor
 *                          (in seconds)
 * @param listener the Subscriber that is called on incomming notifications
 * @param credentials credentials for authentication on the server observed
 * @return boolean true if subscription succeeded, false if subscription failed
 *
 * @see WebdavResource#subscribeMethod
 * @see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/e2k3/e2k3/_webdav_subscribe.asp
 */
public boolean subscribe(String method, String uri, int depth, int lifetime, int notificationDelay,
        Subscriber listener, Credentials credentials) {
    SubscribeMethod subscribeMethod = new SubscribeMethod(repositoryDomain + uri);
    subscribeMethod.addRequestHeader(SubscribeMethod.H_NOTIFICATION_TYPE, method);
    if (udp) {
        subscribeMethod.addRequestHeader(SubscribeMethod.H_CALL_BACK,
                "httpu://" + notificationHost + ":" + notificationPort);
    } else {
        subscribeMethod.addRequestHeader(SubscribeMethod.H_CALL_BACK,
                "http://" + notificationHost + ":" + notificationPort);
    }
    subscribeMethod.addRequestHeader(SubscribeMethod.H_NOTIFICATION_DELAY, String.valueOf(notificationDelay));
    subscribeMethod.addRequestHeader(SubscribeMethod.H_SUBSCRIPTION_LIFETIME, String.valueOf(lifetime));
    subscribeMethod.addRequestHeader(SubscribeMethod.H_DEPTH,
            ((depth == DepthSupport.DEPTH_INFINITY) ? "infinity" : String.valueOf(depth)));
    try {
        subscribeMethod.setDoAuthentication(true);
        HttpState httpState = new HttpState();
        httpState.setCredentials(null, repositoryHost, credentials);
        HttpConnection httpConnection = new HttpConnection(repositoryHost, repositoryPort, protocol);
        httpConnection.setConnectionTimeout(CONNECTION_TIMEOUT);
        int state = subscribeMethod.execute(httpState, httpConnection);
        if (state == HttpStatus.SC_OK) {
            String subscriptionId = subscribeMethod.getResponseHeader(SubscribeMethod.H_SUBSCRIPTION_ID)
                    .getValue();
            logger.log(Level.INFO, "Received subscription id=" + subscriptionId + ", listener: " + listener);
            int id = Integer.valueOf(subscriptionId).intValue();
            synchronized (subscribers) {
                subscribers.add(new Subscription(id, uri, listener));
            }
            if (subscribersAsString == null) {
                subscribersAsString = String.valueOf(id);
            } else {
                subscribersAsString = subscribersAsString + ", " + String.valueOf(id);
            }
            return true;
        } else {
            logger.log(Level.SEVERE, "Subscription for uri='" + uri + "' failed. State: " + state);
        }
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Subscription of listener '" + listener + "' failed!", e);
    }
    return false;
}

From source file:org.apache.webdav.lib.NotificationListener.java

public boolean unsubscribe(String uri, Subscriber listener, Credentials credentials) {
    UnsubscribeMethod unsubscribeMethod = new UnsubscribeMethod(repositoryDomain + uri);
    synchronized (subscribers) {
        for (Iterator i = subscribers.iterator(); i.hasNext();) {
            Subscription subscription = (Subscription) i.next();
            if (subscription.getSubscriber().equals(listener)) {
                String id = String.valueOf(subscription.getId());
                unsubscribeMethod.addRequestHeader(UnsubscribeMethod.H_SUBSCRIPTION_ID, id);
                try {
                    unsubscribeMethod.setDoAuthentication(true);
                    HttpState httpState = new HttpState();
                    httpState.setCredentials(null, repositoryHost, credentials);
                    HttpConnection httpConnection = new HttpConnection(repositoryHost, repositoryPort,
                            protocol);/*from  w  w w  .j  av a 2s. c om*/
                    httpConnection.setConnectionTimeout(CONNECTION_TIMEOUT);
                    int state = unsubscribeMethod.execute(httpState, httpConnection);
                    if (state == HttpStatus.SC_OK) {
                        i.remove();
                        return true;
                    } else {
                        logger.log(Level.SEVERE, "Unsubscription failed. State: " + state);
                    }
                } catch (IOException e) {
                    logger.log(Level.SEVERE, "Unsubscription of listener '" + listener + "' failed!", e);
                }
            }
        }
    }
    logger.log(Level.SEVERE, "Listener not unsubscribed!");
    return false;
}

From source file:org.apache.webdav.lib.NotificationListener.java

protected void poll(String notifiedSubscribers) {
    StringBuffer registeredSubscribers = new StringBuffer(256);
    StringTokenizer tokenizer = new StringTokenizer(notifiedSubscribers, ",");
    boolean first = true;
    while (tokenizer.hasMoreTokens()) {
        String subscriber = tokenizer.nextToken().trim();
        if (isRegistered(Integer.valueOf(subscriber).intValue())) {
            if (!first)
                registeredSubscribers.append(',');
            registeredSubscribers.append(subscriber);
            first = false;//from  w  w  w . j a  v a2 s  .c o  m
        }
    }
    if (!first) {
        String pollSubscribers = registeredSubscribers.toString();
        logger.log(Level.INFO, "Poll for subscribers: " + pollSubscribers);
        PollMethod pollMethod = new PollMethod(repositoryDomain + "/");
        pollMethod.addRequestHeader(SubscribeMethod.H_SUBSCRIPTION_ID, pollSubscribers);
        try {
            pollMethod.setDoAuthentication(true);
            HttpState httpState = new HttpState();
            httpState.setCredentials(null, repositoryHost, credentials);
            HttpConnection httpConnection = new HttpConnection(repositoryHost, repositoryPort, protocol);
            httpConnection.setConnectionTimeout(CONNECTION_TIMEOUT);
            int state = pollMethod.execute(httpState, httpConnection);
            if (state == HttpStatus.SC_MULTI_STATUS) {
                List events = pollMethod.getEvents();
                for (Iterator i = events.iterator(); i.hasNext();) {
                    Event event = (Event) i.next();
                    fireEvent(event.getId(), event.getInformation());
                }
            } else {
                logger.log(Level.SEVERE, "Poll failed. State: " + state);
            }
        } catch (IOException e) {
            logger.log(Level.SEVERE, "Poll for subscribers '" + subscribers + "' failed!");
        }
    }
}