Search in sources :

Example 1 with MetacatJsonException

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);
    }
}
Also used : MetacatNotFoundException(com.netflix.metacat.common.exception.MetacatNotFoundException) RetryableException(feign.RetryableException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) MetacatNotSupportedException(com.netflix.metacat.common.exception.MetacatNotSupportedException) MetacatJsonException(com.netflix.metacat.common.json.MetacatJsonException) MetacatException(com.netflix.metacat.common.exception.MetacatException) IOException(java.io.IOException) MetacatBadRequestException(com.netflix.metacat.common.exception.MetacatBadRequestException) MetacatAlreadyExistsException(com.netflix.metacat.common.exception.MetacatAlreadyExistsException)

Example 2 with MetacatJsonException

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;
}
Also used : Arrays(java.util.Arrays) Connection(java.sql.Connection) URL(java.net.URL) Date(java.util.Date) HasDataMetadata(com.netflix.metacat.common.dto.HasDataMetadata) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DefinitionMetadataDto(com.netflix.metacat.common.dto.DefinitionMetadataDto) Strings(com.google.common.base.Strings) SQLException(java.sql.SQLException) Lists(com.google.common.collect.Lists) ResultSet(java.sql.ResultSet) Map(java.util.Map) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) DataSource(javax.sql.DataSource) Config(com.netflix.metacat.common.server.properties.Config) BaseUserMetadataService(com.netflix.metacat.common.server.usermetadata.BaseUserMetadataService) Nonnull(javax.annotation.Nonnull) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) Charsets(com.google.common.base.Charsets) DataSourceManager(com.netflix.metacat.common.server.util.DataSourceManager) Properties(java.util.Properties) QueryRunner(org.apache.commons.dbutils.QueryRunner) MetacatJson(com.netflix.metacat.common.json.MetacatJson) Files(java.nio.file.Files) HasDefinitionMetadata(com.netflix.metacat.common.dto.HasDefinitionMetadata) Set(java.util.Set) QualifiedName(com.netflix.metacat.common.QualifiedName) Reader(java.io.Reader) PreparedStatement(java.sql.PreparedStatement) Maps(com.google.common.collect.Maps) MetacatJsonException(com.netflix.metacat.common.json.MetacatJsonException) Collectors(java.util.stream.Collectors) HasMetadata(com.netflix.metacat.common.dto.HasMetadata) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) DBUtil(com.netflix.metacat.common.server.util.DBUtil) Paths(java.nio.file.Paths) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) ColumnListHandler(org.apache.commons.dbutils.handlers.ColumnListHandler) Collections(java.util.Collections) ResultSetHandler(org.apache.commons.dbutils.ResultSetHandler) FileSystems(java.nio.file.FileSystems) Joiner(com.google.common.base.Joiner) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SQLException(java.sql.SQLException) MetacatJsonException(com.netflix.metacat.common.json.MetacatJsonException) Connection(java.sql.Connection) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 3 with MetacatJsonException

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;
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SQLException(java.sql.SQLException) MetacatJsonException(com.netflix.metacat.common.json.MetacatJsonException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 MetacatJsonException (com.netflix.metacat.common.json.MetacatJsonException)3 UserMetadataServiceException (com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Charsets (com.google.common.base.Charsets)1 Joiner (com.google.common.base.Joiner)1 Preconditions (com.google.common.base.Preconditions)1 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 QualifiedName (com.netflix.metacat.common.QualifiedName)1 DefinitionMetadataDto (com.netflix.metacat.common.dto.DefinitionMetadataDto)1 HasDataMetadata (com.netflix.metacat.common.dto.HasDataMetadata)1 HasDefinitionMetadata (com.netflix.metacat.common.dto.HasDefinitionMetadata)1 HasMetadata (com.netflix.metacat.common.dto.HasMetadata)1 MetacatAlreadyExistsException (com.netflix.metacat.common.exception.MetacatAlreadyExistsException)1 MetacatBadRequestException (com.netflix.metacat.common.exception.MetacatBadRequestException)1