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.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   * Json Object Command Message class for JsonCommandPacket
35   * <p>
36   * 1 string = comment
37   */
38  @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
39  public class JsonPacket {
40    /**
41     * @return the ObjectNode corresponding to this object
42     *
43     * @throws OpenR66ProtocolPacketException
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     * @return the requestUserPacket
62     */
63    public final byte getRequestUserPacket() {
64      return requestUserPacket;
65    }
66  
67    /**
68     * @param requestUserPacket the requestUserPacket to set
69     */
70    public final void setRequestUserPacket(final byte requestUserPacket) {
71      this.requestUserPacket = requestUserPacket;
72    }
73  
74    /**
75     * Default value to set
76     */
77    public void setRequestUserPacket() {
78      throw new IllegalArgumentException("Not defined");
79    }
80  
81    /**
82     * @return the comment
83     */
84    public final String getComment() {
85      return comment;
86    }
87  
88    /**
89     * @param comment the comment to set
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    * @param value
102    *
103    * @return the new JsonPacket from buffer
104    *
105    * @throws IOException
106    * @throws JsonMappingException
107    * @throws JsonParseException
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    * Set from other Json
119    *
120    * @param json
121    */
122   public void fromJson(final JsonPacket json) {
123     comment = json.comment;
124     requestUserPacket = json.requestUserPacket;
125   }
126 }