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

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

Introduction

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

Prototype

public HttpConnection(String host, int port, Protocol protocol) 

Source Link

Document

Creates a new HTTP connection for the given host and port using the given protocol.

Usage

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

/**
 * Registers a Subscriber with the remote server. 
 * /*from  w w  w  .  j  a  v  a2 s.co  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  ww  .j  ava2  s  .c o  m*/
                    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 fireEvent(EventMethod eventMethod, Credentials credentials) throws IOException {
    eventMethod.setDoAuthentication(true);
    HttpState httpState = new HttpState();
    httpState.setCredentials(null, repositoryHost, credentials);
    int state = eventMethod.execute(httpState, new HttpConnection(repositoryHost, repositoryPort, protocol));
    if (state == HttpStatus.SC_OK) {
    } else {//from   w w w  .j a  v  a2 s.com
        logger.log(Level.SEVERE, "Event failed. State: " + state);
    }
}

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;//w w  w  . ja v a  2  s  .  co 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!");
        }
    }
}