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