Merge "BUG 1082 Migrate sal-rest-connector to Async Data Broker API"
[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 import javax.ws.rs.Consumes;
15 import javax.ws.rs.WebApplicationException;
16 import javax.ws.rs.core.MediaType;
17 import javax.ws.rs.core.MultivaluedMap;
18 import javax.ws.rs.ext.MessageBodyReader;
19 import javax.ws.rs.ext.Provider;
20 import org.opendaylight.controller.sal.rest.api.Draft02;
21 import org.opendaylight.controller.sal.rest.api.RestconfService;
22 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
23 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
24 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
25 import org.opendaylight.yangtools.yang.data.api.Node;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 @Provider
30 @Consumes({ Draft02.MediaTypes.DATA + RestconfService.JSON, Draft02.MediaTypes.OPERATION + RestconfService.JSON,
31         MediaType.APPLICATION_JSON })
32 public enum JsonToCompositeNodeProvider implements MessageBodyReader<Node<?>> {
33     INSTANCE;
34
35     private final static Logger LOG = LoggerFactory.getLogger(JsonToCompositeNodeProvider.class);
36
37     @Override
38     public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
39             final MediaType mediaType) {
40         return true;
41     }
42
43     @Override
44     public Node<?> readFrom(final Class<Node<?>> type, final Type genericType,
45             final Annotation[] annotations, final MediaType mediaType,
46             final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
47             WebApplicationException {
48         try {
49             return JsonToCompositeNodeReader.read(entityStream);
50         } catch (Exception e) {
51             LOG.debug("Error parsing json input", e);
52
53             throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
54                     ErrorTag.MALFORMED_MESSAGE);
55         }
56     }
57 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.