List of usage examples for com.fasterxml.jackson.databind JsonMappingException toString
public String toString()
From source file:com.tomtom.speedtools.json.Json.java
@Nullable private static <T> T fromMapper(@Nonnull final ObjectMapper mapper, @Nonnull final String json, @Nonnull final Class<T> type) { assert mapper != null; assert json != null; assert type != null; try {//from w w w.ja va 2s . c o m final StringReader reader = new StringReader(json); return mapper.readValue(reader, type); } catch (final JsonMappingException e) { LOG.error("fromMapper: Cannot map JSON {} --> object, mapper={}, exception={}", json, mapper.getClass().getCanonicalName(), e.toString()); } catch (final IOException e) { LOG.error("fromMapper: Cannot map JSON {} --> object, mapper={}, exception={}", json, mapper.getClass().getCanonicalName(), e.toString()); } return null; }
From source file:com.tomtom.speedtools.json.Json.java
@Nonnull private static String toMapper(@Nonnull final ObjectMapper mapper, @Nonnull final Object obj) { assert mapper != null; assert obj != null; try {// ww w . j av a2s. c o m return mapper.writeValueAsString(obj); } catch (final JsonMappingException e) { LOG.error("toMapper: Map exception {} --> JSON, mapper={}, exception={}", obj.getClass().getCanonicalName(), mapper.getClass().getCanonicalName(), e.toString()); } catch (final IOException e) { LOG.error("toMapper: Cannot map {} --> JSON, mapper={}, exception={}", obj.getClass().getCanonicalName(), mapper.getClass().getCanonicalName(), e.toString()); } return ""; }
From source file:com.erudika.para.rest.RestUtils.java
/** * Returns a Response with the entity object inside it and 200 status code. * If there was and error the status code is different than 200. * @param is the entity input stream/*from w w w .java 2s.c o m*/ * @param type the type to convert the entity into, for example a Map. * @return response with 200 or error status */ public static Response getEntity(InputStream is, Class<?> type) { Object entity; try { if (is != null && is.available() > 0) { if (is.available() > Config.MAX_ENTITY_SIZE_BYTES) { return getStatusResponse(Response.Status.BAD_REQUEST, "Request is too large - the maximum is " + (Config.MAX_ENTITY_SIZE_BYTES / 1024) + " KB."); } entity = ParaObjectUtils.getJsonReader(type).readValue(is); } else { return getStatusResponse(Response.Status.BAD_REQUEST, "Missing request body."); } } catch (JsonMappingException e) { return getStatusResponse(Response.Status.BAD_REQUEST, e.getMessage()); } catch (JsonParseException e) { return getStatusResponse(Response.Status.BAD_REQUEST, e.getMessage()); } catch (IOException e) { logger.error(null, e); return getStatusResponse(Response.Status.INTERNAL_SERVER_ERROR, e.toString()); } return Response.ok(entity).build(); }
From source file:com.couchbase.lite.replicator.ChangeTracker.java
protected void runLoop() { paused = false;/*from w ww. j ava 2s . com*/ if (client == null) { // This is a race condition that can be reproduced by calling cbpuller.start() and cbpuller.stop() // directly afterwards. What happens is that by the time the Changetracker thread fires up, // the cbpuller has already set this.client to null. See issue #109 Log.w(Log.TAG_CHANGE_TRACKER, "%s: ChangeTracker run() loop aborting because client == null", this); return; } if (mode == ChangeTrackerMode.Continuous) { // there is a failing unit test for this, and from looking at the code the Replication // object will never use Continuous mode anyway. Explicitly prevent its use until // it is demonstrated to actually work. throw new RuntimeException("ChangeTracker does not correctly support continuous mode"); } OkHttpClient httpClient = client.getOkHttpClient(); backoff = new ChangeTrackerBackoff(); while (running) { startTime = System.currentTimeMillis(); Request.Builder builder = new Request.Builder(); URL url = getChangesFeedURL(); builder.url(url); if (usePOST) { builder.header("Content-Type", "application/json").addHeader("User-Agent", Manager.getUserAgent()) .addHeader("Accept-Encoding", "gzip").post(RequestBody.create(JSON, changesFeedPOSTBody())); } addRequestHeaders(builder); // Perform BASIC Authentication if needed builder = RequestUtils.preemptivelySetAuthCredentials(builder, url, authenticator); request = builder.build(); try { String maskedRemoteWithoutCredentials = getChangesFeedURL().toString(); maskedRemoteWithoutCredentials = maskedRemoteWithoutCredentials.replaceAll("://.*:.*@", "://---:---@"); Log.v(Log.TAG_CHANGE_TRACKER, "%s: Making request to %s", this, maskedRemoteWithoutCredentials); call = httpClient.newCall(request); Response response = call.execute(); try { // In case response status is Error, ChangeTracker stops here if (isResponseFailed(response)) { RequestUtils.closeResponseBody(response); if (retryIfFailedPost(response)) continue; break; } // Parse response body ResponseBody responseBody = response.body(); Log.v(Log.TAG_CHANGE_TRACKER, "%s: got response. status: %s mode: %s", this, response.message(), mode); if (responseBody != null) { try { Log.v(Log.TAG_CHANGE_TRACKER, "%s: /entity.getContent(). mode: %s", this, mode); //inputStream = entity.getContent(); inputStream = responseBody.byteStream(); // decompress if contentEncoding is gzip if (Utils.isGzip(response)) inputStream = new GZIPInputStream(inputStream); if (mode == ChangeTrackerMode.LongPoll) { // continuous replications // NOTE: 1. check content length, ObjectMapper().readValue() throws Exception if size is 0. // NOTE: 2. HttpEntity.getContentLength() returns the number of bytes of the content, or a negative number if unknown. // NOTE: 3. If Http Status is error, not parse response body boolean responseOK = false; // default value if (responseBody.contentLength() != 0 && response.code() < 300) { try { Log.v(Log.TAG_CHANGE_TRACKER, "%s: readValue", this); Map<String, Object> fullBody = Manager.getObjectMapper() .readValue(inputStream, Map.class); Log.v(Log.TAG_CHANGE_TRACKER, "%s: /readValue. fullBody: %s", this, fullBody); responseOK = receivedPollResponse(fullBody); } catch (JsonParseException jpe) { Log.w(Log.TAG_CHANGE_TRACKER, "%s: json parsing error; %s", this, jpe.toString()); } catch (JsonMappingException jme) { Log.w(Log.TAG_CHANGE_TRACKER, "%s: json mapping error; %s", this, jme.toString()); } } Log.v(Log.TAG_CHANGE_TRACKER, "%s: responseOK: %s", this, responseOK); if (responseOK) { // TODO: this logic is questionable, there's lots // TODO: of differences in the iOS changetracker code, if (!caughtUp) { caughtUp = true; client.changeTrackerCaughtUp(); } Log.v(Log.TAG_CHANGE_TRACKER, "%s: Starting new longpoll", this); backoff.resetBackoff(); continue; } else { long elapsed = (System.currentTimeMillis() - startTime) / 1000; Log.w(Log.TAG_CHANGE_TRACKER, "%s: Longpoll connection closed (by proxy?) after %d sec", this, elapsed); if (elapsed >= 30) { // Looks like the connection got closed by a proxy (like AWS' load balancer) while the // server was waiting for a change to send, due to lack of activity. // Lower the heartbeat time to work around this, and reconnect: this.heartBeatSeconds = Math.min(this.heartBeatSeconds, (int) (elapsed * 0.75)); Log.v(Log.TAG_CHANGE_TRACKER, "%s: Starting new longpoll", this); backoff.resetBackoff(); continue; } else { Log.d(Log.TAG_CHANGE_TRACKER, "%s: Change tracker calling stop (LongPoll)", this); client.changeTrackerFinished(this); break; } } } else { // one-shot replications Log.v(Log.TAG_CHANGE_TRACKER, "%s: readValue (oneshot)", this); JsonFactory factory = new JsonFactory(); JsonParser jp = factory.createParser(inputStream); JsonToken token; // nextToken() is null => no more token while (((token = jp.nextToken()) != JsonToken.START_ARRAY) && (token != null)) { // ignore these tokens } while (jp.nextToken() == JsonToken.START_OBJECT) { Map<String, Object> change = (Map) Manager.getObjectMapper().readValue(jp, Map.class); if (!receivedChange(change)) { Log.w(Log.TAG_CHANGE_TRACKER, "Received unparseable change line from server: %s", change); } // if not running state anymore, exit from loop. if (!running) break; } if (jp != null) jp.close(); Log.v(Log.TAG_CHANGE_TRACKER, "%s: /readValue (oneshot)", this); client.changeTrackerCaughtUp(); if (isContinuous()) { // if enclosing replication is continuous mode = ChangeTrackerMode.LongPoll; } else { Log.d(Log.TAG_CHANGE_TRACKER, "%s: Change tracker calling stop (OneShot)", this); client.changeTrackerFinished(this); break; } } backoff.resetBackoff(); } finally { try { if (inputStream != null) { inputStream.close(); inputStream = null; } } catch (IOException e) { } } } } finally { RequestUtils.closeResponseBody(response); } } catch (Exception e) { if (!running && e instanceof IOException) { // in this case, just silently absorb the exception because it // frequently happens when we're shutting down and have to // close the socket underneath our read. } else { Log.w(Log.TAG_CHANGE_TRACKER, this + ": Exception in change tracker", e); this.error = e; } backoff.sleepAppropriateAmountOfTime(); } } Log.v(Log.TAG_CHANGE_TRACKER, "%s: Change tracker run loop exiting", this); }