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;
21
22 import com.fasterxml.jackson.core.JsonParseException;
23 import com.fasterxml.jackson.databind.JsonMappingException;
24 import io.netty.buffer.ByteBuf;
25 import io.netty.buffer.ByteBufAllocator;
26 import org.waarp.common.utility.WaarpNettyUtil;
27 import org.waarp.common.utility.WaarpStringUtils;
28 import org.waarp.openr66.protocol.localhandler.LocalChannelReference;
29 import org.waarp.openr66.protocol.localhandler.packet.json.JsonPacket;
30
31 import java.io.IOException;
32
33
34
35
36
37
38 public class JsonCommandPacket extends AbstractLocalPacket {
39 private final String request;
40
41 private String result;
42
43 private final byte send;
44
45
46
47
48
49
50
51
52
53 public static JsonCommandPacket createFromBuffer(final int headerLength,
54 final int middleLength,
55 final int endLength,
56 final ByteBuf buf) {
57 final byte[] bheader = new byte[headerLength - 1];
58 final byte[] bmiddle = new byte[middleLength];
59 final byte bend;
60 if (headerLength - 1 > 0) {
61 buf.readBytes(bheader);
62 }
63 if (middleLength > 0) {
64 buf.readBytes(bmiddle);
65 }
66 bend = buf.readByte();
67 return new JsonCommandPacket(new String(bheader, WaarpStringUtils.UTF8),
68 new String(bmiddle, WaarpStringUtils.UTF8),
69 bend);
70 }
71
72
73
74
75
76
77 public JsonCommandPacket(final String srequest, final String sresult,
78 final byte end) {
79 request = srequest;
80 result = sresult;
81 send = end;
82 }
83
84
85
86
87
88 public JsonCommandPacket(final JsonPacket jrequest, final byte end) {
89 request = jrequest.toString();
90 result = null;
91 send = end;
92 }
93
94
95
96
97
98
99 public JsonCommandPacket(final JsonPacket jrequest, final String sresult,
100 final byte end) {
101 request = jrequest.toString();
102 result = sresult;
103 send = end;
104 }
105
106 @Override
107 public final boolean hasGlobalBuffer() {
108 return true;
109 }
110
111 @Override
112 public final synchronized void createAllBuffers(
113 final LocalChannelReference lcr, final int networkHeader) {
114 final byte[] headerBytes =
115 request != null? request.getBytes(WaarpStringUtils.UTF8) : EMPTY_ARRAY;
116 final int headerSize = headerBytes.length;
117 final byte[] middleBytes =
118 result != null? result.getBytes(WaarpStringUtils.UTF8) : EMPTY_ARRAY;
119 final int middleSize = middleBytes.length;
120 final int endSize = 1;
121 final int globalSize =
122 networkHeader + LOCAL_HEADER_SIZE + headerSize + middleSize + endSize;
123 int offset = networkHeader + LOCAL_HEADER_SIZE;
124 global = ByteBufAllocator.DEFAULT.ioBuffer(globalSize, globalSize);
125 header = WaarpNettyUtil.slice(global, offset, headerSize);
126 if (request != null) {
127 header.writeBytes(headerBytes);
128 }
129 offset += headerSize;
130 middle = WaarpNettyUtil.slice(global, offset, middleSize);
131 if (result != null) {
132 middle.writeBytes(middleBytes);
133 }
134 offset += middleSize;
135 end = WaarpNettyUtil.slice(global, offset, endSize);
136 end.writeByte(send);
137 }
138
139 @Override
140 public final String toString() {
141 return "JsonCommandPacket: " + request + ':' + result + ':' + send;
142 }
143
144 @Override
145 public final byte getType() {
146 return LocalPacketFactory.JSONREQUESTPACKET;
147 }
148
149
150
151
152 public final JsonPacket getJsonRequest() {
153 try {
154 return JsonPacket.createFromBuffer(request);
155 } catch (final JsonParseException e) {
156 return null;
157 } catch (final JsonMappingException e) {
158 return null;
159 } catch (final IOException e) {
160 return null;
161 }
162 }
163
164
165
166
167 public final void setResult(final String result) {
168 this.result = result;
169 middle = null;
170 }
171
172
173
174
175 public final String getRequest() {
176 return request;
177 }
178
179
180
181
182 public final String getResult() {
183 return result;
184 }
185
186
187
188
189 public final byte getTypeValid() {
190 return send;
191 }
192
193 }