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 io.netty.buffer.ByteBuf;
23  import io.netty.buffer.ByteBufAllocator;
24  import org.waarp.common.utility.WaarpNettyUtil;
25  import org.waarp.common.utility.WaarpStringUtils;
26  import org.waarp.openr66.protocol.exception.OpenR66ProtocolPacketException;
27  import org.waarp.openr66.protocol.localhandler.LocalChannelReference;
28  
29  /**
30   * Information of files class
31   * <p>
32   * header = "rulename" middle = requestedInfo end = "FILENAME"
33   */
34  public class InformationPacket extends AbstractLocalPacket {
35  
36    private static final String NOT_ENOUGH_DATA = "Not enough data";
37  
38    public enum ASKENUM {
39      ASKEXIST, ASKMLSDETAIL, ASKLIST, ASKMLSLIST
40    }
41  
42    private final String rulename;
43  
44    private final byte requestedInfo;
45  
46    private final String filename;
47  
48    /**
49     * @param headerLength
50     * @param middleLength
51     * @param endLength
52     * @param buf
53     *
54     * @return the new EndTransferPacket from buffer
55     *
56     * @throws OpenR66ProtocolPacketException
57     */
58    public static InformationPacket createFromBuffer(final int headerLength,
59                                                     final int middleLength,
60                                                     final int endLength,
61                                                     final ByteBuf buf)
62        throws OpenR66ProtocolPacketException {
63      if (headerLength - 1 <= 0) {
64        throw new OpenR66ProtocolPacketException(NOT_ENOUGH_DATA);
65      }
66      if (middleLength != 1) {
67        throw new OpenR66ProtocolPacketException(NOT_ENOUGH_DATA);
68      }
69      final byte[] bheader = new byte[headerLength - 1];
70      final byte[] bend = new byte[endLength];
71      buf.readBytes(bheader);
72      final byte request = buf.readByte();
73      if (endLength > 0) {
74        buf.readBytes(bend);
75      }
76      final String sheader = new String(bheader, WaarpStringUtils.UTF8);
77      final String send = new String(bend, WaarpStringUtils.UTF8);
78      return new InformationPacket(sheader, request, send);
79    }
80  
81    /**
82     * @param rulename
83     * @param request
84     * @param filename
85     */
86    public InformationPacket(final String rulename, final byte request,
87                             final String filename) {
88      this.rulename = rulename;
89      requestedInfo = request;
90      this.filename = filename;
91    }
92  
93    @Override
94    public final boolean hasGlobalBuffer() {
95      return true;
96    }
97  
98    @Override
99    public final synchronized void createAllBuffers(
100       final LocalChannelReference lcr, final int networkHeader)
101       throws OpenR66ProtocolPacketException {
102     if (rulename == null) {
103       throw new OpenR66ProtocolPacketException(NOT_ENOUGH_DATA);
104     }
105     final byte[] headerBytes = rulename.getBytes(WaarpStringUtils.UTF8);
106     final int headerSize = headerBytes.length;
107     final int middleSize = 1;
108     final byte[] endBytes =
109         filename != null? filename.getBytes(WaarpStringUtils.UTF8) :
110             EMPTY_ARRAY;
111     final int endSize = endBytes.length;
112     final int globalSize =
113         networkHeader + LOCAL_HEADER_SIZE + headerSize + middleSize + endSize;
114     int offset = networkHeader + LOCAL_HEADER_SIZE;
115     global = ByteBufAllocator.DEFAULT.ioBuffer(globalSize, globalSize);
116     header = WaarpNettyUtil.slice(global, offset, headerSize);
117     header.writeBytes(headerBytes);
118     offset += headerSize;
119     middle = WaarpNettyUtil.slice(global, offset, middleSize);
120     middle.writeByte(requestedInfo);
121     offset += middleSize;
122     end = WaarpNettyUtil.slice(global, offset, endSize);
123     if (filename != null) {
124       end.writeBytes(endBytes);
125     }
126   }
127 
128   @Override
129   public final byte getType() {
130     return LocalPacketFactory.INFORMATIONPACKET;
131   }
132 
133   @Override
134   public final String toString() {
135     return "InformationPacket: " + requestedInfo + ' ' + rulename + ' ' +
136            filename;
137   }
138 
139   /**
140    * @return the requestId
141    */
142   public final byte getRequest() {
143     return requestedInfo;
144   }
145 
146   /**
147    * @return the rulename
148    */
149   public final String getRulename() {
150     return rulename;
151   }
152 
153   /**
154    * @return the filename
155    */
156   public final String getFilename() {
157     if (filename != null) {
158       return filename;
159     } else {
160       return "";
161     }
162   }
163 }