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

    https://github.com/apache/usergrid/pull/573#discussion_r122741704
  
    --- Diff: 
stack/corepersistence/token/src/main/java/org/apache/usergrid/persistence/token/impl/TokenSerializationImpl.java
 ---
    @@ -0,0 +1,451 @@
    +/*
    + * 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.usergrid.persistence.token.impl;
    +
    +import com.datastax.driver.core.*;
    +import com.datastax.driver.core.querybuilder.*;
    +import com.fasterxml.jackson.databind.ObjectMapper;
    +import com.fasterxml.jackson.dataformat.smile.SmileFactory;
    +import com.google.common.base.Preconditions;
    +import com.google.inject.Inject;
    +import com.google.inject.Singleton;
    +import org.apache.usergrid.persistence.core.CassandraConfig;
    +import org.apache.usergrid.persistence.core.astyanax.*;
    +import org.apache.usergrid.persistence.core.datastax.CQLUtils;
    +import org.apache.usergrid.persistence.core.datastax.TableDefinition;
    +import 
org.apache.usergrid.persistence.core.datastax.impl.TableDefinitionImpl;
    +import org.apache.usergrid.persistence.token.TokenSerialization;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.nio.ByteBuffer;
    +import java.util.*;
    +
    +
    +/**
    + * Serialize tokens and their details to Cassandra.
    + */
    +@Singleton
    +public class TokenSerializationImpl implements TokenSerialization {
    +
    +    public static final Logger logger = 
LoggerFactory.getLogger(TokenSerializationImpl.class);
    +
    +    private SmileFactory smile = new SmileFactory();
    +
    +    private ObjectMapper smileMapper = new ObjectMapper( smile );
    +
    +    public static final String TOKEN_UUID = "uuid";
    +    public static final String TOKEN_TYPE = "type";
    +    public static final String TOKEN_CREATED = "created";
    +    public static final String TOKEN_ACCESSED = "accessed";
    +    public static final String TOKEN_INACTIVE = "inactive";
    +    public static final String TOKEN_DURATION = "duration";
    +    public static final String TOKEN_PRINCIPAL_TYPE = "principal";
    +    public static final String TOKEN_ENTITY = "entity";
    +    public static final String TOKEN_APPLICATION = "application";
    +    public static final String TOKEN_STATE = "state";
    +    public static final String TOKEN_WORKFLOW_ORG_ID = "workflowOrgId";
    +    public static final String TOKEN_TYPE_ACCESS = "access";
    +
    +    private static final Set<String> TOKEN_PROPERTIES;
    +
    +    static {
    +        HashSet<String> set = new HashSet<String>();
    +        set.add( TOKEN_UUID );
    +        set.add( TOKEN_TYPE );
    +        set.add( TOKEN_CREATED );
    +        set.add( TOKEN_ACCESSED );
    +        set.add( TOKEN_INACTIVE );
    +        set.add( TOKEN_PRINCIPAL_TYPE );
    +        set.add( TOKEN_ENTITY );
    +        set.add( TOKEN_APPLICATION );
    +        set.add( TOKEN_STATE );
    +        set.add( TOKEN_DURATION );
    +        set.add( TOKEN_WORKFLOW_ORG_ID );
    +        TOKEN_PROPERTIES = Collections.unmodifiableSet(set);
    +    }
    +
    +    public static final HashSet<String> REQUIRED_TOKEN_PROPERTIES = new 
HashSet<String>();
    +
    +    static {
    +        REQUIRED_TOKEN_PROPERTIES.add( TOKEN_UUID );
    +        REQUIRED_TOKEN_PROPERTIES.add( TOKEN_TYPE );
    +        REQUIRED_TOKEN_PROPERTIES.add( TOKEN_CREATED );
    +        REQUIRED_TOKEN_PROPERTIES.add( TOKEN_ACCESSED );
    +        REQUIRED_TOKEN_PROPERTIES.add( TOKEN_INACTIVE );
    +        REQUIRED_TOKEN_PROPERTIES.add( TOKEN_DURATION );
    +    }
    +
    +    private static final String TOKENS_TABLE = CQLUtils.quote("Tokens");
    +    private static final Collection<String> TOKENS_PARTITION_KEYS = 
Collections.singletonList("key");
    +    private static final Collection<String> TOKENS_COLUMN_KEYS = 
Collections.singletonList("column1");
    +    private static final Map<String, DataType.Name> TOKENS_COLUMNS =
    +        new HashMap<String, DataType.Name>() {{
    +            put( "key", DataType.Name.BLOB );
    +            put( "column1", DataType.Name.BLOB );
    +            put( "value", DataType.Name.BLOB ); }};
    +    private static final Map<String, String> TOKENS_CLUSTERING_ORDER =
    +        new HashMap<String, String>(){{ put( "column1", "ASC" ); }};
    +
    +    private static final String PRINCIPAL_TOKENS_TABLE = 
CQLUtils.quote("PrincipalTokens");
    +    private static final Collection<String> 
PRINCIPAL_TOKENS_PARTITION_KEYS = Collections.singletonList("key");
    +    private static final Collection<String> PRINCIPAL_TOKENS_COLUMN_KEYS = 
Collections.singletonList("column1");
    +    private static final Map<String, DataType.Name> 
PRINCIPAL_TOKENS_COLUMNS =
    +        new HashMap<String, DataType.Name>() {{
    +            put( "key", DataType.Name.BLOB );
    +            put( "column1", DataType.Name.UUID );
    +            put( "value", DataType.Name.BLOB ); }};
    +    private static final Map<String, String> 
PRINCIPAL_TOKENS_CLUSTERING_ORDER =
    +        new HashMap<String, String>(){{ put( "column1", "ASC" ); }};
    +
    +
    +    private final Session session;
    +    private final CassandraConfig cassandraConfig;
    +
    +
    +    @Inject
    +    public TokenSerializationImpl(final Session session,
    +                                  final CassandraConfig cassandraConfig ) {
    +        this.session = session;
    +        this.cassandraConfig = cassandraConfig;
    +
    +    }
    +
    +
    +    @Override
    +    public void deleteTokens(final List<UUID> tokenUUIDs, final ByteBuffer 
principalKeyBuffer){
    +
    +        Preconditions.checkNotNull(tokenUUIDs, "token UUID list is 
required");
    +        Preconditions.checkNotNull(tokenUUIDs, "principalKeyBuffer is 
required");
    +
    +        logger.trace("deleteTokens, token UUIDs: {}", tokenUUIDs);
    +
    +        final BatchStatement batchStatement = new BatchStatement();
    +
    +        tokenUUIDs.forEach( tokenUUID ->
    +            batchStatement.add(
    +                QueryBuilder.delete()
    +                    .from(TOKENS_TABLE)
    +                    .where(QueryBuilder
    +                        .eq("key", DataType.uuid().serialize(tokenUUID, 
ProtocolVersion.NEWEST_SUPPORTED)))
    +            )
    +        );
    +
    +        batchStatement.add(
    +            QueryBuilder.delete()
    +                .from(PRINCIPAL_TOKENS_TABLE)
    +                .where(QueryBuilder
    +                    .eq("key", principalKeyBuffer)));
    +
    +
    +        session.execute(batchStatement);
    +
    +    }
    +
    +
    +    @Override
    +    public void revokeToken(final UUID tokenUUID, final ByteBuffer 
principalKeyBuffer){
    +
    +        Preconditions.checkNotNull(tokenUUID, "token UUID is required");
    +
    +        logger.trace("revokeToken, token UUID: {}", tokenUUID);
    +
    +
    +        final BatchStatement batchStatement = new BatchStatement();
    +
    +        batchStatement.add(
    +            QueryBuilder.delete()
    +                .from(TOKENS_TABLE)
    +                .where(QueryBuilder
    +                    .eq("key", DataType.uuid().serialize(tokenUUID, 
ProtocolVersion.NEWEST_SUPPORTED))));
    +
    +        if(principalKeyBuffer != null){
    +            batchStatement.add(
    +                QueryBuilder.delete()
    +                    .from(PRINCIPAL_TOKENS_TABLE)
    +                    .where(QueryBuilder
    +                        .eq("key", principalKeyBuffer))
    +                    .and(QueryBuilder
    +                        .eq("column1", 
DataType.uuid().serialize(tokenUUID, ProtocolVersion.NEWEST_SUPPORTED))));
    +        }
    +
    +        session.execute(batchStatement);
    +
    +    }
    +
    +
    +    @Override
    +    public void updateTokenAccessTime(UUID tokenUUID, long accessedTime, 
long inactiveTime, int ttl ){
    +
    +        Preconditions.checkNotNull(tokenUUID, "token UUID is required");
    +        Preconditions.checkNotNull(accessedTime, "accessedTime is 
required");
    --- End diff --
    
    accessedTime, inactiveTime and ttl are all numbers, shouldn't be a null 
check here right?


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to