io.micronaut.http.codec.MediaTypeCodec Java Examples

The following examples show how to use io.micronaut.http.codec.MediaTypeCodec. 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: MicronautAwsProxyResponse.java    From micronaut-aws with Apache License 2.0 6 votes vote down vote up
private byte[] encodeInternal(MediaTypeCodec codec) throws InvalidResponseObjectException {
    byte[] encoded = null;
    try {
        if (body != null) {
            if (body instanceof ByteBuffer) {
                encoded = ((ByteBuffer) body).toByteArray();
            } else if (body instanceof byte[]) {
                encoded = (byte[]) body;
            } else {
                final Optional<MediaType> contentType = getContentType();
                if (!contentType.isPresent()) {
                    contentType(MediaType.APPLICATION_JSON_TYPE);
                }
                encoded = codec.encode(body);
            }
        }
    } catch (Exception e) {
        throw new InvalidResponseObjectException("Invalid Response: " + e.getMessage() , e);
    }
    return encoded;
}
 
Example #2
Source File: SqlResultEntityTypeMapper.java    From micronaut-data with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor used to customize the join paths.
 * @param entity The entity
 * @param resultReader The result reader
 * @param joinPaths The join paths
 */
private SqlResultEntityTypeMapper(
        @NonNull RuntimePersistentEntity<R> entity,
        @NonNull ResultReader<RS, String> resultReader,
        @Nullable Set<JoinPath> joinPaths,
        String startingPrefix,
        @Nullable MediaTypeCodec jsonCodec) {
    ArgumentUtils.requireNonNull("entity", entity);
    ArgumentUtils.requireNonNull("resultReader", resultReader);
    this.entity = entity;
    this.jsonCodec = jsonCodec;
    this.resultReader = resultReader;
    if (CollectionUtils.isNotEmpty(joinPaths)) {
        this.joinPaths = new HashMap<>(joinPaths.size());
        for (JoinPath joinPath : joinPaths) {
            this.joinPaths.put(joinPath.getPath(), joinPath);
        }
    } else {
        this.joinPaths = Collections.emptyMap();
    }
    this.startingPrefix = startingPrefix;
}
 
Example #3
Source File: SqlResultEntityTypeMapper.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 * @param entity The entity
 * @param resultReader The result reader
 * @param jsonCodec The json codec
 */
public SqlResultEntityTypeMapper(
        @NonNull RuntimePersistentEntity<R> entity,
        @NonNull ResultReader<RS, String> resultReader,
        @Nullable MediaTypeCodec jsonCodec) {
    this(entity, resultReader, Collections.emptySet(), jsonCodec);
}
 
Example #4
Source File: SqlResultEntityTypeMapper.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 * @param prefix The prefix to startup from.
 * @param entity The entity
 * @param resultReader The result reader
 * @param jsonCodec The JSON codec
 */
public SqlResultEntityTypeMapper(
        String prefix,
        @NonNull RuntimePersistentEntity<R> entity,
        @NonNull ResultReader<RS, String> resultReader,
        @Nullable MediaTypeCodec jsonCodec) {
    this(entity, resultReader, Collections.emptySet(), prefix, jsonCodec);
}
 
Example #5
Source File: SqlResultEntityTypeMapper.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor used to customize the join paths.
 * @param entity The entity
 * @param resultReader The result reader
 * @param joinPaths The join paths
 * @param jsonCodec The JSON codec
 */
public SqlResultEntityTypeMapper(
        @NonNull RuntimePersistentEntity<R> entity,
        @NonNull ResultReader<RS, String> resultReader,
        @Nullable Set<JoinPath> joinPaths,
        @Nullable MediaTypeCodec jsonCodec) {
    this(entity, resultReader, joinPaths, null, jsonCodec);
}
 
Example #6
Source File: DTOMapper.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 * @param persistentEntity The entity
 * @param resultReader The result reader
 * @param jsonCodec The JSON codec
 */
public DTOMapper(
        RuntimePersistentEntity<T> persistentEntity,
        ResultReader<S, String> resultReader,
        @Nullable MediaTypeCodec jsonCodec) {
    ArgumentUtils.requireNonNull("persistentEntity", persistentEntity);
    ArgumentUtils.requireNonNull("resultReader", resultReader);
    this.persistentEntity = persistentEntity;
    this.resultReader = resultReader;
    this.jsonCodec = jsonCodec;
}
 
