private Flux<Object> decodeInternal(
      JsonObjectDecoder objectDecoder,
      Publisher<DataBuffer> inputStream,
      ResolvableType elementType,
      MimeType mimeType,
      Object[] hints) {

    Assert.notNull(inputStream, "'inputStream' must not be null");
    Assert.notNull(elementType, "'elementType' must not be null");

    TypeFactory typeFactory = this.mapper.getTypeFactory();
    JavaType javaType = typeFactory.constructType(elementType.getType());

    ObjectReader reader = this.mapper.readerFor(javaType);

    return objectDecoder
        .decode(inputStream, elementType, mimeType, hints)
        .map(
            dataBuffer -> {
              try {
                Object value = reader.readValue(dataBuffer.asInputStream());
                DataBufferUtils.release(dataBuffer);
                return value;
              } catch (IOException e) {
                return Flux.error(new CodecException("Error while reading the data", e));
              }
            });
  }
 /* (non-Javadoc)
  * @see com.feinno.message.HttpConvertibleOutput#fromHttpResponse(com.ning.http.client.Response, java.lang.Throwable)
  */
 @Override
 public void fromHttpResponse(Response response, Throwable throwable) {
   if (throwable != null) {
     LOGGER.error(throwable.getMessage());
     return;
   }
   if (response.getStatusCode() == 200) {
     try {
       String content;
       if ((content = response.getResponseBody("UTF-8")) != null) {
         ObjectReader reader = mapper.readerForUpdating(this);
         reader.readValue(content);
       }
     } catch (Exception e) {
       LOGGER.error(e.getMessage());
       error_code = "0";
       setError_msg(e.getMessage());
       return;
     }
   } else {
     try {
       ObjectReader reader = mapper.readerForUpdating(this);
       reader.readValue(response.getResponseBody("UTF-8"));
       return;
     } catch (Exception e) {
       LOGGER.error(response.getStatusCode() + "\n" + e.getMessage());
       error_code = "0";
       return;
     }
   }
 }
 private void _testSimpleExplicit(ObjectReader r, boolean useBytes) throws Exception {
   r = r.forType(FiveMinuteUser.class);
   FiveMinuteUser user;
   final String INPUT = "Bob,Robertson,MALE,AQIDBAU=,false\n";
   if (useBytes) {
     user = r.readValue(INPUT);
   } else {
     user = r.readValue(INPUT.getBytes("UTF-8"));
   }
   assertEquals("Bob", user.firstName);
   assertEquals("Robertson", user.lastName);
   assertEquals(Gender.MALE, user.getGender());
   assertFalse(user.isVerified());
   assertArrayEquals(new byte[] {1, 2, 3, 4, 5}, user.getUserImage());
 }
  /**
   * Parse a JSON response to extract an entity document.
   *
   * <p>TODO This method currently contains code to work around Wikibase issue
   * https://phabricator.wikimedia.org/T73349. This should be removed once the issue is fixed.
   *
   * @param entityNode the JSON node that should contain the entity document data
   * @return the entitiy document, or null if there were unrecoverable errors
   * @throws IOException
   * @throws JsonProcessingException
   */
  private EntityDocument parseJsonResponse(JsonNode entityNode)
      throws JsonProcessingException, IOException {
    try {
      JacksonTermedStatementDocument ed =
          mapper.treeToValue(entityNode, JacksonTermedStatementDocument.class);
      ed.setSiteIri(this.siteIri);

      return ed;
    } catch (JsonProcessingException e) {
      logger.warn(
          "Error when reading JSON for entity "
              + entityNode.path("id").asText("UNKNOWN")
              + ": "
              + e.toString()
              + "\nTrying to manually fix issue https://phabricator.wikimedia.org/T73349.");
      String jsonString = entityNode.toString();
      jsonString =
          jsonString
              .replace("\"sitelinks\":[]", "\"sitelinks\":{}")
              .replace("\"labels\":[]", "\"labels\":{}")
              .replace("\"aliases\":[]", "\"aliases\":{}")
              .replace("\"claims\":[]", "\"claims\":{}")
              .replace("\"descriptions\":[]", "\"descriptions\":{}");

      ObjectReader documentReader = this.mapper.reader(JacksonTermedStatementDocument.class);

      JacksonTermedStatementDocument ed;
      ed = documentReader.readValue(jsonString);
      ed.setSiteIri(this.siteIri);
      return ed;
    }
  }
