Search in sources :

Example 96 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException 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 97 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException 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 98 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project ORCID-Source by ORCID.

the class OrcidJacksonJaxbJsonProviderPretty method readFrom.

/**
 * This adds a validation step when converting JSON into ORCID models.
 */
@Override
public Object readFrom(Class<Object> arg0, Type arg1, Annotation[] arg2, MediaType arg3, MultivaluedMap<String, String> arg4, InputStream arg5) throws IOException {
    Object o = null;
    try {
        o = super.readFrom(arg0, arg1, arg2, arg3, arg4, arg5);
    } catch (JsonMappingException e) {
        Map<String, String> params = new HashMap<>();
        params.put("error", e.getMessage());
        throw new InvalidJSONException(params);
    }
    if (jsonInputValidator.canValidate(o.getClass())) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        String apiVersion = (String) requestAttributes.getAttribute(ApiVersionFilter.API_VERSION_REQUEST_ATTRIBUTE_NAME, RequestAttributes.SCOPE_REQUEST);
        if (apiVersion != null && apiVersion.equals("2.1")) {
            jsonInputValidator.validate2_1APIJSONInput(o);
        } else {
            jsonInputValidator.validateJSONInput(o);
        }
    }
    return o;
}
Also used : InvalidJSONException(org.orcid.core.exception.InvalidJSONException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) RequestAttributes(org.springframework.web.context.request.RequestAttributes) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Map(java.util.Map)

Example 99 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project service-proxy by membrane.

the class EtcdResponse method getDirectories.

@SuppressWarnings("unchecked")
public ArrayList<String> getDirectories() {
    JsonParser par = getParser(body);
    String baseKey = originalRequest.baseKey;
    String module = originalRequest.module;
    ArrayList<String> directories = new ArrayList<String>();
    Map<String, Object> respData = null;
    try {
        respData = new ObjectMapper().readValue(par, Map.class);
    } catch (JsonParseException e) {
    } catch (JsonMappingException e) {
    } catch (IOException e) {
    }
    if (respData.containsKey("node")) {
        LinkedHashMap<String, Object> nodeJson = (LinkedHashMap<String, Object>) respData.get("node");
        if (nodeJson.containsKey("nodes")) {
            ArrayList<Object> nodesArray = (ArrayList<Object>) nodeJson.get("nodes");
            for (Object object : nodesArray) {
                LinkedHashMap<String, Object> dirs = (LinkedHashMap<String, Object>) object;
                if (dirs.containsKey("key")) {
                    String servicePath = dirs.get("key").toString();
                    String uuid = servicePath.replaceAll(baseKey + module, "");
                    directories.add(uuid);
                }
            }
        }
    }
    return directories;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) LinkedHashMap(java.util.LinkedHashMap) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 100 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project n4js by eclipse.

the class PingSessionResource method createEvent.

@Override
@SuppressWarnings("unchecked")
protected TestEvent createEvent(final String sessionId, final String body) throws ClientResourceException {
    if (isNullOrEmpty(body))
        throw new ClientResourceException(SC_BAD_REQUEST);
    final Map<?, ?> values = newHashMap();
    try {
        values.putAll(mapper.readValue(body, Map.class));
    } catch (JsonMappingException | JsonParseException e) {
        throw new ClientResourceException(SC_UNPROCESSABLE_ENTITY);
    } catch (final IOException e) {
        throw new ClientResourceException(SC_BAD_REQUEST);
    }
    final Object value = values.get(TIMEOUT_KEY);
    // incorrect schema
    if (null == value) {
        throw new ClientResourceException(SC_UNPROCESSABLE_ENTITY);
    }
    final Object comment = values.get(COMMENT_KEY);
    try {
        final long timeout = parseLong(Objects.toString(value));
        return new SessionPingedEvent(sessionId, timeout, null == comment ? null : valueOf(comment));
    } catch (final NumberFormatException e) {
        // although schema was valid the data was indeed invalid
        throw new ClientResourceException(SC_BAD_REQUEST);
    }
}
Also used : ClientResourceException(org.eclipse.n4js.tester.server.resources.ClientResourceException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) SessionPingedEvent(org.eclipse.n4js.tester.events.SessionPingedEvent) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Map(java.util.Map)

Aggregations

JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)140 IOException (java.io.IOException)68 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)54 JsonParseException (com.fasterxml.jackson.core.JsonParseException)52 Test (org.junit.Test)31 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)19 HashMap (java.util.HashMap)16 ArrayList (java.util.ArrayList)14 Map (java.util.Map)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)12 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)10 File (java.io.File)9 InputStream (java.io.InputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 List (java.util.List)7 Writer (org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer)7 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)6 Response (javax.ws.rs.core.Response)6 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)5