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.hdfs.web.resources;
019
020 import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_WEBHDFS_USER_PATTERN_DEFAULT;
021 import org.apache.hadoop.security.UserGroupInformation;
022 import com.google.common.annotations.VisibleForTesting;
023
024 import java.text.MessageFormat;
025 import java.util.regex.Pattern;
026
027 /** User parameter. */
028 public class UserParam extends StringParam {
029 /** Parameter name. */
030 public static final String NAME = "user.name";
031 /** Default parameter value. */
032 public static final String DEFAULT = "";
033
034 private static Domain domain = new Domain(NAME, Pattern.compile(DFS_WEBHDFS_USER_PATTERN_DEFAULT));
035
036 @VisibleForTesting
037 public static Domain getUserPatternDomain() {
038 return domain;
039 }
040
041 @VisibleForTesting
042 public static void setUserPatternDomain(Domain dm) {
043 domain = dm;
044 }
045
046 public static void setUserPattern(String pattern) {
047 domain = new Domain(NAME, Pattern.compile(pattern));
048 }
049
050 private static String validateLength(String str) {
051 if (str == null) {
052 throw new IllegalArgumentException(
053 MessageFormat.format("Parameter [{0}], cannot be NULL", NAME));
054 }
055 int len = str.length();
056 if (len < 1) {
057 throw new IllegalArgumentException(MessageFormat.format(
058 "Parameter [{0}], it's length must be at least 1", NAME));
059 }
060 return str;
061 }
062
063 /**
064 * Constructor.
065 * @param str a string representation of the parameter value.
066 */
067 public UserParam(final String str) {
068 super(domain, str == null || str.equals(DEFAULT)? null : validateLength(str));
069 }
070
071 /**
072 * Construct an object from a UGI.
073 */
074 public UserParam(final UserGroupInformation ugi) {
075 this(ugi.getShortUserName());
076 }
077
078 @Override
079 public String getName() {
080 return NAME;
081 }
082 }