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