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.ftp.core.command;
21  
22  import org.waarp.common.exception.InvalidArgumentException;
23  
24  import java.nio.charset.Charset;
25  
26  /**
27   * Definition of all Argument of Parameter commands (MODE, STRU, TYPE)
28   */
29  public class FtpArgumentCode {
30  
31    private FtpArgumentCode() {
32    }
33  
34    /**
35     * Type of transmission
36     *
37     * org.waarp.ftp.core.data TransferType
38     */
39    public enum TransferType {
40      /**
41       * Ascii TransferType
42       */
43      ASCII('A', "ASCII"),
44      /**
45       * Ebcdic TransferType
46       */
47      EBCDIC('E', "ebcdic-cp-us"), // could be ebcdic-cp-LG where LG is
48      // language like fr, gb, ...
49      /**
50       * Image TransferType
51       */
52      IMAGE('I'),
53      /**
54       * Specific Length TransferType
55       */
56      LENGTH('L');
57      /**
58       * TransferType
59       */
60      public final char type;
61  
62      /**
63       * Charset Name if any
64       */
65      public final Charset charset;
66  
67      TransferType(final char type) {
68        this.type = type;
69        charset = Charset.defaultCharset();
70      }
71  
72      TransferType(final char type, final String charsetName) {
73        this.type = type;
74        charset = Charset.forName(charsetName);
75      }
76    }
77  
78    /**
79     * SubType of transmission
80     *
81     * org.waarp.ftp.core.data TransferSubType
82     */
83    public enum TransferSubType {
84      /**
85       * Non-print TransferSubType
86       */
87      NONPRINT('N'),
88      /**
89       * Telnet format effectors TransferSubType
90       */
91      TELNET('T'),
92      /**
93       * Carriage Control ASA TransferSubType
94       */
95      CARRIAGE('C');
96      /**
97       * TransferSubType
98       */
99      public final char subtype;
100 
101     TransferSubType(final char subtype) {
102       this.subtype = subtype;
103     }
104   }
105 
106   /**
107    * Structure of transmission
108    *
109    * org.waarp.ftp.core.data TransferStructure
110    */
111   public enum TransferStructure {
112     /**
113      * FileInterface TransferStructure
114      */
115     FILE('F'),
116     /**
117      * Record TransferStructure
118      */
119     RECORD('R'),
120     /**
121      * Page TransferStructure
122      */
123     PAGE('P');
124     /**
125      * TransferStructure
126      */
127     public final char structure;
128 
129     TransferStructure(final char structure) {
130       this.structure = structure;
131     }
132   }
133 
134   /**
135    * Mode of transmission
136    *
137    * org.waarp.ftp.core.data TransferMode
138    */
139   public enum TransferMode {
140     /**
141      * Stream TransferMode
142      */
143     STREAM('S'),
144     /**
145      * Block TransferMode
146      */
147     BLOCK('B'),
148     /**
149      * Compressed TransferMode
150      */
151     COMPRESSED('C'),
152     /**
153      * Standard ZLIB compression
154      */
155     ZLIB('Z');
156 
157     /**
158      * TransferMode
159      */
160     public final char mode;
161 
162     TransferMode(final char mode) {
163       this.mode = mode;
164     }
165   }
166 
167   /**
168    * Get the TransferType according to the char
169    *
170    * @param type
171    *
172    * @return the corresponding TransferType
173    *
174    * @throws InvalidArgumentException if the type is unknown
175    */
176   public static TransferType getTransferType(final char type)
177       throws InvalidArgumentException {
178     switch (type) {
179       case 'A':
180       case 'a':
181         return TransferType.ASCII;
182       case 'E':
183       case 'e':
184         return TransferType.EBCDIC;
185       case 'I':
186       case 'i':
187         return TransferType.IMAGE;
188       case 'L':
189       case 'l':
190         return TransferType.LENGTH;
191       default:
192         throw new InvalidArgumentException(
193             "Argument for TransferType is not allowed: " + type);
194     }
195   }
196 
197   /**
198    * Get the TransferSubType according to the char
199    *
200    * @param subType
201    *
202    * @return the corresponding TransferSubType
203    *
204    * @throws InvalidArgumentException if the TransferSubType is
205    *     unknown
206    */
207   public static TransferSubType getTransferSubType(final char subType)
208       throws InvalidArgumentException {
209     switch (subType) {
210       case 'C':
211       case 'c':
212         return TransferSubType.CARRIAGE;
213       case 'N':
214       case 'n':
215         return TransferSubType.NONPRINT;
216       case 'T':
217       case 't':
218         return TransferSubType.TELNET;
219       default:
220         throw new InvalidArgumentException(
221             "Argument for TransferSubType is not allowed: " + subType);
222     }
223   }
224 
225   /**
226    * Get the TransferStructure according to the char
227    *
228    * @param structure
229    *
230    * @return the corresponding TransferStructure
231    *
232    * @throws InvalidArgumentException if the TransferStructure is
233    *     unknown
234    */
235   public static TransferStructure getTransferStructure(final char structure)
236       throws InvalidArgumentException {
237     switch (structure) {
238       case 'P':
239       case 'p':
240         return TransferStructure.PAGE;
241       case 'F':
242       case 'f':
243         return TransferStructure.FILE;
244       case 'R':
245       case 'r':
246         return TransferStructure.RECORD;
247       default:
248         throw new InvalidArgumentException(
249             "Argument for TransferStructure is not allowed: " + structure);
250     }
251   }
252 
253   /**
254    * Get the TransferMode according to the char
255    *
256    * @param mode
257    *
258    * @return the corresponding TransferMode
259    *
260    * @throws InvalidArgumentException if the TransferMode is unknown
261    */
262   public static TransferMode getTransferMode(final char mode)
263       throws InvalidArgumentException {
264     switch (mode) {
265       case 'B':
266       case 'b':
267         return TransferMode.BLOCK;
268       case 'C':
269       case 'c':
270         return TransferMode.COMPRESSED;
271       case 'Z':
272       case 'z':
273         return TransferMode.ZLIB;
274       case 'S':
275       case 's':
276         return TransferMode.STREAM;
277       default:
278         throw new InvalidArgumentException(
279             "Argument for TransferMode is not allowed: " + mode);
280     }
281   }
282 }