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    PARAMS_DEF.put(Operation.GETALLSTORAGEPOLICY, new Class[] {});
095    PARAMS_DEF.put(Operation.GETSTORAGEPOLICY, new Class[] {});
096    PARAMS_DEF.put(Operation.SETSTORAGEPOLICY,
097        new Class[] {PolicyNameParam.class});
098    PARAMS_DEF.put(Operation.UNSETSTORAGEPOLICY, new Class[] {});
099  }
100
101  public HttpFSParametersProvider() {
102    super(HttpFSFileSystem.OP_PARAM, HttpFSFileSystem.Operation.class,
103          PARAMS_DEF);
104  }
105
106  /**
107   * Class for access-time parameter.
108   */
109  @InterfaceAudience.Private
110  public static class AccessTimeParam extends LongParam {
111
112    /**
113     * Parameter name.
114     */
115    public static final String NAME = HttpFSFileSystem.ACCESS_TIME_PARAM;
116    /**
117     * Constructor.
118     */
119    public AccessTimeParam() {
120      super(NAME, -1l);
121    }
122  }
123
124  /**
125   * Class for block-size parameter.
126   */
127  @InterfaceAudience.Private
128  public static class BlockSizeParam extends LongParam {
129
130    /**
131     * Parameter name.
132     */
133    public static final String NAME = HttpFSFileSystem.BLOCKSIZE_PARAM;
134
135    /**
136     * Constructor.
137     */
138    public BlockSizeParam() {
139      super(NAME, -1l);
140    }
141  }
142
143  /**
144   * Class for data parameter.
145   */
146  @InterfaceAudience.Private
147  public static class DataParam extends BooleanParam {
148
149    /**
150     * Parameter name.
151     */
152    public static final String NAME = "data";
153
154    /**
155     * Constructor.
156     */
157    public DataParam() {
158      super(NAME, false);
159    }
160  }
161
162  /**
163   * Class for operation parameter.
164   */
165  @InterfaceAudience.Private
166  public static class OperationParam extends EnumParam<HttpFSFileSystem.Operation> {
167
168    /**
169     * Parameter name.
170     */
171    public static final String NAME = HttpFSFileSystem.OP_PARAM;
172    /**
173     * Constructor.
174     */
175    public OperationParam(String operation) {
176      super(NAME, HttpFSFileSystem.Operation.class,
177            HttpFSFileSystem.Operation.valueOf(
178                StringUtils.toUpperCase(operation)));
179    }
180  }
181
182  /**
183   * Class for delete's recursive parameter.
184   */
185  @InterfaceAudience.Private
186  public static class RecursiveParam extends BooleanParam {
187
188    /**
189     * Parameter name.
190     */
191    public static final String NAME = HttpFSFileSystem.RECURSIVE_PARAM;
192
193    /**
194     * Constructor.
195     */
196    public RecursiveParam() {
197      super(NAME, false);
198    }
199  }
200
201  /**
202   * Class for filter parameter.
203   */
204  @InterfaceAudience.Private
205  public static class FilterParam extends StringParam {
206
207    /**
208     * Parameter name.
209     */
210    public static final String NAME = "filter";
211
212    /**
213     * Constructor.
214     */
215    public FilterParam() {
216      super(NAME, null);
217    }
218
219  }
220
221  /**
222   * Class for group parameter.
223   */
224  @InterfaceAudience.Private
225  public static class GroupParam extends StringParam {
226
227    /**
228     * Parameter name.
229     */
230    public static final String NAME = HttpFSFileSystem.GROUP_PARAM;
231
232    /**
233     * Constructor.
234     */
235    public GroupParam() {
236      super(NAME, null);
237    }
238
239  }
240
241  /**
242   * Class for len parameter.
243   */
244  @InterfaceAudience.Private
245  public static class LenParam extends LongParam {
246
247    /**
248     * Parameter name.
249     */
250    public static final String NAME = "length";
251
252    /**
253     * Constructor.
254     */
255    public LenParam() {
256      super(NAME, -1l);
257    }
258  }
259
260  /**
261   * Class for modified-time parameter.
262   */
263  @InterfaceAudience.Private
264  public static class ModifiedTimeParam extends LongParam {
265
266    /**
267     * Parameter name.
268     */
269    public static final String NAME = HttpFSFileSystem.MODIFICATION_TIME_PARAM;
270
271    /**
272     * Constructor.
273     */
274    public ModifiedTimeParam() {
275      super(NAME, -1l);
276    }
277  }
278
279  /**
280   * Class for offset parameter.
281   */
282  @InterfaceAudience.Private
283  public static class OffsetParam extends LongParam {
284
285    /**
286     * Parameter name.
287     */
288    public static final String NAME = "offset";
289
290    /**
291     * Constructor.
292     */
293    public OffsetParam() {
294      super(NAME, 0l);
295    }
296  }
297
298  /**
299   * Class for newlength parameter.
300   */
301  @InterfaceAudience.Private
302  public static class NewLengthParam extends LongParam {
303
304    /**
305     * Parameter name.
306     */
307    public static final String NAME = HttpFSFileSystem.NEW_LENGTH_PARAM;
308
309    /**
310     * Constructor.
311     */
312    public NewLengthParam() {
313      super(NAME, 0l);
314    }
315  }
316
317  /**
318   * Class for overwrite parameter.
319   */
320  @InterfaceAudience.Private
321  public static class OverwriteParam extends BooleanParam {
322
323    /**
324     * Parameter name.
325     */
326    public static final String NAME = HttpFSFileSystem.OVERWRITE_PARAM;
327
328    /**
329     * Constructor.
330     */
331    public OverwriteParam() {
332      super(NAME, true);
333    }
334  }
335
336  /**
337   * Class for owner parameter.
338   */
339  @InterfaceAudience.Private
340  public static class OwnerParam extends StringParam {
341
342    /**
343     * Parameter name.
344     */
345    public static final String NAME = HttpFSFileSystem.OWNER_PARAM;
346
347    /**
348     * Constructor.
349     */
350    public OwnerParam() {
351      super(NAME, null);
352    }
353
354  }
355
356  /**
357   * Class for permission parameter.
358   */
359  @InterfaceAudience.Private
360  public static class PermissionParam extends ShortParam {
361
362    /**
363     * Parameter name.
364     */
365    public static final String NAME = HttpFSFileSystem.PERMISSION_PARAM;
366
367
368    /**
369     * Constructor.
370     */
371    public PermissionParam() {
372      super(NAME, HttpFSFileSystem.DEFAULT_PERMISSION, 8);
373    }
374
375  }
376
377  /**
378   * Class for AclPermission parameter.
379   */
380  @InterfaceAudience.Private
381  public static class AclPermissionParam extends StringParam {
382
383    /**
384     * Parameter name.
385     */
386    public static final String NAME = HttpFSFileSystem.ACLSPEC_PARAM;
387
388    /**
389     * Constructor.
390     */
391    public AclPermissionParam() {
392      super(NAME, HttpFSFileSystem.ACLSPEC_DEFAULT,
393              Pattern.compile(DFS_WEBHDFS_ACL_PERMISSION_PATTERN_DEFAULT));
394    }
395  }
396
397  /**
398   * Class for replication parameter.
399   */
400  @InterfaceAudience.Private
401  public static class ReplicationParam extends ShortParam {
402
403    /**
404     * Parameter name.
405     */
406    public static final String NAME = HttpFSFileSystem.REPLICATION_PARAM;
407
408    /**
409     * Constructor.
410     */
411    public ReplicationParam() {
412      super(NAME, (short) -1);
413    }
414  }
415
416  /**
417   * Class for concat sources parameter.
418   */
419  @InterfaceAudience.Private
420  public static class SourcesParam extends StringParam {
421
422    /**
423     * Parameter name.
424     */
425    public static final String NAME = HttpFSFileSystem.SOURCES_PARAM;
426
427    /**
428     * Constructor.
429     */
430    public SourcesParam() {
431      super(NAME, null);
432    }
433  }
434
435  /**
436   * Class for to-path parameter.
437   */
438  @InterfaceAudience.Private
439  public static class DestinationParam extends StringParam {
440
441    /**
442     * Parameter name.
443     */
444    public static final String NAME = HttpFSFileSystem.DESTINATION_PARAM;
445
446    /**
447     * Constructor.
448     */
449    public DestinationParam() {
450      super(NAME, null);
451    }
452  }
453  
454  /**
455   * Class for xattr parameter.
456   */
457  @InterfaceAudience.Private
458  public static class XAttrNameParam extends StringParam {
459    public static final String XATTR_NAME_REGX = 
460        "^(user\\.|trusted\\.|system\\.|security\\.).+";
461    /**
462     * Parameter name.
463     */
464    public static final String NAME = HttpFSFileSystem.XATTR_NAME_PARAM;
465    private static final Pattern pattern = Pattern.compile(XATTR_NAME_REGX);
466
467    /**
468     * Constructor.
469     */
470    public XAttrNameParam() {
471      super(NAME, null, pattern);
472    }
473  }
474
475  /**
476   * Class for xattr parameter.
477   */
478  @InterfaceAudience.Private
479  public static class XAttrValueParam extends StringParam {
480    /**
481     * Parameter name.
482     */
483    public static final String NAME = HttpFSFileSystem.XATTR_VALUE_PARAM;
484
485    /**
486     * Constructor.
487     */
488    public XAttrValueParam() {
489      super(NAME, null);
490    }
491  }
492
493  /**
494   * Class for xattr parameter.
495   */
496  @InterfaceAudience.Private
497  public static class XAttrSetFlagParam extends EnumSetParam<XAttrSetFlag> {
498    /**
499     * Parameter name.
500     */
501    public static final String NAME = HttpFSFileSystem.XATTR_SET_FLAG_PARAM;
502
503    /**
504     * Constructor.
505     */
506    public XAttrSetFlagParam() {
507      super(NAME, XAttrSetFlag.class, null);
508    }
509  }
510
511  /**
512   * Class for xattr parameter.
513   */
514  @InterfaceAudience.Private
515  public static class XAttrEncodingParam extends EnumParam<XAttrCodec> {
516    /**
517     * Parameter name.
518     */
519    public static final String NAME = HttpFSFileSystem.XATTR_ENCODING_PARAM;
520
521    /**
522     * Constructor.
523     */
524    public XAttrEncodingParam() {
525      super(NAME, XAttrCodec.class, null);
526    }
527  }
528
529  /**
530   * Class for policyName parameter.
531   */
532  @InterfaceAudience.Private
533  public static class PolicyNameParam extends StringParam {
534    /**
535     * Parameter name.
536     */
537    public static final String NAME = HttpFSFileSystem.POLICY_NAME_PARAM;
538
539    /**
540     * Constructor.
541     */
542    public PolicyNameParam() {
543      super(NAME, null);
544    }
545  }
546}