View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, 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 GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package openr66.protocol.localhandler.packet;
22  
23  import openr66.protocol.exception.OpenR66ProtocolPacketException;
24  
25  import org.jboss.netty.buffer.ChannelBuffer;
26  import org.jboss.netty.buffer.ChannelBuffers;
27  
28  /**
29   * Information of files class
30   *
31   * header = "rulename" middle = requestedInfo end = "FILENAME"
32   *
33   * @author frederic bregier
34   */
35  public class InformationPacket extends AbstractLocalPacket {
36  
37      public static enum ASKENUM {
38          ASKEXIST, ASKMLSDETAIL, ASKLIST, ASKMLSLIST;
39      }
40  
41      private final String rulename;
42  
43      private final byte requestedInfo;
44  
45      private final String filename;
46  
47      /**
48       * @param headerLength
49       * @param middleLength
50       * @param endLength
51       * @param buf
52       * @return the new EndTransferPacket from buffer
53       * @throws OpenR66ProtocolPacketException
54       */
55      public static InformationPacket createFromBuffer(int headerLength,
56              int middleLength, int endLength, ChannelBuffer buf)
57              throws OpenR66ProtocolPacketException {
58          if (headerLength - 1 <= 0) {
59              throw new OpenR66ProtocolPacketException("Not enough data");
60          }
61          if (middleLength != 1) {
62              throw new OpenR66ProtocolPacketException("Not enough data");
63          }
64          final byte[] bheader = new byte[headerLength - 1];
65          final byte[] bend = new byte[endLength];
66          if (headerLength - 1 > 0) {
67              buf.readBytes(bheader);
68          }
69          byte request = buf.readByte();
70          if (endLength > 0) {
71              buf.readBytes(bend);
72          }
73          final String sheader = new String(bheader);
74          final String send = new String(bend);
75          return new InformationPacket(sheader, request, send);
76      }
77  
78      /**
79       * @param rulename
80       * @param request
81       * @param filename
82       */
83      public InformationPacket(String rulename, byte request, String filename) {
84          this.rulename = rulename;
85          this.requestedInfo = request;
86          this.filename = filename;
87      }
88  
89      /*
90       * (non-Javadoc)
91       *
92       * @see openr66.protocol.localhandler.packet.AbstractLocalPacket#createEnd()
93       */
94      @Override
95      public void createEnd() {
96          if (filename != null) {
97              end = ChannelBuffers.wrappedBuffer(filename.getBytes());
98          }
99      }
100 
101     /*
102      * (non-Javadoc)
103      *
104      * @see
105      * openr66.protocol.localhandler.packet.AbstractLocalPacket#createHeader()
106      */
107     @Override
108     public void createHeader() throws OpenR66ProtocolPacketException {
109         if (rulename == null) {
110             throw new OpenR66ProtocolPacketException("Not enough data");
111         }
112         header = ChannelBuffers.wrappedBuffer(rulename.getBytes());
113     }
114 
115     /*
116      * (non-Javadoc)
117      *
118      * @see
119      * openr66.protocol.localhandler.packet.AbstractLocalPacket#createMiddle()
120      */
121     @Override
122     public void createMiddle() {
123         byte[] newbytes = {
124                 requestedInfo };
125         middle = ChannelBuffers.wrappedBuffer(newbytes);
126     }
127 
128     @Override
129     public byte getType() {
130         return LocalPacketFactory.INFORMATIONPACKET;
131     }
132 
133     /*
134      * (non-Javadoc)
135      *
136      * @see openr66.protocol.localhandler.packet.AbstractLocalPacket#toString()
137      */
138     @Override
139     public String toString() {
140         return "InformationPacket: " + requestedInfo + " " + rulename+" "+filename;
141     }
142 
143     /**
144      * @return the requestId
145      */
146     public byte getRequest() {
147         return requestedInfo;
148     }
149 
150     /**
151      * @return the rulename
152      */
153     public String getRulename() {
154         return rulename;
155     }
156 
157     /**
158      * @return the filename
159      */
160     public String getFilename() {
161         if (filename != null) {
162             return filename;
163         } else {
164             return "";
165         }
166     }
167 }