Search in sources :

Example 86 with JsonParseException

use of 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 87 with JsonParseException

use of 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 88 with JsonParseException

use of 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 89 with JsonParseException

use of 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)

Example 90 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project vespa by vespa-engine.

the class VespaRecordWriter method findDocId.

private String findDocId(String json) throws IOException {
    JsonFactory factory = new JsonFactory();
    try (JsonParser parser = factory.createParser(json)) {
        if (parser.nextToken() != JsonToken.START_OBJECT) {
            return null;
        }
        while (parser.nextToken() != JsonToken.END_OBJECT) {
            String fieldName = parser.getCurrentName();
            parser.nextToken();
            if (VespaDocumentOperation.Operation.valid(fieldName)) {
                String docId = parser.getText();
                return docId;
            } else {
                parser.skipChildren();
            }
        }
    } catch (JsonParseException ex) {
        return null;
    }
    return null;
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParser(com.fasterxml.jackson.core.JsonParser)

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