View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
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   * Json Command Message class for packet
35   * <p>
36   * 2 strings and one byte: request,result,send
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     * @param headerLength
47     * @param middleLength
48     * @param endLength
49     * @param buf
50     *
51     * @return the new ValidPacket from buffer
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     * @param srequest
74     * @param sresult
75     * @param end
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     * @param jrequest
86     * @param end
87     */
88    public JsonCommandPacket(final JsonPacket jrequest, final byte end) {
89      request = jrequest.toString();
90      result = null;
91      send = end;
92    }
93  
94    /**
95     * @param jrequest
96     * @param sresult
97     * @param end
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    * @return the JsonPacket from request
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    * @param result
166    */
167   public final void setResult(final String result) {
168     this.result = result;
169     middle = null;
170   }
171 
172   /**
173    * @return the request
174    */
175   public final String getRequest() {
176     return request;
177   }
178 
179   /**
180    * @return the result
181    */
182   public final String getResult() {
183     return result;
184   }
185 
186   /**
187    * @return the type
188    */
189   public final byte getTypeValid() {
190     return send;
191   }
192 
193 }