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.provider;
19  
20  import java.util.LinkedList;
21  import java.util.List;
22  import javax.ws.rs.ext.MessageBodyReader;
23  import org.w3c.dom.Element;
24  import at.ac.tuwien.infosys.sm4all.copal.api.event.DefaultAction;
25  import at.ac.tuwien.infosys.sm4all.copal.api.event.UnprocessedAction;
26  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.ContextEventTypeUnmarshaller;
27  import at.ac.tuwien.infosys.sm4all.copal.api.publisher.xml.ContextPublisherMarshaller;
28  import at.ac.tuwien.infosys.sm4all.copal.api.util.FailedUnmarshallingException;
29  import at.ac.tuwien.infosys.sm4all.copal.rest.model.RESTfulPublisher;
30  
31  /**
32   * A {@link RESTfulPublisher} {@link XMLReader}.
33   * 
34   * @author sanjin
35   */
36  @SuppressWarnings("unused")
37  public class XMLRESTfulPublisherReader extends XMLReader<RESTfulPublisher>
38          implements MessageBodyReader<RESTfulPublisher> {
39  
40      /**
41       * Creates instance of {@link RESTfulPublisher} {@link XMLReader}.
42       */
43      public XMLRESTfulPublisherReader() {
44          super(RESTfulPublisher.class);
45      }
46  
47      @Override
48      protected RESTfulPublisher unmarshal(final Element element)
49              throws FailedUnmarshallingException {
50          final ContextPublisherMarshaller unmarshaller = new ContextPublisherMarshaller(
51                  element);
52          final RESTfulPublisher result = new RESTfulPublisher(
53                  unmarshaller.unmarshalSourceID(),
54                  unmarshaller.unmarshalPublishedTypes());
55  
56          final ContextEventTypeUnmarshaller defaults = new ContextEventTypeUnmarshaller(
57                  element);
58          result.setTTL(defaults.unmarshalTTL());
59          result.setPriority(defaults.unmarshalPriority());
60          result.addAttributes(defaults.unmarshalAttributes());
61          result.add(defaults.unmarshalAuthorizations());
62          result.appendActions(toUnprocessedActions(defaults.unmarshalActions()));
63  
64          return result;
65      }
66  
67      private static UnprocessedAction[] toUnprocessedActions(
68              final DefaultAction... actions) {
69          final List<UnprocessedAction> result = new LinkedList<UnprocessedAction>();
70  
71          for (final DefaultAction action : actions) {
72              result.add(new UnprocessedAction(action));
73          }
74  
75          return result.toArray(new UnprocessedAction[result.size()]);
76      }
77  }