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.client;
19  
20  import java.util.Map;
21  
22  import org.apache.hadoop.classification.InterfaceAudience;
23  import org.apache.hadoop.classification.InterfaceStability;
24  import org.apache.hadoop.hbase.exceptions.DeserializationException;
25  import org.apache.hadoop.hbase.filter.Filter;
26  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
27  import org.apache.hadoop.hbase.security.access.AccessControlConstants;
28  import org.apache.hadoop.hbase.security.access.Permission;
29  import org.apache.hadoop.hbase.security.visibility.Authorizations;
30  import org.apache.hadoop.hbase.security.visibility.VisibilityConstants;
31  import org.apache.hadoop.hbase.util.Bytes;
32  
33  import com.google.common.collect.ArrayListMultimap;
34  import com.google.common.collect.ListMultimap;
35  
36  @InterfaceAudience.Public
37  @InterfaceStability.Evolving
38  public abstract class Query extends OperationWithAttributes {
39    protected Filter filter = null;
40  
41    /**
42     * @return Filter
43     */
44    public Filter getFilter() {
45      return filter;
46    }
47  
48    /**
49     * Apply the specified server-side filter when performing the Query.
50     * Only {@link Filter#filterKeyValue(Cell)} is called AFTER all tests
51     * for ttl, column match, deletes and max versions have been run.
52     * @param filter filter to run on the server
53     * @return this for invocation chaining
54     */
55    public Query setFilter(Filter filter) {
56      this.filter = filter;
57      return this;
58    }
59  
60    /**
61     * Sets the authorizations to be used by this Query
62     * @param authorizations
63     */
64    public void setAuthorizations(Authorizations authorizations) {
65      this.setAttribute(VisibilityConstants.VISIBILITY_LABELS_ATTR_KEY, ProtobufUtil
66          .toAuthorizations(authorizations).toByteArray());
67    }
68  
69    /**
70     * @return The authorizations this Query is associated with.
71     * @throws DeserializationException
72     */
73    public Authorizations getAuthorizations() throws DeserializationException {
74      byte[] authorizationsBytes = this.getAttribute(VisibilityConstants.VISIBILITY_LABELS_ATTR_KEY);
75      if (authorizationsBytes == null) return null;
76      return ProtobufUtil.toAuthorizations(authorizationsBytes);
77    }
78  
79    /**
80     * @return The serialized ACL for this operation, or null if none
81     */
82    public byte[] getACL() {
83      return getAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL);
84    }
85  
86    /**
87     * @param user User short name
88     * @param perms Permissions for the user
89     */
90    public void setACL(String user, Permission perms) {
91      setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL,
92        ProtobufUtil.toUsersAndPermissions(user, perms).toByteArray());
93    }
94  
95    /**
96     * @param perms A map of permissions for a user or users
97     */
98    public void setACL(Map<String, Permission> perms) {
99      ListMultimap<String, Permission> permMap = ArrayListMultimap.create();
100     for (Map.Entry<String, Permission> entry : perms.entrySet()) {
101       permMap.put(entry.getKey(), entry.getValue());
102     }
103     setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL,
104       ProtobufUtil.toUsersAndPermissions(permMap).toByteArray());
105   }
106 
107   /**
108    * @return true if ACLs should be evaluated on the cell level first
109    */
110   public boolean getACLStrategy() {
111     byte[] bytes = getAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL_STRATEGY);
112     if (bytes != null) {
113       return Bytes.equals(bytes, AccessControlConstants.OP_ATTRIBUTE_ACL_STRATEGY_CELL_FIRST);
114     }
115     return false;
116   }
117 
118   /**
119    * @param cellFirstStrategy true if ACLs should be evaluated on the cell
120    * level first, false if ACL should first be checked at the CF and table
121    * levels
122    */
123   public void setACLStrategy(boolean cellFirstStrategy) {
124     if (cellFirstStrategy) {
125       setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL_STRATEGY,
126         AccessControlConstants.OP_ATTRIBUTE_ACL_STRATEGY_CELL_FIRST);
127     }
128   }
129 }