File: Subscription.java

package info (click to toggle)
libjboss-web-services-java 0.0%2Bsvn5660%2Bdak2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 7,252 kB
  • ctags: 12,475
  • sloc: java: 79,207; xml: 29; makefile: 19; sh: 16
file content (235 lines) | stat: -rw-r--r-- 7,930 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.ws.extensions.eventing.mgmt;

// $Id: Subscription.java 4510 2007-09-03 09:31:45Z heiko.braun@jboss.com $

import java.io.ByteArrayInputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Date;

import javax.xml.bind.JAXBElement;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.TransformerException;

import org.apache.xpath.XPathAPI;
import org.apache.xpath.objects.XObject;
import org.jboss.logging.Logger;
import org.jboss.ws.core.soap.SOAPConnectionImpl;
import org.jboss.ws.extensions.eventing.EventingConstants;
import org.jboss.ws.extensions.eventing.jaxws.AttributedURIType;
import org.jboss.ws.extensions.eventing.jaxws.EndpointReferenceType;
import org.jboss.ws.Constants;
import org.jboss.wsf.common.DOMWriter;
import org.w3c.dom.Element;

/**
 * Represents a subscription.
 *
 * @author Heiko Braun, <heiko@openj.net>
 * @since 05-Jan-2006
 */
class Subscription
{
   final private static Logger log = Logger.getLogger(Subscription.class);

   final private EndpointReferenceType notifyTo;
   final private EndpointReferenceType endTo;
   private Date expires;
   final private Filter filter;
   final private EndpointReferenceType endpointReference;
   final private URI eventSourceNS;

   private SubscriptionManagerFactory factory = SubscriptionManagerFactory.getInstance();

   public Subscription(URI eventSourceNS, EndpointReferenceType epr, EndpointReferenceType notifyTo, EndpointReferenceType endTo, Date expires, Filter filter)
   {
      this.eventSourceNS = eventSourceNS;
      this.notifyTo = notifyTo;
      this.endTo = endTo; // is optional, can be null
      this.expires = expires;
      this.filter = filter;
      this.endpointReference = epr;
   }

   public void notify(Element event)
   {
      if(log.isDebugEnabled()) log.debug(getIdentifier() + " dispatching " + event);

      try
      {
         String eventXML = DOMWriter.printNode(event, false);
         MessageFactory msgFactory = MessageFactory.newInstance();

         // notification elements need to declare their namespace locally
         StringBuilder sb = new StringBuilder();
         sb.append("<env:Envelope xmlns:env='"+ Constants.NS_SOAP11_ENV+"' ");
         sb.append("xmlns:wse='").append(EventingConstants.NS_EVENTING).append("' ");
         sb.append("xmlns:wsa='").append(EventingConstants.NS_ADDRESSING).append("'>");
         sb.append("<env:Header>");
         sb.append("<wsa:Action>").append(getNotificationAction()).append("</wsa:Action>");
         // todo: add reference parameters when wildcards are supported
         sb.append("<wsa:To>").append(notifyTo.getAddress().toString()).append("</wsa:To>");
         sb.append("</env:Header>");
         sb.append("<env:Body>");
         sb.append(eventXML);
         sb.append("</env:Body>");
         sb.append("</env:Envelope>");

         SOAPMessage reqMsg = msgFactory.createMessage(null, new ByteArrayInputStream(sb.toString().getBytes()));
         URL epURL = new URL(notifyTo.getAddress().getValue());
         new SOAPConnectionImpl().callOneWay(reqMsg, epURL);
      }
      catch (Exception e)
      {
         SubscriptionManagerMBean manager = factory.getSubscriptionManager();
         AttributedURIType address = this.endTo.getAddress();
         NotificationFailure failure = new NotificationFailure(address.getValue(), event, e);
         manager.addNotificationFailure(failure);
         log.error("Failed to send notification message", e);
      }
   }

   public boolean accepts(Element event)
   {

      boolean b = true;
      if (filter != null)
      {

         try
         {
            XObject o = XPathAPI.eval(event, filter.getExpression());
            b = o.bool();
         }
         catch (TransformerException e)
         {
            log.error("Failed to evalute xpath expression", e);
         }

      }
      return b;
   }

   public void end(String status)
   {
      if (null == endTo) // it's an optional field.
         return;

      if(log.isDebugEnabled()) log.debug("Ending subscription " + getIdentifier());

      StringBuffer sb = new StringBuffer();
      sb.append("<env:Envelope xmlns:env='http://www.w3.org/2003/05/soap-envelope' ");
      sb.append("xmlns:wse='").append(EventingConstants.NS_EVENTING).append("' ");
      sb.append("xmlns:wsa='").append(EventingConstants.NS_ADDRESSING).append("'>");
      sb.append("<env:Header>");
      sb.append("<wsa:Action>").append(EventingConstants.SUBSCRIPTION_END_ACTION).append("</wsa:Action>");
      sb.append("<wsa:To>").append(endTo.getAddress().toString()).append("</wsa:To>");
      sb.append("</env:Header>");
      sb.append("<env:Body>");

      sb.append("<wse:SubscriptionEnd>");
      sb.append("<wse:SubscriptionManager>");
      sb.append("<wsa:Address>");
      sb.append(endpointReference.getAddress().toString());
      sb.append("</wsa:Address>");
      sb.append("<wsa:ReferenceParameters>");
      sb.append("<wse:Identifier>");
      sb.append(getIdentifier().toString());
      sb.append("</wse:Identifier>");
      sb.append("</wsa:ReferenceParameters>");
      sb.append("</wse:SubscriptionManager>");

      sb.append("<wse:Status>").append(status).append("</wse:Status>");
      sb.append("<wse:Reason/>");
      sb.append("</wse:SubscriptionEnd>");

      sb.append("</env:Body>");
      sb.append("</env:Envelope>");

      try
      {
         MessageFactory msgFactory = MessageFactory.newInstance();
         SOAPMessage reqMsg = msgFactory.createMessage(null, new ByteArrayInputStream(sb.toString().getBytes()));
         URL epURL = new URL(endTo.getAddress().getValue());
         new SOAPConnectionImpl().callOneWay(reqMsg, epURL);
      }
      catch (Exception e)
      {
         log.warn("Failed to send subscription end message to: " + this.endTo + "("+e.getMessage()+")");
      }

   }

   private String getNotificationAction()
   {
      return this.eventSourceNS.toString() + "/Notification";
   }

   public boolean isExpired()
   {
      return System.currentTimeMillis() > expires.getTime();
   }

   public EndpointReferenceType getNotifyTo()
   {
      return notifyTo;
   }

   public EndpointReferenceType getEndTo()
   {
      return endTo;
   }

   public Date getExpires()
   {
      return expires;
   }

   public Filter getFilter()
   {
      return filter;
   }

   public EndpointReferenceType getEndpointReferenceType()
   {
      return endpointReference;
   }

   public URI getIdentifier()
   {
      try {
         JAXBElement<String> jaxbElement = (JAXBElement<String>)endpointReference.getReferenceParameters().getAny().get(0);
         return new URI(jaxbElement.getValue());
      } catch (URISyntaxException e) {
         throw new RuntimeException(e);
      }
   }

   public void setExpires(Date expires)
   {
      this.expires = expires;
   }
}