use of org.codehaus.jackson.JsonGenerator in project SimianArmy by Netflix.
the class JanitorMonkeyResource method getJanitorStatus.
/**
* 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();
}
use of org.codehaus.jackson.JsonGenerator in project SimianArmy by Netflix.
the class JanitorMonkeyResource method addEventThroughHttpGet.
/**
* GET /api/v1/janitor/addEvent will try to a add a new event with the information in the url query string.
* This is the same as the regular POST addEvent except through a query string. This technically isn't
* very REST-ful as it is a GET call that creates an Opt-out/in event, but is a convenience method
* for exposing opt-in/opt-out functionality more directly, for example in an email notification.
*
* @param eventType eventType from the query string
* @param resourceId resourceId from the query string
* @return the response
* @throws IOException
*/
@GET
@Path("addEvent")
public Response addEventThroughHttpGet(@QueryParam("eventType") String eventType, @QueryParam("resourceId") String resourceId, @QueryParam("region") String region) throws IOException {
Response.Status responseStatus;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write("<html><body style=\"text-align:center\"><img src=\"https://raw.githubusercontent.com/Netflix/SimianArmy/master/assets/janitor.png\" height=\"300\" width=\"300\"><br/>".getBytes());
if (StringUtils.isEmpty(eventType) || StringUtils.isEmpty(resourceId)) {
responseStatus = Response.Status.BAD_REQUEST;
baos.write("<p>NOPE!<br/><br/>Janitor didn't get that: eventType and resourceId parameters are both required</p>".getBytes());
} else {
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
JsonGenerator gen = JSON_FACTORY.createJsonGenerator(baos2, JsonEncoding.UTF8);
gen.writeStartObject();
gen.writeStringField("eventType", eventType);
gen.writeStringField("resourceId", resourceId);
if (eventType.equals("OPTIN")) {
responseStatus = optInResource(resourceId, true, region, gen);
} else if (eventType.equals("OPTOUT")) {
responseStatus = optInResource(resourceId, false, region, gen);
} else {
responseStatus = Response.Status.BAD_REQUEST;
gen.writeStringField("message", String.format("Unrecognized event type: %s", eventType));
}
gen.writeEndObject();
gen.close();
if (responseStatus == Response.Status.OK) {
baos.write(("<p>SUCCESS!<br/><br/>Resource <strong>" + resourceId + "</strong> has been " + eventType + " of Janitor Monkey!</p>").getBytes());
} else {
baos.write(("<p>NOPE!<br/><br/>Janitor is Confused! Error processing Resource <strong>" + resourceId + "</strong></p>").getBytes());
}
String jsonout = String.format("<p><em>Monkey JSON Response:</em><br/><br/><textarea cols=40 rows=20>%s</textarea></p>", baos2.toString());
baos.write(jsonout.getBytes());
}
baos.write("</body></html>".getBytes());
return Response.status(responseStatus).entity(baos.toString("UTF-8")).build();
}
use of org.codehaus.jackson.JsonGenerator in project SimianArmy by Netflix.
the class ChaosMonkeyResource method getChaosEvents.
/**
* Gets the chaos events. Creates GET /api/v1/chaos api which outputs the chaos events in json. Users can specify
* cgi query params to filter the results and use "since" query param to set the start of a timerange. "since" should
* be specified in milliseconds since the epoch.
*
* @param uriInfo
* the uri info
* @return the chaos events json response
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@GET
public Response getChaosEvents(@Context UriInfo uriInfo) throws IOException {
Map<String, String> query = new HashMap<String, String>();
Date date = null;
for (Map.Entry<String, List<String>> pair : uriInfo.getQueryParameters().entrySet()) {
if (pair.getValue().isEmpty()) {
continue;
}
if (pair.getKey().equals("since")) {
date = new Date(Long.parseLong(pair.getValue().get(0)));
} else {
query.put(pair.getKey(), pair.getValue().get(0));
}
}
// if "since" not set, default to 24 hours ago
if (date == null) {
Calendar now = monkey.context().calendar().now();
now.add(Calendar.DAY_OF_YEAR, -1);
date = now.getTime();
}
List<Event> evts = monkey.context().recorder().findEvents(ChaosMonkey.Type.CHAOS, ChaosMonkey.EventTypes.CHAOS_TERMINATION, query, date);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JsonGenerator gen = JSON_FACTORY.createJsonGenerator(baos, JsonEncoding.UTF8);
gen.writeStartArray();
for (Event evt : evts) {
gen.writeStartObject();
gen.writeStringField("monkeyType", evt.monkeyType().name());
gen.writeStringField("eventId", evt.id());
gen.writeStringField("eventType", evt.eventType().name());
gen.writeNumberField("eventTime", evt.eventTime().getTime());
gen.writeStringField("region", evt.region());
for (Map.Entry<String, String> pair : evt.fields().entrySet()) {
gen.writeStringField(pair.getKey(), pair.getValue());
}
gen.writeEndObject();
}
gen.writeEndArray();
gen.close();
return Response.status(Response.Status.OK).entity(baos.toString("UTF-8")).build();
}
use of org.codehaus.jackson.JsonGenerator in project WSPerfLab by Netflix-Skunkworks.
the class MockResponse method generateJson.
/**
*
* @param id
* ID from client used to assert correct client/server interaction
* @param delay
* How long to delay delivery to simulate server-side latency
* @param itemSize
* Length of each item String.
* @param numItems
* Number of items in response.
*
* @return String json
*/
public static Observable<ByteBuf> generateJson(long id, int delay, int itemSize, int numItems) {
return Observable.create((Subscriber<? super ByteBuf> subscriber) -> {
Worker worker = Schedulers.computation().createWorker();
subscriber.add(worker);
worker.schedule(() -> {
try {
ByteBuf buffer = Unpooled.buffer();
ByteBufOutputStream jsonAsBytes = new ByteBufOutputStream(buffer);
JsonGenerator json = jsonFactory.createJsonGenerator(jsonAsBytes);
json.writeStartObject();
// manipulate the ID such that we can know the response is from the server (client will know the logic)
long responseKey = getResponseKey(id);
json.writeNumberField("responseKey", responseKey);
json.writeNumberField("delay", delay);
if (itemSize > MAX_ITEM_LENGTH) {
throw new IllegalArgumentException("itemSize can not be larger than: " + MAX_ITEM_LENGTH);
}
json.writeNumberField("itemSize", itemSize);
json.writeNumberField("numItems", numItems);
json.writeArrayFieldStart("items");
for (int i = 0; i < numItems; i++) {
json.writeString(RAW_ITEM_LONG.substring(0, itemSize));
}
json.writeEndArray();
json.writeEndObject();
json.close();
subscriber.onNext(buffer);
subscriber.onCompleted();
} catch (Exception e) {
subscriber.onError(e);
}
}, delay, TimeUnit.MILLISECONDS);
});
}
use of org.codehaus.jackson.JsonGenerator in project WSPerfLab by Netflix-Skunkworks.
the class ServiceResponseBuilder method buildTestBResponse.
public static ByteArrayOutputStream buildTestBResponse(JsonFactory jsonFactory, BackendResponse response) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(bos);
jsonGenerator.writeStartObject();
// delay values of each response
jsonGenerator.writeArrayFieldStart("delay");
writeTuple(jsonGenerator, "a", response.getDelay());
jsonGenerator.writeEndArray();
// itemSize values of each response
jsonGenerator.writeArrayFieldStart("itemSize");
writeTuple(jsonGenerator, "a", response.getItemSize());
jsonGenerator.writeEndArray();
// numItems values of each response
jsonGenerator.writeArrayFieldStart("numItems");
writeTuple(jsonGenerator, "a", response.getNumItems());
jsonGenerator.writeEndArray();
// all items from responses
jsonGenerator.writeArrayFieldStart("items");
addItemsFromResponse(jsonGenerator, response);
jsonGenerator.writeEndArray();
jsonGenerator.writeEndObject();
jsonGenerator.close();
return bos;
}
Aggregations