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.hdfs.web.resources;
019
020import static org.apache.hadoop.hdfs.client.HdfsClientConfigKeys
021    .DFS_WEBHDFS_ACL_PERMISSION_PATTERN_DEFAULT;
022
023import java.util.Iterator;
024import java.util.List;
025import java.util.regex.Pattern;
026
027import org.apache.hadoop.fs.permission.AclEntry;
028
029/** AclPermission parameter. */
030public class AclPermissionParam extends StringParam {
031  /** Parameter name. */
032  public static final String NAME = "aclspec";
033  /** Default parameter value. */
034  public static final String DEFAULT = "";
035
036  private static final Domain DOMAIN = new Domain(NAME,
037      Pattern.compile(DFS_WEBHDFS_ACL_PERMISSION_PATTERN_DEFAULT));
038
039  /**
040   * Constructor.
041   *
042   * @param str a string representation of the parameter value.
043   */
044  public AclPermissionParam(final String str) {
045    super(DOMAIN, str == null || str.equals(DEFAULT) ? null : str);
046  }
047
048  public AclPermissionParam(List<AclEntry> acl) {
049    super(DOMAIN,parseAclSpec(acl).equals(DEFAULT) ? null : parseAclSpec(acl));
050  }
051
052  @Override
053  public String getName() {
054    return NAME;
055  }
056
057  public List<AclEntry> getAclPermission(boolean includePermission) {
058    final String v = getValue();
059    return (v != null ? AclEntry.parseAclSpec(v, includePermission) : AclEntry
060        .parseAclSpec(DEFAULT, includePermission));
061  }
062
063  /**
064   * @return parse {@code aclEntry} and return aclspec
065   */
066  private static String parseAclSpec(List<AclEntry> aclEntries) {
067    if (aclEntries == null) {
068      return null;
069    }
070    if (aclEntries.isEmpty()) {
071      return "";
072    }
073    if (aclEntries.size() == 1) {
074      AclEntry entry = aclEntries.get(0);
075      return entry == null ? "" : entry.toStringStable();
076    }
077    StringBuilder sb = new StringBuilder();
078    Iterator<AclEntry> iter = aclEntries.iterator();
079    sb.append(iter.next().toStringStable());
080    while (iter.hasNext()) {
081      AclEntry entry = iter.next();
082      sb.append(',').append(entry == null ? "" : entry.toStringStable());
083    }
084    return sb.toString();
085  }
086}