Search in sources :

Example 86 with JsonParseException

use of org.apache.flink.shaded.jackson2.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)

Example 87 with JsonParseException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParseException in project carbon-apimgt by wso2.

the class JSONAnalyzer method analyze.

/**
 * @param payload json payload
 * @throws APIMThreatAnalyzerException if defined limits for json payload exceeds
 */
@Override
public void analyze(String payload, String apiContext) throws APIMThreatAnalyzerException {
    try (JsonParser parser = factory.createParser(new StringReader(payload))) {
        int currentDepth = 0;
        int currentFieldCount = 0;
        JsonToken token;
        while ((token = parser.nextToken()) != null) {
            switch(token) {
                case START_OBJECT:
                    currentDepth += 1;
                    try {
                        analyzeDepth(maxJsonDepth, currentDepth, apiContext);
                    } catch (APIMThreatAnalyzerException e) {
                        throw e;
                    }
                    break;
                case END_OBJECT:
                    currentDepth -= 1;
                    break;
                case FIELD_NAME:
                    currentFieldCount += 1;
                    String name = parser.getCurrentName();
                    try {
                        analyzeField(name, maxFieldCount, currentFieldCount, maxFieldLength, apiContext);
                    } catch (APIMThreatAnalyzerException e) {
                        throw e;
                    }
                    break;
                case VALUE_STRING:
                    String value = parser.getText();
                    try {
                        analyzeString(value, maxStringLength, apiContext);
                    } catch (APIMThreatAnalyzerException e) {
                        throw e;
                    }
                    break;
                case START_ARRAY:
                    try {
                        analyzeArray(parser, maxArrayElementCount, maxStringLength, apiContext);
                    } catch (APIMThreatAnalyzerException e) {
                        throw e;
                    }
                    break;
            }
        }
    } catch (JsonParseException e) {
        logger.error(JSON_THREAT_PROTECTION_MSG_PREFIX + apiContext + " - Payload parsing failed", e);
        throw new APIMThreatAnalyzerException(JSON_THREAT_PROTECTION_MSG_PREFIX + apiContext + " - Payload parsing failed", e);
    } catch (IOException e) {
        logger.error(JSON_THREAT_PROTECTION_MSG_PREFIX + apiContext + " - Payload build failed", e);
        throw new APIMThreatAnalyzerException(JSON_THREAT_PROTECTION_MSG_PREFIX + apiContext + " - Payload build failed", e);
    }
}
Also used : StringReader(java.io.StringReader) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) APIMThreatAnalyzerException(org.wso2.carbon.apimgt.ballerina.threatprotection.APIMThreatAnalyzerException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 88 with JsonParseException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParseException in project portal by ixinportal.

the class AuthService method getToken.

// @Autowired
// private static RealNameAuthenticationSerivceImpl authenticationSerivceImpl = new RealNameAuthenticationSerivceImpl();
/**
 * 获得token
 * 访问其他接口的时候 在头信息增加 Authorization :bear+token bear和token之间加个空格
 */
private static String getToken() {
    // client_secret:43b484748d3a936330bc50da70d6ce69e1dfef90
    try {
        // RealNameAuthentication realNameAuthentication =  authenticationSerivceImpl.getRealNameAuthenticationExample(new RealNameAuthenticationExample());
        Long nowDate = System.currentTimeMillis();
        if (ACCESS_TOKEN != null && new Date(nowDate + inDateTime).before(inDate)) {
            return ACCESS_TOKEN;
        }
        List<RealNameAuthentication> list = sqlSession.selectList("com.itrus.portal.db.RealNameAuthenticationMapper.selectByExample", new RealNameAuthenticationExample());
        if (list == null || list.isEmpty()) {
            return null;
        }
        RealNameAuthentication realNameAuthentication = null;
        for (RealNameAuthentication nameAuthentication : list) {
            if (nameAuthentication.getType() == 1)
                realNameAuthentication = nameAuthentication;
        }
        if (realNameAuthentication == null) {
            return null;
        }
        Map params = new HashMap();
        params.put("grant_type", "client_credentials");
        params.put("client_id", realNameAuthentication.getIdCode());
        params.put("client_secret", realNameAuthentication.getKeyCode());
        String rep = HttpClientUtil.postForm(realNameAuthentication.getAccessTokenaddress() + TOKEN, null, params);
        JSONObject data = JSON.parseObject(rep);
        ACCESS_TOKEN = data.getString("access_token");
        inDate = new Date(nowDate + data.getInteger("expires_in") * 1000);
        return ACCESS_TOKEN;
    } catch (RestClientException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : IOException(java.io.IOException) RealNameAuthentication(com.itrus.portal.db.RealNameAuthentication) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) RestClientException(org.springframework.web.client.RestClientException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) RealNameAuthenticationExample(com.itrus.portal.db.RealNameAuthenticationExample) JSONObject(com.alibaba.fastjson.JSONObject) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) RestClientException(org.springframework.web.client.RestClientException)

Example 89 with JsonParseException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParseException in project portal by ixinportal.

the class WeixinUtil method getJsApiTicketFromWeixin.

public void getJsApiTicketFromWeixin() {
    synchronized (jsapitLock) {
        String tokenUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={accToken}&type=jsapi";
        try {
            Long nowDate = System.currentTimeMillis();
            if (JS_API_TICKET != null && new Date(nowDate + inDateTime).before(JS_API_TICKET.getInDate()))
                return;
            if (ACCESS_TOKEN == null || new Date(nowDate + inDateTime).after(ACCESS_TOKEN.getInDate()))
                getAccTokenFromWeixin();
            ByteArrayResource retRes = restTemplate.getForObject(tokenUrl, ByteArrayResource.class, ACCESS_TOKEN.accessToken);
            Map<String, Object> ticketMap = jsonTool.readValue(new String(retRes.getByteArray()), Map.class);
            if (ticketMap == null || ticketMap.isEmpty()) {
                log.error("微信获取ticket失败,返回为空");
                return;
            }
            Integer ei = (Integer) ticketMap.get("expires_in");
            Date inDate = new Date(nowDate + ei * 1000);
            JsApiTicket TICKET_TMP = new JsApiTicket((String) ticketMap.get("ticket"), inDate);
            JS_API_TICKET = TICKET_TMP;
        } catch (RestClientException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) RestClientException(org.springframework.web.client.RestClientException) ByteArrayResource(org.springframework.core.io.ByteArrayResource) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) Date(java.util.Date)

Example 90 with JsonParseException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParseException in project drill by axbaretto.

the class JSONRecordReader method handleAndRaise.

protected void handleAndRaise(String suffix, Exception e) throws UserException {
    String message = e.getMessage();
    int columnNr = -1;
    if (e instanceof JsonParseException) {
        final JsonParseException ex = (JsonParseException) e;
        message = ex.getOriginalMessage();
        columnNr = ex.getLocation().getColumnNr();
    }
    UserException.Builder exceptionBuilder = UserException.dataReadError(e).message("%s - %s", suffix, message);
    if (columnNr > 0) {
        exceptionBuilder.pushContext("Column ", columnNr);
    }
    if (hadoopPath != null) {
        exceptionBuilder.pushContext("Record ", currentRecordNumberInFile()).pushContext("File ", hadoopPath.toUri().getPath());
    }
    throw exceptionBuilder.build(logger);
}
Also used : UserException(org.apache.drill.common.exceptions.UserException) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Aggregations

JsonParseException (com.fasterxml.jackson.core.JsonParseException)145 IOException (java.io.IOException)75 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)58 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)36 JsonParser (com.fasterxml.jackson.core.JsonParser)23 JsonNode (com.fasterxml.jackson.databind.JsonNode)20 Map (java.util.Map)19 JsonToken (com.fasterxml.jackson.core.JsonToken)15 InputStream (java.io.InputStream)15 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)14 HashMap (java.util.HashMap)12 File (java.io.File)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