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 / RestconfDocumentedExceptionMapper.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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
9 package org.opendaylight.controller.sal.rest.impl;
10
11 import static org.opendaylight.controller.sal.rest.api.Draft02.RestConfModule.ERRORS_CONTAINER_QNAME;
12 import static org.opendaylight.controller.sal.rest.api.Draft02.RestConfModule.ERROR_APP_TAG_QNAME;
13 import static org.opendaylight.controller.sal.rest.api.Draft02.RestConfModule.ERROR_INFO_QNAME;
14 import static org.opendaylight.controller.sal.rest.api.Draft02.RestConfModule.ERROR_LIST_QNAME;
15 import static org.opendaylight.controller.sal.rest.api.Draft02.RestConfModule.ERROR_MESSAGE_QNAME;
16 import static org.opendaylight.controller.sal.rest.api.Draft02.RestConfModule.ERROR_TAG_QNAME;
17 import static org.opendaylight.controller.sal.rest.api.Draft02.RestConfModule.ERROR_TYPE_QNAME;
18 import static org.opendaylight.controller.sal.rest.api.Draft02.RestConfModule.NAMESPACE;
19
20 import com.google.common.base.Charsets;
21 import com.google.common.base.Strings;
22 import com.google.common.collect.ImmutableList;
23 import com.google.gson.stream.JsonWriter;
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.OutputStreamWriter;
27 import java.io.StringReader;
28 import java.io.UnsupportedEncodingException;
29 import java.util.List;
30 import java.util.Map.Entry;
31 import javax.activation.UnsupportedDataTypeException;
32 import javax.ws.rs.core.Context;
33 import javax.ws.rs.core.HttpHeaders;
34 import javax.ws.rs.core.MediaType;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.ext.ExceptionMapper;
37 import javax.ws.rs.ext.Provider;
38 import javax.xml.parsers.DocumentBuilderFactory;
39 import javax.xml.transform.OutputKeys;
40 import javax.xml.transform.Transformer;
41 import javax.xml.transform.TransformerConfigurationException;
42 import javax.xml.transform.TransformerException;
43 import javax.xml.transform.TransformerFactory;
44 import javax.xml.transform.TransformerFactoryConfigurationError;
45 import javax.xml.transform.dom.DOMSource;
46 import javax.xml.transform.stream.StreamResult;
47 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
48 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
49 import org.opendaylight.controller.sal.restconf.impl.RestconfError;
50 import org.opendaylight.yangtools.yang.common.QName;
51 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
52 import org.opendaylight.yangtools.yang.data.api.Node;
53 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
54 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils;
55 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
56 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59 import org.w3c.dom.Document;
60 import org.xml.sax.InputSource;
61
62 /**
63  * This class defines an ExceptionMapper that handles RestconfDocumentedExceptions thrown by resource implementations
64  * and translates appropriately to restconf error response as defined in the RESTCONF RFC draft.
65  *
66  * @author Thomas Pantelis
67  */
68 @Provider
69 public class RestconfDocumentedExceptionMapper implements ExceptionMapper<RestconfDocumentedException> {
70
71     private final static Logger LOG = LoggerFactory.getLogger(RestconfDocumentedExceptionMapper.class);
72
73     @Context
74     private HttpHeaders headers;
75
76     @Override
77     public Response toResponse(final RestconfDocumentedException exception) {
78
79         LOG.debug("In toResponse: {}", exception.getMessage());
80
81         // Default to the content type if there's no Accept header
82
83         MediaType mediaType = headers.getMediaType();
84
85         List<MediaType> accepts = headers.getAcceptableMediaTypes();
86
87         LOG.debug("Accept headers: {}", accepts);
88
89         if (accepts != null && accepts.size() > 0) {
90             mediaType = accepts.get(0); // just pick the first one
91         }
92
93         LOG.debug("Using MediaType: {}", mediaType);
94
95         List<RestconfError> errors = exception.getErrors();
96         if (errors.isEmpty()) {
97             // We don't actually want to send any content but, if we don't set any content here,
98             // the tomcat front-end will send back an html error report. To prevent that, set a
99             // single space char in the entity.
100
101             return Response.status(exception.getStatus()).type(MediaType.TEXT_PLAIN_TYPE).entity(" ").build();
102         }
103
104         int status = errors.iterator().next().getErrorTag().getStatusCode();
105
106         ControllerContext context = ControllerContext.getInstance();
107         DataNodeContainer errorsSchemaNode = (DataNodeContainer) context.getRestconfModuleErrorsSchemaNode();
108
109         if (errorsSchemaNode == null) {
110             return Response.status(status).type(MediaType.TEXT_PLAIN_TYPE).entity(exception.getMessage()).build();
111         }
112
113         ImmutableList.Builder<Node<?>> errorNodes = ImmutableList.<Node<?>> builder();
114         for (RestconfError error : errors) {
115             errorNodes.add(toDomNode(error));
116         }
117
118         ImmutableCompositeNode errorsNode = ImmutableCompositeNode.create(ERRORS_CONTAINER_QNAME, errorNodes.build());
119
120         Object responseBody;
121         if (mediaType.getSubtype().endsWith("json")) {
122             responseBody = toJsonResponseBody(errorsNode, errorsSchemaNode);
123         } else {
124             responseBody = toXMLResponseBody(errorsNode, errorsSchemaNode);
125         }
126
127         return Response.status(status).type(mediaType).entity(responseBody).build();
128     }
129
130     private Object toJsonResponseBody(final ImmutableCompositeNode errorsNode, final DataNodeContainer errorsSchemaNode) {
131
132         JsonMapper jsonMapper = new JsonMapper(null);
133
134         Object responseBody = null;
135         try {
136             ByteArrayOutputStream outStream = new ByteArrayOutputStream();
137             JsonWriter writer = new JsonWriter(new OutputStreamWriter(outStream, Charsets.UTF_8));
138             writer.setIndent("    ");
139
140             jsonMapper.write(writer, errorsNode, errorsSchemaNode);
141             writer.flush();
142
143             responseBody = outStream.toString("UTF-8");
144         } catch (IOException e) {
145             LOG.error("Error writing error response body", e);
146         }
147
148         return responseBody;
149     }
150
151     private Object toXMLResponseBody(final ImmutableCompositeNode errorsNode, final DataNodeContainer errorsSchemaNode) {
152
153         XmlMapper xmlMapper = new XmlMapper();
154
155         Object responseBody = null;
156         try {
157             Document xmlDoc = xmlMapper.write(errorsNode, errorsSchemaNode);
158
159             responseBody = documentToString(xmlDoc);
160         } catch (TransformerException | UnsupportedDataTypeException | UnsupportedEncodingException e) {
161             LOG.error("Error writing error response body", e);
162         }
163
164         return responseBody;
165     }
166
167     private String documentToString(final Document doc) throws TransformerException, UnsupportedEncodingException {
168         Transformer transformer = createTransformer();
169         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
170
171         transformer.transform(new DOMSource(doc), new StreamResult(outStream));
172
173         return outStream.toString("UTF-8");
174     }
175
176     private Transformer createTransformer() throws TransformerFactoryConfigurationError,
177             TransformerConfigurationException {
178         TransformerFactory tf = TransformerFactory.newInstance();
179         Transformer transformer = tf.newTransformer();
180         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
181         transformer.setOutputProperty(OutputKeys.METHOD, "xml");
182         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
183         transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
184         transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
185         return transformer;
186     }
187
188     private Node<?> toDomNode(final RestconfError error) {
189
190         CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder();
191         builder.setQName(ERROR_LIST_QNAME);
192
193         addLeaf(builder, ERROR_TYPE_QNAME, error.getErrorType().getErrorTypeTag());
194         addLeaf(builder, ERROR_TAG_QNAME, error.getErrorTag().getTagValue());
195         addLeaf(builder, ERROR_MESSAGE_QNAME, error.getErrorMessage());
196         addLeaf(builder, ERROR_APP_TAG_QNAME, error.getErrorAppTag());
197
198         Node<?> errorInfoNode = parseErrorInfo(error.getErrorInfo());
199         if (errorInfoNode != null) {
200             builder.add(errorInfoNode);
201         }
202
203         return builder.toInstance();
204     }
205
206     private Node<?> parseErrorInfo(final String errorInfo) {
207         if (Strings.isNullOrEmpty(errorInfo)) {
208             return null;
209         }
210
211         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
212         factory.setNamespaceAware(true);
213         factory.setCoalescing(true);
214         factory.setIgnoringElementContentWhitespace(true);
215         factory.setIgnoringComments(true);
216
217         // Wrap the error info content in a root <error-info> element so it can be parsed
218         // as XML. The error info content may or may not be XML. If not then it will be
219         // parsed as text content of the <error-info> element.
220
221         String errorInfoWithRoot = new StringBuilder("<error-info xmlns=\"").append(NAMESPACE).append("\">")
222                 .append(errorInfo).append("</error-info>").toString();
223
224         Document doc = null;
225         try {
226             doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(errorInfoWithRoot)));
227         } catch (Exception e) {
228             // TODO: what if the content is text that happens to contain invalid markup?
229             // Could wrap in CDATA and try again.
230
231             LOG.warn("Error parsing restconf error-info, \"{}\", as XML", errorInfo, e);
232             return null;
233         }
234
235         Node<?> errorInfoNode = XmlDocumentUtils.toDomNode(doc);
236
237         if (errorInfoNode instanceof CompositeNode) {
238             CompositeNode compositeNode = (CompositeNode) XmlDocumentUtils.toDomNode(doc);
239
240             // At this point the QName for the "error-info" CompositeNode doesn't contain the revision
241             // as it isn't present in the XML. So we'll copy all the child nodes and create a new
242             // CompositeNode with the full QName. This is done so the XML/JSON mapping code can
243             // locate the schema.
244
245             ImmutableList.Builder<Node<?>> childNodes = ImmutableList.builder();
246             for (Entry<QName, List<Node<?>>> entry : compositeNode.entrySet()) {
247                 childNodes.addAll(entry.getValue());
248             }
249
250             errorInfoNode = ImmutableCompositeNode.create(ERROR_INFO_QNAME, childNodes.build());
251         }
252
253         return errorInfoNode;
254     }
255
256     private void addLeaf(final CompositeNodeBuilder<ImmutableCompositeNode> builder, final QName qname,
257             final String value) {
258         if (!Strings.isNullOrEmpty(value)) {
259             builder.addLeaf(qname, value);
260         }
261     }
262 }

©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.