Search in sources :

Example 1 with DocumentNotFoundException

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);
    }
}
Also used : DocumentNotFoundException(org.ektorp.DocumentNotFoundException) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 2 with DocumentNotFoundException

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();
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project) DocumentNotFoundException(org.ektorp.DocumentNotFoundException) AttachmentInputStream(org.ektorp.AttachmentInputStream) InputStream(java.io.InputStream) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) AttachmentInputStream(org.ektorp.AttachmentInputStream) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with DocumentNotFoundException

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);
    }
}
Also used : DocumentNotFoundException(org.ektorp.DocumentNotFoundException) InputStream(java.io.InputStream) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 4 with DocumentNotFoundException

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;
}
Also used : DocumentNotFoundException(org.ektorp.DocumentNotFoundException) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ViewQuery(org.ektorp.ViewQuery) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) ViewResult(org.ektorp.ViewResult)

Aggregations

IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 DocumentNotFoundException (org.ektorp.DocumentNotFoundException)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)1 AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)1 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)1 AttachmentInputStream (org.ektorp.AttachmentInputStream)1 ViewQuery (org.ektorp.ViewQuery)1 ViewResult (org.ektorp.ViewResult)1 Test (org.junit.Test)1