io.micronaut.http.codec.CodecException Java Examples

The following examples show how to use io.micronaut.http.codec.CodecException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProtobufferCodec.java    From micronaut-grpc with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T decode(Argument<T> type, ByteBuffer<?> buffer) throws CodecException {
    try {
        if (type.getType() == byte[].class) {
            return (T) buffer.toByteArray();
        } else {
            Message.Builder builder = getBuilder(type)
                    .orElseThrow(() -> new CodecException("Unable to create builder"));
            if (type.hasTypeVariables()) {
                throw new IllegalStateException("Generic type arguments are not supported");
            } else {
                builder.mergeFrom(buffer.toByteArray(), extensionRegistry);
                return type.getType().cast(builder.build());
            }
        }
    } catch (Exception e) {
        throw new CodecException("Error decoding Protobuff bytes for type [" + type.getName() + "]: " + e.getMessage(), e);
    }
}
 
Example #2
Source File: ProtobufferCodec.java    From micronaut-grpc with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T decode(Argument<T> type, byte[] bytes) throws CodecException {
    try {
        if (type.getType() == byte[].class) {
            return (T) bytes;
        } else {
            Message.Builder builder = getBuilder(type)
                    .orElseThrow(() -> new CodecException("Unable to create builder"));
            if (type.hasTypeVariables()) {
                throw new IllegalStateException("Generic type arguments are not supported");
            } else {
                builder.mergeFrom(bytes, extensionRegistry);
                return type.getType().cast(builder.build());
            }
        }
    } catch (Exception e) {
        throw new CodecException("Error decoding Protobuff bytes for type [" + type.getName() + "]: " + e.getMessage(), e);
    }
}
 
Example #3
Source File: ProtobufferCodec.java    From micronaut-grpc with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T decode(Argument<T> type, ByteBuffer<?> buffer) throws CodecException {
    try {
        if (type.getType() == byte[].class) {
            return (T) buffer.toByteArray();
        } else {
            Message.Builder builder = getBuilder(type)
                    .orElseThrow(() -> new CodecException("Unable to create builder"));
            if (type.hasTypeVariables()) {
                throw new IllegalStateException("Generic type arguments are not supported");
            } else {
                builder.mergeFrom(buffer.toByteArray(), extensionRegistry);
                return type.getType().cast(builder.build());
            }
        }
    } catch (Exception e) {
        throw new CodecException("Error decoding Protobuff bytes for type [" + type.getName() + "]: " + e.getMessage(), e);
    }
}
 
Example #4
Source File: ProtobufferCodec.java    From micronaut-grpc with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T decode(Argument<T> type, byte[] bytes) throws CodecException {
    try {
        if (type.getType() == byte[].class) {
            return (T) bytes;
        } else {
            Message.Builder builder = getBuilder(type)
                    .orElseThrow(() -> new CodecException("Unable to create builder"));
            if (type.hasTypeVariables()) {
                throw new IllegalStateException("Generic type arguments are not supported");
            } else {
                builder.mergeFrom(bytes, extensionRegistry);
                return type.getType().cast(builder.build());
            }
        }
    } catch (Exception e) {
        throw new CodecException("Error decoding Protobuff bytes for type [" + type.getName() + "]: " + e.getMessage(), e);
    }
}
 
Example #5
Source File: MicronautAwsProxyRequest.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
private String decodeValue(CharSequence name, String v) {
    try {
        return URLDecoder.decode(v, getCharacterEncoding().name());
    } catch (UnsupportedEncodingException e) {
        throw new CodecException("Error decoding parameter: " + name, e);
    }
}
 
Example #6
Source File: ProtobufferCodec.java    From micronaut-grpc with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T decode(Argument<T> type, InputStream inputStream) throws CodecException {
    try {
        Message.Builder builder = getBuilder(type)
                .orElseThrow(() -> new CodecException("Unable to create builder"));
        if (type.hasTypeVariables()) {
            throw new IllegalStateException("Generic type arguments are not supported");
        } else {
            builder.mergeFrom(inputStream, extensionRegistry);
            return type.getType().cast(builder.build());
        }
    } catch (Exception e) {
        throw new CodecException("Error decoding Protobuff stream for type [" + type.getName() + "]: " + e.getMessage(), e);
    }
}
 