Esempio n. 5
0
  public void send(GCMMessage messageBase) throws Exception {
    if (gcmURI == null) {
      throw new Exception("Error sending push. Google cloud messaging properties not provided.");
    }

    HttpPost httpPost = new HttpPost(gcmURI);
    httpPost.setHeader("Authorization", API_KEY);
    httpPost.setEntity(new StringEntity(messageBase.toJson(), ContentType.APPLICATION_JSON));

    try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
      HttpEntity entity = response.getEntity();
      String responseMsg = EntityUtils.toString(entity);
      if (response.getStatusLine().getStatusCode() == 200) {
        GCMResponseMessage gcmResponseMessage = gcmResponseReader.readValue(responseMsg);
        if (gcmResponseMessage.failure == 1) {
          if (gcmResponseMessage.results != null && gcmResponseMessage.results.length > 0) {
            throw new Exception(
                "Error sending push. Problem : " + gcmResponseMessage.results[0].error);
          } else {
            throw new Exception("Error sending push. Token : " + messageBase.getToken());
          }
        }
      } else {
        EntityUtils.consume(entity);
        throw new Exception(responseMsg);
      }
    } finally {
      httpPost.releaseConnection();
    }
  }
  public void testSimple() throws Exception {
    final ObjectMapper mapper = new ObjectMapper();
    final ObjectReader jsonReader = mapper.reader(POJO.class);
    final String JSON = "{\"name\":\"Bob\", \"id\":3}";

    byte[] doc = _smileDoc(JSON, true);

    ObjectReader detecting =
        jsonReader.withFormatDetection(jsonReader, jsonReader.with(new SmileFactory()));
    POJO pojo = detecting.readValue(doc);
    assertEquals(3, pojo.id);
    assertEquals("Bob", pojo.name);

    // let's verify it also works for plain JSON...
    pojo = detecting.readValue(JSON.getBytes("UTF-8"));
    assertEquals(3, pojo.id);
    assertEquals("Bob", pojo.name);
  }
 @Override
 public Document read(Identifier identifier) throws RESTException, IOException {
   if (identifier.size() < 3) {
     throw new IllegalArgumentException(
         "Identifier not precise enough. Needs ID as well. " + identifier.toString());
   }
   JSONObject response = super.doGetCall(super.createFullURL(identifier));
   return r.readValue(response.toJSONString());
 }
 @Override
 public QueryResponseWrapper readAll(Identifier identifier, int limit, long offset)
     throws RESTException, IOException {
   if (identifier.size() < 2) {
     throw new IllegalArgumentException(
         "Identifier not precise enough. Needs Database and Table. " + identifier.toString());
   }
   JSONObject response = super.doGetCall(super.createFullURL(identifier) + "/");
   return rQuery.readValue(response.toJSONString());
 }
