Github user isper3at commented on a diff in the pull request:

    https://github.com/apache/incubator-rya/pull/124#discussion_r89904761
  
    --- Diff: 
dao/mongodb.rya/src/main/java/org/apache/rya/mongodb/document/visibility/Authorizations.java
 ---
    @@ -0,0 +1,369 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.rya.mongodb.document.visibility;
    +
    +import static com.google.common.base.Charsets.UTF_8;
    +
    +import java.io.Serializable;
    +import java.nio.ByteBuffer;
    +import java.util.ArrayList;
    +import java.util.Collection;
    +import java.util.Collections;
    +import java.util.HashSet;
    +import java.util.Iterator;
    +import java.util.List;
    +import java.util.Set;
    +import java.util.TreeSet;
    +
    +import com.google.common.base.Charsets;
    +
    +/**
    + * A collection of authorization strings.
    + */
    +public class Authorizations implements Iterable<byte[]>, Serializable, 
AuthorizationContainer {
    +
    +  private static final long serialVersionUID = 1L;
    +
    +  private final Set<ByteSequence> auths = new HashSet<ByteSequence>();
    +  private final List<byte[]> authsList = new ArrayList<byte[]>(); // 
sorted order
    +
    +  /**
    +   * An empty set of authorizations.
    +   */
    +  public static final Authorizations EMPTY = new Authorizations();
    +
    +  private static final boolean[] validAuthChars = new boolean[256];
    +
    +  /**
    +   * A special header string used when serializing instances of this class.
    +   *
    +   * @see #serialize()
    +   */
    +  public static final String HEADER = "!AUTH1:";
    +
    +  static {
    +    for (int i = 0; i < 256; i++) {
    +      validAuthChars[i] = false;
    +    }
    +
    +    for (int i = 'a'; i <= 'z'; i++) {
    +      validAuthChars[i] = true;
    +    }
    +
    +    for (int i = 'A'; i <= 'Z'; i++) {
    +      validAuthChars[i] = true;
    +    }
    +
    +    for (int i = '0'; i <= '9'; i++) {
    +      validAuthChars[i] = true;
    +    }
    +
    +    validAuthChars['_'] = true;
    +    validAuthChars['-'] = true;
    +    validAuthChars[':'] = true;
    +    validAuthChars['.'] = true;
    +    validAuthChars['/'] = true;
    +  }
    +
    +  static final boolean isValidAuthChar(final byte b) {
    +    return validAuthChars[0xff & b];
    +  }
    +
    +  private void checkAuths() {
    +    final Set<ByteSequence> sortedAuths = new TreeSet<ByteSequence>(auths);
    +
    +    for (final ByteSequence bs : sortedAuths) {
    +      if (bs.length() == 0) {
    +        throw new IllegalArgumentException("Empty authorization");
    +      }
    +
    +      authsList.add(bs.toArray());
    +    }
    +  }
    +
    +  /**
    +   * Constructs an authorization object from a collection of string 
authorizations that have each already been encoded as UTF-8 bytes. Warning: 
This method does
    +   * not verify that each encoded string is valid UTF-8.
    +   *
    +   * @param authorizations
    +   *          collection of authorizations, as strings encoded in UTF-8
    +   * @throws IllegalArgumentException
    +   *           if authorizations is null
    +   * @see #Authorizations(String...)
    +   */
    +  public Authorizations(final Collection<byte[]> authorizations) {
    +    ArgumentChecker.notNull(authorizations);
    +    for (final byte[] auth : authorizations) {
    +           auths.add(new ArrayByteSequence(auth));
    +   }
    +    checkAuths();
    +  }
    +
    +  /**
    +   * Constructs an authorization object from a list of string 
authorizations that have each already been encoded as UTF-8 bytes. Warning: 
This method does not
    +   * verify that each encoded string is valid UTF-8.
    +   *
    +   * @param authorizations
    +   *          list of authorizations, as strings encoded in UTF-8 and 
placed in buffers
    +   * @throws IllegalArgumentException
    +   *           if authorizations is null
    +   * @see #Authorizations(String...)
    +   */
    +  public Authorizations(final List<ByteBuffer> authorizations) {
    +    ArgumentChecker.notNull(authorizations);
    +    for (final ByteBuffer buffer : authorizations) {
    +      auths.add(new ArrayByteSequence(ByteBufferUtil.toBytes(buffer)));
    +    }
    +    checkAuths();
    +  }
    +
    +  /**
    +   * Constructs an authorizations object from a serialized form. This is 
NOT a constructor for a set of authorizations of size one. Warning: This method 
does
    +   * not verify that the encoded serialized form is valid UTF-8.
    +   *
    +   * @param authorizations
    +   *          a serialized authorizations string produced by {@link 
#getAuthorizationsArray()} or {@link #serialize()}, converted to UTF-8 bytes
    +   * @throws IllegalArgumentException
    +   *           if authorizations is null
    +   */
    +  public Authorizations(final byte[] authorizations) {
    +
    +    ArgumentChecker.notNull(authorizations);
    +
    +    String authsString = new String(authorizations, UTF_8);
    +    if (authsString.startsWith(HEADER)) {
    +      // it's the new format
    +      authsString = authsString.substring(HEADER.length());
    +      if (authsString.length() > 0) {
    +        for (final String encAuth : authsString.split(",")) {
    +          final byte[] auth = Base64.decodeBase64(encAuth.getBytes(UTF_8));
    +          auths.add(new ArrayByteSequence(auth));
    +        }
    +        checkAuths();
    +      }
    +    } else {
    +      // it's the old format
    +      if (authorizations.length > 0) {
    +           setAuthorizations(authsString.split(","));
    +   }
    +    }
    +  }
    +
    +  /**
    +   * Constructs an empty set of authorizations.
    +   *
    +   * @see #Authorizations(String...)
    +   */
    +  public Authorizations() {}
    +
    +  /**
    +   * Constructs an authorizations object from a set of human-readable 
authorizations.
    +   *
    +   * @param authorizations
    +   *          array of authorizations
    +   * @throws IllegalArgumentException
    +   *           if authorizations is null
    +   */
    +  public Authorizations(final String... authorizations) {
    +    setAuthorizations(authorizations);
    +  }
    +
    +  private void setAuthorizations(final String... authorizations) {
    +    ArgumentChecker.notNull(authorizations);
    +    auths.clear();
    +    for (String str : authorizations) {
    +      str = str.trim();
    +      auths.add(new ArrayByteSequence(str.getBytes(UTF_8)));
    +    }
    +
    +    checkAuths();
    +  }
    +
    +  /**
    +   * Returns a serialized form of these authorizations.
    --- End diff --
    
    if this is the serialized form, put it in the Serialize.serialize()


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to