コード例 #1
0
  /**
   * POST /api/v1/janitor will try a add a new event with the information in the url context.
   *
   * @param content the Json content passed to the http POST request
   * @return the response
   * @throws IOException
   */
  @POST
  public Response addEvent(String content) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    LOGGER.info(String.format("JSON content: '%s'", content));
    JsonNode input = mapper.readTree(content);

    String eventType = getStringField(input, "eventType");
    String resourceId = getStringField(input, "resourceId");

    Response.Status responseStatus;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JsonGenerator gen = JSON_FACTORY.createJsonGenerator(baos, JsonEncoding.UTF8);
    gen.writeStartObject();
    gen.writeStringField("eventType", eventType);
    gen.writeStringField("resourceId", resourceId);

    if (StringUtils.isEmpty(eventType) || StringUtils.isEmpty(resourceId)) {
      responseStatus = Response.Status.BAD_REQUEST;
      gen.writeStringField("message", "eventType and resourceId parameters are all required");
    } else {
      if (eventType.equals("OPTIN")) {
        responseStatus = optInResource(resourceId, true, gen);
      } else if (eventType.equals("OPTOUT")) {
        responseStatus = optInResource(resourceId, false, gen);
      } else {
        responseStatus = Response.Status.BAD_REQUEST;
        gen.writeStringField("message", String.format("Unrecognized event type: %s", eventType));
      }
    }
    gen.writeEndObject();
    gen.close();
    LOGGER.info("entity content is '{}'", baos.toString("UTF-8"));
    return Response.status(responseStatus).entity(baos.toString("UTF-8")).build();
  }
コード例 #2
0
 /**
  * Gets the janitor status (e.g. to support an AWS ELB Healthcheck on an instance running
  * JanitorMonkey). Creates GET /api/v1/janitor api which responds 200 OK if JanitorMonkey is
  * running.
  *
  * @param uriInfo the uri info
  * @return the chaos events json response
  * @throws IOException Signals that an I/O exception has occurred.
  */
 @GET
 public Response getJanitorStatus(@Context UriInfo uriInfo) throws IOException {
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   JsonGenerator gen = JSON_FACTORY.createJsonGenerator(baos, JsonEncoding.UTF8);
   gen.writeStartArray();
   gen.writeStartObject();
   gen.writeStringField("JanitorMonkeyStatus", "OnLikeDonkeyKong");
   gen.writeEndObject();
   gen.writeEndArray();
   gen.close();
   return Response.status(Response.Status.OK).entity(baos.toString("UTF-8")).build();
 }
コード例 #3
0
  @Override
  public void enqueue(NycQueuedInferredLocationBean r) {
    try {
      final StringWriter sw = new StringWriter();
      final MappingJsonFactory jsonFactory = new MappingJsonFactory();
      final JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(sw);
      _mapper.writeValue(jsonGenerator, r);
      sw.close();

      _outputBuffer.put(sw.toString());
    } catch (final IOException e) {
      _log.info("Could not serialize inferred location record: " + e.getMessage());
    } catch (final InterruptedException e) {
      // discard if thread is interrupted or serialization fails
      return;
    }
  }