KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > webdav > method > PollMethod


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/PollMethod.java,v 1.11 2004/08/05 14:43:29 dflorey Exp $
3  * $Revision: 1.11 $
4  * $Date: 2004/08/05 14:43:29 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.webdav.method;
25
26 import java.util.EventObject JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.slide.common.NamespaceAccessToken;
31 import org.apache.slide.event.EventDispatcher;
32 import org.apache.slide.event.RemoteInformation;
33 import org.apache.slide.webdav.WebdavException;
34 import org.apache.slide.webdav.WebdavServletConfig;
35 import org.apache.slide.webdav.event.NotificationTrigger;
36 import org.apache.slide.webdav.event.Subscriber;
37 import org.apache.slide.webdav.event.WebdavEvent;
38 import org.apache.slide.webdav.util.NotificationConstants;
39 import org.apache.slide.webdav.util.WebdavStatus;
40 import org.jdom.Document;
41 import org.jdom.Element;
42 import org.jdom.Namespace;
43 import org.jdom.output.Format;
44 import org.jdom.output.XMLOutputter;
45
46 /**
47  * Poll Method.
48  *
49  */

50 public class PollMethod extends AbstractWebdavMethod implements NotificationConstants {
51     protected final static String JavaDoc LOG_CHANNEL = PollMethod.class.getName();
52     protected final static Namespace NNSP = Namespace.getNamespace(N_PREFIX, N_URI);
53
54     private XMLOutputter xmlOut;
55
56
57     /**
58      * Constructor.
59      *
60      * @param token the token for accessing the namespace
61      * @param config configuration of the WebDAV servlet
62      */

63     public PollMethod(NamespaceAccessToken token, WebdavServletConfig config) {
64         super(token, config);
65         Format format = org.jdom.output.Format.getPrettyFormat();
66         format.setIndent(XML_RESPONSE_INDENT);
67         xmlOut = new XMLOutputter(format);
68     }
69     
70     protected void parseRequest() throws WebdavException {
71     }
72     
73     protected void executeRequest() throws WebdavException {
74         try {
75             if ( WebdavEvent.POLL.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(WebdavEvent.POLL, new WebdavEvent(this));
76             int []subscriptionID = requestHeaders.getSubscriptionId();
77             // includes subscription id's that are not valid any more but requested in the poll-request
78
resp.setContentType( TEXT_XML_UTF_8 );
79             resp.setStatus(WebdavStatus.SC_MULTI_STATUS);
80             Element multistatus = new Element(E_MULTISTATUS, DNSP);
81             boolean content = false;
82             boolean noContent = false;
83             Element contentResponse = new Element(E_RESPONSE, DNSP);
84             int statusCode = WebdavStatus.SC_OK;
85             generateStatusText(contentResponse, requestUri, statusCode);
86             Element noContentResponse = new Element(E_RESPONSE, DNSP);
87             statusCode = WebdavStatus.SC_NO_CONTENT;
88             generateStatusText(noContentResponse, requestUri, statusCode);
89             Element subscriptionIdContent = new Element(E_SUBSCRIPTION_ID, NNSP);
90             Element subscriptionIdNoContent = new Element(E_SUBSCRIPTION_ID, NNSP);
91             contentResponse.addContent(subscriptionIdContent);
92             noContentResponse.addContent(subscriptionIdNoContent);
93             for ( int i = 0; i < subscriptionID.length; i++ ) {
94                 // sort subscribers by the occurance of events
95
Subscriber subscriber = NotificationTrigger.getInstance().getSubscriber(subscriptionID[i]);
96                 if ( subscriber != null ) {
97                     NotificationTrigger.getInstance().refreshSubscriber(subscriber, true);
98                     List JavaDoc events = subscriber.getEvents();
99                     if ( events.size() == 0 ) {
100                         noContent = true;
101                         Element listener = new Element(E_LISTENER);
102                         listener.addContent(String.valueOf(subscriber.getId()));
103                         subscriptionIdNoContent.addContent(listener);
104                     } else {
105                         content = true;
106                         Element listener = new Element(E_LISTENER);
107                         listener.addContent(String.valueOf(subscriber.getId()));
108                         subscriptionIdContent.addContent(listener);
109                         for ( Iterator JavaDoc j = events.iterator(); j.hasNext(); ) {
110                             EventObject JavaDoc event = (EventObject JavaDoc)j.next();
111                             if ( event instanceof RemoteInformation ) {
112                                 Element eventElement = new Element(E_EVENT, NNSP);
113                                 String JavaDoc[][] information = ((RemoteInformation)event).getInformation();
114                                 for ( int k = 0; k < information.length; k++ ) {
115                                     Element entry = new Element(E_INFORMATION, NNSP);
116                                     entry.setAttribute(A_INFORMATION_KEY, information[k][0]);
117                                     // FIXME: if the information value is a Slide URI, we
118
// have to add the slideContext
119
// FIXME: this should be generalized
120
if ("uri".equals(information[k][0])) {
121                                        entry.addContent(
122                                              getSlideContextPath() +
123                                              String.valueOf(information[k][1]));
124                                     } else {
125                                        entry.addContent(String.valueOf(information[k][1]));
126                                     }
127                                     eventElement.addContent(entry);
128                                 }
129                                 subscriptionIdContent.addContent(eventElement);
130                             }
131                         }
132                         subscriber.clearEvents();
133                     }
134                 }
135             }
136             if ( content ) {
137                 multistatus.addContent(contentResponse);
138             }
139             if ( noContent ) {
140                 multistatus.addContent(noContentResponse);
141             }
142             xmlOut.output(new Document(multistatus), resp.getWriter());
143         } catch (Exception JavaDoc e) {
144             int statusCode = getErrorCode( (Exception JavaDoc)e );
145             sendError( statusCode, e );
146             throw new WebdavException( statusCode );
147         }
148     }
149 }
Popular Tags