use of org.ektorp.DocumentNotFoundException in project pac4j by pac4j.
the class CouchProfileService method update.
@Override
protected void update(final Map<String, Object> attributes) {
final String id = (String) attributes.get(COUCH_ID);
try {
final InputStream oldDocStream = couchDbConnector.getAsStream(id);
final Map<String, Object> res = objectMapper.readValue(oldDocStream, typeRef);
res.putAll(attributes);
couchDbConnector.update(res);
logger.debug("Updating id: {} with attributes: {}", id, attributes);
} catch (DocumentNotFoundException e) {
logger.debug("Insert doc (not found by update(): {}", attributes);
couchDbConnector.create(attributes);
} catch (IOException e) {
logger.error("Unexpected IO CouchDB Exception", e);
}
}
use of org.ektorp.DocumentNotFoundException in project sw360portal by sw360.
the class AttachmentStreamConnectorTest method testGetConcatenatedStreamReadThrowsOnNonExistent.
@Test
public void testGetConcatenatedStreamReadThrowsOnNonExistent() throws Exception {
AttachmentContent attachment = mock(AttachmentContent.class);
when(attachment.isOnlyRemote()).thenReturn(false);
when(attachment.isSetPartsCount()).thenReturn(true);
when(attachment.getPartsCount()).thenReturn("2");
when(attachment.getFilename()).thenReturn("fil");
String attachmentId = "id";
when(attachment.getId()).thenReturn(attachmentId);
AttachmentInputStream part1 = mock(AttachmentInputStream.class);
when(connector.getAttachment(attachmentId, "fil_part1")).thenReturn(part1);
when(connector.getAttachment(attachmentId, "fil_part2")).thenThrow(new DocumentNotFoundException(""));
when(part1.read()).thenReturn(1, -1);
InputStream attachmentStream = attachmentStreamConnector.getAttachmentStream(attachment, dummyUser, new Project().setVisbility(Visibility.ME_AND_MODERATORS).setCreatedBy(dummyUser.getEmail()).setAttachments(Collections.singleton(new Attachment().setAttachmentContentId(attachmentId))));
assertThat(attachmentStream.read(), is(1));
try {
assertThat(attachmentStream.read(), is(2));
fail("expected Exception not thrown");
} catch (IOException ignored) {
}
verify(part1).close();
}
use of org.ektorp.DocumentNotFoundException in project pac4j by pac4j.
the class CouchProfileService method deleteById.
@Override
protected void deleteById(final String id) {
logger.debug("Delete id: {}", id);
try {
final InputStream oldDocStream = couchDbConnector.getAsStream(id);
final JsonNode oldDoc = objectMapper.readTree(oldDocStream);
final String rev = oldDoc.get("_rev").asText();
couchDbConnector.delete(id, rev);
} catch (DocumentNotFoundException e) {
logger.debug("id {} is not in the database", id);
} catch (IOException e) {
logger.error("Unexpected IO CouchDB Exception", e);
}
}
use of org.ektorp.DocumentNotFoundException in project pac4j by pac4j.
the class CouchProfileService method read.
@Override
protected List<Map<String, Object>> read(final List<String> names, final String key, final String value) {
logger.debug("Reading key / value: {} / {}", key, value);
final List<Map<String, Object>> listAttributes = new ArrayList<>();
if (key.equals(COUCH_ID)) {
try {
final InputStream oldDocStream = couchDbConnector.getAsStream(value);
final Map<String, Object> res = objectMapper.readValue(oldDocStream, typeRef);
listAttributes.add(populateAttributes(res, names));
} catch (DocumentNotFoundException e) {
logger.debug("Document id {} not found", value);
} catch (IOException e) {
logger.error("Unexpected IO CouchDB Exception", e);
}
} else {
// supposes a by_$key view in the design document, see documentation
final ViewQuery query = new ViewQuery().designDocId("_design/pac4j").viewName("by_" + key).key(value);
final ViewResult result = couchDbConnector.queryView(query);
for (ViewResult.Row row : result.getRows()) {
final String stringValue = row.getValue();
Map<String, Object> res = null;
try {
res = objectMapper.readValue(stringValue, typeRef);
listAttributes.add(populateAttributes(res, names));
} catch (IOException e) {
logger.error("Unexpected IO CouchDB Exception", e);
}
}
}
logger.debug("Found: {}", listAttributes);
return listAttributes;
}
Aggregations