Java Code Examples for me.prettyprint.cassandra.serializers.StringSerializer

The following examples show how to use me.prettyprint.cassandra.serializers.StringSerializer. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: carbon-identity   Source File: CassandraUserStoreManager.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Get the list of users mapped to a role.
 */
@Override
public String[] doGetUserListOfRole(String roleName, String filter) throws UserStoreException {

    List<String> usersList = new ArrayList<String>();
    Composite key = new Composite();
    key.addComponent(roleName, stringSerializer);
    key.addComponent(tenantIdString, stringSerializer);
    SliceQuery<Composite, String, String> query = HFactory
            .createSliceQuery(keyspace, CompositeSerializer.get(), StringSerializer.get(), StringSerializer.get())
            .setKey(key).setColumnFamily(CFConstants.UM_ROLE_USER_INDEX);

    ColumnSliceIterator<Composite, String, String> iterator = new ColumnSliceIterator<Composite, String, String>(
            query, null, "\uFFFF", false);

    while (iterator.hasNext()) {
        HColumn<String, String> column = iterator.next();
        usersList.add(column.getValue());
    }
    return usersList.toArray(new String[usersList.size()]);
}
 
Example 2
Source Project: carbon-identity   Source File: CassandraUserStoreManager.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Gets the external role list of a user.
 */
@Override
public String[] doGetExternalRoleListOfUser(String userName, String filter) throws UserStoreException {

    List<String> roles = new ArrayList<String>();
    int arrayLength = 0;
    Composite key = new Composite();
    key.addComponent(userName, stringSerializer);
    key.addComponent(tenantIdString, stringSerializer);
    SliceQuery<Composite, String, String> query = HFactory
            .createSliceQuery(keyspace, CompositeSerializer.get(), StringSerializer.get(), StringSerializer.get())
            .setKey(key).setColumnFamily(CFConstants.UM_USER_ROLE);

    ColumnSliceIterator<Composite, String, String> iterator = new ColumnSliceIterator<Composite, String, String>(
            query, null, "\uFFFF", false);

    while (iterator.hasNext()) {
        HColumn<String, String> column = iterator.next();
        roles.add(column.getValue());
    }
    return roles.toArray(new String[arrayLength]);
}
 
Example 3
Source Project: carbon-identity   Source File: CassandraUserStoreManager.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Deletes a user by userName.
 */
@Override
public void doDeleteUser(String userName) throws UserStoreException {

    Mutator<Composite> mutator = HFactory.createMutator(keyspace, CompositeSerializer.get());
    String[] roles = doGetExternalRoleListOfUser(userName, "");
    for (String role : roles) {
        Composite key = new Composite();
        key.addComponent(role, stringSerializer);
        key.addComponent(tenantIdString, stringSerializer);
        ColumnFamilyTemplate<Composite, String> userCFTemplate = new ThriftColumnFamilyTemplate<Composite, String>(
                keyspace, CFConstants.UM_ROLE_USER_INDEX, CompositeSerializer.get(), StringSerializer.get());
        try {
            userCFTemplate.deleteColumn(key, userName);
        } catch (HectorException e) {
            log.error("Error during deletion ", e);
        }
    }

    Composite userKey = new Composite();
    userKey.addComponent(userName, stringSerializer);
    userKey.addComponent(tenantIdString, stringSerializer);
    mutator.addDeletion(userKey, CFConstants.UM_USER_ROLE, null, CompositeSerializer.get());
    mutator.addDeletion(userKey, CFConstants.UM_USER, null, CompositeSerializer.get());
    mutator.execute();

    if (log.isDebugEnabled()) {
        log.debug("Deleted user " + userName + " successfully");
    }
}
 
Example 4
Source Project: carbon-identity   Source File: CassandraUserStoreManager.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Authenticates a user given the user name and password against the user
 * store.
 */
