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.hbase.util; 019 020import java.io.UnsupportedEncodingException; 021import java.net.URI; 022import java.net.URLDecoder; 023import java.nio.charset.StandardCharsets; 024import java.util.Collections; 025import java.util.Map; 026import java.util.stream.Collectors; 027import org.apache.commons.lang3.StringUtils; 028import org.apache.hadoop.conf.Configuration; 029import org.apache.yetus.audience.InterfaceAudience; 030 031import org.apache.hbase.thirdparty.com.google.common.base.Joiner; 032import org.apache.hbase.thirdparty.com.google.common.base.Splitter; 033 034/** 035 * Utility for Strings. 036 */ 037@InterfaceAudience.Private 038public final class Strings { 039 public static final String DEFAULT_SEPARATOR = "="; 040 public static final String DEFAULT_KEYVALUE_SEPARATOR = ", "; 041 042 public static final Joiner JOINER = Joiner.on(","); 043 public static final Splitter SPLITTER = Splitter.on(","); 044 045 private Strings() { 046 } 047 048 /** 049 * Append to a StringBuilder a key/value. Uses default separators. 050 * @param sb StringBuilder to use 051 * @param key Key to append. 052 * @param value Value to append. 053 * @return Passed <code>sb</code> populated with key/value. 054 */ 055 public static StringBuilder appendKeyValue(final StringBuilder sb, final String key, 056 final Object value) { 057 return appendKeyValue(sb, key, value, DEFAULT_SEPARATOR, DEFAULT_KEYVALUE_SEPARATOR); 058 } 059 060 /** 061 * Append to a StringBuilder a key/value. Uses default separators. 062 * @param sb StringBuilder to use 063 * @param key Key to append. 064 * @param value Value to append. 065 * @param separator Value to use between key and value. 066 * @param keyValueSeparator Value to use between key/value sets. 067 * @return Passed <code>sb</code> populated with key/value. 068 */ 069 public static StringBuilder appendKeyValue(final StringBuilder sb, final String key, 070 final Object value, final String separator, final String keyValueSeparator) { 071 if (sb.length() > 0) { 072 sb.append(keyValueSeparator); 073 } 074 return sb.append(key).append(separator).append(value); 075 } 076 077 /** 078 * Given a PTR string generated via reverse DNS lookup, return everything except the trailing 079 * period. Example for host.example.com., return host.example.com 080 * @param dnPtr a domain name pointer (PTR) string. 081 * @return Sanitized hostname with last period stripped off. 082 */ 083 public static String domainNamePointerToHostName(String dnPtr) { 084 if (dnPtr == null) { 085 return null; 086 } 087 088 return dnPtr.endsWith(".") ? dnPtr.substring(0, dnPtr.length() - 1) : dnPtr; 089 } 090 091 /** 092 * Push the input string to the right by appending a character before it, usually a space. 093 * @param input the string to pad 094 * @param padding the character to repeat to the left of the input string 095 * @param length the desired total length including the padding 096 * @return padding characters + input 097 */ 098 public static String padFront(String input, char padding, int length) { 099 if (input.length() > length) { 100 throw new IllegalArgumentException("input \"" + input + "\" longer than maxLength=" + length); 101 } 102 int numPaddingCharacters = length - input.length(); 103 return StringUtils.repeat(padding, numPaddingCharacters) + input; 104 } 105 106 /** 107 * Parse the query string of an URI to a key value map. If a single key occurred multiple times, 108 * only the first one will take effect. 109 */ 110 public static Map<String, String> parseURIQueries(URI uri) { 111 if (StringUtils.isBlank(uri.getRawQuery())) { 112 return Collections.emptyMap(); 113 } 114 return Splitter.on('&').trimResults().splitToStream(uri.getRawQuery()).map(kv -> { 115 int idx = kv.indexOf('='); 116 try { 117 if (idx > 0) { 118 return Pair.newPair( 119 URLDecoder.decode(kv.substring(0, idx), StandardCharsets.UTF_8.name()), 120 URLDecoder.decode(kv.substring(idx + 1), StandardCharsets.UTF_8.name())); 121 } else { 122 return Pair.newPair(URLDecoder.decode(kv, StandardCharsets.UTF_8.name()), ""); 123 } 124 } catch (UnsupportedEncodingException e) { 125 // should not happen 126 throw new AssertionError(e); 127 } 128 }).collect(Collectors.toMap(Pair::getFirst, Pair::getSecond, (v1, v2) -> v1)); 129 } 130 131 /** 132 * Apply the key value pairs in the query string of the given URI to the given Configuration. If a 133 * single key occurred multiple times, only the first one will take effect. 134 */ 135 public static void applyURIQueriesToConf(URI uri, Configuration conf) { 136 parseURIQueries(uri).forEach(conf::set); 137 } 138}