1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.openr66.protocol.localhandler.packet.json;
21
22 import com.fasterxml.jackson.annotation.JsonTypeInfo;
23 import com.fasterxml.jackson.core.JsonParseException;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.JsonMappingException;
26 import com.fasterxml.jackson.databind.node.ObjectNode;
27 import org.waarp.common.json.JsonHandler;
28 import org.waarp.common.utility.ParametersChecker;
29 import org.waarp.openr66.protocol.exception.OpenR66ProtocolPacketException;
30
31 import java.io.IOException;
32
33
34
35
36
37
38 @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
39 public class JsonPacket {
40
41
42
43
44
45 public final ObjectNode createObjectNode()
46 throws OpenR66ProtocolPacketException {
47 try {
48 final String value = JsonHandler.mapper.writeValueAsString(this);
49 return (ObjectNode) JsonHandler.mapper.readTree(value);
50 } catch (final JsonProcessingException e) {
51 throw new OpenR66ProtocolPacketException("Json exception", e);
52 } catch (final Exception e) {
53 throw new OpenR66ProtocolPacketException("Json exception", e);
54 }
55 }
56
57 private String comment;
58 private byte requestUserPacket;
59
60
61
62
63 public final byte getRequestUserPacket() {
64 return requestUserPacket;
65 }
66
67
68
69
70 public final void setRequestUserPacket(final byte requestUserPacket) {
71 this.requestUserPacket = requestUserPacket;
72 }
73
74
75
76
77 public void setRequestUserPacket() {
78 throw new IllegalArgumentException("Not defined");
79 }
80
81
82
83
84 public final String getComment() {
85 return comment;
86 }
87
88
89
90
91 public final void setComment(final String comment) {
92 this.comment = comment;
93 }
94
95 @Override
96 public final String toString() {
97 return JsonHandler.writeAsString(this);
98 }
99
100
101
102
103
104
105
106
107
108
109 public static JsonPacket createFromBuffer(final String value)
110 throws JsonParseException, JsonMappingException, IOException {
111 if (ParametersChecker.isNotEmpty(value)) {
112 return JsonHandler.mapper.readValue(value, JsonPacket.class);
113 }
114 return null;
115 }
116
117
118
119
120
121
122 public void fromJson(final JsonPacket json) {
123 comment = json.comment;
124 requestUserPacket = json.requestUserPacket;
125 }
126 }