View Javadoc

1   /* This file is part of COPAL (COntext Provisioning for All).
2    *
3    * COPAL is a part of SM4All (Smart hoMes for All) project.
4    *
5    * COPAL is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * COPAL is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with COPAL. If not, see <http://www.gnu.org/licenses/>.
17   */
18  package at.ac.tuwien.infosys.sm4all.copal.rest.model;
19  
20  import java.util.Set;
21  import java.util.concurrent.CopyOnWriteArraySet;
22  import org.w3c.dom.Document;
23  import org.w3c.dom.Element;
24  import at.ac.tuwien.infosys.sm4all.copal.api.event.ContextEvent;
25  import at.ac.tuwien.infosys.sm4all.copal.api.event.ContextEventType;
26  import at.ac.tuwien.infosys.sm4all.copal.api.event.MalformedDocumentException;
27  import at.ac.tuwien.infosys.sm4all.copal.api.event.UnprocessedAction;
28  import at.ac.tuwien.infosys.sm4all.copal.api.event.XMLContextEvent;
29  import at.ac.tuwien.infosys.sm4all.copal.api.event.XMLContextEventType;
30  import at.ac.tuwien.infosys.sm4all.copal.api.publisher.BasePublisher;
31  import at.ac.tuwien.infosys.sm4all.copal.api.publisher.ContextPublisher;
32  import at.ac.tuwien.infosys.sm4all.copal.api.security.Authorization;
33  import at.ac.tuwien.infosys.sm4all.copal.api.service.FailedPublishingException;
34  import at.ac.tuwien.infosys.sm4all.copal.api.util.Attribute;
35  
36  /**
37   * {@link ContextPublisher} that is used by REST service to publish
38   * {@link Event}s.
39   * 
40   * @author sanjin
41   */
42  public class RESTfulPublisher extends BasePublisher {
43  
44      private final Set<XMLContextEventType> eventTypes;
45  
46      /**
47       * Creates an instance of RESTful {@link ContextPublisher} that publishes
48       * {@link Event}s of specified types and using specified source ID.
49       * 
50       * @param sourceID the source ID.
51       * @param publishedTypes the types of published {@link Event}s.
52       */
53      public RESTfulPublisher(final String sourceID,
54              final String... publishedTypes) {
55          super(sourceID, publishedTypes);
56  
57          this.eventTypes = new CopyOnWriteArraySet<XMLContextEventType>();
58      }
59  
60      @Override
61      public void setTTL(final long ttl) {
62          super.setTTL(ttl);
63      }
64  
65      @Override
66      public void setPriority(final int priority) {
67          super.setPriority(priority);
68      }
69  
70      /**
71       * Sets specified {@link Attribute}s in all {@link ContextEvent}s published
72       * by this {@link RESTfulPublisher}.
73       * 
74       * @param attributes the {@link Attribute}s.
75       */
76      public void addAttributes(final Attribute... attributes) {
77          for (final Attribute attribute : attributes) {
78              super.add(attribute);
79          }
80      }
81  
82      /**
83       * Adds specified {@link Authorization}s to all {@link ContextEvent}s
84       * published by this {@link RESTfulPublisher}.
85       * 
86       * @param authorizations the {@link Authorization}s.
87       */
88      public void add(final Authorization... authorizations) {
89          for (final Authorization authorization : authorizations) {
90              super.add(authorization);
91          }
92      }
93  
94      /**
95       * Appends specified {@link UnprocessedAction}s to all {@link ContextEvent}s
96       * published by this {@link RESTfulPublisher}.
97       * 
98       * @param actions the {@link UnprocessedAction}s.
99       */
100     public void appendActions(final UnprocessedAction... actions) {
101         for (final UnprocessedAction action : actions) {
102             super.appendAction(action);
103         }
104     }
105 
106     @Override
107     protected boolean start(final ContextEventType type) {
108         boolean result = false;
109 
110         if (type instanceof XMLContextEventType) {
111             this.eventTypes.add((XMLContextEventType) type);
112             result = true;
113         }
114 
115         return result;
116     }
117 
118     @Override
119     protected void stop(final ContextEventType type) {
120         this.eventTypes.remove(type);
121     }
122 
123     /**
124      * Publish specified {@link Event}.
125      * 
126      * @param event the {@link Event}.
127      * @throws MalformedDocumentException if specified {@link Event} is not
128      *         valid for specified type of published {@link Event}s.
129      * @throws FailedPublishingException if publishing of specified
130      *         {@link Event} fails.
131      */
132     public void publish(final Event event) throws FailedPublishingException,
133             MalformedDocumentException {
134         final Document document = event.toDocument();
135         final Element element = document.getDocumentElement();
136         final String rootElementName = element.getLocalName();
137         final String namespaceURI = element.getNamespaceURI();
138 
139         for (final XMLContextEventType eventType : this.eventTypes) {
140             if ((((null == namespaceURI) && !eventType.hasNamespace()) || ((null != namespaceURI) && namespaceURI.equals(eventType.getNamespaceURI().toString())))
141                     && rootElementName.equals(eventType.getRootElementName())) {
142                 publish(new XMLContextEvent(eventType, getSourceID(), document));
143                 break;
144             }
145         }
146     }
147 }