Search in sources :

Example 6 with DuplicateKeyException

use of com.mongodb.DuplicateKeyException in project GNS by MobilityFirst.

the class MongoRecords method insert.

@Override
public void insert(String collectionName, String guid, JSONObject value) throws FailedDBOperationException, RecordExistsException {
    db.requestStart();
    try {
        db.requestEnsureConnection();
        DBCollection collection = db.getCollection(collectionName);
        DBObject dbObject;
        try {
            dbObject = (DBObject) JSON.parse(value.toString());
        } catch (Exception e) {
            throw new FailedDBOperationException(collectionName, guid, "Unable to parse json" + e.getMessage());
        }
        try {
            collection.insert(dbObject);
        } catch (DuplicateKeyException e) {
            throw new RecordExistsException(collectionName, guid);
        } catch (MongoException e) {
            DatabaseConfig.getLogger().log(Level.FINE, "{0} insert failed: {1}", new Object[] { dbName, e.getMessage() });
            throw new FailedDBOperationException(collectionName, dbObject.toString(), "Original mongo exception:" + e.getMessage());
        }
    } finally {
        db.requestDone();
    }
}
Also used : RecordExistsException(edu.umass.cs.gnscommon.exceptions.server.RecordExistsException) DBCollection(com.mongodb.DBCollection) MongoException(com.mongodb.MongoException) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DuplicateKeyException(com.mongodb.DuplicateKeyException) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) RecordExistsException(edu.umass.cs.gnscommon.exceptions.server.RecordExistsException) JSONException(org.json.JSONException) MongoException(com.mongodb.MongoException) UnknownHostException(java.net.UnknownHostException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) DuplicateKeyException(com.mongodb.DuplicateKeyException)

Example 7 with DuplicateKeyException

use of com.mongodb.DuplicateKeyException in project jackrabbit-oak by apache.

the class MongoUtilsTest method documentStoreExceptionType.

@Test
public void documentStoreExceptionType() {
    assertEquals(GENERIC, getDocumentStoreExceptionTypeFor(new IOException()));
    assertEquals(GENERIC, getDocumentStoreExceptionTypeFor(new MongoException("message")));
    assertEquals(GENERIC, getDocumentStoreExceptionTypeFor(newMongoCommandException(42)));
    assertEquals(GENERIC, getDocumentStoreExceptionTypeFor(new DuplicateKeyException(response(11000), new ServerAddress(), null)));
    assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(newWriteConcernException(11600)));
    assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(newWriteConcernException(11601)));
    assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(newWriteConcernException(11602)));
    assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(newMongoCommandException(11600)));
    assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(newMongoCommandException(11601)));
    assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(newMongoCommandException(11602)));
    assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(new MongoSocketException("message", new ServerAddress())));
}
Also used : MongoException(com.mongodb.MongoException) ServerAddress(com.mongodb.ServerAddress) MongoSocketException(com.mongodb.MongoSocketException) IOException(java.io.IOException) DuplicateKeyException(com.mongodb.DuplicateKeyException) Test(org.junit.Test)

Example 8 with DuplicateKeyException

use of com.mongodb.DuplicateKeyException in project graylog2-server by Graylog2.

the class IndexSetsResource method save.

@POST
@Timed
@ApiOperation(value = "Create index set")
@RequiresPermissions(RestPermissions.INDEXSETS_CREATE)
@Consumes(MediaType.APPLICATION_JSON)
@AuditEvent(type = AuditEventTypes.INDEX_SET_CREATE)
@ApiResponses(value = { @ApiResponse(code = 403, message = "Unauthorized") })
public IndexSetSummary save(@ApiParam(name = "Index set configuration", required = true) @Valid @NotNull IndexSetSummary indexSet) {
    try {
        final IndexSetConfig indexSetConfig = indexSet.toIndexSetConfig(true);
        final Optional<IndexSetValidator.Violation> violation = indexSetValidator.validate(indexSetConfig);
        if (violation.isPresent()) {
            throw new BadRequestException(violation.get().message());
        }
        final IndexSetConfig savedObject = indexSetService.save(indexSetConfig);
        final IndexSetConfig defaultIndexSet = indexSetService.getDefault();
        return IndexSetSummary.fromIndexSetConfig(savedObject, savedObject.equals(defaultIndexSet));
    } catch (DuplicateKeyException e) {
        throw new BadRequestException(e.getMessage());
    }
}
Also used : IndexSetConfig(org.graylog2.indexer.indexset.IndexSetConfig) DefaultIndexSetConfig(org.graylog2.indexer.indexset.DefaultIndexSetConfig) BadRequestException(javax.ws.rs.BadRequestException) DuplicateKeyException(com.mongodb.DuplicateKeyException) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) ApiResponses(io.swagger.annotations.ApiResponses)

Example 9 with DuplicateKeyException

use of com.mongodb.DuplicateKeyException in project graylog2-server by Graylog2.

the class AccessTokenServiceImpl method create.

@Override
public AccessToken create(String username, String name) {
    Map<String, Object> fields = Maps.newHashMap();
    AccessTokenImpl accessToken;
    String id = null;
    int iterations = 0;
    // this loop should never have a collision, but we will try up to 10 times anyway.
    do {
        // 256 bits of randomness should be plenty hard to guess, no?
        final String token = new BigInteger(256, RANDOM).toString(32);
        fields.put(AccessTokenImpl.TOKEN, token);
        fields.put(AccessTokenImpl.USERNAME, username);
        fields.put(AccessTokenImpl.NAME, name);
        // aka never.
        fields.put(AccessTokenImpl.LAST_ACCESS, Tools.dateTimeFromDouble(0));
        accessToken = new AccessTokenImpl(fields);
        try {
            id = saveWithoutValidation(encrypt(accessToken));
        } catch (DuplicateKeyException ignore) {
        }
    } while (iterations++ < 10 && id == null);
    if (id == null) {
        throw new IllegalStateException("Could not create unique access token, tried 10 times. This is bad.");
    }
    return accessToken;
}
Also used : BigInteger(java.math.BigInteger) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) DuplicateKeyException(com.mongodb.DuplicateKeyException)

Aggregations

DuplicateKeyException (com.mongodb.DuplicateKeyException)9 MongoException (com.mongodb.MongoException)4 BasicDBObject (com.mongodb.BasicDBObject)2 DBObject (com.mongodb.DBObject)2 ApiOperation (io.swagger.annotations.ApiOperation)2 IOException (java.io.IOException)2 BadRequestException (javax.ws.rs.BadRequestException)2 POST (javax.ws.rs.POST)2 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)2 AuditEvent (org.graylog2.audit.jersey.AuditEvent)2 IndexSetConfig (org.graylog2.indexer.indexset.IndexSetConfig)2 Timed (com.codahale.metrics.annotation.Timed)1 DBCollection (com.mongodb.DBCollection)1 MongoSocketException (com.mongodb.MongoSocketException)1 ServerAddress (com.mongodb.ServerAddress)1 WriteConcernException (com.mongodb.WriteConcernException)1 IndexOptions (com.mongodb.client.model.IndexOptions)1 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)1 RecordExistsException (edu.umass.cs.gnscommon.exceptions.server.RecordExistsException)1 RecordNotFoundException (edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException)1