Search in sources :

Example 1 with JacksonException

use of com.fasterxml.jackson.core.JacksonException in project ksan by infinistor.

the class DeleteObjects method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_DELETE_OBJECTS_START);
    String bucket = s3Parameter.getBucketName();
    initBucketInfo(bucket);
    S3Bucket s3Bucket = new S3Bucket();
    s3Bucket.setCors(getBucketInfo().getCors());
    s3Bucket.setAccess(getBucketInfo().getAccess());
    s3Parameter.setBucket(s3Bucket);
    GWUtils.checkCors(s3Parameter);
    if (s3Parameter.isPublicAccess() && GWUtils.isIgnorePublicAcls(s3Parameter)) {
        throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
    }
    checkGrantBucketOwner(s3Parameter.isPublicAccess(), String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_WRITE);
    DataDeleteObjects dataDeleteObjects = new DataDeleteObjects(s3Parameter);
    dataDeleteObjects.extract();
    String deleteXml = dataDeleteObjects.getDeleteXml();
    XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
    try {
        DeleteMultipleObjectsRequest deleteMultipleObjectsRequest = new XmlMapper().readValue(deleteXml, DeleteMultipleObjectsRequest.class);
        Collection<Objects> objectNames = new ArrayList<>();
        if (deleteMultipleObjectsRequest.objects != null) {
            for (DeleteMultipleObjectsRequest.S3Object s3Object : deleteMultipleObjectsRequest.objects) {
                Objects object = new Objects();
                object.objectName = s3Object.key;
                if (Strings.isNullOrEmpty(s3Object.versionId)) {
                    object.versionId = GWConstants.VERSIONING_DISABLE_TAIL;
                } else if (GWConstants.VERSIONING_DISABLE_TAIL.equalsIgnoreCase(s3Object.versionId)) {
                    object.versionId = GWConstants.VERSIONING_DISABLE_TAIL;
                } else {
                    object.versionId = s3Object.versionId;
                }
                objectNames.add(object);
            }
        }
        Writer writer = s3Parameter.getResponse().getWriter();
        s3Parameter.getResponse().setContentType(GWConstants.XML_CONTENT_TYPE);
        XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(writer);
        xmlStreamWriter.writeStartDocument();
        xmlStreamWriter.writeStartElement(GWConstants.DELETE_RESULT);
        xmlStreamWriter.writeDefaultNamespace(GWConstants.AWS_XMLNS);
        logger.debug(GWConstants.LOG_DELETE_OBJECTS_SIZE, objectNames.size());
        for (Objects object : objectNames) {
            deleteObject(s3Parameter, object.objectName, object.versionId, xmlStreamWriter, deleteMultipleObjectsRequest.quiet);
        // xmlStreamWriter.flush(); // In Tomcat, if you use flush(), you lose connection. jakarta, need to check
        }
        xmlStreamWriter.writeEndElement();
        xmlStreamWriter.flush();
    } catch (JsonParseException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    } catch (JacksonException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    } catch (IOException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    } catch (XMLStreamException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
}
Also used : JacksonException(com.fasterxml.jackson.core.JacksonException) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) DataDeleteObjects(com.pspace.ifs.ksan.gw.data.DataDeleteObjects) ArrayList(java.util.ArrayList) DeleteMultipleObjectsRequest(com.pspace.ifs.ksan.gw.format.DeleteMultipleObjectsRequest) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) DataDeleteObjects(com.pspace.ifs.ksan.gw.data.DataDeleteObjects) Objects(com.pspace.ifs.ksan.gw.format.Objects) GWException(com.pspace.ifs.ksan.gw.exception.GWException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Writer(java.io.Writer)

Example 2 with JacksonException

use of com.fasterxml.jackson.core.JacksonException in project briar by briar.

the class MailboxApiImpl method getContacts.

@Override
public Collection<ContactId> getContacts(MailboxProperties properties) throws IOException, ApiException {
    if (!properties.isOwner())
        throw new IllegalArgumentException();
    Response response = sendGetRequest(properties, "/contacts");
    if (response.code() != 200)
        throw new ApiException();
    ResponseBody body = response.body();
    if (body == null)
        throw new ApiException();
    try {
        JsonNode node = mapper.readTree(body.string());
        ArrayNode contactsNode = getArray(node, "contacts");
        List<ContactId> list = new ArrayList<>();
        for (JsonNode contactNode : contactsNode) {
            if (!contactNode.isNumber())
                throw new ApiException();
            int id = contactNode.intValue();
            if (id < 1)
                throw new ApiException();
            list.add(new ContactId(id));
        }
        return list;
    } catch (JacksonException e) {
        throw new ApiException();
    }
}
Also used : Response(okhttp3.Response) JacksonException(com.fasterxml.jackson.core.JacksonException) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ContactId(org.briarproject.bramble.api.contact.ContactId) ResponseBody(okhttp3.ResponseBody)

Example 3 with JacksonException

use of com.fasterxml.jackson.core.JacksonException in project briar by briar.

the class MailboxApiImpl method getFiles.

@Override
public List<MailboxFile> getFiles(MailboxProperties properties, MailboxFolderId folderId) throws IOException, ApiException {
    String path = "/files/" + folderId;
    Response response = sendGetRequest(properties, path);
    if (response.code() != 200)
        throw new ApiException();
    ResponseBody body = response.body();
    if (body == null)
        throw new ApiException();
    try {
        JsonNode node = mapper.readTree(body.string());
        ArrayNode filesNode = getArray(node, "files");
        List<MailboxFile> list = new ArrayList<>();
        for (JsonNode fileNode : filesNode) {
            if (!fileNode.isObject())
                throw new ApiException();
            ObjectNode objectNode = (ObjectNode) fileNode;
            JsonNode nameNode = objectNode.get("name");
            JsonNode timeNode = objectNode.get("time");
            if (nameNode == null || !nameNode.isTextual()) {
                throw new ApiException();
            }
            if (timeNode == null || !timeNode.isNumber()) {
                throw new ApiException();
            }
            String name = nameNode.asText();
            long time = timeNode.asLong();
            if (time < 1)
                throw new ApiException();
            list.add(new MailboxFile(MailboxFileId.fromString(name), time));
        }
        Collections.sort(list);
        return list;
    } catch (JacksonException | InvalidMailboxIdException e) {
        throw new ApiException();
    }
}
Also used : JacksonException(com.fasterxml.jackson.core.JacksonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ResponseBody(okhttp3.ResponseBody) InvalidMailboxIdException(org.briarproject.bramble.api.mailbox.InvalidMailboxIdException) Response(okhttp3.Response) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 4 with JacksonException

use of com.fasterxml.jackson.core.JacksonException in project briar by briar.

the class MailboxApiImpl method getFolders.

@Override
public List<MailboxFolderId> getFolders(MailboxProperties properties) throws IOException, ApiException {
    if (!properties.isOwner())
        throw new IllegalArgumentException();
    Response response = sendGetRequest(properties, "/folders");
    if (response.code() != 200)
        throw new ApiException();
    ResponseBody body = response.body();
    if (body == null)
        throw new ApiException();
    try {
        JsonNode node = mapper.readTree(body.string());
        ArrayNode filesNode = getArray(node, "folders");
        List<MailboxFolderId> list = new ArrayList<>();
        for (JsonNode fileNode : filesNode) {
            if (!fileNode.isObject())
                throw new ApiException();
            ObjectNode objectNode = (ObjectNode) fileNode;
            JsonNode idNode = objectNode.get("id");
            if (idNode == null || !idNode.isTextual()) {
                throw new ApiException();
            }
            String id = idNode.asText();
            list.add(MailboxFolderId.fromString(id));
        }
        return list;
    } catch (JacksonException | InvalidMailboxIdException e) {
        throw new ApiException();
    }
}
Also used : JacksonException(com.fasterxml.jackson.core.JacksonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ResponseBody(okhttp3.ResponseBody) MailboxFolderId(org.briarproject.bramble.api.mailbox.MailboxFolderId) InvalidMailboxIdException(org.briarproject.bramble.api.mailbox.InvalidMailboxIdException) Response(okhttp3.Response) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 5 with JacksonException

use of com.fasterxml.jackson.core.JacksonException in project tracdap by finos.

the class JsonDecoder method decodeChunk.

@Override
protected void decodeChunk(ByteBuf chunk) {
    try {
        var bytes = new byte[chunk.readableBytes()];
        chunk.readBytes(bytes);
        parser.feedInput(bytes, 0, bytes.length);
        JsonToken token;
        while ((token = parser.nextToken()) != JsonToken.NOT_AVAILABLE) parser.acceptToken(token);
    } catch (JacksonException e) {
        // This exception is a "well-behaved" parse failure, parse location and message should be meaningful
        var errorMessage = String.format("JSON decoding failed on line %d: %s", e.getLocation().getLineNr(), e.getOriginalMessage());
        log.error(errorMessage, e);
        throw new EDataCorruption(errorMessage, e);
    } catch (IOException e) {
        // Decoders work on a stream of buffers, "real" IO exceptions should not occur
        // IO exceptions here indicate parse failures, not file/socket communication errors
        // This is likely to be a more "badly-behaved" failure, or at least one that was not anticipated
        var errorMessage = "JSON decoding failed, content is garbled: " + e.getMessage();
        log.error(errorMessage, e);
        throw new EDataCorruption(errorMessage, e);
    } catch (Throwable e) {
        // Ensure unexpected errors are still reported to the Flow API
        log.error("Unexpected error during decoding", e);
        throw new EUnexpected(e);
    } finally {
        chunk.release();
    }
}
Also used : JacksonException(com.fasterxml.jackson.core.JacksonException) EDataCorruption(com.accenture.trac.common.exception.EDataCorruption) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) EUnexpected(com.accenture.trac.common.exception.EUnexpected)

Aggregations

JacksonException (com.fasterxml.jackson.core.JacksonException)17 IOException (java.io.IOException)9 JsonToken (com.fasterxml.jackson.core.JsonToken)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ArrayList (java.util.ArrayList)4 Response (okhttp3.Response)4 ResponseBody (okhttp3.ResponseBody)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 InvalidMailboxIdException (org.briarproject.bramble.api.mailbox.InvalidMailboxIdException)3 EDataCorruption (com.accenture.trac.common.exception.EDataCorruption)2 EUnexpected (com.accenture.trac.common.exception.EUnexpected)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)2 JsonParser (com.fasterxml.jackson.core.JsonParser)2 TreeNode (com.fasterxml.jackson.core.TreeNode)2 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)2 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)2 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 CsvFactory (com.fasterxml.jackson.dataformat.csv.CsvFactory)2