@Override
public boolean doAuthenticate(String userName, Object credential) throws UserStoreException {

    String password = (String) credential;
    boolean isAuthed = false;
    if (!checkUserNameValid(userName)) {
        log.error("Invalid Username");
        return false;
    }

    if (!checkUserPasswordValid(credential)) {
        log.error("Invalid password");
        return false;
    }

    if (UserCoreUtil.isRegistryAnnonymousUser(userName)) {
        log.error("Anonnymous user trying to login");
        return false;
    }

    Composite key = new Composite();
    key.addComponent(userName, stringSerializer);
    key.addComponent(tenantIdString, stringSerializer);

    ColumnFamilyTemplate<Composite, String> userCFTemplate = new ThriftColumnFamilyTemplate<Composite, String>(
            keyspace, CFConstants.UM_USER, CompositeSerializer.get(), StringSerializer.get());

    ColumnFamilyResult<Composite, String> result = userCFTemplate.queryColumns(key);
    String saltVallue = result.getString(CFConstants.UM_SALT_VALUE);
    String storedPassword = result.getString(CFConstants.UM_SECRET);

    if (TRUE.equalsIgnoreCase(realmConfig.getUserStoreProperty(JDBCRealmConstants.STORE_SALTED_PASSWORDS))) {
        password = Util.preparePassword(password, saltVallue);
        if ((storedPassword != null) && (storedPassword.equals(password))) {
            isAuthed = true;
        }
    }
    return isAuthed;
}
 
Example 5
Source Project: archiva   Source File: CassandraUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static <T> String getAsStringValue( ColumnSlice<String, T> columnSlice, String columnName )
{
    StringSerializer ss = StringSerializer.get();
    if ( StringUtils.isEmpty( columnName ) )
    {
        return null;
    }

    HColumn<String, T> hColumn = columnSlice.getColumnByName( columnName );
    return hColumn == null ? null : ss.fromByteBuffer( hColumn.getValueBytes() );
}
 
Example 6
Source Project: archiva   Source File: CassandraMetadataRepository.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * if the repository doesn't exist it will be created
 *
 * @param repositoryId
 * @return
 */
public Repository getOrCreateRepository( String repositoryId )
    throws MetadataRepositoryException
{
    String cf = cassandraArchivaManager.getRepositoryFamilyName();

    QueryResult<OrderedRows<String, String, String>> result = HFactory //
        .createRangeSlicesQuery( keyspace, StringSerializer.get(), StringSerializer.get(),
                                 StringSerializer.get() ) //
        .setColumnFamily( cf ) //
        .setColumnNames( REPOSITORY_NAME.toString() ) //
        .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
        .execute();

    if ( result.get().getCount() < 1 )
    {
        // we need to create the repository
        Repository repository = new Repository( repositoryId );

        try
        {
            MutationResult mutationResult = HFactory.createMutator( keyspace, StringSerializer.get() ) //
                .addInsertion( repositoryId, cf,
                               CassandraUtils.column( REPOSITORY_NAME.toString(), repository.getName() ) ) //
                .execute();
            logger.debug( "time to insert repository: {}", mutationResult.getExecutionTimeMicro() );
            return repository;
        }
        catch ( HInvalidRequestException e )
        {
            logger.error( e.getMessage(), e );
            throw new MetadataRepositoryException( e.getMessage(), e );
        }

    }

    return new Repository(
        result.get().getList().get( 0 ).getColumnSlice().getColumnByName( REPOSITORY_NAME.toString() ).getValue() );
}
 
Example 7
Source Project: archiva   Source File: CassandraMetadataRepository.java    License: Apache License 2.0 5 votes vote down vote up
protected Repository getRepository( String repositoryId )
    throws MetadataRepositoryException
{

    QueryResult<OrderedRows<String, String, String>> result = HFactory //
        .createRangeSlicesQuery( keyspace, StringSerializer.get(), StringSerializer.get(),
                                 StringSerializer.get() ) //
        .setColumnFamily( cassandraArchivaManager.getRepositoryFamilyName() ) //
        .setColumnNames( REPOSITORY_NAME.toString() ) //
        .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
        .execute();
    return ( result.get().getCount() > 0 ) ? new Repository( repositoryId ) : null;
}
 
