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