1 23 24 package org.apache.slide.webdav.method; 25 26 import java.util.EventObject ; 27 import java.util.Iterator ; 28 import java.util.List ; 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 50 public class PollMethod extends AbstractWebdavMethod implements NotificationConstants { 51 protected final static String 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 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 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 Subscriber subscriber = NotificationTrigger.getInstance().getSubscriber(subscriptionID[i]); 96 if ( subscriber != null ) { 97 NotificationTrigger.getInstance().refreshSubscriber(subscriber, true); 98 List 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 j = events.iterator(); j.hasNext(); ) { 110 EventObject event = (EventObject )j.next(); 111 if ( event instanceof RemoteInformation ) { 112 Element eventElement = new Element(E_EVENT, NNSP); 113 String [][] 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 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 e) { 144 int statusCode = getErrorCode( (Exception )e ); 145 sendError( statusCode, e ); 146 throw new WebdavException( statusCode ); 147 } 148 } 149 } | Popular Tags |