Example usage for com.mongodb MongoWriteException getError

List of usage examples for com.mongodb MongoWriteException getError

Introduction

In this page you can find the example usage for com.mongodb MongoWriteException getError.

Prototype

public WriteError getError() 

Source Link

Document

Gets the error.

Usage

From source file:alxpez.blog.UserDAO.java

License:Apache License

public boolean addUser(String username, String password, String email) {

    String passwordHash = makePasswordHash(password, Integer.toString(getRandom().nextInt()));

    Document user = new Document();

    user.append("_id", username).append("password", passwordHash);

    if (email != null && !email.equals("")) {
        // the provided email address
        user.append("email", email);
    }//  ww w . j  a v  a2  s  .  c o  m

    try {
        usersCollection.insertOne(user);
        return true;
    } catch (MongoWriteException e) {
        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            System.out.println("Username already in use: " + username);
            return false;
        }
        throw e;
    }
}

From source file:blog.UserDAO.java

License:Apache License

public boolean addUser(String username, String password, String email) {

    String passwordHash = makePasswordHash(password, Integer.toString(random.nextInt()));

    Document user = new Document();

    user.append("_id", username).append("password", passwordHash);

    if (email != null && !email.equals("")) {
        // the provided email address
        user.append("email", email);
    }//from  ww  w.j  ava 2s  . c  o  m

    try {
        usersCollection.insertOne(user);
        return true;
    } catch (MongoWriteException e) {
        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            System.out.println("Username already in use: " + username);
            return false;
        }
        throw e;
    }
}

From source file:com.bilko.dao.UserDao.java

License:Apache License

public boolean addUser(final String username, final String password, final String email) {
    final Document user = new Document().append("_id", username).append("password",
            makePasswordHash(password, Integer.toString(random.nextInt())));

    if (StringUtils.isNotEmpty(email)) {
        user.append("email", email);
    }/*from   w  w  w . ja  v a2s . com*/

    try {
        collection.insertOne(user);
        return true;
    } catch (final MongoWriteException e) {
        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            System.out.println("USERNAME ALREADY IN USE: " + username);
            return false;
        }
        throw e;
    }
}

From source file:com.enlightendev.mongodb.blog.UserDAO.java

License:Apache License

public boolean addUser(String username, String password, String email) {

    String passwordHash = makePasswordHash(password, Integer.toString(random.nextInt()));

    // XXX WORK HERE
    // create an object suitable for insertion into the user collection
    // be sure to add username and hashed password to the document. problem instructions
    // will tell you the schema that the documents must follow.
    Document doc = new Document().append("username", username).append("password", passwordHash).append("_id",
            username);// w  w w  .  j a va 2s  .c  o m

    if (email != null && !email.equals("")) {
        // XXX WORK HERE
        // if there is an email address specified, add it to the document too.
        doc.append("email", email);
    }

    try {
        // XXX WORK HERE
        // insert the document into the user collection here
        usersCollection.insertOne(doc);
        return true;
    } catch (MongoWriteException e) {
        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            System.out.println("Username already in use: " + username);
            return false;
        }
        throw e;
    }
}

From source file:course.UserDAO.java

License:Apache License

public boolean addUser(String username, String password, String email) {

    String passwordHash = makePasswordHash(password, Integer.toString(random.nextInt()));

    // XXX WORK HERE
    // create an object suitable for insertion into the user collection
    // be sure to add username and hashed password to the document. problem instructions
    // will tell you the schema that the documents must follow.
    Document user = new Document("_id", username).append("password", passwordHash);

    if (email != null && !email.equals("")) {
        // XXX WORK HERE
        // if there is an email address specified, add it to the document too.
        user.append("email", email);
    }/*from   w  ww .ja  v a 2s  .co  m*/

    try {
        // XXX WORK HERE
        // insert the document into the user collection here
        usersCollection.insertOne(user);
        return true;
    } catch (MongoWriteException e) {
        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            System.out.println("Username already in use: " + username);
            return false;
        }
        throw e;
    }
}

From source file:course.UserDAO.java

License:Apache License

public boolean addUser(String username, String password, String email) {

    String passwordHash = makePasswordHash(password, Integer.toString(random.nextInt()));
    Document user = new Document("_id", username).append("password", passwordHash);
    // XXX WORK HERE
    // create an object suitable for insertion into the user collection
    // be sure to add username and hashed password to the document. problem instructions
    // will tell you the schema that the documents must follow.

    if (email != null && !email.equals("")) {
        // XXX WORK HERE
        // if there is an email address specified, add it to the document too.
        user.append("email", email);
    }/*from  www  .ja va2  s  . c o  m*/

    try {
        // XXX WORK HERE
        // insert the document into the user collection here
        usersCollection.insertOne(user);
        return true;
    } catch (MongoWriteException e) {
        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            System.out.println("Username already in use: " + username);
            return false;
        }
        throw e;
    }
}

From source file:edu.uniandes.ecos.codeaholics.config.AuthenticationJWT.java