Example 8
Source Project: archiva   Source File: CassandraMetadataRepository.java    License: Apache License 2.0 5 votes vote down vote up
private Namespace updateOrAddNamespace( String repositoryId, String namespaceId )
    throws MetadataRepositoryException
{
    try
    {
        Repository repository = getOrCreateRepository( repositoryId );

        String key =
            new Namespace.KeyBuilder().withNamespace( namespaceId ).withRepositoryId( repositoryId ).build();

        Namespace namespace = getNamespace( repositoryId, namespaceId );
        if ( namespace == null )
        {
            String cf = cassandraArchivaManager.getNamespaceFamilyName();
            namespace = new Namespace( namespaceId, repository );
            HFactory.createMutator( keyspace, StringSerializer.get() )
                //  values
                .addInsertion( key, cf, CassandraUtils.column( NAME.toString(), namespace.getName() ) ) //
                .addInsertion( key, cf, CassandraUtils.column( REPOSITORY_NAME.toString(), repository.getName() ) ) //
                .execute();
        }

        return namespace;
    }
    catch ( HInvalidRequestException e )
    {
        logger.error( e.getMessage(), e );
        throw new MetadataRepositoryException( e.getMessage(), e );
    }
}
 
Example 9
Source Project: elasticactors   Source File: BackplaneConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void initialize() {
    String cassandraHosts = env.getProperty("ea.cassandra.hosts","localhost:9160");
    CassandraHostConfigurator hostConfigurator = new CassandraHostConfigurator(cassandraHosts);
    hostConfigurator.setAutoDiscoverHosts(false);
    hostConfigurator.setMaxActive(env.getProperty("ea.cassandra.maxActive",Integer.class,Runtime.getRuntime().availableProcessors() * 3));
    hostConfigurator.setRetryDownedHosts(true);
    hostConfigurator.setRetryDownedHostsDelayInSeconds(env.getProperty("ea.cassandra.retryDownedHostsDelayInSeconds",Integer.class,1));
    hostConfigurator.setMaxWaitTimeWhenExhausted(2000L);
    String cassandraClusterName = env.getProperty("ea.cassandra.cluster","ElasticActorsCluster");
    // it seems that there are issues with the CassandraHostRetryService and retrying downed hosts
    // if we don't let the HFactory manage the cluster then CassandraHostRetryService doesn't try to
    // be smart about finding out if a host was removed from the ring and so it will keep on retrying
    // all configured hosts (and ultimately fail-back when the host comes back online)
    // the default is TRUE, which will let HFactory manage the cluster
    Boolean manageClusterThroughHFactory = env.getProperty("ea.cassandra.hfactory.manageCluster", Boolean.class, Boolean.TRUE);
    Cluster cluster;
    if(manageClusterThroughHFactory) {
        cluster = HFactory.getOrCreateCluster(cassandraClusterName, hostConfigurator);
    } else {
        cluster = new ThriftCluster(cassandraClusterName, hostConfigurator, null);
    }
    String cassandraKeyspaceName = env.getProperty("ea.cassandra.keyspace","ElasticActors");
    Keyspace keyspace = HFactory.createKeyspace(cassandraKeyspaceName,cluster);
    persistentActorsColumnFamilyTemplate =
        new ThriftColumnFamilyTemplate<>(keyspace,"PersistentActors", CompositeSerializer.get(),StringSerializer.get());
    scheduledMessagesColumnFamilyTemplate =
        new ThriftColumnFamilyTemplate<>(keyspace,"ScheduledMessages",CompositeSerializer.get(), CompositeSerializer.get());
    actorSystemEventListenersColumnFamilyTemplate =
            new ThriftColumnFamilyTemplate<>(keyspace,"ActorSystemEventListeners", CompositeSerializer.get(),StringSerializer.get());
    // return
    // @TODO: make this configurable and use the ColumnSliceIterator
    scheduledMessagesColumnFamilyTemplate.setCount(Integer.MAX_VALUE);
    actorSystemEventListenersColumnFamilyTemplate.setCount(Integer.MAX_VALUE);
}
 
Example 10
Source Project: carbon-identity   Source File: CassandraUserStoreManager.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Update the user list mapped to a role.
 */