Example #7
Source File: DefaultJdbcRepositoryOperations.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 *
 * @param dataSourceName        The data source name
 * @param dataSource            The datasource
 * @param transactionOperations The JDBC operations for the data source
 * @param executorService       The executor service
 * @param beanContext           The bean context
 * @param codecs                The codecs
 * @param dateTimeProvider      The dateTimeProvider
 */
protected DefaultJdbcRepositoryOperations(@Parameter String dataSourceName,
                                          DataSource dataSource,
                                          @Parameter TransactionOperations<Connection> transactionOperations,
                                          @Named("io") @Nullable ExecutorService executorService,
                                          BeanContext beanContext,
                                          List<MediaTypeCodec> codecs,
                                          @NonNull DateTimeProvider dateTimeProvider) {
    super(
            new ColumnNameResultSetReader(),
            new ColumnIndexResultSetReader(),
            new JdbcQueryStatement(),
            codecs,
            dateTimeProvider
    );
    ArgumentUtils.requireNonNull("dataSource", dataSource);
    ArgumentUtils.requireNonNull("transactionOperations", transactionOperations);
    this.dataSource = dataSource;
    this.transactionOperations = transactionOperations;
    this.executorService = executorService;
    Collection<BeanDefinition<GenericRepository>> beanDefinitions = beanContext.getBeanDefinitions(GenericRepository.class, Qualifiers.byStereotype(Repository.class));
    for (BeanDefinition<GenericRepository> beanDefinition : beanDefinitions) {
        String targetDs = beanDefinition.stringValue(Repository.class).orElse("default");
        if (targetDs.equalsIgnoreCase(dataSourceName)) {
            Dialect dialect = beanDefinition.enumValue(JdbcRepository.class, "dialect", Dialect.class).orElseGet(() -> beanDefinition.enumValue(JdbcRepository.class, "dialectName", Dialect.class).orElse(Dialect.ANSI));
            dialects.put(beanDefinition.getBeanType(), dialect);
            QueryBuilder qb = queryBuilders.get(dialect);
            if (qb == null) {
                queryBuilders.put(dialect, new SqlQueryBuilder(dialect));
            }
        }
    }
}
 
Example #8
Source File: AbstractSqlRepositoryOperations.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 *
 * @param columnNameResultSetReader  The column name result reader
 * @param columnIndexResultSetReader The column index result reader
 * @param preparedStatementWriter    The prepared statement writer
 * @param codecs                     The media type codecs
 * @param dateTimeProvider           The injected dateTimeProvider instance
 */
protected AbstractSqlRepositoryOperations(
        ResultReader<RS, String> columnNameResultSetReader,
        ResultReader<RS, Integer> columnIndexResultSetReader,
        QueryStatement<PS, Integer> preparedStatementWriter,
        List<MediaTypeCodec> codecs,
        @NonNull DateTimeProvider dateTimeProvider) {
    this.columnNameResultSetReader = columnNameResultSetReader;
    this.columnIndexResultSetReader = columnIndexResultSetReader;
    this.preparedStatementWriter = preparedStatementWriter;
    this.jsonCodec = resolveJsonCodec(codecs);
    this.dateTimeProvider = dateTimeProvider;
}
 
Example #9
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 #10
Source File: AbstractSqlRepositoryOperations.java    From micronaut-data with Apache License 2.0 4 votes vote down vote up
private MediaTypeCodec resolveJsonCodec(List<MediaTypeCodec> codecs) {
    return CollectionUtils.isNotEmpty(codecs) ? codecs.stream().filter(c -> c.getMediaTypes().contains(MediaType.APPLICATION_JSON_TYPE)).findFirst().orElse(null) : null;
}
 
Example #11
Source File: SqlDTOMapper.java    From micronaut-data with Apache License 2.0 2 votes vote down vote up
/**
 * Default constructor.
 *
 * @param persistentEntity The entity
 * @param resultReader     The result reader
 * @param jsonCodec        The json codec
 */
public SqlDTOMapper(RuntimePersistentEntity<T> persistentEntity, ResultReader<S, String> resultReader, MediaTypeCodec jsonCodec) {
    super(persistentEntity, resultReader, jsonCodec);
}