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();
}
}
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())));
}
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());
}
}
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;
}
Aggregations