/**
 * * Crea una sesion para un usuario dado su email.
 * //from   ww  w.jav  a 2  s.c  o m
 * @param pEmail
 *            correo del usuario al que se le crea la sesion
 * @param pUserProfile
 *            perfil del usuario citizen, functionary, etc
 */
private static void createSession(String pEmail, String pProfile, String pSalt) {

    if (pProfile.equals(Constants.CITIZEN_USER_PROFILE)) {

        //TODO: this is not safe
        Document filter = new Document();
        filter.append("email", pEmail);
        ArrayList<Document> citizen = DataBaseUtil.find(filter, Constants.CITIZEN_COLLECTION);

        String citizenName = citizen.get(0).get("name").toString();
        String citizenLast = citizen.get(0).get("lastName1").toString();

        token = createJWT(pEmail, pProfile, pSalt, citizenName, citizenLast);

    } else {

        log.info("User is functionary: creating token for functionary");
        //TODO: this is not safe
        Document filter = new Document();
        filter.append("email", pEmail);
        ArrayList<Document> functionary = DataBaseUtil.find(filter, Constants.FUNCTIONARY_COLLECTION);

        String citizenName = functionary.get(0).get("name").toString();
        String citizenLast = functionary.get(0).get("lastName1").toString();
        String citizenMayoralty = functionary.get(0).get("mayoralty").toString();

        token = createJWT(pEmail, pProfile, pSalt, citizenName, citizenLast, citizenMayoralty);

    }

    Document session = new Document();
    session.append("email", pEmail);
    session.append("user-profile", pProfile);
    session.append("token", token);
    session.append("salt", pSalt);
    log.info("Creating Session...");

    try {

        hasSession(pEmail);

        DataBaseUtil.save(session, Constants.SESSION_COLLECTION);

    } catch (SessionAlreadyExistsException ssEx) {

        log.info(ssEx.getMessage() + " : " + pEmail);
        closeSession(pEmail);
        DataBaseUtil.save(session, Constants.SESSION_COLLECTION);

    } catch (MongoWriteException e) {

        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            log.info("Already exist session for user: " + pEmail);
        }
        throw e;
    }
}

From source file:ELK.UserDAO.java

License:Apache License

public boolean addUser(String username, String password, String email) {

    String passwordHash = makePasswordHash(password, Integer.toString(random.nextInt()));

    // XXX WORK HERE
    // create an object suitable for insertion into the user collection
    // be sure to add username and hashed password to the document. problem instructions
    // will tell you the schema that the documents must follow.
    Document user = new Document("_id", username).append("password", passwordHash);

    if (email != null && !email.equals("")) {
        // XXX WORK HERE
        // if there is an email address specified, add it to the document too.
        user.append("email", email);
    }//w  w w  .j  a  v a2s. c o  m

    try {
        // XXX WORK HERE
        // insert the document into the user collection here
        usersCollection.insertOne(user);
        System.out.println("Added New User : username");
        return true;
    } catch (MongoWriteException e) {
        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            System.out.println("Username already in use: " + username);
            return false;
        }
        throw e;
    }
}

From source file:io.lumeer.storage.mongodb.MongoDbStorage.java

License:Open Source License

@Override
public void createOldDocument(final String collectionName, final DataDocument dataDocument,
        final String documentId, final int version) throws UnsuccessfulOperationException {
    Document doc = new Document(dataDocument);
    doc.put(DOCUMENT_ID, new BasicDBObject(DOCUMENT_ID, new ObjectId(documentId)));
    try {/*  w  w  w  .j  a v  a  2 s  .  c  om*/
        database.getCollection(collectionName).insertOne(doc);
    } catch (MongoWriteException e) {
        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            throw new UnsuccessfulOperationException(e.getMessage(), e.getCause());
        } else {
            throw e;
        }
    }
}

From source file:org.axonframework.mongo.eventsourcing.tokenstore.MongoTokenStore.java

License:Apache License

private void updateOrInsertTokenEntry(TrackingToken token, String processorName, int segment) {
    AbstractTokenEntry<?> tokenEntry = new GenericTokenEntry<>(token, serializer, contentType, processorName,
            segment);/* w w w. j a v a 2s . c o  m*/
    tokenEntry.claim(nodeId, claimTimeout);
    Bson update = combine(set("owner", nodeId), set("timestamp", tokenEntry.timestamp().toEpochMilli()),
            set("token", tokenEntry.getSerializedToken().getData()),
            set("tokenType", tokenEntry.getSerializedToken().getType().getName()));
    UpdateResult updateResult = mongoTemplate.trackingTokensCollection()
            .updateOne(claimableTokenEntryFilter(processorName, segment), update);

    if (updateResult.getModifiedCount() == 0) {
        try {
            mongoTemplate.trackingTokensCollection().insertOne(tokenEntryToDocument(tokenEntry));
        } catch (MongoWriteException exception) {
            if (ErrorCategory.fromErrorCode(exception.getError().getCode()) == ErrorCategory.DUPLICATE_KEY) {
                throw new UnableToClaimTokenException(
                        format("Unable to claim token '%s[%s]'", processorName, segment));
            }
        }
    }
}