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    
037    /**
038     * HttpFS ParametersProvider.
039     */
040    @Provider
041    @InterfaceAudience.Private
042    public class HttpFSParametersProvider extends ParametersProvider {
043    
044      private static final Map<Enum, Class<Param<?>>[]> PARAMS_DEF =
045        new HashMap<Enum, Class<Param<?>>[]>();
046    
047      static {
048        PARAMS_DEF.put(Operation.OPEN,
049          new Class[]{DoAsParam.class, OffsetParam.class, LenParam.class});
050        PARAMS_DEF.put(Operation.GETFILESTATUS, new Class[]{DoAsParam.class});
051        PARAMS_DEF.put(Operation.LISTSTATUS,
052          new Class[]{DoAsParam.class, FilterParam.class});
053        PARAMS_DEF.put(Operation.GETHOMEDIRECTORY, new Class[]{DoAsParam.class});
054        PARAMS_DEF.put(Operation.GETCONTENTSUMMARY, new Class[]{DoAsParam.class});
055        PARAMS_DEF.put(Operation.GETFILECHECKSUM, new Class[]{DoAsParam.class});
056        PARAMS_DEF.put(Operation.GETFILEBLOCKLOCATIONS,
057          new Class[]{DoAsParam.class});
058        PARAMS_DEF.put(Operation.INSTRUMENTATION, new Class[]{DoAsParam.class});
059        PARAMS_DEF.put(Operation.APPEND,
060          new Class[]{DoAsParam.class, DataParam.class});
061        PARAMS_DEF.put(Operation.CREATE,
062          new Class[]{DoAsParam.class, PermissionParam.class, OverwriteParam.class,
063                      ReplicationParam.class, BlockSizeParam.class, DataParam.class});
064        PARAMS_DEF.put(Operation.MKDIRS,
065          new Class[]{DoAsParam.class, PermissionParam.class});
066        PARAMS_DEF.put(Operation.RENAME,
067          new Class[]{DoAsParam.class, DestinationParam.class});
068        PARAMS_DEF.put(Operation.SETOWNER,
069          new Class[]{DoAsParam.class, OwnerParam.class, GroupParam.class});
070        PARAMS_DEF.put(Operation.SETPERMISSION,
071          new Class[]{DoAsParam.class, PermissionParam.class});
072        PARAMS_DEF.put(Operation.SETREPLICATION,
073          new Class[]{DoAsParam.class, ReplicationParam.class});
074        PARAMS_DEF.put(Operation.SETTIMES,
075          new Class[]{DoAsParam.class, ModifiedTimeParam.class,
076                      AccessTimeParam.class});
077        PARAMS_DEF.put(Operation.DELETE,
078          new Class[]{DoAsParam.class, RecursiveParam.class});
079      }
080    
081      public HttpFSParametersProvider() {
082        super(HttpFSFileSystem.OP_PARAM, HttpFSFileSystem.Operation.class,
083              PARAMS_DEF);
084      }
085    
086      /**
087       * Class for access-time parameter.
088       */
089      @InterfaceAudience.Private
090      public static class AccessTimeParam extends LongParam {
091    
092        /**
093         * Parameter name.
094         */
095        public static final String NAME = HttpFSFileSystem.ACCESS_TIME_PARAM;
096        /**
097         * Constructor.
098         */
099        public AccessTimeParam() {
100          super(NAME, -1l);
101        }
102      }
103    
104      /**
105       * Class for block-size parameter.
106       */
107      @InterfaceAudience.Private
108      public static class BlockSizeParam extends LongParam {
109    
110        /**
111         * Parameter name.
112         */
113        public static final String NAME = HttpFSFileSystem.BLOCKSIZE_PARAM;
114    
115        /**
116         * Constructor.
117         */
118        public BlockSizeParam() {
119          super(NAME, -1l);
120        }
121      }
122    
123      /**
124       * Class for data parameter.
125       */
126      @InterfaceAudience.Private
127      public static class DataParam extends BooleanParam {
128    
129        /**
130         * Parameter name.
131         */
132        public static final String NAME = "data";
133    
134        /**
135         * Constructor.
136         */
137        public DataParam() {
138          super(NAME, false);
139        }
140      }
141    
142      /**
143       * Class for operation parameter.
144       */
145      @InterfaceAudience.Private
146      public static class OperationParam extends EnumParam<HttpFSFileSystem.Operation> {
147    
148        /**
149         * Parameter name.
150         */
151        public static final String NAME = HttpFSFileSystem.OP_PARAM;
152        /**
153         * Constructor.
154         */
155        public OperationParam(String operation) {
156          super(NAME, HttpFSFileSystem.Operation.class,
157                HttpFSFileSystem.Operation.valueOf(operation.toUpperCase()));
158        }
159      }
160    
161      /**
162       * Class for delete's recursive parameter.
163       */
164      @InterfaceAudience.Private
165      public static class RecursiveParam extends BooleanParam {
166    
167        /**
168         * Parameter name.
169         */
170        public static final String NAME = HttpFSFileSystem.RECURSIVE_PARAM;
171    
172        /**
173         * Constructor.
174         */
175        public RecursiveParam() {
176          super(NAME, false);
177        }
178      }
179    
180      /**
181       * Class for do-as parameter.
182       */
183      @InterfaceAudience.Private
184      public static class DoAsParam extends StringParam {
185    
186        /**
187         * Parameter name.
188         */
189        public static final String NAME = HttpFSFileSystem.DO_AS_PARAM;
190    
191        /**
192         * Constructor.
193         */
194        public DoAsParam() {
195          super(NAME, null, UserProvider.USER_PATTERN);
196        }
197    
198        /**
199         * Delegates to parent and then adds do-as user to
200         * MDC context for logging purposes.
201         *
202         *
203         * @param str parameter value.
204         *
205         * @return parsed parameter
206         */
207        @Override
208        public String parseParam(String str) {
209          String doAs = super.parseParam(str);
210          MDC.put(getName(), (doAs != null) ? doAs : "-");
211          return doAs;
212        }
213      }
214    
215      /**
216       * Class for filter parameter.
217       */
218      @InterfaceAudience.Private
219      public static class FilterParam extends StringParam {
220    
221        /**
222         * Parameter name.
223         */
224        public static final String NAME = "filter";
225    
226        /**
227         * Constructor.
228         */
229        public FilterParam() {
230          super(NAME, null);
231        }
232    
233      }
234    
235      /**
236       * Class for group parameter.
237       */
238      @InterfaceAudience.Private
239      public static class GroupParam extends StringParam {
240    
241        /**
242         * Parameter name.
243         */
244        public static final String NAME = HttpFSFileSystem.GROUP_PARAM;
245    
246        /**
247         * Constructor.
248         */
249        public GroupParam() {
250          super(NAME, null, UserProvider.USER_PATTERN);
251        }
252    
253      }
254    
255      /**
256       * Class for len parameter.
257       */
258      @InterfaceAudience.Private
259      public static class LenParam extends LongParam {
260    
261        /**
262         * Parameter name.
263         */
264        public static final String NAME = "len";
265    
266        /**
267         * Constructor.
268         */
269        public LenParam() {
270          super(NAME, -1l);
271        }
272      }
273    
274      /**
275       * Class for modified-time parameter.
276       */
277      @InterfaceAudience.Private
278      public static class ModifiedTimeParam extends LongParam {
279    
280        /**
281         * Parameter name.
282         */
283        public static final String NAME = HttpFSFileSystem.MODIFICATION_TIME_PARAM;
284    
285        /**
286         * Constructor.
287         */
288        public ModifiedTimeParam() {
289          super(NAME, -1l);
290        }
291      }
292    
293      /**
294       * Class for offset parameter.
295       */
296      @InterfaceAudience.Private
297      public static class OffsetParam extends LongParam {
298    
299        /**
300         * Parameter name.
301         */
302        public static final String NAME = "offset";
303    
304        /**
305         * Constructor.
306         */
307        public OffsetParam() {
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, UserProvider.USER_PATTERN);
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 replication parameter.
374       */
375      @InterfaceAudience.Private
376      public static class ReplicationParam extends ShortParam {
377    
378        /**
379         * Parameter name.
380         */
381        public static final String NAME = HttpFSFileSystem.REPLICATION_PARAM;
382    
383        /**
384         * Constructor.
385         */
386        public ReplicationParam() {
387          super(NAME, (short) -1);
388        }
389      }
390    
391      /**
392       * Class for to-path parameter.
393       */
394      @InterfaceAudience.Private
395      public static class DestinationParam extends StringParam {
396    
397        /**
398         * Parameter name.
399         */
400        public static final String NAME = HttpFSFileSystem.DESTINATION_PARAM;
401    
402        /**
403         * Constructor.
404         */
405        public DestinationParam() {
406          super(NAME, null);
407        }
408      }
409    }