Example #7
Source File: ProtobufferCodec.java    From micronaut-grpc with Apache License 2.0 5 votes vote down vote up
@Override
public <T> void encode(T object, OutputStream outputStream) throws CodecException {
    try {
        if (object instanceof Message) {
            ((Message) object).writeTo(outputStream);
        }
    } catch (IOException e) {
        throw new CodecException("Error encoding object [" + object + "] to OutputStream:" + e.getMessage());
    }
}
 
Example #8
Source File: ProtobufferCodec.java    From micronaut-grpc with Apache License 2.0 5 votes vote down vote up
@Override
public <T> byte[] encode(T object) throws CodecException {
    if (object instanceof Message) {
        return ((Message) object).toByteArray();
    } else if (object instanceof byte[]) {
        return ((byte[]) object);
    }
    return new byte[0];
}
 
Example #9
Source File: ProtobufferCodec.java    From micronaut-grpc with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T decode(Argument<T> type, InputStream inputStream) throws CodecException {
    try {
        Message.Builder builder = getBuilder(type)
                .orElseThrow(() -> new CodecException("Unable to create builder"));
        if (type.hasTypeVariables()) {
            throw new IllegalStateException("Generic type arguments are not supported");
        } else {
            builder.mergeFrom(inputStream, extensionRegistry);
            return type.getType().cast(builder.build());
        }
    } catch (Exception e) {
        throw new CodecException("Error decoding Protobuff stream for type [" + type.getName() + "]: " + e.getMessage(), e);
    }
}
 
Example #10
Source File: ProtobufferCodec.java    From micronaut-grpc with Apache License 2.0 5 votes vote down vote up
@Override
public <T> void encode(T object, OutputStream outputStream) throws CodecException {
    try {
        if (object instanceof Message) {
            ((Message) object).writeTo(outputStream);
        }
    } catch (IOException e) {
        throw new CodecException("Error encoding object [" + object + "] to OutputStream:" + e.getMessage());
    }
}
 
Example #11
Source File: ProtobufferCodec.java    From micronaut-grpc with Apache License 2.0 5 votes vote down vote up
@Override
public <T> byte[] encode(T object) throws CodecException {
    if (object instanceof Message) {
        return ((Message) object).toByteArray();
    } else if (object instanceof byte[]) {
        return ((byte[]) object);
    }
    return new byte[0];
}
 
Example #12
Source File: GoogleFunctionHttpRequest.java    From micronaut-gcp with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public <T> Optional<T> getBody(@Nonnull Argument<T> arg) {
    if (arg != null) {
        final Class<T> type = arg.getType();
        final MediaType contentType = getContentType().orElse(MediaType.APPLICATION_JSON_TYPE);
        if (body == null) {

            if (isFormSubmission(contentType)) {
                body = getParameters();
                if (ConvertibleValues.class == type) {
                    return (Optional<T>) Optional.of(body);
                } else {
                    return Optional.empty();
                }
            } else {

                final MediaTypeCodec codec = codecRegistry.findCodec(contentType, type).orElse(null);
                if (codec != null) {
                    try (InputStream inputStream = googleRequest.getInputStream()) {
                        if (ConvertibleValues.class == type) {
                            final Map map = codec.decode(Map.class, inputStream);
                            body = ConvertibleValues.of(map);
                            return (Optional<T>) Optional.of(body);
                        } else {
                            final T value = codec.decode(arg, inputStream);
                            body = value;
                            return Optional.ofNullable(value);
                        }
                    } catch (IOException e) {
                        throw new CodecException("Error decoding request body: " + e.getMessage(), e);
                    }

                }
            }
        } else {
            if (type.isInstance(body)) {
                return (Optional<T>) Optional.of(body);
            } else {
                if (body != httpParameters) {
                    final T result = ConversionService.SHARED.convertRequired(body, arg);
                    return Optional.ofNullable(result);
                }
            }

        }
    }
    return Optional.empty();
}
 
Example #13
Source File: ProtobufferCodec.java    From micronaut-grpc with Apache License 2.0 4 votes vote down vote up
@Override
public <T, B> ByteBuffer<B> encode(T object, ByteBufferFactory<?, B> allocator) throws CodecException {
    return allocator.copiedBuffer(encode(object));
}
 
Example #14
Source File: ProtobufferCodec.java    From micronaut-grpc with Apache License 2.0 4 votes vote down vote up
@Override
public <T, B> ByteBuffer<B> encode(T object, ByteBufferFactory<?, B> allocator) throws CodecException {
    return allocator.copiedBuffer(encode(object));
}