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