View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.security.visibility;
19  
20  import org.apache.hadoop.hbase.classification.InterfaceAudience;
21  import org.apache.hadoop.hbase.classification.InterfaceStability;
22  import org.apache.hadoop.hbase.util.Bytes;
23  
24  /**
25   * This contains a visibility expression which can be associated with a cell. When it is set with a
26   * Mutation, all the cells in that mutation will get associated with this expression. A visibility
27   * expression can contain visibility labels combined with logical
28   * operators AND(&), OR(|) and NOT(!)
29   */
30  @InterfaceAudience.Public
31  @InterfaceStability.Evolving
32  public class CellVisibility {
33  
34    private String expression;
35  
36    public CellVisibility(String expression) {
37      this.expression = expression;
38    }
39  
40    /**
41     * @return The visibility expression
42     */
43    public String getExpression() {
44      return this.expression;
45    }
46  
47    @Override
48    public String toString() {
49      return this.expression;
50    }
51  
52    /**
53     * Helps in quoting authentication Strings. Use this if unicode characters to
54     * be used in expression or special characters like '(', ')',
55     * '"','\','&','|','!'
56     */
57    public static String quote(String auth) {
58      return quote(Bytes.toBytes(auth));
59    }
60  
61    /**
62     * Helps in quoting authentication Strings. Use this if unicode characters to
63     * be used in expression or special characters like '(', ')',
64     * '"','\','&','|','!'
65     */
66    public static String quote(byte[] auth) {
67      int escapeChars = 0;
68  
69      for (int i = 0; i < auth.length; i++)
70        if (auth[i] == '"' || auth[i] == '\\')
71          escapeChars++;
72  
73      byte[] escapedAuth = new byte[auth.length + escapeChars + 2];
74      int index = 1;
75      for (int i = 0; i < auth.length; i++) {
76        if (auth[i] == '"' || auth[i] == '\\') {
77          escapedAuth[index++] = '\\';
78        }
79        escapedAuth[index++] = auth[i];
80      }
81  
82      escapedAuth[0] = '"';
83      escapedAuth[escapedAuth.length - 1] = '"';
84  
85      return Bytes.toString(escapedAuth);
86    }
87  }