Esempio n. 9
0
 @VisibleForTesting
 static Map<String, AccessLevel> deserializeListProjectsMap(JsonNode result) {
   try {
     return listProjectsReader.<Map<String, AccessLevel>>readValue(result);
   } catch (JsonProcessingException e) {
     throw new RuntimeException(e);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
 @Override
 public Document create(Table table, Document entity)
     throws RESTException, ParseException, IOException {
   JSONParser parser = new JSONParser();
   String documentJson = SDKUtils.createJSON(entity.getObject());
   JSONObject response =
       (JSONObject)
           super.doPostCall(
               super.createFullURL(table) + "/documents", (JSONObject) parser.parse(documentJson));
   return r.readValue(response.toJSONString());
 }
 /**
  * 解析返回的内容,默认已经200返回了。如果解析不了,那么this中只有error对象不为空
  *
  * @param response
  * @throws IOException
  */
 private void parseResponse(Response response) throws Exception {
   String content;
   if ((content = response.getResponseBody("UTF-8")) != null) {
     try {
       statuses = (mapper.readValue(content, type));
     } catch (JsonMappingException e) { // 正常的status解析不出来,估计就是error返回了,解析一下error吧!
       ObjectReader reader = mapper.readerForUpdating(this);
       reader.readValue(content);
     }
   }
 }
Esempio n. 12
0
  public static final AbstractClass fromJson(String json) {
    ObjectMapper mapper = new ObjectMapper();
    ObjectReader reader = mapper.reader(AbstractClass.class);
    AbstractClass hydrated = null;

    try {
      hydrated = reader.readValue(json);
    } catch (IOException e) {
      e.printStackTrace();
    }

    System.out.println(hydrated);

    return hydrated;
  }
 @Override
 public <O> O deserialise(String source, Class<? extends O> type, Map<String, Object> parameters)
     throws DeserialiserException {
   try {
     ObjectReader reader = reader(type);
     for (Entry<String, Object> entry : parameters.entrySet()) {
       reader = reader.withAttribute(entry.getKey(), entry.getValue());
     }
     return reader.readValue(source);
   } catch (DeserialiserException ex) {
     throw ex;
   } catch (IOException ex) {
     throw new DeserialiserException(ex);
   }
 }
Esempio n. 14
0
 public static Personnage getPersonnage(final String name, final String realm) {
   String readCharacter = queryBattle(BattleApiConstants.getCharacterQueryUrl(name, realm));
   LOGGER.debug(readCharacter);
   if (readCharacter == null) {
     return null;
   }
   Personnage p = new Personnage();
   try {
     ObjectMapper mapper = new ObjectMapper();
     ObjectReader reader = mapper.readerFor(Personnage.class);
     p = reader.readValue(readCharacter);
   } catch (Exception e) {
     LOGGER.error(e);
   }
   return p;
 }
Esempio n. 15
0
 private List<ArrayNode> downloadResult(String jobId) {
   return client.jobResult(
       jobId,
       TDResultFormat.JSON,
       input -> {
         try {
           List<String> lines = CharStreams.readLines(new InputStreamReader(input));
           ObjectReader reader = objectMapper().readerFor(ArrayNode.class);
           List<ArrayNode> result = new ArrayList<>();
           for (String line : lines) {
             result.add(reader.readValue(line));
           }
           return result;
         } catch (IOException e) {
           throw Throwables.propagate(e);
         }
       });
 }
Esempio n. 16
0
  public static Guilde getGuilde() {
    String readGuildMembersCount = queryBattle(BattleApiConstants.getGuildeMembersQueryUrl());
    LOGGER.debug(readGuildMembersCount);
    if (readGuildMembersCount == null) {
      return null;
    }
    readGuildMembersCount = readGuildMembersCount.substring(readGuildMembersCount.indexOf("(") + 1);
    readGuildMembersCount = readGuildMembersCount.substring(0, readGuildMembersCount.length() - 2);
    Guilde g = new Guilde();
    try {
      ObjectMapper mapper = new ObjectMapper();
      ObjectReader reader = mapper.readerFor(Guilde.class);
      g = reader.readValue(readGuildMembersCount);

      LOGGER.info("Nombre de membres " + g.getMembers().size());
    } catch (Exception e) {
      LOGGER.error(e);
    }
    return g;
  }
Esempio n. 17
0
  public Stream<T> parse(InputStream is, CsvErrorSniffer context) {
    ObjectMapper mapper = objectMapper.copy();
    formatter.initMixIn(mapper);

    ObjectReader reader = mapper.readerFor(formatter.getTargetClass());

    CsvSchema schema = new CsvSchema(formatter);

    return parseToCsvLine(is)
        .map(
            line -> {
              line.getException()
                  .ifPresent(
                      e -> context.mark(new Location(line.getLineNumber(), OptionalInt.empty())));

              Set<String> ignoreField = new HashSet<>();
              while (true) {
                try {
                  return reader.readValue(schema.toJson(line, ignoreField));
                } catch (JsonMappingException e) {
                  String path = buildPath(e.getPath());
                  ;
                  Location location =
                      new Location(
                          line.getLineNumber(), OptionalInt.of(schema.getColumnNumber(path)));
                  if (context.contains(location)) {
                    throw new IllegalStateException("invalid row state: " + e.getLocation());
                  }
                  context.mark(location);
                  ignoreField.add(path);
                } catch (IOException e) {
                  context.mark(new Location(line.getLineNumber(), OptionalInt.empty()));
                  try {
                    return formatter.getTargetClass().newInstance();
                  } catch (ReflectiveOperationException e2) {
                    throw new ReflectiveOperationRuntimeException(e2);
                  }
                }
              }
            });
  }
 @Override
 public StreamDefinition findOne(String id) {
   try {
     byte[] bytes = zkConnection.getClient().getData().forPath(Paths.build(Paths.STREAMS, id));
     if (bytes == null) {
       return null;
     }
     Map<String, String> map = ZooKeeperUtils.bytesToMap(bytes);
     StreamDefinition streamDefinition = new StreamDefinition(id, map.get(DEFINITION_KEY));
     if (map.get(MODULE_DEFINITIONS_KEY) != null) {
       List<ModuleDefinition> moduleDefinitions =
           objectReader.readValue(map.get(MODULE_DEFINITIONS_KEY));
       streamDefinition.setModuleDefinitions(moduleDefinitions);
     }
     return streamDefinition;
   } catch (Exception e) {
     // NoNodeException - the definition does not exist
     ZooKeeperUtils.wrapAndThrowIgnoring(e, NoNodeException.class);
   }
   return null;
 }
Esempio n. 19
0
 private static List<Race> getListRaces() {
   String sRaces = queryBattle(BattleApiConstants.getRacesQueryUrl());
   LOGGER.debug(sRaces);
   if (sRaces == null) {
     return null;
   }
   List<Race> races = new ArrayList<Race>();
   try {
     ObjectMapper mapper = new ObjectMapper();
     ObjectReader reader =
         mapper.readerFor(new TypeReference<List<Race>>() {}).withRootName("races");
     races = reader.readValue(sRaces);
     LOGGER.debug("Nombre de races " + races.size());
     for (Race race : races) {
       LOGGER.debug(race.toString());
     }
   } catch (Exception e) {
     LOGGER.error(e);
   }
   return races;
 }
  public void testSimpleExplicitWithBOM() throws Exception {
    ObjectReader r = MAPPER.reader(SIMPLE_SCHEMA);
    r = r.forType(FiveMinuteUser.class);
    FiveMinuteUser user;

    ByteArrayOutputStream b = new ByteArrayOutputStream();

    // first, UTF-8 BOM:
    b.write(new byte[] {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});
    b.write("Bob,Robertson,MALE,AQIDBAU=,false\n".getBytes("UTF-8"));
    b.close();

    user = r.readValue(b.toByteArray());
    String fn = user.firstName;

    if (!fn.equals("Bob")) {
      fail("Expected 'Bob' (3), got '" + fn + "' (" + fn.length() + ")");
    }
    assertEquals("Robertson", user.lastName);
    assertEquals(Gender.MALE, user.getGender());
    assertFalse(user.isVerified());
    assertArrayEquals(new byte[] {1, 2, 3, 4, 5}, user.getUserImage());
  }
    public void run() {
      String id = null; // document id
      String type = null; // document type
      String indexName = null; // document index
      Map<String, Object> data = null; // document data for indexing
      ObjectReader dataReader = mapper.reader(new TypeReference<Map<String, Object>>() {});
      int interval =
          (LONGPOLLING_INTERVAL < 0 || LONGPOLLING_INTERVAL > 20)
              ? DEFAULT_LONGPOLLING_INTERVAL
              : LONGPOLLING_INTERVAL;

      while (!closed) {
        // pull messages from SQS
        if (DEBUG) logger.info("Waiting {}s for messages...", interval);

        List<JsonNode> msgs = pullMessages(interval);

        try {
          BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();

          for (JsonNode msg : msgs) {
            if (msg.has("_id") && msg.has("_type")) {
              id = msg.get("_id").textValue();
              type = msg.get("_type").textValue();
              // Support for dynamic indexes
              indexName = msg.has("_index") ? msg.get("_index").textValue() : INDEX;

              JsonNode dataNode = msg.get("_data");
              if (dataNode != null && dataNode.isObject()) {
                data = dataReader.readValue(msg.get("_data"));
                bulkRequestBuilder.add(
                    client.prepareIndex(indexName, type, id).setSource(data).request());
              } else {
                bulkRequestBuilder.add(client.prepareDelete(indexName, type, id).request());
              }
            }
          }

          if (bulkRequestBuilder.numberOfActions() > 0) {
            BulkResponse response = bulkRequestBuilder.execute().actionGet();
            if (response.hasFailures()) {
              logger.warn(
                  "Bulk operation completed with errors: " + response.buildFailureMessage());
            }
            idleCount = 0;
          } else {
            idleCount++;
            if (DEBUG) logger.info("No new messages. {}", idleCount);
            // no tasks in queue => throttle down pull requests
            if (SLEEP > 0 && idleCount >= 3) {
              try {
                if (DEBUG) logger.info("Queue is empty. Sleeping for {}s", interval);
                Thread.sleep(SLEEP * 1000);
              } catch (InterruptedException e) {
                if (closed) {
                  if (DEBUG) logger.info("Done.");
                  break;
                }
              }
            }
          }
        } catch (Exception e) {
          logger.error("Bulk index operation failed {}", e);
          continue;
        }
      }
    }
 public static MyClass getObjectFromJson(final String jsonString) throws IOException {
   return joReader.readValue(jsonString);
 }