Github user mdunker commented on a diff in the pull request:
https://github.com/apache/usergrid/pull/573#discussion_r122742832
--- 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");
+ Preconditions.checkNotNull(inactiveTime, "inactiveTime is
required");
+ Preconditions.checkNotNull(ttl, "ttl is required");
+
+ logger.trace("updateTokenAccessTime, token UUID: {}, accessedTime:
{}, inactiveTime: {}, ttl: {}",
+ tokenUUID, accessedTime, inactiveTime, ttl);
+
+ final BatchStatement batchStatement = new BatchStatement();
+ final Clause inKey =
+ QueryBuilder.eq("key", DataType.uuid().serialize(tokenUUID,
ProtocolVersion.NEWEST_SUPPORTED));
+ final Clause whereTokenAccessed =
+ QueryBuilder.eq("column1",
DataType.serializeValue(TOKEN_ACCESSED, ProtocolVersion.NEWEST_SUPPORTED));
+ final Clause whereTokenInactive =
+ QueryBuilder.eq("column1",
DataType.serializeValue(TOKEN_INACTIVE, ProtocolVersion.NEWEST_SUPPORTED));
+
+ final Assignment setAccessedTime =
+ QueryBuilder.set("value",
DataType.serializeValue(accessedTime, ProtocolVersion.NEWEST_SUPPORTED));
+ final Assignment setInactiveTime =
+ QueryBuilder.set("value",
DataType.serializeValue(inactiveTime, ProtocolVersion.NEWEST_SUPPORTED));
+
+ final Using usingTTL = QueryBuilder.ttl(ttl);
+
+ if( inactiveTime != Long.MIN_VALUE){
+ batchStatement.add(
+ QueryBuilder
+ .update(TOKENS_TABLE)
+ .with(setInactiveTime)
+ .where(inKey).and(whereTokenInactive)
+ .using(usingTTL)
+ );
+ }
+
+ batchStatement.add(
+ QueryBuilder
+ .update(TOKENS_TABLE)
+ .with(setAccessedTime)
+ .where(inKey).and(whereTokenAccessed)
+ .using(usingTTL)
+ );
+
+ session.execute(batchStatement);
+
+ }
+
+
+ @Override
+ public Map<String, Object> getTokenInfo(UUID tokenUUID){
+
+ Preconditions.checkNotNull(tokenUUID, "token UUID is required");
+
+ List<ByteBuffer> tokenProperties = new ArrayList<>();
+ TOKEN_PROPERTIES.forEach( prop ->
+ tokenProperties.add(DataType.serializeValue(prop,
ProtocolVersion.NEWEST_SUPPORTED)));
+
+ final ByteBuffer key = DataType.uuid().serialize(tokenUUID,
ProtocolVersion.NEWEST_SUPPORTED);
+
+ final Clause inKey = QueryBuilder.eq("key", key);
+ final Clause inColumn = QueryBuilder.in("column1", tokenProperties
);
+
+ final Statement statement =
QueryBuilder.select().all().from(TOKENS_TABLE)
+ .where(inKey)
+ .and(inColumn)
+ .setConsistencyLevel(cassandraConfig.getDataStaxReadCl());
+
+ final ResultSet resultSet = session.execute(statement);
+ final List<Row> rows = resultSet.all();
+
+ Map<String, Object> tokenInfo = new HashMap<>();
+
+ rows.forEach( row -> {
+
+ final String name = (String)DataType.text()
+ .deserialize(row.getBytes("column1"),
ProtocolVersion.NEWEST_SUPPORTED);
+ final Object value = deserializeColumnValue(name,
row.getBytes("value"));
+
+ if (value == null){
+ throw new RuntimeException("error deserializing token info
for property: "+name);
+ }
+
+ tokenInfo.put(name, value);
+
+ });
+
+ logger.trace("getTokenInfo, info: {}", tokenInfo);
+
+ return tokenInfo;
+ }
+
+
+ @Override
+ public void putTokenInfo(final UUID tokenUUID, final Map<String,
Object> tokenInfo,
+ final ByteBuffer principalKeyBuffer, final
int ttl){
+
+ Preconditions.checkNotNull(tokenUUID, "tokenUUID is required");
+ Preconditions.checkNotNull(tokenUUID, "tokenInfo is required");
+ Preconditions.checkNotNull(ttl, "ttl is required");
--- End diff --
not null check for int?
---
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.
---