/** * Extracts subnet mask from a JSON string * * @param fmJson The JSON formatted string * @return The subnet mask * @throws IOException If there was an error parsing the JSON */ public static String jsonExtractSubnetMask(String fmJson) throws IOException { String subnet_mask = ""; MappingJsonFactory f = new MappingJsonFactory(); JsonParser jp; try { jp = f.createJsonParser(fmJson); } catch (JsonParseException e) { throw new IOException(e); } jp.nextToken(); if (jp.getCurrentToken() != JsonToken.START_OBJECT) { throw new IOException("Expected START_OBJECT"); } while (jp.nextToken() != JsonToken.END_OBJECT) { if (jp.getCurrentToken() != JsonToken.FIELD_NAME) { throw new IOException("Expected FIELD_NAME"); } String n = jp.getCurrentName(); jp.nextToken(); if (jp.getText().equals("")) continue; if (n == "subnet-mask") { subnet_mask = jp.getText(); break; } } return subnet_mask; }
@Test public void testJsonRoundtrip() throws Exception { List<Object> providers = new ArrayList<Object>(); providers.add(new org.codehaus.jackson.jaxrs.JacksonJsonProvider()); JsonBean inputBean = new JsonBean(); inputBean.setVal1("Maple"); WebClient client = WebClient.create(endpointUrl + "/hello/jsonBean", providers); Response r = client.accept("application/json").type("application/json").post(inputBean); assertEquals(Response.Status.OK.getStatusCode(), r.getStatus()); MappingJsonFactory factory = new MappingJsonFactory(); JsonParser parser = factory.createJsonParser((InputStream) r.getEntity()); JsonBean output = parser.readValueAs(JsonBean.class); assertEquals("Maple", output.getVal2()); }
/** * 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(); }
@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; } }
/** * 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(); }
protected LBMonitor jsonToMonitor(String json) throws IOException { MappingJsonFactory f = new MappingJsonFactory(); JsonParser jp; LBMonitor monitor = new LBMonitor(); try { jp = f.createJsonParser(json); } catch (JsonParseException e) { throw new IOException(e); } jp.nextToken(); if (jp.getCurrentToken() != JsonToken.START_OBJECT) { throw new IOException("Expected START_OBJECT"); } while (jp.nextToken() != JsonToken.END_OBJECT) { if (jp.getCurrentToken() != JsonToken.FIELD_NAME) { throw new IOException("Expected FIELD_NAME"); } String n = jp.getCurrentName(); jp.nextToken(); if (jp.getText().equals("")) continue; else if (n.equals("monitor")) { while (jp.nextToken() != JsonToken.END_OBJECT) { String field = jp.getCurrentName(); if (field.equals("id")) { monitor.id = jp.getText(); continue; } if (field.equals("name")) { monitor.name = jp.getText(); continue; } if (field.equals("type")) { monitor.type = Short.parseShort(jp.getText()); continue; } if (field.equals("delay")) { monitor.delay = Short.parseShort(jp.getText()); continue; } if (field.equals("timeout")) { monitor.timeout = Short.parseShort(jp.getText()); continue; } if (field.equals("attempts_before_deactivation")) { monitor.attemptsBeforeDeactivation = Short.parseShort(jp.getText()); continue; } if (field.equals("network_id")) { monitor.netId = jp.getText(); continue; } if (field.equals("address")) { monitor.address = Integer.parseInt(jp.getText()); continue; } if (field.equals("protocol")) { monitor.protocol = Byte.parseByte(jp.getText()); continue; } if (field.equals("port")) { monitor.port = Short.parseShort(jp.getText()); continue; } if (field.equals("admin_state")) { monitor.adminState = Short.parseShort(jp.getText()); continue; } if (field.equals("status")) { monitor.status = Short.parseShort(jp.getText()); continue; } log.warn("Unrecognized field {} in " + "parsing Vips", jp.getText()); } } } jp.close(); return monitor; }