001    package org.apache.hadoop.fs.http.server;
002    
003    import org.apache.hadoop.fs.http.client.HttpFSFileSystem;
004    import org.apache.hadoop.lib.wsrs.BooleanParam;
005    import org.apache.hadoop.lib.wsrs.EnumParam;
006    import org.apache.hadoop.lib.wsrs.LongParam;
007    import org.apache.hadoop.lib.wsrs.ShortParam;
008    import org.apache.hadoop.lib.wsrs.StringParam;
009    import org.apache.hadoop.lib.wsrs.UserProvider;
010    import org.slf4j.MDC;
011    
012    import java.util.regex.Pattern;
013    
014    /**
015     * HttpFS HTTP Parameters used by {@link HttpFSServer}.
016     */
017    public class HttpFSParams {
018    
019      /**
020       * To avoid instantiation.
021       */
022      private HttpFSParams() {
023      }
024    
025      /**
026       * Class for access-time parameter.
027       */
028      public static class AccessTimeParam extends LongParam {
029    
030        /**
031         * Parameter name.
032         */
033        public static final String NAME = HttpFSFileSystem.ACCESS_TIME_PARAM;
034    
035        /**
036         * Default parameter value.
037         */
038        public static final String DEFAULT = "-1";
039    
040        /**
041         * Constructor.
042         *
043         * @param str parameter value.
044         */
045        public AccessTimeParam(String str) {
046          super(NAME, str);
047        }
048      }
049    
050      /**
051       * Class for block-size parameter.
052       */
053      public static class BlockSizeParam extends LongParam {
054    
055        /**
056         * Parameter name.
057         */
058        public static final String NAME = HttpFSFileSystem.BLOCKSIZE_PARAM;
059    
060        /**
061         * Default parameter value.
062         */
063        public static final String DEFAULT = "-1";
064    
065        /**
066         * Constructor.
067         *
068         * @param str parameter value.
069         */
070        public BlockSizeParam(String str) {
071          super(NAME, str);
072        }
073      }
074    
075      /**
076       * Class for data parameter.
077       */
078      public static class DataParam extends BooleanParam {
079    
080        /**
081         * Parameter name.
082         */
083        public static final String NAME = "data";
084    
085        /**
086         * Default parameter value.
087         */
088        public static final String DEFAULT = "false";
089    
090        /**
091         * Constructor.
092         *
093         * @param str parameter value.
094         */
095        public DataParam(String str) {
096          super(NAME, str);
097        }
098      }
099    
100      /**
101       * Class for DELETE operation parameter.
102       */
103      public static class DeleteOpParam extends EnumParam<HttpFSFileSystem.DeleteOpValues> {
104    
105        /**
106         * Parameter name.
107         */
108        public static final String NAME = HttpFSFileSystem.OP_PARAM;
109    
110        /**
111         * Constructor.
112         *
113         * @param str parameter value.
114         */
115        public DeleteOpParam(String str) {
116          super(NAME, str, HttpFSFileSystem.DeleteOpValues.class);
117        }
118      }
119    
120      /**
121       * Class for delete's recursive parameter.
122       */
123      public static class DeleteRecursiveParam extends BooleanParam {
124    
125        /**
126         * Parameter name.
127         */
128        public static final String NAME = HttpFSFileSystem.RECURSIVE_PARAM;
129    
130        /**
131         * Default parameter value.
132         */
133        public static final String DEFAULT = "false";
134    
135        /**
136         * Constructor.
137         *
138         * @param str parameter value.
139         */
140        public DeleteRecursiveParam(String str) {
141          super(NAME, str);
142        }
143      }
144    
145      /**
146       * Class for do-as parameter.
147       */
148      public static class DoAsParam extends StringParam {
149    
150        /**
151         * Parameter name.
152         */
153        public static final String NAME = HttpFSFileSystem.DO_AS_PARAM;
154    
155        /**
156         * Default parameter value.
157         */
158        public static final String DEFAULT = "";
159    
160        /**
161         * Constructor.
162         *
163         * @param str parameter value.
164         */
165        public DoAsParam(String str) {
166          super(NAME, str, UserProvider.USER_PATTERN);
167        }
168    
169        /**
170         * Delegates to parent and then adds do-as user to
171         * MDC context for logging purposes.
172         *
173         * @param name parameter name.
174         * @param str parameter value.
175         *
176         * @return parsed parameter
177         */
178        @Override
179        public String parseParam(String name, String str) {
180          String doAs = super.parseParam(name, str);
181          MDC.put(NAME, (doAs != null) ? doAs : "-");
182          return doAs;
183        }
184      }
185    
186      /**
187       * Class for filter parameter.
188       */
189      public static class FilterParam extends StringParam {
190    
191        /**
192         * Parameter name.
193         */
194        public static final String NAME = "filter";
195    
196        /**
197         * Default parameter value.
198         */
199        public static final String DEFAULT = "";
200    
201        /**
202         * Constructor.
203         *
204         * @param expr parameter value.
205         */
206        public FilterParam(String expr) {
207          super(NAME, expr);
208        }
209    
210      }
211    
212      /**
213       * Class for path parameter.
214       */
215      public static class FsPathParam extends StringParam {
216    
217        /**
218         * Constructor.
219         *
220         * @param path parameter value.
221         */
222        public FsPathParam(String path) {
223          super("path", path);
224        }
225    
226        /**
227         * Makes the path absolute adding '/' to it.
228         * <p/>
229         * This is required because JAX-RS resolution of paths does not add
230         * the root '/'.
231         */
232        public void makeAbsolute() {
233          String path = value();
234          path = "/" + ((path != null) ? path : "");
235          setValue(path);
236        }
237    
238      }
239    
240      /**
241       * Class for GET operation parameter.
242       */
243      public static class GetOpParam extends EnumParam<HttpFSFileSystem.GetOpValues> {
244    
245        /**
246         * Parameter name.
247         */
248        public static final String NAME = HttpFSFileSystem.OP_PARAM;
249    
250        /**
251         * Constructor.
252         *
253         * @param str parameter value.
254         */
255        public GetOpParam(String str) {
256          super(NAME, str, HttpFSFileSystem.GetOpValues.class);
257        }
258      }
259    
260      /**
261       * Class for group parameter.
262       */
263      public static class GroupParam extends StringParam {
264    
265        /**
266         * Parameter name.
267         */
268        public static final String NAME = HttpFSFileSystem.GROUP_PARAM;
269    
270        /**
271         * Default parameter value.
272         */
273        public static final String DEFAULT = "";
274    
275        /**
276         * Constructor.
277         *
278         * @param str parameter value.
279         */
280        public GroupParam(String str) {
281          super(NAME, str, UserProvider.USER_PATTERN);
282        }
283    
284      }
285    
286      /**
287       * Class for len parameter.
288       */
289      public static class LenParam extends LongParam {
290    
291        /**
292         * Parameter name.
293         */
294        public static final String NAME = "len";
295    
296        /**
297         * Default parameter value.
298         */
299        public static final String DEFAULT = "-1";
300    
301        /**
302         * Constructor.
303         *
304         * @param str parameter value.
305         */
306        public LenParam(String str) {
307          super(NAME, str);
308        }
309      }
310    
311      /**
312       * Class for modified-time parameter.
313       */
314      public static class ModifiedTimeParam extends LongParam {
315    
316        /**
317         * Parameter name.
318         */
319        public static final String NAME = HttpFSFileSystem.MODIFICATION_TIME_PARAM;
320    
321        /**
322         * Default parameter value.
323         */
324        public static final String DEFAULT = "-1";
325    
326        /**
327         * Constructor.
328         *
329         * @param str parameter value.
330         */
331        public ModifiedTimeParam(String str) {
332          super(NAME, str);
333        }
334      }
335    
336      /**
337       * Class for offset parameter.
338       */
339      public static class OffsetParam extends LongParam {
340    
341        /**
342         * Parameter name.
343         */
344        public static final String NAME = "offset";
345    
346        /**
347         * Default parameter value.
348         */
349        public static final String DEFAULT = "0";
350    
351        /**
352         * Constructor.
353         *
354         * @param str parameter value.
355         */
356        public OffsetParam(String str) {
357          super(NAME, str);
358        }
359      }
360    
361      /**
362       * Class for overwrite parameter.
363       */
364      public static class OverwriteParam extends BooleanParam {
365    
366        /**
367         * Parameter name.
368         */
369        public static final String NAME = HttpFSFileSystem.OVERWRITE_PARAM;
370    
371        /**
372         * Default parameter value.
373         */
374        public static final String DEFAULT = "true";
375    
376        /**
377         * Constructor.
378         *
379         * @param str parameter value.
380         */
381        public OverwriteParam(String str) {
382          super(NAME, str);
383        }
384      }
385    
386      /**
387       * Class for owner parameter.
388       */
389      public static class OwnerParam extends StringParam {
390    
391        /**
392         * Parameter name.
393         */
394        public static final String NAME = HttpFSFileSystem.OWNER_PARAM;
395    
396        /**
397         * Default parameter value.
398         */
399        public static final String DEFAULT = "";
400    
401        /**
402         * Constructor.
403         *
404         * @param str parameter value.
405         */
406        public OwnerParam(String str) {
407          super(NAME, str, UserProvider.USER_PATTERN);
408        }
409    
410      }
411    
412      /**
413       * Class for permission parameter.
414       */
415      public static class PermissionParam extends StringParam {
416    
417        /**
418         * Parameter name.
419         */
420        public static final String NAME = HttpFSFileSystem.PERMISSION_PARAM;
421    
422        /**
423         * Default parameter value.
424         */
425        public static final String DEFAULT = HttpFSFileSystem.DEFAULT_PERMISSION;
426    
427    
428        /**
429         * Symbolic Unix permissions regular expression pattern.
430         */
431        private static final Pattern PERMISSION_PATTERN =
432          Pattern.compile(DEFAULT + "|(-[-r][-w][-x][-r][-w][-x][-r][-w][-x])" + "|[0-7][0-7][0-7]");
433    
434        /**
435         * Constructor.
436         *
437         * @param permission parameter value.
438         */
439        public PermissionParam(String permission) {
440          super(NAME, permission.toLowerCase(), PERMISSION_PATTERN);
441        }
442    
443      }
444    
445      /**
446       * Class for POST operation parameter.
447       */
448      public static class PostOpParam extends EnumParam<HttpFSFileSystem.PostOpValues> {
449    
450        /**
451         * Parameter name.
452         */
453        public static final String NAME = HttpFSFileSystem.OP_PARAM;
454    
455        /**
456         * Constructor.
457         *
458         * @param str parameter value.
459         */
460        public PostOpParam(String str) {
461          super(NAME, str, HttpFSFileSystem.PostOpValues.class);
462        }
463      }
464    
465      /**
466       * Class for PUT operation parameter.
467       */
468      public static class PutOpParam extends EnumParam<HttpFSFileSystem.PutOpValues> {
469    
470        /**
471         * Parameter name.
472         */
473        public static final String NAME = HttpFSFileSystem.OP_PARAM;
474    
475        /**
476         * Constructor.
477         *
478         * @param str parameter value.
479         */
480        public PutOpParam(String str) {
481          super(NAME, str, HttpFSFileSystem.PutOpValues.class);
482        }
483      }
484    
485      /**
486       * Class for replication parameter.
487       */
488      public static class ReplicationParam extends ShortParam {
489    
490        /**
491         * Parameter name.
492         */
493        public static final String NAME = HttpFSFileSystem.REPLICATION_PARAM;
494    
495        /**
496         * Default parameter value.
497         */
498        public static final String DEFAULT = "-1";
499    
500        /**
501         * Constructor.
502         *
503         * @param str parameter value.
504         */
505        public ReplicationParam(String str) {
506          super(NAME, str);
507        }
508      }
509    
510      /**
511       * Class for to-path parameter.
512       */
513      public static class ToPathParam extends StringParam {
514    
515        /**
516         * Parameter name.
517         */
518        public static final String NAME = HttpFSFileSystem.DESTINATION_PARAM;
519    
520        /**
521         * Default parameter value.
522         */
523        public static final String DEFAULT = "";
524    
525        /**
526         * Constructor.
527         *
528         * @param path parameter value.
529         */
530        public ToPathParam(String path) {
531          super(NAME, path);
532        }
533      }
534    }