use of org.codehaus.jackson.map.JsonMappingException in project ranger by apache.
the class JSONUtil method readMapToString.
public String readMapToString(Map<?, ?> map) {
ObjectMapper mapper = new ObjectMapper();
String jsonString = null;
try {
jsonString = mapper.writeValueAsString(map);
} 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.map.JsonMappingException in project wonderdog by infochimps-labs.
the class ElasticSearchStorage method putNext.
/**
* Here we handle both the delimited record case and the json case.
*/
@SuppressWarnings("unchecked")
@Override
public void putNext(Tuple t) throws IOException {
UDFContext context = UDFContext.getUDFContext();
Properties property = context.getUDFProperties(ResourceSchema.class);
MapWritable record = new MapWritable();
String isJson = property.getProperty(ES_IS_JSON);
// Handle delimited records (ie. isJson == false)
if (isJson != null && isJson.equals("false")) {
String[] fieldNames = property.getProperty(PIG_ES_FIELD_NAMES).split(COMMA);
for (int i = 0; i < t.size(); i++) {
if (i < fieldNames.length) {
try {
record.put(new Text(fieldNames[i]), new Text(t.get(i).toString()));
} catch (NullPointerException e) {
// LOG.info("Increment null field counter.");
}
}
}
} else {
if (!t.isNull(0)) {
String jsonData = t.get(0).toString();
// parse json data and put into mapwritable record
try {
HashMap<String, Object> data = mapper.readValue(jsonData, HashMap.class);
record = (MapWritable) toWritable(data);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
}
}
}
try {
writer.write(NullWritable.get(), record);
} catch (InterruptedException e) {
throw new IOException(e);
}
}
use of org.codehaus.jackson.map.JsonMappingException in project coprhd-controller by CoprHD.
the class KHRequests method postRequest.
/*
* Send POST request to KittyHawk server, and handle redirect/cookies
*
* @param resource webResource
*
* @param ParamBase parameters for post
*
* @throws VNXeException
*/
public ClientResponse postRequest(ParamBase param) throws VNXeException {
_logger.debug("post data: " + _url);
ObjectMapper mapper = new ObjectMapper();
String parmString = null;
if (param != null) {
try {
parmString = mapper.writeValueAsString(param);
_logger.debug("Content of the post: {}", parmString);
} catch (JsonGenerationException e) {
_logger.error("Post request param is not valid. ", e);
throw VNXeException.exceptions.vnxeCommandFailed("Post request param is not valid.", e);
} catch (JsonMappingException e) {
_logger.error("Post request param is not valid. ", e);
throw VNXeException.exceptions.vnxeCommandFailed("Post request param is not valid.", e);
} catch (IOException e) {
_logger.error("Post request param is not valid. ", e);
throw VNXeException.exceptions.vnxeCommandFailed("Post request param is not valid.", e);
}
}
ClientResponse response = buildRequest(addQueryParameters(buildResource(_resource)).getRequestBuilder()).entity(parmString).post(ClientResponse.class);
Status statusCode = response.getClientResponseStatus();
if (statusCode == ClientResponse.Status.CREATED || statusCode == ClientResponse.Status.ACCEPTED || statusCode == ClientResponse.Status.OK || statusCode == ClientResponse.Status.NO_CONTENT) {
return response;
} else if (statusCode == ClientResponse.Status.UNAUTHORIZED) {
authenticate();
response = buildRequest(addQueryParameters(buildResource(_resource)).getRequestBuilder()).entity(parmString).post(ClientResponse.class);
;
statusCode = response.getClientResponseStatus();
if (statusCode == ClientResponse.Status.OK || statusCode == ClientResponse.Status.ACCEPTED || statusCode == ClientResponse.Status.NO_CONTENT || statusCode == ClientResponse.Status.CREATED) {
return response;
}
}
int redirectTimes = 1;
// handle redirect
while (response.getClientResponseStatus() == ClientResponse.Status.FOUND && redirectTimes < VNXeConstants.REDIRECT_MAX) {
String code = response.getClientResponseStatus().toString();
String data = response.getEntity(String.class);
_logger.debug("Returned code: {}, returned data {}", code, data);
WebResource newResource = handelRedirect(response);
if (newResource != null) {
response = buildRequest(newResource.getRequestBuilder()).entity(parmString).post(ClientResponse.class);
redirectTimes++;
} else {
// could not find the redirect url, return
_logger.error(String.format("The post request to: %s failed with: %s %s", _url, response.getClientResponseStatus().toString(), response.getEntity(String.class)));
throw VNXeException.exceptions.unexpectedDataError("Got redirect status code, but could not get redirected URL");
}
}
if (redirectTimes >= VNXeConstants.REDIRECT_MAX) {
_logger.error("redirected too many times for the request {}", _url);
throw VNXeException.exceptions.unexpectedDataError("Redirected too many times while sending the request for " + _url);
}
checkResponse(response, POST_REQUEST);
return response;
}
use of org.codehaus.jackson.map.JsonMappingException in project coprhd-controller by CoprHD.
the class KHRequests method postRequestSync.
public VNXeCommandResult postRequestSync(ParamBase param) {
ClientResponse response = postRequest(param);
if (response.getClientResponseStatus() == ClientResponse.Status.NO_CONTENT) {
VNXeCommandResult result = new VNXeCommandResult();
result.setSuccess(true);
return result;
}
String resString = response.getEntity(String.class);
_logger.debug("KH API returned: {} ", resString);
JSONObject res;
String objectString = null;
VNXeCommandResult returnedObject = null;
try {
res = new JSONObject(resString);
if (res != null) {
JSONObject object = (JSONObject) res.get(VNXeConstants.CONTENT);
if (object != null) {
objectString = object.toString();
ObjectMapper mapper = new ObjectMapper();
try {
returnedObject = mapper.readValue(objectString, VNXeCommandResult.class);
returnedObject.setSuccess(true);
} catch (JsonParseException e) {
_logger.error(String.format("unexpected data returned: %s", objectString), e);
throw VNXeException.exceptions.unexpectedDataError(String.format("unexpected data returned: %s", objectString), e);
} catch (JsonMappingException e) {
_logger.error(String.format("unexpected data returned: %s", objectString), e);
throw VNXeException.exceptions.unexpectedDataError(String.format("unexpected data returned: %s", objectString), e);
} catch (IOException e) {
_logger.error(String.format("unexpected data returned: %s", objectString), e);
throw VNXeException.exceptions.unexpectedDataError(String.format("unexpected data returned: %s", objectString), e);
}
}
}
} catch (JSONException e) {
_logger.error(String.format("unexpected data returned: %s from: %s", resString, _url), e);
throw VNXeException.exceptions.unexpectedDataError(String.format("unexpected data returned: %s", objectString), e);
}
return returnedObject;
}
use of org.codehaus.jackson.map.JsonMappingException in project coprhd-controller by CoprHD.
the class KHRequests method postRequestAsync.
public VNXeCommandJob postRequestAsync(ParamBase param) {
setAsyncMode();
ClientResponse response = postRequest(param);
VNXeCommandJob job;
String resString = response.getEntity(String.class);
ObjectMapper mapper = new ObjectMapper();
try {
job = mapper.readValue(resString, VNXeCommandJob.class);
} catch (JsonParseException e) {
_logger.error(String.format("unexpected data returned: %s from: %s", resString, _url), e);
throw VNXeException.exceptions.unexpectedDataError("unexpected data returned:" + resString, e);
} catch (JsonMappingException e) {
_logger.error(String.format("unexpected data returned: %s from: %s", resString, _url), e);
throw VNXeException.exceptions.unexpectedDataError("unexpected data returned:" + resString, e);
} catch (IOException e) {
_logger.error(String.format("unexpected data returned: %s from: %s", resString, _url), e);
throw VNXeException.exceptions.unexpectedDataError("unexpected data returned:" + resString, e);
}
if (job != null) {
_logger.info("submitted the job: " + job.getId());
} else {
_logger.warn("No job returned.");
}
return job;
}
Aggregations