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.lang.annotation.Annotation;
21  import java.lang.reflect.Type;
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  import javax.ws.rs.core.MediaType;
25  import javax.ws.rs.ext.MessageBodyReader;
26  import org.w3c.dom.Element;
27  import at.ac.tuwien.infosys.sm4all.copal.api.listener.xml.ContextListenerMarshaller;
28  import at.ac.tuwien.infosys.sm4all.copal.api.util.FailedUnmarshallingException;
29  import at.ac.tuwien.infosys.sm4all.copal.api.util.InvalidFieldValueException;
30  import at.ac.tuwien.infosys.sm4all.copal.api.xml.StringAttribute;
31  import at.ac.tuwien.infosys.sm4all.copal.rest.model.RESTfulListener;
32  
33  /**
34   * A {@link RESTfulListener} {@link XMLReader}.
35   * 
36   * @author sanjin
37   */
38  @SuppressWarnings("unused")
39  public class XMLRESTfulListenerReader extends XMLReader<RESTfulListener>
40          implements MessageBodyReader<RESTfulListener> {
41  
42      /**
43       * Creates instance of {@link RESTfulListener} {@link XMLReader}.
44       */
45      public XMLRESTfulListenerReader() {
46          super(RESTfulListener.class);
47      }
48  
49      @Override
50      public boolean isReadable(final Class<?> type, final Type genericType,
51              final Annotation[] annotations, final MediaType mediaType) {
52          return RESTfulListener.class.isAssignableFrom(type);
53      }
54  
55      @Override
56      protected RESTfulListener unmarshal(final Element element)
57              throws FailedUnmarshallingException {
58          final RESTfulListener result;
59  
60          try {
61              result = new RESTfulListener(
62                      new ContextListenerMarshaller(element).unmarshalName(),
63                      new URL(new StringAttribute("url", element).unmarshal()));
64          } catch (final MalformedURLException ex) {
65              throw new InvalidFieldValueException("URL is malformed.", ex);
66          }
67  
68          return result;
69      }
70  }