@Override
    public void serialize(
        SimpleEvaluationObject value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException {

      synchronized (value) {
        String id = getUpdateManager().register(value);
        jgen.writeStartObject();
        jgen.writeObjectField("type", "SimpleEvaluationObject");
        jgen.writeStringField("update_id", id);
        jgen.writeStringField("expression", value.getExpression());
        jgen.writeObjectField("status", value.getStatus());
        if (value.getMessage() != null) jgen.writeStringField("message", value.getMessage());
        if (value.getProgressBar() > 0)
          jgen.writeNumberField("progressBar", value.getProgressBar());
        if (value.getPayloadChanged()) {
          EvaluationResult o = value.getPayload();
          if (o != null && o.getValue() != null) {
            jgen.writeFieldName("payload");
            if (!getObjectSerializer().writeObject(o, jgen, true)) jgen.writeObject(o.toString());
          } else if (value.getJsonRes() != null) {
            jgen.writeFieldName("payload");
            if (ControlCharacterUtils.containsControlCharacters(value.getJsonRes())) {
              jgen.writeRawValue(ControlCharacterUtils.escapeControlCharacters(value.getJsonRes()));
            } else {
              jgen.writeRawValue(value.getJsonRes());
            }
          }
        }
        if (value.getJsonRes() != null
            && value.getPayload() != null
            && value.getPayload().getValue() != null) {
          logger.trace("adding raw json data: '" + value.getJsonRes() + "'");
          jgen.writeFieldName("jsonres");
          if (ControlCharacterUtils.containsControlCharacters(value.getJsonRes())) {
            jgen.writeRawValue(ControlCharacterUtils.escapeControlCharacters(value.getJsonRes()));
          } else {
            jgen.writeRawValue(value.getJsonRes());
          }
        }
        jgen.writeArrayFieldStart("outputdata");
        for (Object o : value.getOutputdata()) {
          if (o instanceof EvaluationStdOutput) {
            jgen.writeStartObject();
            jgen.writeStringField("type", "out");
            jgen.writeStringField("value", ((EvaluationStdOutput) o).payload);
            jgen.writeEndObject();
          } else if (o instanceof EvaluationStdError) {
            jgen.writeStartObject();
            jgen.writeStringField("type", "err");
            jgen.writeStringField("value", ((EvaluationStdError) o).payload);
            jgen.writeEndObject();
          }
        }
        jgen.writeEndArray();
        jgen.writeEndObject();
      }
    }
    void send(List<String> eventLog) throws IOException {
      CloseableHttpClient httpClient = HttpClients.createDefault();
      try {

        JsonFactory factory = new JsonFactory();
        StringWriter writer = new StringWriter();
        JsonGenerator generator = factory.createJsonGenerator(writer);
        generator.writeStartObject();
        generator.writeArrayFieldStart("events");
        for (String eventJson : eventLog) {
          generator.writeRawValue(eventJson);
        }
        generator.writeEndArray();
        generator.writeEndObject();
        generator.close();

        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
            AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        HttpClientContext context = HttpClientContext.create();
        context.setCredentialsProvider(credsProvider);

        RestPostProcessorDelegate.LOG.info(
            "Posting " + eventLog.size() + " events to " + targetUrl);
        HttpPost httpPost = new HttpPost(targetUrl);
        httpPost.setEntity(new StringEntity(writer.toString(), ContentType.APPLICATION_JSON));
        CloseableHttpResponse response = httpClient.execute(httpPost, context);
        int statusCode = response.getStatusLine().getStatusCode();
        LOG.info("Status code was " + statusCode + " when invoking " + targetUrl);
        if (statusCode >= 400) {
          throw new RiceRuntimeException(
              "Failed to invoke " + targetUrl + ", response code was " + statusCode);
        }

      } finally {
        httpClient.close();
      }
    }
Ejemplo n.º 3
0
 protected boolean fetchProperty(
     JsonGenerator jg, ObjectResolver resolver, Object value, String path) throws IOException {
   if (value == null) {
     return false;
   }
   boolean fetched = false;
   if (resolver != null) {
     String propertyPath = path.replaceFirst("/", "");
     String genericPropertyPath = propertyPath.replaceAll("\\[[0-9]*\\]", "");
     Set<String> fetchElements = ctx.getFetched(ENTITY_TYPE);
     boolean fetch = false;
     for (String fetchElement : fetchElements) {
       if ("properties".equals(fetchElement)
           || propertyPath.startsWith(fetchElement)
           || genericPropertyPath.startsWith(fetchElement)) {
         fetch = true;
         break;
       }
     }
     if (fetch) {
       Object object = resolver.fetch(value);
       if (object != null) {
         try {
           ByteArrayOutputStream baos = new ByteArrayOutputStream();
           writeEntity(object, baos);
           jg.writeRawValue(baos.toString());
           fetched = true;
         } catch (MarshallingException e) {
           log.error(
               "Unable to marshall as json the entity referenced by the property " + path, e);
         }
       }
     }
   }
   return fetched;
 }