9e6966540534c9c79d2a120f89c35ebf603d956d
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / rest / impl / JsonToCompositeNodeProvider.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sal.rest.impl;
9
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.lang.annotation.Annotation;
13 import java.lang.reflect.Type;
14
15 import javax.ws.rs.Consumes;
16 import javax.ws.rs.WebApplicationException;
17 import javax.ws.rs.core.MediaType;
18 import javax.ws.rs.core.MultivaluedMap;
19 import javax.ws.rs.ext.MessageBodyReader;
20 import javax.ws.rs.ext.Provider;
21
22 import org.opendaylight.controller.sal.rest.api.Draft02;
23 import org.opendaylight.controller.sal.rest.api.RestconfService;
24 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
25 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
26 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
27 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 @Provider
32 @Consumes({ Draft02.MediaTypes.DATA + RestconfService.JSON, Draft02.MediaTypes.OPERATION + RestconfService.JSON,
33     MediaType.APPLICATION_JSON })
34 public enum JsonToCompositeNodeProvider implements MessageBodyReader<CompositeNode> {
35     INSTANCE;
36
37     private final static Logger LOG = LoggerFactory.getLogger( JsonToCompositeNodeProvider.class );
38
39     @Override
40     public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
41         return true;
42     }
43
44     @Override
45     public CompositeNode readFrom(final Class<CompositeNode> type, final Type genericType, final Annotation[] annotations,
46             final MediaType mediaType, final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream)
47                     throws IOException, WebApplicationException {
48         try {
49             return JsonReader.read(entityStream);
50         } catch (Exception e) {
51             LOG.debug( "Error parsing json input", e);
52             throw new RestconfDocumentedException(
53                     "Error parsing input: " + e.getMessage(),
54                     ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
55         }
56     }
57
58 }