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.http.client.HttpFSFileSystem;
022    import org.apache.hadoop.fs.http.client.HttpFSFileSystem.Operation;
023    import org.apache.hadoop.lib.wsrs.BooleanParam;
024    import org.apache.hadoop.lib.wsrs.EnumParam;
025    import org.apache.hadoop.lib.wsrs.LongParam;
026    import org.apache.hadoop.lib.wsrs.Param;
027    import org.apache.hadoop.lib.wsrs.ParametersProvider;
028    import org.apache.hadoop.lib.wsrs.ShortParam;
029    import org.apache.hadoop.lib.wsrs.StringParam;
030    import org.apache.hadoop.lib.wsrs.UserProvider;
031    import org.slf4j.MDC;
032    
033    import javax.ws.rs.ext.Provider;
034    import java.util.HashMap;
035    import java.util.Map;
036    import java.util.regex.Pattern;
037    
038    /**
039     * HttpFS ParametersProvider.
040     */
041    @Provider
042    @InterfaceAudience.Private
043    public class HttpFSParametersProvider extends ParametersProvider {
044    
045      private static final Map<Enum, Class<Param<?>>[]> PARAMS_DEF =
046        new HashMap<Enum, Class<Param<?>>[]>();
047    
048      static {
049        PARAMS_DEF.put(Operation.OPEN,
050          new Class[]{DoAsParam.class, OffsetParam.class, LenParam.class});
051        PARAMS_DEF.put(Operation.GETFILESTATUS, new Class[]{DoAsParam.class});
052        PARAMS_DEF.put(Operation.LISTSTATUS,
053          new Class[]{DoAsParam.class, FilterParam.class});
054        PARAMS_DEF.put(Operation.GETHOMEDIRECTORY, new Class[]{DoAsParam.class});
055        PARAMS_DEF.put(Operation.GETCONTENTSUMMARY, new Class[]{DoAsParam.class});
056        PARAMS_DEF.put(Operation.GETFILECHECKSUM, new Class[]{DoAsParam.class});
057        PARAMS_DEF.put(Operation.GETFILEBLOCKLOCATIONS,
058          new Class[]{DoAsParam.class});
059        PARAMS_DEF.put(Operation.INSTRUMENTATION, new Class[]{DoAsParam.class});
060        PARAMS_DEF.put(Operation.APPEND,
061          new Class[]{DoAsParam.class, DataParam.class});
062        PARAMS_DEF.put(Operation.CONCAT, new Class[]{SourcesParam.class});
063        PARAMS_DEF.put(Operation.CREATE,
064          new Class[]{DoAsParam.class, PermissionParam.class, OverwriteParam.class,
065                      ReplicationParam.class, BlockSizeParam.class, DataParam.class});
066        PARAMS_DEF.put(Operation.MKDIRS,
067          new Class[]{DoAsParam.class, PermissionParam.class});
068        PARAMS_DEF.put(Operation.RENAME,
069          new Class[]{DoAsParam.class, DestinationParam.class});
070        PARAMS_DEF.put(Operation.SETOWNER,
071          new Class[]{DoAsParam.class, OwnerParam.class, GroupParam.class});
072        PARAMS_DEF.put(Operation.SETPERMISSION,
073          new Class[]{DoAsParam.class, PermissionParam.class});
074        PARAMS_DEF.put(Operation.SETREPLICATION,
075          new Class[]{DoAsParam.class, ReplicationParam.class});
076        PARAMS_DEF.put(Operation.SETTIMES,
077          new Class[]{DoAsParam.class, ModifiedTimeParam.class,
078                      AccessTimeParam.class});
079        PARAMS_DEF.put(Operation.DELETE,
080          new Class[]{DoAsParam.class, RecursiveParam.class});
081      }
082    
083      public HttpFSParametersProvider() {
084        super(HttpFSFileSystem.OP_PARAM, HttpFSFileSystem.Operation.class,
085              PARAMS_DEF);
086      }
087    
088      /**
089       * Class for access-time parameter.
090       */
091      @InterfaceAudience.Private
092      public static class AccessTimeParam extends LongParam {
093    
094        /**
095         * Parameter name.
096         */
097        public static final String NAME = HttpFSFileSystem.ACCESS_TIME_PARAM;
098        /**
099         * Constructor.
100         */
101        public AccessTimeParam() {
102          super(NAME, -1l);
103        }
104      }
105    
106      /**
107       * Class for block-size parameter.
108       */
109      @InterfaceAudience.Private
110      public static class BlockSizeParam extends LongParam {
111    
112        /**
113         * Parameter name.
114         */
115        public static final String NAME = HttpFSFileSystem.BLOCKSIZE_PARAM;
116    
117        /**
118         * Constructor.
119         */
120        public BlockSizeParam() {
121          super(NAME, -1l);
122        }
123      }
124    
125      /**
126       * Class for data parameter.
127       */
128      @InterfaceAudience.Private
129      public static class DataParam extends BooleanParam {
130    
131        /**
132         * Parameter name.
133         */
134        public static final String NAME = "data";
135    
136        /**
137         * Constructor.
138         */
139        public DataParam() {
140          super(NAME, false);
141        }
142      }
143    
144      /**
145       * Class for operation parameter.
146       */
147      @InterfaceAudience.Private
148      public static class OperationParam extends EnumParam<HttpFSFileSystem.Operation> {
149    
150        /**
151         * Parameter name.
152         */
153        public static final String NAME = HttpFSFileSystem.OP_PARAM;
154        /**
155         * Constructor.
156         */
157        public OperationParam(String operation) {
158          super(NAME, HttpFSFileSystem.Operation.class,
159                HttpFSFileSystem.Operation.valueOf(operation.toUpperCase()));
160        }
161      }
162    
163      /**
164       * Class for delete's recursive parameter.
165       */
166      @InterfaceAudience.Private
167      public static class RecursiveParam extends BooleanParam {
168    
169        /**
170         * Parameter name.
171         */
172        public static final String NAME = HttpFSFileSystem.RECURSIVE_PARAM;
173    
174        /**
175         * Constructor.
176         */
177        public RecursiveParam() {
178          super(NAME, false);
179        }
180      }
181    
182      /**
183       * Class for do-as parameter.
184       */
185      @InterfaceAudience.Private
186      public static class DoAsParam extends StringParam {
187    
188        /**
189         * Parameter name.
190         */
191        public static final String NAME = HttpFSFileSystem.DO_AS_PARAM;
192    
193        /**
194         * Constructor.
195         */
196        public DoAsParam() {
197          super(NAME, null, UserProvider.getUserPattern());
198        }
199    
200        /**
201         * Delegates to parent and then adds do-as user to
202         * MDC context for logging purposes.
203         *
204         *
205         * @param str parameter value.
206         *
207         * @return parsed parameter
208         */
209        @Override
210        public String parseParam(String str) {
211          String doAs = super.parseParam(str);
212          MDC.put(getName(), (doAs != null) ? doAs : "-");
213          return doAs;
214        }
215      }
216    
217      /**
218       * Class for filter parameter.
219       */
220      @InterfaceAudience.Private
221      public static class FilterParam extends StringParam {
222    
223        /**
224         * Parameter name.
225         */
226        public static final String NAME = "filter";
227    
228        /**
229         * Constructor.
230         */
231        public FilterParam() {
232          super(NAME, null);
233        }
234    
235      }
236    
237      /**
238       * Class for group parameter.
239       */
240      @InterfaceAudience.Private
241      public static class GroupParam extends StringParam {
242    
243        /**
244         * Parameter name.
245         */
246        public static final String NAME = HttpFSFileSystem.GROUP_PARAM;
247    
248        /**
249         * Constructor.
250         */
251        public GroupParam() {
252          super(NAME, null, UserProvider.getUserPattern());
253        }
254    
255      }
256    
257      /**
258       * Class for len parameter.
259       */
260      @InterfaceAudience.Private
261      public static class LenParam extends LongParam {
262    
263        /**
264         * Parameter name.
265         */
266        public static final String NAME = "length";
267    
268        /**
269         * Constructor.
270         */
271        public LenParam() {
272          super(NAME, -1l);
273        }
274      }
275    
276      /**
277       * Class for modified-time parameter.
278       */
279      @InterfaceAudience.Private
280      public static class ModifiedTimeParam extends LongParam {
281    
282        /**
283         * Parameter name.
284         */
285        public static final String NAME = HttpFSFileSystem.MODIFICATION_TIME_PARAM;
286    
287        /**
288         * Constructor.
289         */
290        public ModifiedTimeParam() {
291          super(NAME, -1l);
292        }
293      }
294    
295      /**
296       * Class for offset parameter.
297       */
298      @InterfaceAudience.Private
299      public static class OffsetParam extends LongParam {
300    
301        /**
302         * Parameter name.
303         */
304        public static final String NAME = "offset";
305    
306        /**
307         * Constructor.
308         */
309        public OffsetParam() {
310          super(NAME, 0l);
311        }
312      }
313    
314      /**
315       * Class for overwrite parameter.
316       */
317      @InterfaceAudience.Private
318      public static class OverwriteParam extends BooleanParam {
319    
320        /**
321         * Parameter name.
322         */
323        public static final String NAME = HttpFSFileSystem.OVERWRITE_PARAM;
324    
325        /**
326         * Constructor.
327         */
328        public OverwriteParam() {
329          super(NAME, true);
330        }
331      }
332    
333      /**
334       * Class for owner parameter.
335       */
336      @InterfaceAudience.Private
337      public static class OwnerParam extends StringParam {
338    
339        /**
340         * Parameter name.
341         */
342        public static final String NAME = HttpFSFileSystem.OWNER_PARAM;
343    
344        /**
345         * Constructor.
346         */
347        public OwnerParam() {
348          super(NAME, null, UserProvider.getUserPattern());
349        }
350    
351      }
352    
353      /**
354       * Class for permission parameter.
355       */
356      @InterfaceAudience.Private
357      public static class PermissionParam extends ShortParam {
358    
359        /**
360         * Parameter name.
361         */
362        public static final String NAME = HttpFSFileSystem.PERMISSION_PARAM;
363    
364    
365        /**
366         * Constructor.
367         */
368        public PermissionParam() {
369          super(NAME, HttpFSFileSystem.DEFAULT_PERMISSION, 8);
370        }
371    
372      }
373    
374      /**
375       * Class for replication parameter.
376       */
377      @InterfaceAudience.Private
378      public static class ReplicationParam extends ShortParam {
379    
380        /**
381         * Parameter name.
382         */
383        public static final String NAME = HttpFSFileSystem.REPLICATION_PARAM;
384    
385        /**
386         * Constructor.
387         */
388        public ReplicationParam() {
389          super(NAME, (short) -1);
390        }
391      }
392    
393      /**
394       * Class for concat sources parameter.
395       */
396      @InterfaceAudience.Private
397      public static class SourcesParam extends StringParam {
398    
399        /**
400         * Parameter name.
401         */
402        public static final String NAME = HttpFSFileSystem.SOURCES_PARAM;
403    
404        /**
405         * Constructor.
406         */
407        public SourcesParam() {
408          super(NAME, null);
409        }
410      }
411    
412      /**
413       * Class for to-path parameter.
414       */
415      @InterfaceAudience.Private
416      public static class DestinationParam extends StringParam {
417    
418        /**
419         * Parameter name.
420         */
421        public static final String NAME = HttpFSFileSystem.DESTINATION_PARAM;
422    
423        /**
424         * Constructor.
425         */
426        public DestinationParam() {
427          super(NAME, null);
428        }
429      }
430    }