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.classification.InterfaceAudience;
021    import org.apache.hadoop.fs.XAttrCodec;
022    import org.apache.hadoop.fs.XAttrSetFlag;
023    import org.apache.hadoop.fs.http.client.HttpFSFileSystem;
024    import org.apache.hadoop.fs.http.client.HttpFSFileSystem.Operation;
025    import org.apache.hadoop.lib.wsrs.BooleanParam;
026    import org.apache.hadoop.lib.wsrs.EnumParam;
027    import org.apache.hadoop.lib.wsrs.EnumSetParam;
028    import org.apache.hadoop.lib.wsrs.LongParam;
029    import org.apache.hadoop.lib.wsrs.Param;
030    import org.apache.hadoop.lib.wsrs.ParametersProvider;
031    import org.apache.hadoop.lib.wsrs.ShortParam;
032    import org.apache.hadoop.lib.wsrs.StringParam;
033    import org.apache.hadoop.lib.wsrs.UserProvider;
034    import org.slf4j.MDC;
035    
036    import javax.ws.rs.ext.Provider;
037    import java.util.HashMap;
038    import java.util.Map;
039    import java.util.regex.Pattern;
040    
041    import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_WEBHDFS_ACL_PERMISSION_PATTERN_DEFAULT;
042    
043    /**
044     * HttpFS ParametersProvider.
045     */
046    @Provider
047    @InterfaceAudience.Private
048    @SuppressWarnings("unchecked")
049    public class HttpFSParametersProvider extends ParametersProvider {
050    
051      private static final Map<Enum, Class<Param<?>>[]> PARAMS_DEF =
052        new HashMap<Enum, Class<Param<?>>[]>();
053    
054      static {
055        PARAMS_DEF.put(Operation.OPEN,
056          new Class[]{DoAsParam.class, OffsetParam.class, LenParam.class});
057        PARAMS_DEF.put(Operation.GETFILESTATUS, new Class[]{DoAsParam.class});
058        PARAMS_DEF.put(Operation.LISTSTATUS,
059          new Class[]{DoAsParam.class, FilterParam.class});
060        PARAMS_DEF.put(Operation.GETHOMEDIRECTORY, new Class[]{DoAsParam.class});
061        PARAMS_DEF.put(Operation.GETCONTENTSUMMARY, new Class[]{DoAsParam.class});
062        PARAMS_DEF.put(Operation.GETFILECHECKSUM, new Class[]{DoAsParam.class});
063        PARAMS_DEF.put(Operation.GETFILEBLOCKLOCATIONS,
064          new Class[]{DoAsParam.class});
065        PARAMS_DEF.put(Operation.GETACLSTATUS, new Class[]{DoAsParam.class});
066        PARAMS_DEF.put(Operation.INSTRUMENTATION, new Class[]{DoAsParam.class});
067        PARAMS_DEF.put(Operation.APPEND,
068          new Class[]{DoAsParam.class, DataParam.class});
069        PARAMS_DEF.put(Operation.CONCAT, new Class[]{SourcesParam.class});
070        PARAMS_DEF.put(Operation.CREATE,
071          new Class[]{DoAsParam.class, PermissionParam.class, OverwriteParam.class,
072                      ReplicationParam.class, BlockSizeParam.class, DataParam.class});
073        PARAMS_DEF.put(Operation.MKDIRS,
074          new Class[]{DoAsParam.class, PermissionParam.class});
075        PARAMS_DEF.put(Operation.RENAME,
076          new Class[]{DoAsParam.class, DestinationParam.class});
077        PARAMS_DEF.put(Operation.SETOWNER,
078          new Class[]{DoAsParam.class, OwnerParam.class, GroupParam.class});
079        PARAMS_DEF.put(Operation.SETPERMISSION,
080          new Class[]{DoAsParam.class, PermissionParam.class});
081        PARAMS_DEF.put(Operation.SETREPLICATION,
082          new Class[]{DoAsParam.class, ReplicationParam.class});
083        PARAMS_DEF.put(Operation.SETTIMES,
084          new Class[]{DoAsParam.class, ModifiedTimeParam.class,
085                      AccessTimeParam.class});
086        PARAMS_DEF.put(Operation.DELETE,
087          new Class[]{DoAsParam.class, RecursiveParam.class});
088        PARAMS_DEF.put(Operation.SETACL,
089                new Class[]{DoAsParam.class, AclPermissionParam.class});
090        PARAMS_DEF.put(Operation.REMOVEACL,
091                new Class[]{DoAsParam.class});
092        PARAMS_DEF.put(Operation.MODIFYACLENTRIES,
093                new Class[]{DoAsParam.class, AclPermissionParam.class});
094        PARAMS_DEF.put(Operation.REMOVEACLENTRIES,
095                new Class[]{DoAsParam.class, AclPermissionParam.class});
096        PARAMS_DEF.put(Operation.REMOVEDEFAULTACL,
097                new Class[]{DoAsParam.class});
098        PARAMS_DEF.put(Operation.SETXATTR,
099          new Class[]{DoAsParam.class, XAttrNameParam.class, XAttrValueParam.class, 
100                      XAttrSetFlagParam.class});
101        PARAMS_DEF.put(Operation.REMOVEXATTR, 
102          new Class[]{DoAsParam.class, XAttrNameParam.class});
103        PARAMS_DEF.put(Operation.GETXATTRS, 
104          new Class[]{DoAsParam.class, XAttrNameParam.class, XAttrEncodingParam.class});
105        PARAMS_DEF.put(Operation.LISTXATTRS,
106          new Class[]{DoAsParam.class});
107      }
108    
109      public HttpFSParametersProvider() {
110        super(HttpFSFileSystem.OP_PARAM, HttpFSFileSystem.Operation.class,
111              PARAMS_DEF);
112      }
113    
114      /**
115       * Class for access-time parameter.
116       */
117      @InterfaceAudience.Private
118      public static class AccessTimeParam extends LongParam {
119    
120        /**
121         * Parameter name.
122         */
123        public static final String NAME = HttpFSFileSystem.ACCESS_TIME_PARAM;
124        /**
125         * Constructor.
126         */
127        public AccessTimeParam() {
128          super(NAME, -1l);
129        }
130      }
131    
132      /**
133       * Class for block-size parameter.
134       */
135      @InterfaceAudience.Private
136      public static class BlockSizeParam extends LongParam {
137    
138        /**
139         * Parameter name.
140         */
141        public static final String NAME = HttpFSFileSystem.BLOCKSIZE_PARAM;
142    
143        /**
144         * Constructor.
145         */
146        public BlockSizeParam() {
147          super(NAME, -1l);
148        }
149      }
150    
151      /**
152       * Class for data parameter.
153       */
154      @InterfaceAudience.Private
155      public static class DataParam extends BooleanParam {
156    
157        /**
158         * Parameter name.
159         */
160        public static final String NAME = "data";
161    
162        /**
163         * Constructor.
164         */
165        public DataParam() {
166          super(NAME, false);
167        }
168      }
169    
170      /**
171       * Class for operation parameter.
172       */
173      @InterfaceAudience.Private
174      public static class OperationParam extends EnumParam<HttpFSFileSystem.Operation> {
175    
176        /**
177         * Parameter name.
178         */
179        public static final String NAME = HttpFSFileSystem.OP_PARAM;
180        /**
181         * Constructor.
182         */
183        public OperationParam(String operation) {
184          super(NAME, HttpFSFileSystem.Operation.class,
185                HttpFSFileSystem.Operation.valueOf(operation.toUpperCase()));
186        }
187      }
188    
189      /**
190       * Class for delete's recursive parameter.
191       */
192      @InterfaceAudience.Private
193      public static class RecursiveParam extends BooleanParam {
194    
195        /**
196         * Parameter name.
197         */
198        public static final String NAME = HttpFSFileSystem.RECURSIVE_PARAM;
199    
200        /**
201         * Constructor.
202         */
203        public RecursiveParam() {
204          super(NAME, false);
205        }
206      }
207    
208      /**
209       * Class for do-as parameter.
210       */
211      @InterfaceAudience.Private
212      public static class DoAsParam extends StringParam {
213    
214        /**
215         * Parameter name.
216         */
217        public static final String NAME = HttpFSFileSystem.DO_AS_PARAM;
218    
219        /**
220         * Constructor.
221         */
222        public DoAsParam() {
223          super(NAME, null, UserProvider.getUserPattern());
224        }
225    
226        /**
227         * Delegates to parent and then adds do-as user to
228         * MDC context for logging purposes.
229         *
230         *
231         * @param str parameter value.
232         *
233         * @return parsed parameter
234         */
235        @Override
236        public String parseParam(String str) {
237          String doAs = super.parseParam(str);
238          MDC.put(getName(), (doAs != null) ? doAs : "-");
239          return doAs;
240        }
241      }
242    
243      /**
244       * Class for filter parameter.
245       */
246      @InterfaceAudience.Private
247      public static class FilterParam extends StringParam {
248    
249        /**
250         * Parameter name.
251         */
252        public static final String NAME = "filter";
253    
254        /**
255         * Constructor.
256         */
257        public FilterParam() {
258          super(NAME, null);
259        }
260    
261      }
262    
263      /**
264       * Class for group parameter.
265       */
266      @InterfaceAudience.Private
267      public static class GroupParam extends StringParam {
268    
269        /**
270         * Parameter name.
271         */
272        public static final String NAME = HttpFSFileSystem.GROUP_PARAM;
273    
274        /**
275         * Constructor.
276         */
277        public GroupParam() {
278          super(NAME, null, UserProvider.getUserPattern());
279        }
280    
281      }
282    
283      /**
284       * Class for len parameter.
285       */
286      @InterfaceAudience.Private
287      public static class LenParam extends LongParam {
288    
289        /**
290         * Parameter name.
291         */
292        public static final String NAME = "length";
293    
294        /**
295         * Constructor.
296         */
297        public LenParam() {
298          super(NAME, -1l);
299        }
300      }
301    
302      /**
303       * Class for modified-time parameter.
304       */
305      @InterfaceAudience.Private
306      public static class ModifiedTimeParam extends LongParam {
307    
308        /**
309         * Parameter name.
310         */
311        public static final String NAME = HttpFSFileSystem.MODIFICATION_TIME_PARAM;
312    
313        /**
314         * Constructor.
315         */
316        public ModifiedTimeParam() {
317          super(NAME, -1l);
318        }
319      }
320    
321      /**
322       * Class for offset parameter.
323       */
324      @InterfaceAudience.Private
325      public static class OffsetParam extends LongParam {
326    
327        /**
328         * Parameter name.
329         */
330        public static final String NAME = "offset";
331    
332        /**
333         * Constructor.
334         */
335        public OffsetParam() {
336          super(NAME, 0l);
337        }
338      }
339    
340      /**
341       * Class for overwrite parameter.
342       */
343      @InterfaceAudience.Private
344      public static class OverwriteParam extends BooleanParam {
345    
346        /**
347         * Parameter name.
348         */
349        public static final String NAME = HttpFSFileSystem.OVERWRITE_PARAM;
350    
351        /**
352         * Constructor.
353         */
354        public OverwriteParam() {
355          super(NAME, true);
356        }
357      }
358    
359      /**
360       * Class for owner parameter.
361       */
362      @InterfaceAudience.Private
363      public static class OwnerParam extends StringParam {
364    
365        /**
366         * Parameter name.
367         */
368        public static final String NAME = HttpFSFileSystem.OWNER_PARAM;
369    
370        /**
371         * Constructor.
372         */
373        public OwnerParam() {
374          super(NAME, null, UserProvider.getUserPattern());
375        }
376    
377      }
378    
379      /**
380       * Class for permission parameter.
381       */
382      @InterfaceAudience.Private
383      public static class PermissionParam extends ShortParam {
384    
385        /**
386         * Parameter name.
387         */
388        public static final String NAME = HttpFSFileSystem.PERMISSION_PARAM;
389    
390    
391        /**
392         * Constructor.
393         */
394        public PermissionParam() {
395          super(NAME, HttpFSFileSystem.DEFAULT_PERMISSION, 8);
396        }
397    
398      }
399    
400      /**
401       * Class for AclPermission parameter.
402       */
403      @InterfaceAudience.Private
404      public static class AclPermissionParam extends StringParam {
405    
406        /**
407         * Parameter name.
408         */
409        public static final String NAME = HttpFSFileSystem.ACLSPEC_PARAM;
410    
411        /**
412         * Constructor.
413         */
414        public AclPermissionParam() {
415          super(NAME, HttpFSFileSystem.ACLSPEC_DEFAULT,
416                  Pattern.compile(DFS_WEBHDFS_ACL_PERMISSION_PATTERN_DEFAULT));
417        }
418      }
419    
420      /**
421       * Class for replication parameter.
422       */
423      @InterfaceAudience.Private
424      public static class ReplicationParam extends ShortParam {
425    
426        /**
427         * Parameter name.
428         */
429        public static final String NAME = HttpFSFileSystem.REPLICATION_PARAM;
430    
431        /**
432         * Constructor.
433         */
434        public ReplicationParam() {
435          super(NAME, (short) -1);
436        }
437      }
438    
439      /**
440       * Class for concat sources parameter.
441       */
442      @InterfaceAudience.Private
443      public static class SourcesParam extends StringParam {
444    
445        /**
446         * Parameter name.
447         */
448        public static final String NAME = HttpFSFileSystem.SOURCES_PARAM;
449    
450        /**
451         * Constructor.
452         */
453        public SourcesParam() {
454          super(NAME, null);
455        }
456      }
457    
458      /**
459       * Class for to-path parameter.
460       */
461      @InterfaceAudience.Private
462      public static class DestinationParam extends StringParam {
463    
464        /**
465         * Parameter name.
466         */
467        public static final String NAME = HttpFSFileSystem.DESTINATION_PARAM;
468    
469        /**
470         * Constructor.
471         */
472        public DestinationParam() {
473          super(NAME, null);
474        }
475      }
476      
477      /**
478       * Class for xattr parameter.
479       */
480      @InterfaceAudience.Private
481      public static class XAttrNameParam extends StringParam {
482        public static final String XATTR_NAME_REGX = 
483            "^(user\\.|trusted\\.|system\\.|security\\.).+";
484        /**
485         * Parameter name.
486         */
487        public static final String NAME = HttpFSFileSystem.XATTR_NAME_PARAM;
488        private static final Pattern pattern = Pattern.compile(XATTR_NAME_REGX);
489    
490        /**
491         * Constructor.
492         */
493        public XAttrNameParam() {
494          super(NAME, null, pattern);
495        }
496      }
497    
498      /**
499       * Class for xattr parameter.
500       */
501      @InterfaceAudience.Private
502      public static class XAttrValueParam extends StringParam {
503        /**
504         * Parameter name.
505         */
506        public static final String NAME = HttpFSFileSystem.XATTR_VALUE_PARAM;
507    
508        /**
509         * Constructor.
510         */
511        public XAttrValueParam() {
512          super(NAME, null);
513        }
514      }
515    
516      /**
517       * Class for xattr parameter.
518       */
519      @InterfaceAudience.Private
520      public static class XAttrSetFlagParam extends EnumSetParam<XAttrSetFlag> {
521        /**
522         * Parameter name.
523         */
524        public static final String NAME = HttpFSFileSystem.XATTR_SET_FLAG_PARAM;
525    
526        /**
527         * Constructor.
528         */
529        public XAttrSetFlagParam() {
530          super(NAME, XAttrSetFlag.class, null);
531        }
532      }
533    
534      /**
535       * Class for xattr parameter.
536       */
537      @InterfaceAudience.Private
538      public static class XAttrEncodingParam extends EnumParam<XAttrCodec> {
539        /**
540         * Parameter name.
541         */
542        public static final String NAME = HttpFSFileSystem.XATTR_ENCODING_PARAM;
543    
544        /**
545         * Constructor.
546         */
547        public XAttrEncodingParam() {
548          super(NAME, XAttrCodec.class, null);
549        }
550      }
551    }