Search in sources :

Example 81 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project kie-wb-common by kiegroup.

the class Bpmn2JsonUnmarshaller method unmarshall.

/**
 * NOTE: This method has been set protected for Stunner support. Stunner bpmn implementation provides a custom JsonParser that
 * is used instead of the one used in jbpm-designer-backend.
 * <p>
 * Start unmarshalling using the parser.
 * @param parser
 * @param preProcessingData
 * @return the root element of a bpmn2 document.
 * @throws JsonParseException
 * @throws IOException
 */
protected Bpmn2Resource unmarshall(JsonParser parser, String preProcessingData) throws JsonParseException, IOException {
    try {
        // open the object
        parser.nextToken();
        ResourceSet rSet = new ResourceSetImpl();
        rSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("bpmn2", new JBPMBpmn2ResourceFactoryImpl());
        Bpmn2Resource bpmn2 = (Bpmn2Resource) rSet.createResource(URI.createURI("virtual.bpmn2"));
        rSet.getResources().add(bpmn2);
        _currentResource = bpmn2;
        if (preProcessingData == null || preProcessingData.length() < 1) {
            preProcessingData = "ReadOnlyService";
        }
        // do the unmarshalling now:
        Definitions def = (Definitions) unmarshallItem(parser, preProcessingData);
        def.setExporter("jBPM Designer");
        def.setExporterVersion("6.2.0");
        revisitUserTasks(def);
        revisitServiceTasks(def);
        revisitMessages(def);
        revisitCatchEvents(def);
        revisitThrowEvents(def);
        revisitLanes(def);
        revisitSubProcessItemDefs(def);
        revisitArtifacts(def);
        revisitGroups(def);
        revisitTaskAssociations(def);
        revisitSendReceiveTasks(def);
        reconnectFlows();
        revisitGateways(def);
        revisitCatchEventsConvertToBoundary(def);
        revisitBoundaryEventsPositions(def);
        createDiagram(def);
        updateIDs(def);
        revisitDataObjects(def);
        revisitAssociationsIoSpec(def);
        revisitWsdlImports(def);
        revisitMultiInstanceTasks(def);
        addSimulation(def);
        revisitItemDefinitions(def);
        revisitProcessDoc(def);
        revisitDI(def);
        revisitSignalRef(def);
        orderDiagramElements(def);
        // return def;
        _currentResource.getContents().add(def);
        return _currentResource;
    } catch (Exception e) {
        _logger.error(e.getMessage());
        return _currentResource;
    } finally {
        parser.close();
        _objMap.clear();
        _idMap.clear();
        _outgoingFlows.clear();
        _sequenceFlowTargets.clear();
        _bounds.clear();
        _currentResource = null;
    }
}
Also used : ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) Bpmn2Resource(org.eclipse.bpmn2.util.Bpmn2Resource) Definitions(org.eclipse.bpmn2.Definitions) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) JBPMBpmn2ResourceFactoryImpl(org.kie.workbench.common.stunner.bpmn.backend.legacy.resource.JBPMBpmn2ResourceFactoryImpl) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 82 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project kie-wb-common by kiegroup.

the class DefaultProfileImpl method createMarshaller.

