use of org.codehaus.jackson.JsonParseException in project openhab1-addons by openhab.
the class OpenPathsBinding method getUserLocation.
@SuppressWarnings("unchecked")
private Location getUserLocation(String accessKey, String secretKey) {
// build the OAuth service using the access/secret keys
OAuthService service = new ServiceBuilder().provider(new OpenPathsApi()).apiKey(accessKey).apiSecret(secretKey).build();
// build the request
OAuthRequest request = new OAuthRequest(Verb.GET, "https://openpaths.cc/api/1");
service.signRequest(Token.empty(), request);
request.addQuerystringParameter("num_points", "1");
// send the request and check we got a successful response
Response response = request.send();
if (!response.isSuccessful()) {
logger.error("Failed to request the OpenPaths location, response code: " + response.getCode());
return null;
}
// parse the response to build our location object
Map<String, Object> locationData;
String toParse = "{}";
try {
ObjectMapper jsonReader = new ObjectMapper();
toParse = response.getBody();
toParse = toParse.substring(1, toParse.length() - 2);
locationData = jsonReader.readValue(toParse, Map.class);
} catch (JsonParseException e) {
logger.error("Error parsing JSON:\n" + toParse, e);
return null;
} catch (JsonMappingException e) {
logger.error("Error mapping JSON:\n" + toParse, e);
return null;
} catch (IOException e) {
logger.error("An I/O error occured while decoding JSON:\n" + response.getBody());
return null;
}
float latitude = Float.parseFloat(locationData.get("lat").toString());
float longitude = Float.parseFloat(locationData.get("lon").toString());
String device = locationData.get("device").toString();
return new Location(latitude, longitude, device);
}
use of org.codehaus.jackson.JsonParseException in project meteo by pierre.
the class TopicListener method onMessage.
@Override
public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) message;
String txt = null;
try {
txt = txtMsg.getText();
log.debug("Received a message, yay!\n" + txt);
Map event = mapper.readValue(txt, Map.class);
esperSink.getEPRuntime().sendEvent(event, esperTopicKey);
} catch (JMSException ex) {
log.warn("Got an error from the message queue", ex);
} catch (ClassCastException ex) {
log.info("Received message that I couldn't parse: " + txt, ex);
} catch (JsonMappingException ex) {
log.info("Received message that I couldn't parse: " + txt, ex);
} catch (JsonParseException ex) {
log.info("Received message that I couldn't parse: " + txt, ex);
} catch (IOException ex) {
log.warn("Got an error from the message queue", ex);
}
} else if (message instanceof BytesMessage) {
final BytesMessage byteMessage = (BytesMessage) message;
long llen;
try {
llen = byteMessage.getBodyLength();
} catch (JMSException e) {
log.warn("Unable to get message length", e);
return;
}
if (llen > Integer.MAX_VALUE) {
// should never occur but...
log.error("Ridiculously huge message payload, above 32-bit length");
} else {
final int len = (int) llen;
final byte[] data = new byte[len];
final int readLen;
try {
readLen = byteMessage.readBytes(data);
} catch (JMSException e) {
log.warn("Unable to get message bytes", e);
return;
}
if (readLen < len) {
log.error("Failed to read byte message contents; read {}, was trying to read {}", readLen, data.length);
} else {
final Map event;
try {
event = mapper.readValue(data, Map.class);
esperSink.getEPRuntime().sendEvent(event, esperTopicKey);
} catch (IOException e) {
log.error("Failed to convert message to Esper Event", readLen, data.length);
}
}
}
} else {
log.error("Unexpected message type '{}' from AMQ broker: must skip", message.getClass().getName());
}
}
use of org.codehaus.jackson.JsonParseException in project flink by apache.
the class PreviewPlanDumpTest method dump.
private void dump(Plan p) {
try {
List<DataSinkNode> sinks = Optimizer.createPreOptimizedPlan(p);
PlanJSONDumpGenerator dumper = new PlanJSONDumpGenerator();
String json = dumper.getPactPlanAsJSON(sinks);
JsonParser parser = new JsonFactory().createJsonParser(json);
while (parser.nextToken() != null) ;
} catch (JsonParseException e) {
e.printStackTrace();
Assert.fail("JSON Generator produced malformatted output: " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
Assert.fail("An error occurred in the test: " + e.getMessage());
}
}
use of org.codehaus.jackson.JsonParseException in project ranger by apache.
the class JSONUtil method readListToString.
public String readListToString(List<?> list) {
ObjectMapper mapper = new ObjectMapper();
String jsonString = null;
try {
jsonString = mapper.writeValueAsString(list);
} catch (JsonParseException e) {
throw restErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA);
} catch (JsonMappingException e) {
throw restErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA);
} catch (IOException e) {
throw restErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA);
}
return jsonString;
}
use of org.codehaus.jackson.JsonParseException in project ranger by apache.
the class RangerDataHistService method writeObjectAsString.
public String writeObjectAsString(RangerBaseModelObject vObj) {
ObjectMapper mapper = new ObjectMapper();
String jsonStr;
try {
jsonStr = mapper.writeValueAsString(vObj);
return jsonStr;
} catch (JsonParseException e) {
throw restErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA);
} catch (JsonMappingException e) {
throw restErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA);
} catch (IOException e) {
throw restErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA);
}
}
Aggregations