use of com.netflix.metacat.common.json.MetacatJsonException in project metacat by Netflix.
the class MetacatErrorDecoder method decode.
/**
* {@inheritDoc}
*/
@Override
public Exception decode(final String methodKey, final Response response) {
try {
String message = "";
if (response.body() != null) {
message = Util.toString(response.body().asReader());
try {
final ObjectNode body = METACAT_JSON.parseJsonObject(message);
message = body.path("error").asText();
} catch (MetacatJsonException ignored) {
}
}
switch(response.status()) {
//NOT IMPLEMENTED
case 501:
case //UNSUPPORTED_MEDIA_TYPE
415:
return new MetacatNotSupportedException(message);
case //BAD_REQUEST
400:
return new MetacatBadRequestException(message);
case //NOT_FOUND
404:
return new MetacatNotFoundException(message);
case //CONFLICT
409:
return new MetacatAlreadyExistsException(message);
//INTERNAL_SERVER_ERROR
case 500:
case //SERVICE_UNAVAILABLE
503:
return new RetryableException(message, null);
default:
return new MetacatException(message, javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR, null);
}
} catch (final IOException e) {
return super.decode(methodKey, response);
}
}
use of com.netflix.metacat.common.json.MetacatJsonException in project metacat by Netflix.
the class MysqlUserMetadataService method _getMetadataMap.
@SuppressWarnings("checkstyle:methodname")
private Map<String, ObjectNode> _getMetadataMap(@Nullable final List<?> keys, final String sql) {
final Map<String, ObjectNode> result = Maps.newHashMap();
if (keys == null || keys.isEmpty()) {
return result;
}
final List<String> paramVariables = keys.stream().map(s -> "?").collect(Collectors.toList());
final String[] aKeys = keys.stream().map(Object::toString).toArray(String[]::new);
final String query = String.format(sql, Joiner.on(",").join(paramVariables));
final Connection connection = DBUtil.getReadConnection(poolingDataSource);
try {
final ResultSetHandler<Void> handler = resultSet -> {
while (resultSet.next()) {
final String json = resultSet.getString("data");
final String name = resultSet.getString("name");
if (json != null) {
try {
result.put(name, metacatJson.parseJsonObject(json));
} catch (MetacatJsonException e) {
log.error("Invalid json '{}' for name '{}'", json, name);
throw new UserMetadataServiceException(String.format("Invalid json %s for name %s", json, name), e);
}
}
}
return null;
};
new QueryRunner().query(connection, query, handler, (Object[]) aKeys);
} catch (SQLException e) {
log.error("Sql exception", e);
throw new UserMetadataServiceException(String.format("Failed to get data for %s", keys), e);
} finally {
DBUtil.closeReadConnection(connection);
}
return result;
}
use of com.netflix.metacat.common.json.MetacatJsonException in project metacat by Netflix.
the class MysqlUserMetadataService method getJsonForKey.
private Optional<ObjectNode> getJsonForKey(final String query, final String keyValue) {
Optional<ObjectNode> result = Optional.empty();
String json = null;
final Connection connection = DBUtil.getReadConnection(poolingDataSource);
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, keyValue);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
final String key = resultSet.getString(1);
if (keyValue.equalsIgnoreCase(key)) {
json = resultSet.getString(2);
if (Strings.isNullOrEmpty(json)) {
return Optional.empty();
}
result = Optional.ofNullable(metacatJson.parseJsonObject(json));
break;
}
}
}
} catch (SQLException e) {
log.error("Sql exception", e);
throw new UserMetadataServiceException(String.format("Failed to get data for %s", keyValue), e);
} catch (MetacatJsonException e) {
log.error("Invalid json '{}' for keyValue '{}'", json, keyValue, e);
throw new UserMetadataServiceException(String.format("Invalid json %s for name %s", json, keyValue), e);
} finally {
DBUtil.closeReadConnection(connection);
}
return result;
}
Aggregations