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.BlockStoragePolicySpi;
022import org.apache.hadoop.fs.ContentSummary;
023import org.apache.hadoop.fs.FileChecksum;
024import org.apache.hadoop.fs.FileStatus;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.GlobFilter;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.fs.PathFilter;
029import org.apache.hadoop.fs.StorageType;
030import org.apache.hadoop.fs.XAttrCodec;
031import org.apache.hadoop.fs.XAttrSetFlag;
032import org.apache.hadoop.fs.http.client.HttpFSFileSystem;
033import org.apache.hadoop.fs.permission.AclEntry;
034import org.apache.hadoop.fs.permission.AclStatus;
035import org.apache.hadoop.fs.permission.FsPermission;
036import org.apache.hadoop.hdfs.protocol.BlockStoragePolicy;
037import org.apache.hadoop.io.IOUtils;
038import org.apache.hadoop.lib.service.FileSystemAccess;
039import org.apache.hadoop.util.StringUtils;
040import org.json.simple.JSONArray;
041import org.json.simple.JSONObject;
042
043import java.io.IOException;
044import java.io.InputStream;
045import java.io.OutputStream;
046import java.util.Collection;
047import java.util.EnumSet;
048import java.util.LinkedHashMap;
049import java.util.List;
050import java.util.Map;
051import java.util.Map.Entry;
052
053import static org.apache.hadoop.hdfs.DFSConfigKeys.HTTPFS_BUFFER_SIZE_KEY;
054import static org.apache.hadoop.hdfs.DFSConfigKeys.HTTP_BUFFER_SIZE_DEFAULT;
055
056/**
057 * FileSystem operation executors used by {@link HttpFSServer}.
058 */
059@InterfaceAudience.Private
060public class FSOperations {
061
062  /**
063   * @param fileStatus a FileStatus object
064   * @return JSON map suitable for wire transport
065   */
066  private static Map<String, Object> toJson(FileStatus fileStatus) {
067    Map<String, Object> json = new LinkedHashMap<>();
068    json.put(HttpFSFileSystem.FILE_STATUS_JSON, toJsonInner(fileStatus, true));
069    return json;
070  }
071
072  /**
073   * @param fileStatuses list of FileStatus objects
074   * @return JSON map suitable for wire transport
075   */
076  @SuppressWarnings({"unchecked"})
077  private static Map<String, Object> toJson(FileStatus[] fileStatuses) {
078    Map<String, Object> json = new LinkedHashMap<>();
079    Map<String, Object> inner = new LinkedHashMap<>();
080    JSONArray statuses = new JSONArray();
081    for (FileStatus f : fileStatuses) {
082      statuses.add(toJsonInner(f, false));
083    }
084    inner.put(HttpFSFileSystem.FILE_STATUS_JSON, statuses);
085    json.put(HttpFSFileSystem.FILE_STATUSES_JSON, inner);
086    return json;
087  }
088
089  /**
090   * Not meant to be called directly except by the other toJson functions.
091   */
092  private static Map<String, Object> toJsonInner(FileStatus fileStatus,
093      boolean emptyPathSuffix) {
094    Map<String, Object> json = new LinkedHashMap<String, Object>();
095    json.put(HttpFSFileSystem.PATH_SUFFIX_JSON,
096        (emptyPathSuffix) ? "" : fileStatus.getPath().getName());
097    json.put(HttpFSFileSystem.TYPE_JSON,
098        HttpFSFileSystem.FILE_TYPE.getType(fileStatus).toString());
099    json.put(HttpFSFileSystem.LENGTH_JSON, fileStatus.getLen());
100    json.put(HttpFSFileSystem.OWNER_JSON, fileStatus.getOwner());
101    json.put(HttpFSFileSystem.GROUP_JSON, fileStatus.getGroup());
102    json.put(HttpFSFileSystem.PERMISSION_JSON,
103        HttpFSFileSystem.permissionToString(fileStatus.getPermission()));
104    json.put(HttpFSFileSystem.ACCESS_TIME_JSON, fileStatus.getAccessTime());
105    json.put(HttpFSFileSystem.MODIFICATION_TIME_JSON,
106        fileStatus.getModificationTime());
107    json.put(HttpFSFileSystem.BLOCK_SIZE_JSON, fileStatus.getBlockSize());
108    json.put(HttpFSFileSystem.REPLICATION_JSON, fileStatus.getReplication());
109    if (fileStatus.getPermission().getAclBit()) {
110      json.put(HttpFSFileSystem.ACL_BIT_JSON, true);
111    }
112    if (fileStatus.getPermission().getEncryptedBit()) {
113      json.put(HttpFSFileSystem.ENC_BIT_JSON, true);
114    }
115    return json;
116  }
117
118  /** Converts an <code>AclStatus</code> object into a JSON object.
119   *
120   * @param aclStatus AclStatus object
121   *
122   * @return The JSON representation of the ACLs for the file
123   */
124  @SuppressWarnings({"unchecked"})
125  private static Map<String,Object> aclStatusToJSON(AclStatus aclStatus) {
126    Map<String,Object> json = new LinkedHashMap<String,Object>();
127    Map<String,Object> inner = new LinkedHashMap<String,Object>();
128    JSONArray entriesArray = new JSONArray();
129    inner.put(HttpFSFileSystem.OWNER_JSON, aclStatus.getOwner());
130    inner.put(HttpFSFileSystem.GROUP_JSON, aclStatus.getGroup());
131    inner.put(HttpFSFileSystem.ACL_STICKY_BIT_JSON, aclStatus.isStickyBit());
132    for ( AclEntry e : aclStatus.getEntries() ) {
133      entriesArray.add(e.toString());
134    }
135    inner.put(HttpFSFileSystem.ACL_ENTRIES_JSON, entriesArray);
136    json.put(HttpFSFileSystem.ACL_STATUS_JSON, inner);
137    return json;
138  }
139
140  /**
141   * Converts a <code>FileChecksum</code> object into a JSON array
142   * object.
143   *
144   * @param checksum file checksum.
145   *
146   * @return The JSON representation of the file checksum.
147   */
148  @SuppressWarnings({"unchecked"})
149  private static Map fileChecksumToJSON(FileChecksum checksum) {
150    Map json = new LinkedHashMap();
151    json.put(HttpFSFileSystem.CHECKSUM_ALGORITHM_JSON, checksum.getAlgorithmName());
152    json.put(HttpFSFileSystem.CHECKSUM_BYTES_JSON,
153             org.apache.hadoop.util.StringUtils.byteToHexString(checksum.getBytes()));
154    json.put(HttpFSFileSystem.CHECKSUM_LENGTH_JSON, checksum.getLength());
155    Map response = new LinkedHashMap();
156    response.put(HttpFSFileSystem.FILE_CHECKSUM_JSON, json);
157    return response;
158  }
159
160  /**
161   * Converts xAttrs to a JSON object.
162   *
163   * @param xAttrs file xAttrs.
164   * @param encoding format of xattr values.
165   *
166   * @return The JSON representation of the xAttrs.
167   * @throws IOException 
168   */
169  @SuppressWarnings({"unchecked", "rawtypes"})
170  private static Map xAttrsToJSON(Map<String, byte[]> xAttrs, 
171      XAttrCodec encoding) throws IOException {
172    Map jsonMap = new LinkedHashMap();
173    JSONArray jsonArray = new JSONArray();
174    if (xAttrs != null) {
175      for (Entry<String, byte[]> e : xAttrs.entrySet()) {
176        Map json = new LinkedHashMap();
177        json.put(HttpFSFileSystem.XATTR_NAME_JSON, e.getKey());
178        if (e.getValue() != null) {
179          json.put(HttpFSFileSystem.XATTR_VALUE_JSON, 
180              XAttrCodec.encodeValue(e.getValue(), encoding));
181        }
182        jsonArray.add(json);
183      }
184    }
185    jsonMap.put(HttpFSFileSystem.XATTRS_JSON, jsonArray);
186    return jsonMap;
187  }
188
189  /**
190   * Converts xAttr names to a JSON object.
191   *
192   * @param names file xAttr names.
193   *
194   * @return The JSON representation of the xAttr names.
195   * @throws IOException 
196   */
197  @SuppressWarnings({"unchecked", "rawtypes"})
198  private static Map xAttrNamesToJSON(List<String> names) throws IOException {
199    Map jsonMap = new LinkedHashMap();
200    jsonMap.put(HttpFSFileSystem.XATTRNAMES_JSON, JSONArray.toJSONString(names));
201    return jsonMap;
202  }
203
204  /**
205   * Converts a <code>ContentSummary</code> object into a JSON array
206   * object.
207   *
208   * @param contentSummary the content summary
209   *
210   * @return The JSON representation of the content summary.
211   */
212  @SuppressWarnings({"unchecked"})
213  private static Map contentSummaryToJSON(ContentSummary contentSummary) {
214    Map json = new LinkedHashMap();
215    json.put(HttpFSFileSystem.CONTENT_SUMMARY_DIRECTORY_COUNT_JSON, contentSummary.getDirectoryCount());
216    json.put(HttpFSFileSystem.CONTENT_SUMMARY_FILE_COUNT_JSON, contentSummary.getFileCount());
217    json.put(HttpFSFileSystem.CONTENT_SUMMARY_LENGTH_JSON, contentSummary.getLength());
218    json.put(HttpFSFileSystem.CONTENT_SUMMARY_QUOTA_JSON, contentSummary.getQuota());
219    json.put(HttpFSFileSystem.CONTENT_SUMMARY_SPACE_CONSUMED_JSON, contentSummary.getSpaceConsumed());
220    json.put(HttpFSFileSystem.CONTENT_SUMMARY_SPACE_QUOTA_JSON, contentSummary.getSpaceQuota());
221    Map response = new LinkedHashMap();
222    response.put(HttpFSFileSystem.CONTENT_SUMMARY_JSON, json);
223    return response;
224  }
225
226  /**
227   * Converts an object into a Json Map with with one key-value entry.
228   * <p/>
229   * It assumes the given value is either a JSON primitive type or a
230   * <code>JsonAware</code> instance.
231   *
232   * @param name name for the key of the entry.
233   * @param value for the value of the entry.
234   *
235   * @return the JSON representation of the key-value pair.
236   */
237  @SuppressWarnings("unchecked")
238  private static JSONObject toJSON(String name, Object value) {
239    JSONObject json = new JSONObject();
240    json.put(name, value);
241    return json;
242  }
243
244  @SuppressWarnings({ "unchecked" })
245  private static JSONObject storagePolicyToJSON(BlockStoragePolicySpi policy) {
246    BlockStoragePolicy p = (BlockStoragePolicy) policy;
247    JSONObject policyJson = new JSONObject();
248    policyJson.put("id", p.getId());
249    policyJson.put("name", p.getName());
250    policyJson.put("storageTypes", toJsonArray(p.getStorageTypes()));
251    policyJson.put("creationFallbacks", toJsonArray(p.getCreationFallbacks()));
252    policyJson.put("replicationFallbacks",
253        toJsonArray(p.getReplicationFallbacks()));
254    policyJson.put("copyOnCreateFile", p.isCopyOnCreateFile());
255    return policyJson;
256  }
257
258  @SuppressWarnings("unchecked")
259  private static JSONArray toJsonArray(StorageType[] storageTypes) {
260    JSONArray jsonArray = new JSONArray();
261    for (StorageType type : storageTypes) {
262      jsonArray.add(type.toString());
263    }
264    return jsonArray;
265  }
266
267  @SuppressWarnings("unchecked")
268  private static JSONObject storagePoliciesToJSON(
269      Collection<? extends BlockStoragePolicySpi> storagePolicies) {
270    JSONObject json = new JSONObject();
271    JSONArray jsonArray = new JSONArray();
272    JSONObject policies = new JSONObject();
273    if (storagePolicies != null) {
274      for (BlockStoragePolicySpi policy : storagePolicies) {
275        JSONObject policyMap = storagePolicyToJSON(policy);
276        jsonArray.add(policyMap);
277      }
278    }
279    policies.put(HttpFSFileSystem.STORAGE_POLICY_JSON, jsonArray);
280    json.put(HttpFSFileSystem.STORAGE_POLICIES_JSON, policies);
281    return json;
282  }
283
284  /**
285   * Executor that performs an append FileSystemAccess files system operation.
286   */
287  @InterfaceAudience.Private
288  public static class FSAppend implements FileSystemAccess.FileSystemExecutor<Void> {
289    private InputStream is;
290    private Path path;
291
292    /**
293     * Creates an Append executor.
294     *
295     * @param is input stream to append.
296     * @param path path of the file to append.
297     */
298    public FSAppend(InputStream is, String path) {
299      this.is = is;
300      this.path = new Path(path);
301    }
302
303    /**
304     * Executes the filesystem operation.
305     *
306     * @param fs filesystem instance to use.
307     *
308     * @return void.
309     *
310     * @throws IOException thrown if an IO error occured.
311     */
312    @Override
313    public Void execute(FileSystem fs) throws IOException {
314      int bufferSize = fs.getConf().getInt("httpfs.buffer.size", 4096);
315      OutputStream os = fs.append(path, bufferSize);
316      IOUtils.copyBytes(is, os, bufferSize, true);
317      os.close();
318      return null;
319    }
320
321  }
322
323  /**
324   * Executor that performs a concat FileSystemAccess files system operation.
325   */
326  @InterfaceAudience.Private
327  public static class FSConcat implements FileSystemAccess.FileSystemExecutor<Void> {
328    private Path path;
329    private Path[] sources;
330
331    /**
332     * Creates a Concat executor.
333     *
334     * @param path target path to concat to.
335     * @param sources comma seperated absolute paths to use as sources.
336     */
337    public FSConcat(String path, String[] sources) {
338      this.sources = new Path[sources.length];
339
340      for(int i = 0; i < sources.length; i++) {
341        this.sources[i] = new Path(sources[i]);
342      }
343
344      this.path = new Path(path);
345    }
346
347    /**
348     * Executes the filesystem operation.
349     *
350     * @param fs filesystem instance to use.
351     *
352     * @return void.
353     *
354     * @throws IOException thrown if an IO error occured.
355     */
356    @Override
357    public Void execute(FileSystem fs) throws IOException {
358      fs.concat(path, sources);
359      return null;
360    }
361
362  }
363
364  /**
365   * Executor that performs a truncate FileSystemAccess files system operation.
366   */
367  @InterfaceAudience.Private
368  public static class FSTruncate implements 
369      FileSystemAccess.FileSystemExecutor<JSONObject> {
370    private Path path;
371    private long newLength;
372
373    /**
374     * Creates a Truncate executor.
375     *
376     * @param path target path to truncate to.
377     * @param newLength The size the file is to be truncated to.
378     */
379    public FSTruncate(String path, long newLength) {
380      this.path = new Path(path);
381      this.newLength = newLength;
382    }
383
384    /**
385     * Executes the filesystem operation.
386     *
387     * @param fs filesystem instance to use.
388     *
389     * @return <code>true</code> if the file has been truncated to the desired,
390     *         <code>false</code> if a background process of adjusting the 
391     *         length of the last block has been started, and clients should 
392     *         wait for it to complete before proceeding with further file 
393     *         updates.
394     *
395     * @throws IOException thrown if an IO error occured.
396     */
397    @Override
398    public JSONObject execute(FileSystem fs) throws IOException {
399      boolean result = fs.truncate(path, newLength);
400      return toJSON(
401          StringUtils.toLowerCase(HttpFSFileSystem.TRUNCATE_JSON), result);
402    }
403
404  }
405
406  /**
407   * Executor that performs a content-summary FileSystemAccess files system operation.
408   */
409  @InterfaceAudience.Private
410  public static class FSContentSummary implements FileSystemAccess.FileSystemExecutor<Map> {
411    private Path path;
412
413    /**
414     * Creates a content-summary executor.
415     *
416     * @param path the path to retrieve the content-summary.
417     */
418    public FSContentSummary(String path) {
419      this.path = new Path(path);
420    }
421
422    /**
423     * Executes the filesystem operation.
424     *
425     * @param fs filesystem instance to use.
426     *
427     * @return a Map object (JSON friendly) with the content-summary.
428     *
429     * @throws IOException thrown if an IO error occured.
430     */
431    @Override
432    public Map execute(FileSystem fs) throws IOException {
433      ContentSummary contentSummary = fs.getContentSummary(path);
434      return contentSummaryToJSON(contentSummary);
435    }
436
437  }
438
439  /**
440   * Executor that performs a create FileSystemAccess files system operation.
441   */
442  @InterfaceAudience.Private
443  public static class FSCreate implements FileSystemAccess.FileSystemExecutor<Void> {
444    private InputStream is;
445    private Path path;
446    private short permission;
447    private boolean override;
448    private short replication;
449    private long blockSize;
450
451    /**
452     * Creates a Create executor.
453     *
454     * @param is input stream to for the file to create.
455     * @param path path of the file to create.
456     * @param perm permission for the file.
457     * @param override if the file should be overriden if it already exist.
458     * @param repl the replication factor for the file.
459     * @param blockSize the block size for the file.
460     */
461    public FSCreate(InputStream is, String path, short perm, boolean override,
462                    short repl, long blockSize) {
463      this.is = is;
464      this.path = new Path(path);
465      this.permission = perm;
466      this.override = override;
467      this.replication = repl;
468      this.blockSize = blockSize;
469    }
470
471    /**
472     * Executes the filesystem operation.
473     *
474     * @param fs filesystem instance to use.
475     *
476     * @return The URI of the created file.
477     *
478     * @throws IOException thrown if an IO error occured.
479     */
480    @Override
481    public Void execute(FileSystem fs) throws IOException {
482      if (replication == -1) {
483        replication = fs.getDefaultReplication(path);
484      }
485      if (blockSize == -1) {
486        blockSize = fs.getDefaultBlockSize(path);
487      }
488      FsPermission fsPermission = new FsPermission(permission);
489      int bufferSize = fs.getConf().getInt(HTTPFS_BUFFER_SIZE_KEY,
490          HTTP_BUFFER_SIZE_DEFAULT);
491      OutputStream os = fs.create(path, fsPermission, override, bufferSize, replication, blockSize, null);
492      IOUtils.copyBytes(is, os, bufferSize, true);
493      os.close();
494      return null;
495    }
496
497  }
498
499  /**
500   * Executor that performs a delete FileSystemAccess files system operation.
501   */
502  @InterfaceAudience.Private
503  public static class FSDelete implements FileSystemAccess.FileSystemExecutor<JSONObject> {
504    private Path path;
505    private boolean recursive;
506
507    /**
508     * Creates a Delete executor.
509     *
510     * @param path path to delete.
511     * @param recursive if the delete should be recursive or not.
512     */
513    public FSDelete(String path, boolean recursive) {
514      this.path = new Path(path);
515      this.recursive = recursive;
516    }
517
518    /**
519     * Executes the filesystem operation.
520     *
521     * @param fs filesystem instance to use.
522     *
523     * @return <code>true</code> if the delete operation was successful,
524     *         <code>false</code> otherwise.
525     *
526     * @throws IOException thrown if an IO error occured.
527     */
528    @Override
529    public JSONObject execute(FileSystem fs) throws IOException {
530      boolean deleted = fs.delete(path, recursive);
531      return toJSON(
532          StringUtils.toLowerCase(HttpFSFileSystem.DELETE_JSON), deleted);
533    }
534
535  }
536
537  /**
538   * Executor that performs a file-checksum FileSystemAccess files system operation.
539   */
540  @InterfaceAudience.Private
541  public static class FSFileChecksum implements FileSystemAccess.FileSystemExecutor<Map> {
542    private Path path;
543
544    /**
545     * Creates a file-checksum executor.
546     *
547     * @param path the path to retrieve the checksum.
548     */
549    public FSFileChecksum(String path) {
550      this.path = new Path(path);
551    }
552
553    /**
554     * Executes the filesystem operation.
555     *
556     * @param fs filesystem instance to use.
557     *
558     * @return a Map object (JSON friendly) with the file checksum.
559     *
560     * @throws IOException thrown if an IO error occured.
561     */
562    @Override
563    public Map execute(FileSystem fs) throws IOException {
564      FileChecksum checksum = fs.getFileChecksum(path);
565      return fileChecksumToJSON(checksum);
566    }
567
568  }
569
570  /**
571   * Executor that performs a file-status FileSystemAccess files system operation.
572   */
573  @InterfaceAudience.Private
574  public static class FSFileStatus implements FileSystemAccess.FileSystemExecutor<Map> {
575    private Path path;
576
577    /**
578     * Creates a file-status executor.
579     *
580     * @param path the path to retrieve the status.
581     */
582    public FSFileStatus(String path) {
583      this.path = new Path(path);
584    }
585
586    /**
587     * Executes the filesystem getFileStatus operation and returns the
588     * result in a JSONish Map.
589     *
590     * @param fs filesystem instance to use.
591     *
592     * @return a Map object (JSON friendly) with the file status.
593     *
594     * @throws IOException thrown if an IO error occurred.
595     */
596    @Override
597    public Map execute(FileSystem fs) throws IOException {
598      FileStatus status = fs.getFileStatus(path);
599      return toJson(status);
600    }
601
602  }
603
604  /**
605   * Executor that performs a home-dir FileSystemAccess files system operation.
606   */
607  @InterfaceAudience.Private
608  public static class FSHomeDir implements FileSystemAccess.FileSystemExecutor<JSONObject> {
609
610    /**
611     * Executes the filesystem operation.
612     *
613     * @param fs filesystem instance to use.
614     *
615     * @return a JSON object with the user home directory.
616     *
617     * @throws IOException thrown if an IO error occured.
618     */
619    @Override
620    @SuppressWarnings("unchecked")
621    public JSONObject execute(FileSystem fs) throws IOException {
622      Path homeDir = fs.getHomeDirectory();
623      JSONObject json = new JSONObject();
624      json.put(HttpFSFileSystem.HOME_DIR_JSON, homeDir.toUri().getPath());
625      return json;
626    }
627
628  }
629
630  /**
631   * Executor that performs a list-status FileSystemAccess files system operation.
632   */
633  @InterfaceAudience.Private
634  public static class FSListStatus implements FileSystemAccess.FileSystemExecutor<Map>, PathFilter {
635    private Path path;
636    private PathFilter filter;
637
638    /**
639     * Creates a list-status executor.
640     *
641     * @param path the directory to retrieve the status of its contents.
642     * @param filter glob filter to use.
643     *
644     * @throws IOException thrown if the filter expression is incorrect.
645     */
646    public FSListStatus(String path, String filter) throws IOException {
647      this.path = new Path(path);
648      this.filter = (filter == null) ? this : new GlobFilter(filter);
649    }
650
651    /**
652     * Returns data for a JSON Map containing the information for
653     * the set of files in 'path' that match 'filter'.
654     *
655     * @param fs filesystem instance to use.
656     *
657     * @return a Map with the file status of the directory
658     *         contents that match the filter
659     *
660     * @throws IOException thrown if an IO error occurred.
661     */
662    @Override
663    public Map execute(FileSystem fs) throws IOException {
664      FileStatus[] fileStatuses = fs.listStatus(path, filter);
665      return toJson(fileStatuses);
666    }
667
668    @Override
669    public boolean accept(Path path) {
670      return true;
671    }
672
673  }
674
675  /**
676   * Executor that performs a mkdirs FileSystemAccess files system operation.
677   */
678  @InterfaceAudience.Private
679  public static class FSMkdirs implements FileSystemAccess.FileSystemExecutor<JSONObject> {
680
681    private Path path;
682    private short permission;
683
684    /**
685     * Creates a mkdirs executor.
686     *
687     * @param path directory path to create.
688     * @param permission permission to use.
689     */
690    public FSMkdirs(String path, short permission) {
691      this.path = new Path(path);
692      this.permission = permission;
693    }
694
695    /**
696     * Executes the filesystem operation.
697     *
698     * @param fs filesystem instance to use.
699     *
700     * @return <code>true</code> if the mkdirs operation was successful,
701     *         <code>false</code> otherwise.
702     *
703     * @throws IOException thrown if an IO error occured.
704     */
705    @Override
706    public JSONObject execute(FileSystem fs) throws IOException {
707      FsPermission fsPermission = new FsPermission(permission);
708      boolean mkdirs = fs.mkdirs(path, fsPermission);
709      return toJSON(HttpFSFileSystem.MKDIRS_JSON, mkdirs);
710    }
711
712  }
713
714  /**
715   * Executor that performs a open FileSystemAccess files system operation.
716   */
717  @InterfaceAudience.Private
718  public static class FSOpen implements FileSystemAccess.FileSystemExecutor<InputStream> {
719    private Path path;
720
721    /**
722     * Creates a open executor.
723     *
724     * @param path file to open.
725     */
726    public FSOpen(String path) {
727      this.path = new Path(path);
728    }
729
730    /**
731     * Executes the filesystem operation.
732     *
733     * @param fs filesystem instance to use.
734     *
735     * @return The inputstream of the file.
736     *
737     * @throws IOException thrown if an IO error occured.
738     */
739    @Override
740    public InputStream execute(FileSystem fs) throws IOException {
741      int bufferSize = HttpFSServerWebApp.get().getConfig().getInt(
742          HTTPFS_BUFFER_SIZE_KEY, HTTP_BUFFER_SIZE_DEFAULT);
743      return fs.open(path, bufferSize);
744    }
745
746  }
747
748  /**
749   * Executor that performs a rename FileSystemAccess files system operation.
750   */
751  @InterfaceAudience.Private
752  public static class FSRename implements FileSystemAccess.FileSystemExecutor<JSONObject> {
753    private Path path;
754    private Path toPath;
755
756    /**
757     * Creates a rename executor.
758     *
759     * @param path path to rename.
760     * @param toPath new name.
761     */
762    public FSRename(String path, String toPath) {
763      this.path = new Path(path);
764      this.toPath = new Path(toPath);
765    }
766
767    /**
768     * Executes the filesystem operation.
769     *
770     * @param fs filesystem instance to use.
771     *
772     * @return <code>true</code> if the rename operation was successful,
773     *         <code>false</code> otherwise.
774     *
775     * @throws IOException thrown if an IO error occured.
776     */
777    @Override
778    public JSONObject execute(FileSystem fs) throws IOException {
779      boolean renamed = fs.rename(path, toPath);
780      return toJSON(HttpFSFileSystem.RENAME_JSON, renamed);
781    }
782
783  }
784
785  /**
786   * Executor that performs a set-owner FileSystemAccess files system operation.
787   */
788  @InterfaceAudience.Private
789  public static class FSSetOwner implements FileSystemAccess.FileSystemExecutor<Void> {
790    private Path path;
791    private String owner;
792    private String group;
793
794    /**
795     * Creates a set-owner executor.
796     *
797     * @param path the path to set the owner.
798     * @param owner owner to set.
799     * @param group group to set.
800     */
801    public FSSetOwner(String path, String owner, String group) {
802      this.path = new Path(path);
803      this.owner = owner;
804      this.group = group;
805    }
806
807    /**
808     * Executes the filesystem operation.
809     *
810     * @param fs filesystem instance to use.
811     *
812     * @return void.
813     *
814     * @throws IOException thrown if an IO error occured.
815     */
816    @Override
817    public Void execute(FileSystem fs) throws IOException {
818      fs.setOwner(path, owner, group);
819      return null;
820    }
821
822  }
823
824  /**
825   * Executor that performs a set-permission FileSystemAccess files system operation.
826   */
827  @InterfaceAudience.Private
828  public static class FSSetPermission implements FileSystemAccess.FileSystemExecutor<Void> {
829
830    private Path path;
831    private short permission;
832
833    /**
834     * Creates a set-permission executor.
835     *
836     * @param path path to set the permission.
837     * @param permission permission to set.
838     */
839    public FSSetPermission(String path, short permission) {
840      this.path = new Path(path);
841      this.permission = permission;
842    }
843
844    /**
845     * Executes the filesystem operation.
846     *
847     * @param fs filesystem instance to use.
848     *
849     * @return void.
850     *
851     * @throws IOException thrown if an IO error occured.
852     */
853    @Override
854    public Void execute(FileSystem fs) throws IOException {
855      FsPermission fsPermission = new FsPermission(permission);
856      fs.setPermission(path, fsPermission);
857      return null;
858    }
859
860  }
861
862  /**
863   * Executor that sets the acl for a file in a FileSystem
864   */
865  @InterfaceAudience.Private
866  public static class FSSetAcl implements FileSystemAccess.FileSystemExecutor<Void> {
867
868    private Path path;
869    private List<AclEntry> aclEntries;
870
871    /**
872     * Creates a set-acl executor.
873     *
874     * @param path path to set the acl.
875     * @param aclSpec acl to set.
876     */
877    public FSSetAcl(String path, String aclSpec) {
878      this.path = new Path(path);
879      this.aclEntries = AclEntry.parseAclSpec(aclSpec, true);
880    }
881
882    /**
883     * Executes the filesystem operation.
884     *
885     * @param fs filesystem instance to use.
886     *
887     * @return void.
888     *
889     * @throws IOException thrown if an IO error occurred.
890     */
891    @Override
892    public Void execute(FileSystem fs) throws IOException {
893      fs.setAcl(path, aclEntries);
894      return null;
895    }
896
897  }
898
899  /**
900   * Executor that removes all acls from a file in a FileSystem
901   */
902  @InterfaceAudience.Private
903  public static class FSRemoveAcl implements FileSystemAccess.FileSystemExecutor<Void> {
904
905    private Path path;
906
907    /**
908     * Creates a remove-acl executor.
909     *
910     * @param path path from which to remove the acl.
911     */
912    public FSRemoveAcl(String path) {
913      this.path = new Path(path);
914    }
915
916    /**
917     * Executes the filesystem operation.
918     *
919     * @param fs filesystem instance to use.
920     *
921     * @return void.
922     *
923     * @throws IOException thrown if an IO error occurred.
924     */
925    @Override
926    public Void execute(FileSystem fs) throws IOException {
927      fs.removeAcl(path);
928      return null;
929    }
930
931  }
932
933  /**
934   * Executor that modifies acl entries for a file in a FileSystem
935   */
936  @InterfaceAudience.Private
937  public static class FSModifyAclEntries implements FileSystemAccess.FileSystemExecutor<Void> {
938
939    private Path path;
940    private List<AclEntry> aclEntries;
941
942    /**
943     * Creates a modify-acl executor.
944     *
945     * @param path path to set the acl.
946     * @param aclSpec acl to set.
947     */
948    public FSModifyAclEntries(String path, String aclSpec) {
949      this.path = new Path(path);
950      this.aclEntries = AclEntry.parseAclSpec(aclSpec, true);
951    }
952
953    /**
954     * Executes the filesystem operation.
955     *
956     * @param fs filesystem instance to use.
957     *
958     * @return void.
959     *
960     * @throws IOException thrown if an IO error occurred.
961     */
962    @Override
963    public Void execute(FileSystem fs) throws IOException {
964      fs.modifyAclEntries(path, aclEntries);
965      return null;
966    }
967
968  }
969
970  /**
971   * Executor that removes acl entries from a file in a FileSystem
972   */
973  @InterfaceAudience.Private
974  public static class FSRemoveAclEntries implements FileSystemAccess.FileSystemExecutor<Void> {
975
976    private Path path;
977    private List<AclEntry> aclEntries;
978
979    /**
980     * Creates a remove acl entry executor.
981     *
982     * @param path path to set the acl.
983     * @param aclSpec acl parts to remove.
984     */
985    public FSRemoveAclEntries(String path, String aclSpec) {
986      this.path = new Path(path);
987      this.aclEntries = AclEntry.parseAclSpec(aclSpec, false);
988    }
989
990    /**
991     * Executes the filesystem operation.
992     *
993     * @param fs filesystem instance to use.
994     *
995     * @return void.
996     *
997     * @throws IOException thrown if an IO error occurred.
998     */
999    @Override
1000    public Void execute(FileSystem fs) throws IOException {
1001      fs.removeAclEntries(path, aclEntries);
1002      return null;
1003    }
1004
1005  }
1006
1007  /**
1008   * Executor that removes the default acl from a directory in a FileSystem
1009   */
1010  @InterfaceAudience.Private
1011  public static class FSRemoveDefaultAcl implements FileSystemAccess.FileSystemExecutor<Void> {
1012
1013    private Path path;
1014
1015    /**
1016     * Creates an executor for removing the default acl.
1017     *
1018     * @param path path to set the acl.
1019     */
1020    public FSRemoveDefaultAcl(String path) {
1021      this.path = new Path(path);
1022    }
1023
1024    /**
1025     * Executes the filesystem operation.
1026     *
1027     * @param fs filesystem instance to use.
1028     *
1029     * @return void.
1030     *
1031     * @throws IOException thrown if an IO error occurred.
1032     */
1033    @Override
1034    public Void execute(FileSystem fs) throws IOException {
1035      fs.removeDefaultAcl(path);
1036      return null;
1037    }
1038
1039  }
1040
1041  /**
1042   * Executor that gets the ACL information for a given file.
1043   */
1044  @InterfaceAudience.Private
1045  public static class FSAclStatus implements FileSystemAccess.FileSystemExecutor<Map> {
1046    private Path path;
1047
1048    /**
1049     * Creates an executor for getting the ACLs for a file.
1050     *
1051     * @param path the path to retrieve the ACLs.
1052     */
1053    public FSAclStatus(String path) {
1054      this.path = new Path(path);
1055    }
1056
1057    /**
1058     * Executes the filesystem operation.
1059     *
1060     * @param fs filesystem instance to use.
1061     *
1062     * @return a Map object (JSON friendly) with the file status.
1063     *
1064     * @throws IOException thrown if an IO error occurred.
1065     */
1066    @Override
1067    public Map execute(FileSystem fs) throws IOException {
1068      AclStatus status = fs.getAclStatus(path);
1069      return aclStatusToJSON(status);
1070    }
1071
1072  }
1073
1074  /**
1075   * Executor that performs a set-replication FileSystemAccess files system operation.
1076   */
1077  @InterfaceAudience.Private
1078  public static class FSSetReplication implements FileSystemAccess.FileSystemExecutor<JSONObject> {
1079    private Path path;
1080    private short replication;
1081
1082    /**
1083     * Creates a set-replication executor.
1084     *
1085     * @param path path to set the replication factor.
1086     * @param replication replication factor to set.
1087     */
1088    public FSSetReplication(String path, short replication) {
1089      this.path = new Path(path);
1090      this.replication = replication;
1091    }
1092
1093    /**
1094     * Executes the filesystem operation.
1095     *
1096     * @param fs filesystem instance to use.
1097     *
1098     * @return <code>true</code> if the replication value was set,
1099     *         <code>false</code> otherwise.
1100     *
1101     * @throws IOException thrown if an IO error occured.
1102     */
1103    @Override
1104    @SuppressWarnings("unchecked")
1105    public JSONObject execute(FileSystem fs) throws IOException {
1106      boolean ret = fs.setReplication(path, replication);
1107      JSONObject json = new JSONObject();
1108      json.put(HttpFSFileSystem.SET_REPLICATION_JSON, ret);
1109      return json;
1110    }
1111
1112  }
1113
1114  /**
1115   * Executor that performs a set-times FileSystemAccess files system operation.
1116   */
1117  @InterfaceAudience.Private
1118  public static class FSSetTimes implements FileSystemAccess.FileSystemExecutor<Void> {
1119    private Path path;
1120    private long mTime;
1121    private long aTime;
1122
1123    /**
1124     * Creates a set-times executor.
1125     *
1126     * @param path path to set the times.
1127     * @param mTime modified time to set.
1128     * @param aTime access time to set.
1129     */
1130    public FSSetTimes(String path, long mTime, long aTime) {
1131      this.path = new Path(path);
1132      this.mTime = mTime;
1133      this.aTime = aTime;
1134    }
1135
1136    /**
1137     * Executes the filesystem operation.
1138     *
1139     * @param fs filesystem instance to use.
1140     *
1141     * @return void.
1142     *
1143     * @throws IOException thrown if an IO error occured.
1144     */
1145    @Override
1146    public Void execute(FileSystem fs) throws IOException {
1147      fs.setTimes(path, mTime, aTime);
1148      return null;
1149    }
1150
1151  }
1152
1153  /**
1154   * Executor that performs a setxattr FileSystemAccess files system operation.
1155   */
1156  @InterfaceAudience.Private
1157  public static class FSSetXAttr implements 
1158      FileSystemAccess.FileSystemExecutor<Void> {
1159
1160    private Path path;
1161    private String name;
1162    private byte[] value;
1163    private EnumSet<XAttrSetFlag> flag;
1164
1165    public FSSetXAttr(String path, String name, String encodedValue, 
1166        EnumSet<XAttrSetFlag> flag) throws IOException {
1167      this.path = new Path(path);
1168      this.name = name;
1169      this.value = XAttrCodec.decodeValue(encodedValue);
1170      this.flag = flag;
1171    }
1172
1173    @Override
1174    public Void execute(FileSystem fs) throws IOException {
1175      fs.setXAttr(path, name, value, flag);
1176      return null;
1177    }
1178  }
1179
1180  /**
1181   * Executor that performs a removexattr FileSystemAccess files system 
1182   * operation.
1183   */
1184  @InterfaceAudience.Private
1185  public static class FSRemoveXAttr implements 
1186      FileSystemAccess.FileSystemExecutor<Void> {
1187
1188    private Path path;
1189    private String name;
1190
1191    public FSRemoveXAttr(String path, String name) {
1192      this.path = new Path(path);
1193      this.name = name;
1194    }
1195
1196    @Override
1197    public Void execute(FileSystem fs) throws IOException {
1198      fs.removeXAttr(path, name);
1199      return null;
1200    }
1201  }
1202
1203  /**
1204   * Executor that performs listing xattrs FileSystemAccess files system 
1205   * operation.
1206   */
1207  @SuppressWarnings("rawtypes")
1208  @InterfaceAudience.Private
1209  public static class FSListXAttrs implements 
1210      FileSystemAccess.FileSystemExecutor<Map> {
1211    private Path path;
1212
1213    /**
1214     * Creates listing xattrs executor.
1215     *
1216     * @param path the path to retrieve the xattrs.
1217     */
1218    public FSListXAttrs(String path) {
1219      this.path = new Path(path);
1220    }
1221
1222    /**
1223     * Executes the filesystem operation.
1224     *
1225     * @param fs filesystem instance to use.
1226     *
1227     * @return Map a map object (JSON friendly) with the xattr names.
1228     *
1229     * @throws IOException thrown if an IO error occured.
1230     */
1231    @Override
1232    public Map execute(FileSystem fs) throws IOException {
1233      List<String> names = fs.listXAttrs(path);
1234      return xAttrNamesToJSON(names);
1235    }
1236  }
1237
1238  /**
1239   * Executor that performs getting xattrs FileSystemAccess files system 
1240   * operation.
1241   */
1242  @SuppressWarnings("rawtypes")
1243  @InterfaceAudience.Private
1244  public static class FSGetXAttrs implements 
1245      FileSystemAccess.FileSystemExecutor<Map> {
1246    private Path path;
1247    private List<String> names;
1248    private XAttrCodec encoding;
1249
1250    /**
1251     * Creates getting xattrs executor.
1252     *
1253     * @param path the path to retrieve the xattrs.
1254     */
1255    public FSGetXAttrs(String path, List<String> names, XAttrCodec encoding) {
1256      this.path = new Path(path);
1257      this.names = names;
1258      this.encoding = encoding;
1259    }
1260
1261    /**
1262     * Executes the filesystem operation.
1263     *
1264     * @param fs filesystem instance to use.
1265     *
1266     * @return Map a map object (JSON friendly) with the xattrs.
1267     *
1268     * @throws IOException thrown if an IO error occured.
1269     */
1270    @Override
1271    public Map execute(FileSystem fs) throws IOException {
1272      Map<String, byte[]> xattrs = null;
1273      if (names != null && !names.isEmpty()) {
1274        xattrs = fs.getXAttrs(path, names);
1275      } else {
1276        xattrs = fs.getXAttrs(path);
1277      }
1278      return xAttrsToJSON(xattrs, encoding);
1279    }
1280  }
1281
1282
1283  /**
1284   * Executor that performs a getAllStoragePolicies FileSystemAccess files
1285   * system operation.
1286   */
1287  @SuppressWarnings({ "unchecked" })
1288  @InterfaceAudience.Private
1289  public static class FSGetAllStoragePolicies implements
1290      FileSystemAccess.FileSystemExecutor<JSONObject> {
1291
1292    @Override
1293    public JSONObject execute(FileSystem fs) throws IOException {
1294      Collection<? extends BlockStoragePolicySpi> storagePolicies = fs
1295          .getAllStoragePolicies();
1296      return storagePoliciesToJSON(storagePolicies);
1297    }
1298  }
1299
1300  /**
1301   * Executor that performs a getStoragePolicy FileSystemAccess files system
1302   * operation.
1303   */
1304  @SuppressWarnings({ "unchecked" })
1305  @InterfaceAudience.Private
1306  public static class FSGetStoragePolicy implements
1307      FileSystemAccess.FileSystemExecutor<JSONObject> {
1308
1309    private Path path;
1310
1311    public FSGetStoragePolicy(String path) {
1312      this.path = new Path(path);
1313    }
1314
1315    @Override
1316    public JSONObject execute(FileSystem fs) throws IOException {
1317      BlockStoragePolicySpi storagePolicy = fs.getStoragePolicy(path);
1318      JSONObject json = new JSONObject();
1319      json.put(HttpFSFileSystem.STORAGE_POLICY_JSON,
1320          storagePolicyToJSON(storagePolicy));
1321      return json;
1322    }
1323  }
1324
1325  /**
1326   * Executor that performs a setStoragePolicy FileSystemAccess files system
1327   * operation.
1328   */
1329  @InterfaceAudience.Private
1330  public static class FSSetStoragePolicy implements
1331      FileSystemAccess.FileSystemExecutor<Void> {
1332
1333    private Path path;
1334    private String policyName;
1335
1336    public FSSetStoragePolicy(String path, String policyName) {
1337      this.path = new Path(path);
1338      this.policyName = policyName;
1339    }
1340
1341    @Override
1342    public Void execute(FileSystem fs) throws IOException {
1343      fs.setStoragePolicy(path, policyName);
1344      return null;
1345    }
1346  }
1347
1348  /**
1349   * Executor that performs a unsetStoragePolicy FileSystemAccess files system
1350   * operation.
1351   */
1352  @InterfaceAudience.Private
1353  public static class FSUnsetStoragePolicy implements
1354      FileSystemAccess.FileSystemExecutor<Void> {
1355
1356    private Path path;
1357
1358    public FSUnsetStoragePolicy(String path) {
1359      this.path = new Path(path);
1360    }
1361
1362    @Override
1363    public Void execute(FileSystem fs) throws IOException {
1364      fs.unsetStoragePolicy(path);
1365      return null;
1366    }
1367  }
1368}