2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
10 import com.google.gson.stream.JsonWriter;
11 import java.io.IOException;
12 import java.io.OutputStream;
13 import java.io.OutputStreamWriter;
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Type;
16 import java.nio.charset.StandardCharsets;
17 import java.util.List;
18 import javax.ws.rs.Produces;
19 import javax.ws.rs.WebApplicationException;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.core.MultivaluedMap;
22 import javax.ws.rs.ext.MessageBodyWriter;
23 import javax.ws.rs.ext.Provider;
24 import org.opendaylight.restconf.common.errors.RestconfError;
25 import org.opendaylight.restconf.common.patch.PatchStatusContext;
26 import org.opendaylight.restconf.common.patch.PatchStatusEntity;
27 import org.opendaylight.restconf.nb.rfc8040.MediaTypes;
28 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
31 @Produces(MediaTypes.APPLICATION_YANG_DATA_JSON)
32 public class PatchJsonBodyWriter implements MessageBodyWriter<PatchStatusContext> {
35 public boolean isWriteable(final Class<?> type, final Type genericType,
36 final Annotation[] annotations, final MediaType mediaType) {
37 return type.equals(PatchStatusContext.class);
41 public void writeTo(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
42 final Annotation[] annotations, final MediaType mediaType,
43 final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
44 throws IOException, WebApplicationException {
46 final JsonWriter jsonWriter = createJsonWriter(entityStream);
47 jsonWriter.beginObject().name("ietf-yang-patch:yang-patch-status");
48 jsonWriter.beginObject();
49 jsonWriter.name("patch-id").value(patchStatusContext.getPatchId());
50 if (patchStatusContext.isOk()) {
51 reportSuccess(jsonWriter);
53 if (patchStatusContext.getGlobalErrors() != null) {
54 reportErrors(patchStatusContext.getGlobalErrors(), jsonWriter);
57 jsonWriter.name("edit-status");
58 jsonWriter.beginObject();
59 jsonWriter.name("edit");
60 jsonWriter.beginArray();
61 for (final PatchStatusEntity patchStatusEntity : patchStatusContext.getEditCollection()) {
62 jsonWriter.beginObject();
63 jsonWriter.name("edit-id").value(patchStatusEntity.getEditId());
64 if (patchStatusEntity.getEditErrors() != null) {
65 reportErrors(patchStatusEntity.getEditErrors(), jsonWriter);
67 if (patchStatusEntity.isOk()) {
68 reportSuccess(jsonWriter);
71 jsonWriter.endObject();
73 jsonWriter.endArray();
74 jsonWriter.endObject();
76 jsonWriter.endObject();
77 jsonWriter.endObject();
81 private static void reportSuccess(final JsonWriter jsonWriter) throws IOException {
82 jsonWriter.name("ok").beginArray().nullValue().endArray();
85 private static void reportErrors(final List<RestconfError> errors, final JsonWriter jsonWriter) throws IOException {
86 jsonWriter.name("errors");
87 jsonWriter.beginObject();
88 jsonWriter.name("error");
89 jsonWriter.beginArray();
91 for (final RestconfError restconfError : errors) {
92 jsonWriter.beginObject();
93 jsonWriter.name("error-type").value(restconfError.getErrorType().getErrorTypeTag());
94 jsonWriter.name("error-tag").value(restconfError.getErrorTag().getTagValue());
97 if (restconfError.getErrorPath() != null) {
98 jsonWriter.name("error-path").value(restconfError.getErrorPath().toString());
102 if (restconfError.getErrorMessage() != null) {
103 jsonWriter.name("error-message").value(restconfError.getErrorMessage());
107 if (restconfError.getErrorInfo() != null) {
108 jsonWriter.name("error-info").value(restconfError.getErrorInfo());
111 jsonWriter.endObject();
114 jsonWriter.endArray();
115 jsonWriter.endObject();
118 private static JsonWriter createJsonWriter(final OutputStream entityStream) {
119 return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));