@Override
public void doUpdateUserListOfRole(String roleName, String[] deletedUsers, String[] newUsers)
        throws UserStoreException {

    Mutator<Composite> mutator = HFactory.createMutator(keyspace, CompositeSerializer.get());
    RoleContext ctx = createRoleContext(roleName);
    roleName = ctx.getRoleName();
    boolean isShared = ctx.isShared();
    if (!isShared) {
        //TODO TO BE Implemented
    }
    if (deletedUsers != null && deletedUsers.length > 0) {
        if (isShared) {
            //TODO TO BE Implemented
        } else {
            if (deletedUsers.length > 0) {
                Composite key = new Composite();
                key.addComponent(roleName, stringSerializer);
                key.addComponent(tenantIdString, stringSerializer);

                for (String user : deletedUsers) {

                    Composite userKey = new Composite();
                    userKey.addComponent(user, stringSerializer);
                    userKey.addComponent(tenantIdString, stringSerializer);

                    ColumnFamilyTemplate<Composite, String> userCFTemplate = new ThriftColumnFamilyTemplate<Composite, String>(
                            keyspace, CFConstants.UM_USER_ROLE, CompositeSerializer.get(), StringSerializer.get());
                    ColumnFamilyTemplate<Composite, String> roleCFTemplate = new ThriftColumnFamilyTemplate<Composite, String>(
                            keyspace, CFConstants.UM_ROLE_USER_INDEX, CompositeSerializer.get(),
                            StringSerializer.get());
                    try {
                        roleCFTemplate.deleteColumn(mutator, key, user);
                        userCFTemplate.deleteColumn(mutator, userKey, roleName);
                    } catch (HectorException e) {
                        log.error(e.getMessage(), e);
                        throw new UserStoreException("Error during the updating of a user's role list");
                    }
                }
            }

        }
    }
    // need to clear user roles cache upon roles update
    clearUserRolesCacheByTenant(this.tenantId);

    if (newUsers != null && newUsers.length > 0) {
        if (isShared) {
            //TODO TO BE Implemented
        } else {
            addRoleToUsersList(newUsers, roleName, mutator);
        }
    }
    mutator.execute();

}
 
Example 11
Source Project: archiva   Source File: CassandraMetadataRepository.java    License: Apache License 2.0 4 votes vote down vote up
public CassandraMetadataRepository( MetadataService metadataService,
                                    CassandraArchivaManager cassandraArchivaManager )
{
    super( metadataService );
    this.cassandraArchivaManager = cassandraArchivaManager;
    this.keyspace = cassandraArchivaManager.getKeyspace();

    this.projectVersionMetadataTemplate =
        new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
                                          cassandraArchivaManager.getProjectVersionMetadataFamilyName(), //
                                          StringSerializer.get(), //
                                          StringSerializer.get() );

    this.projectTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
                                                             cassandraArchivaManager.getProjectFamilyName(), //
                                                             //
                                                             StringSerializer.get(), //
                                                             StringSerializer.get() );

    this.artifactMetadataTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
                                                                      cassandraArchivaManager.getArtifactMetadataFamilyName(),
                                                                      StringSerializer.get(), //
                                                                      StringSerializer.get() );

    this.metadataFacetTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
                                                                   cassandraArchivaManager.getMetadataFacetFamilyName(),
                                                                   //
                                                                   StringSerializer.get(), //
                                                                   StringSerializer.get() );

    this.mailingListTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
                                                                 cassandraArchivaManager.getMailingListFamilyName(),
                                                                 //
                                                                 StringSerializer.get(), //
                                                                 StringSerializer.get() );

    this.licenseTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
                                                             cassandraArchivaManager.getLicenseFamilyName(),
                                                             //
                                                             StringSerializer.get(), //
                                                             StringSerializer.get() );

    this.dependencyTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
                                                                cassandraArchivaManager.getDependencyFamilyName(),
                                                                //
                                                                StringSerializer.get(), //
                                                                StringSerializer.get() );

    this.checksumTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
            cassandraArchivaManager.getChecksumFamilyName(),
            //
            StringSerializer.get(), //
            StringSerializer.get() );
}