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
9 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
11 import com.google.gson.stream.JsonWriter;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.io.OutputStreamWriter;
15 import java.lang.annotation.Annotation;
16 import java.lang.reflect.Type;
17 import java.nio.charset.StandardCharsets;
18 import java.util.List;
19 import javax.ws.rs.Produces;
20 import javax.ws.rs.WebApplicationException;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.MultivaluedMap;
23 import javax.ws.rs.ext.MessageBodyWriter;
24 import javax.ws.rs.ext.Provider;
25 import org.opendaylight.restconf.common.errors.RestconfError;
26 import org.opendaylight.restconf.common.patch.PatchStatusContext;
27 import org.opendaylight.restconf.common.patch.PatchStatusEntity;
28 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.MediaTypes;
29 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
30 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
33 @Produces({ MediaTypes.YANG_PATCH_STATUS + RestconfConstants.JSON, MediaTypes.DATA + RestconfConstants.JSON })
34 public class PatchJsonBodyWriter implements MessageBodyWriter<PatchStatusContext> {
37 public boolean isWriteable(final Class<?> type, final Type genericType,
38 final Annotation[] annotations, final MediaType mediaType) {
39 return type.equals(PatchStatusContext.class);
43 public long getSize(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
44 final Annotation[] annotations, final MediaType mediaType) {
49 public void writeTo(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
50 final Annotation[] annotations, final MediaType mediaType,
51 final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
52 throws IOException, WebApplicationException {
54 final JsonWriter jsonWriter = createJsonWriter(entityStream);
55 jsonWriter.beginObject().name("ietf-yang-patch:yang-patch-status");
56 jsonWriter.beginObject();
57 jsonWriter.name("patch-id").value(patchStatusContext.getPatchId());
58 if (patchStatusContext.isOk()) {
59 reportSuccess(jsonWriter);
61 if (patchStatusContext.getGlobalErrors() != null) {
62 reportErrors(patchStatusContext.getGlobalErrors(), jsonWriter);
65 jsonWriter.name("edit-status");
66 jsonWriter.beginObject();
67 jsonWriter.name("edit");
68 jsonWriter.beginArray();
69 for (final PatchStatusEntity patchStatusEntity : patchStatusContext.getEditCollection()) {
70 jsonWriter.beginObject();
71 jsonWriter.name("edit-id").value(patchStatusEntity.getEditId());
72 if (patchStatusEntity.getEditErrors() != null) {
73 reportErrors(patchStatusEntity.getEditErrors(), jsonWriter);
75 if (patchStatusEntity.isOk()) {
76 reportSuccess(jsonWriter);
79 jsonWriter.endObject();
81 jsonWriter.endArray();
82 jsonWriter.endObject();
84 jsonWriter.endObject();
85 jsonWriter.endObject();
89 private static void reportSuccess(final JsonWriter jsonWriter) throws IOException {
90 jsonWriter.name("ok").beginArray().nullValue().endArray();
93 private static void reportErrors(final List<RestconfError> errors, final JsonWriter jsonWriter) throws IOException {
94 jsonWriter.name("errors");
95 jsonWriter.beginObject();
96 jsonWriter.name("error");
97 jsonWriter.beginArray();
99 for (final RestconfError restconfError : errors) {
100 jsonWriter.beginObject();
101 jsonWriter.name("error-type").value(restconfError.getErrorType().getErrorTypeTag());
102 jsonWriter.name("error-tag").value(restconfError.getErrorTag().getTagValue());
105 if (restconfError.getErrorPath() != null) {
106 jsonWriter.name("error-path").value(restconfError.getErrorPath().toString());
110 if (restconfError.getErrorMessage() != null) {
111 jsonWriter.name("error-message").value(restconfError.getErrorMessage());
115 if (restconfError.getErrorInfo() != null) {
116 jsonWriter.name("error-info").value(restconfError.getErrorInfo());
119 jsonWriter.endObject();
122 jsonWriter.endArray();
123 jsonWriter.endObject();
126 private static JsonWriter createJsonWriter(final OutputStream entityStream) {
127 return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));