public IDiagramMarshaller createMarshaller() {
    return new IDiagramMarshaller() {

        public String parseModel(String jsonModel, String preProcessingData) {
            Bpmn2JsonUnmarshaller unmarshaller = new Bpmn2JsonUnmarshaller();
            // Definitions def;
            Resource res;
            try {
                res = unmarshaller.unmarshall(jsonModel, preProcessingData);
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                Map saveMap = new HashMap();
                saveMap.put(XMLResource.OPTION_ENCODING, "UTF-8");
                saveMap.put(XMLResource.OPTION_DEFER_IDREF_RESOLUTION, true);
                saveMap.put(XMLResource.OPTION_DISABLE_NOTIFY, true);
                saveMap.put(XMLResource.OPTION_PROCESS_DANGLING_HREF, XMLResource.OPTION_PROCESS_DANGLING_HREF_RECORD);
                res.save(outputStream, saveMap);
                return outputStream.toString();
            } catch (JsonParseException e) {
                _logger.error(e.getMessage(), e);
            } catch (IOException e) {
                _logger.error(e.getMessage(), e);
            }
            return "";
        }

        public Definitions getDefinitions(String jsonModel, String preProcessingData) {
            try {
                Bpmn2JsonUnmarshaller unmarshaller = new Bpmn2JsonUnmarshaller();
                JBPMBpmn2ResourceImpl res = (JBPMBpmn2ResourceImpl) unmarshaller.unmarshall(jsonModel, preProcessingData);
                return (Definitions) res.getContents().get(0);
            } catch (JsonParseException e) {
                _logger.error(e.getMessage(), e);
            } catch (IOException e) {
                _logger.error(e.getMessage(), e);
            }
            return null;
        }

        public Resource getResource(String jsonModel, String preProcessingData) {
            try {
                Bpmn2JsonUnmarshaller unmarshaller = new Bpmn2JsonUnmarshaller();
                return (JBPMBpmn2ResourceImpl) unmarshaller.unmarshall(jsonModel, preProcessingData);
            } catch (JsonParseException e) {
                _logger.error(e.getMessage(), e);
            } catch (IOException e) {
                _logger.error(e.getMessage(), e);
            }
            return null;
        }
    };
}
Also used : Bpmn2JsonUnmarshaller(org.kie.workbench.common.stunner.bpmn.backend.legacy.Bpmn2JsonUnmarshaller) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JBPMBpmn2ResourceImpl(org.kie.workbench.common.stunner.bpmn.backend.legacy.resource.JBPMBpmn2ResourceImpl) Definitions(org.eclipse.bpmn2.Definitions) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) Resource(org.eclipse.emf.ecore.resource.Resource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 83 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project bboss-elastic by bbossgroups.

the class ContentBuilderUtil method addComplexField.

public static void addComplexField(XContentBuilder builder, String fieldName, XContentType contentType, byte[] data) throws IOException {
    XContentParser parser = null;
    try {
        // Elasticsearch will accept JSON directly but we need to validate that
        // the incoming event is JSON first. Sadly, the elasticsearch JSON parser
        // is a stream parser so we need to instantiate it, parse the event to
        // validate it, then instantiate it again to provide the JSON to
        // elasticsearch.
        // If validation fails then the incoming event is submitted to
        // elasticsearch as plain text.
        parser = XContentFactory.xContent(contentType).createParser(NamedXContentRegistry.EMPTY, data);
        while (parser.nextToken() != null) {
        }
        ;
        // If the JSON is valid then include it
        try {
            parser = XContentFactory.xContent(contentType).createParser(NamedXContentRegistry.EMPTY, data);
            // Add the field name, but not the value.
            builder.field(fieldName);
            // This will add the whole parsed content as the value of the field.
            builder.copyCurrentStructure(parser);
        } finally {
            if (parser != null) {
                parser.close();
            }
        }
    } catch (JsonParseException ex) {
        // If we get an exception here the most likely cause is nested JSON that
        // can't be figured out in the body. At this point just push it through
        // as is
        addSimpleField(builder, fieldName, data);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
Also used : JsonParseException(com.fasterxml.jackson.core.JsonParseException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 84 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project nifi by apache.

the class SiteToSiteRestApiClient method readResponse.

private TransactionResultEntity readResponse(final InputStream inputStream) throws IOException {
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    StreamUtils.copy(inputStream, bos);
    String responseMessage = null;
    try {
        responseMessage = new String(bos.toByteArray(), "UTF-8");
        logger.debug("readResponse responseMessage={}", responseMessage);
        final ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(responseMessage, TransactionResultEntity.class);
    } catch (JsonParseException | JsonMappingException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to parse JSON.", e);
        }
        final TransactionResultEntity entity = new TransactionResultEntity();
        entity.setResponseCode(ResponseCode.ABORT.getCode());
        entity.setMessage(responseMessage);
        return entity;
    }
}
Also used : TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 85 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project IPK-BrAPI-Validator by plantbreeding.

the class TestItemRunner method schemaMatch.

/**
 * Check if response matches schema
 *
 * @param p Path to the schema to be tested
 * @return TestItemReport
 */
private TestExecReport schemaMatch(String p) {
    LOGGER.info("Testing Schema");
    TestExecReport tr = new TestExecReport("Json matches schema: " + p, false);
    tr.setType("schema mismatch");
    tr.setSchema(p);
    try {
        String jsonString = vr.extract().response().asString();
        SchemaValidator schemaValidator = new SchemaValidator();
        ProcessingReport r = schemaValidator.validate(p, jsonString);
        if (r.isSuccess()) {
            LOGGER.info("Schema Test Passed");
            tr.addMessage("Response structure matches schema.");
            tr.setPassed(true);
        } else {
            LOGGER.info("Schema Test Failed");
            tr.addMessage("Response structure doesn't match schema.");
            r.forEach(message -> tr.addError(message.asJson()));
        }
        return tr;
    } catch (ConnectionClosedException | JsonParseException e1) {
        LOGGER.info("Invalid response");
        LOGGER.info("== cause ==");
        LOGGER.info(e1.getMessage());
        tr.addMessage("Server response is not valid JSON.");
        return tr;
    } catch (AssertionError | IOException | ProcessingException e1) {
        LOGGER.info("Doesn't match schema");
        LOGGER.info("== cause ==");
        LOGGER.info(e1.getMessage());
        tr.addMessage(e1.getMessage());
        return tr;
    }
}
Also used : TestExecReport(de.ipk_gatersleben.bit.bi.bridge.brapicomp.testing.reports.TestExecReport) ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) ConnectionClosedException(org.apache.http.ConnectionClosedException) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException)

Aggregations

JsonParseException (com.fasterxml.jackson.core.JsonParseException)143 IOException (java.io.IOException)73 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)58 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)35 JsonParser (com.fasterxml.jackson.core.JsonParser)23 JsonNode (com.fasterxml.jackson.databind.JsonNode)18 Map (java.util.Map)18 JsonToken (com.fasterxml.jackson.core.JsonToken)15 InputStream (java.io.InputStream)15 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)14 File (java.io.File)11 HashMap (java.util.HashMap)11 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 JsonFactory (com.fasterxml.jackson.core.JsonFactory)7 JsonLocation (com.fasterxml.jackson.core.JsonLocation)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 InputStreamReader (java.io.InputStreamReader)5 Date (java.util.Date)5 GZIPInputStream (java.util.zip.GZIPInputStream)5