Search in sources :

Example 6 with GenericType

use of io.helidon.common.GenericType in project helidon by oracle.

the class JdbcStatementQuery method createMetadata.

static Map<Long, DbColumn> createMetadata(ResultSet rs) throws SQLException {
    ResultSetMetaData metaData = rs.getMetaData();
    int columnCount = metaData.getColumnCount();
    Map<Long, DbColumn> byNumbers = new HashMap<>();
    for (int i = 1; i <= columnCount; i++) {
        String name = metaData.getColumnLabel(i);
        String sqlType = metaData.getColumnTypeName(i);
        Class<?> javaClass = classByName(metaData.getColumnClassName(i));
        DbColumn column = new DbColumn() {

            @Override
            public <T> T as(Class<T> type) {
                return null;
            }

            @Override
            public <T> T as(GenericType<T> type) {
                return null;
            }

            @Override
            public Class<?> javaType() {
                return javaClass;
            }

            @Override
            public String dbType() {
                return sqlType;
            }

            @Override
            public String name() {
                return name;
            }
        };
        byNumbers.put((long) i, column);
    }
    return byNumbers;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) DbColumn(io.helidon.dbclient.DbColumn) GenericType(io.helidon.common.GenericType) HashMap(java.util.HashMap)

Example 7 with GenericType

use of io.helidon.common.GenericType in project helidon by oracle.

the class BuilderImpl method addMapper.

@Override
public <T> Config.Builder addMapper(GenericType<T> type, Function<Config, T> mappingFunction) {
    Objects.requireNonNull(type);
    Objects.requireNonNull(mappingFunction);
    addMapper(new ConfigMapperProvider() {

        @Override
        public Map<Class<?>, Function<Config, ?>> mappers() {
            return Map.of();
        }

        @Override
        public Map<GenericType<?>, BiFunction<Config, ConfigMapper, ?>> genericTypeMappers() {
            return Map.of(type, (config, aMapper) -> mappingFunction.apply(config));
        }
    });
    return this;
}
Also used : Prioritized(io.helidon.common.Prioritized) BiFunction(java.util.function.BiFunction) ConfigFilter(io.helidon.config.spi.ConfigFilter) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) MergingStrategy(io.helidon.config.spi.MergingStrategy) ConfigMapperProvider(io.helidon.config.spi.ConfigMapperProvider) Map(java.util.Map) LinkedList(java.util.LinkedList) IdentityHashMap(java.util.IdentityHashMap) Executor(java.util.concurrent.Executor) ConfigSource(io.helidon.config.spi.ConfigSource) Priorities(io.helidon.common.serviceloader.Priorities) ServiceLoader(java.util.ServiceLoader) HelidonServiceLoader(io.helidon.common.serviceloader.HelidonServiceLoader) GenericType(io.helidon.common.GenericType) MapperProviders(io.helidon.config.ConfigMapperManager.MapperProviders) Executors(java.util.concurrent.Executors) Objects(java.util.Objects) List(java.util.List) ConfigMapper(io.helidon.config.spi.ConfigMapper) OverrideSource(io.helidon.config.spi.OverrideSource) ConfigParser(io.helidon.config.spi.ConfigParser) Optional(java.util.Optional) ConfigContext(io.helidon.config.spi.ConfigContext) Collections(java.util.Collections) ConfigMapperProvider(io.helidon.config.spi.ConfigMapperProvider) ConfigMapper(io.helidon.config.spi.ConfigMapper) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 8 with GenericType

use of io.helidon.common.GenericType in project helidon by oracle.

the class MockZipkinService method mockZipkin.

private void mockZipkin(final ServerRequest request, final ServerResponse response) {
    request.queryParams().all("serviceName").forEach(s -> System.out.println(">>>" + s));
    request.content().registerReader(new MessageBodyStreamReader<JsonValue>() {

        @Override
        public PredicateResult accept(final GenericType<?> type, final MessageBodyReaderContext context) {
            return PredicateResult.COMPATIBLE;
        }

        @Override
        @SuppressWarnings("unchecked")
        public <U extends JsonValue> Flow.Publisher<U> read(final Flow.Publisher<DataChunk> publisher, final GenericType<U> type, final MessageBodyReaderContext context) {
            return (Flow.Publisher<U>) Multi.create(publisher).map(d -> ByteBuffer.wrap(d.bytes())).reduce((buf, buf2) -> ByteBuffer.allocate(buf.capacity() + buf2.capacity()).put(buf.array()).put(buf2.array())).flatMap(b -> {
                try (ByteArrayInputStream bais = new ByteArrayInputStream(b.array());
                    GZIPInputStream gzipInputStream = new GZIPInputStream(bais)) {
                    return Single.just(Json.createReader(new StringReader(new String(gzipInputStream.readAllBytes()))).readArray());
                } catch (EOFException e) {
                    // ignore
                    return Multi.empty();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }).flatMap(a -> Multi.create(a.stream()));
        }
    }).asStream(JsonValue.class).map(JsonValue::asJsonObject).filter(json -> TAGS_POINTER.containsValue(json) && COMPONENT_POINTER.containsValue(json) && filteredComponents.stream().anyMatch(s -> s.equals(((JsonString) COMPONENT_POINTER.getValue(json)).getString()))).onError(Throwable::printStackTrace).onError(t -> response.status(500).send(t)).onComplete(response::send).peek(json -> LOGGER.info(json.toString())).forEach(e -> next.getAndSet(new CompletableFuture<>()).complete(e));
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) JsonValue(jakarta.json.JsonValue) DataChunk(io.helidon.common.http.DataChunk) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Flow(java.util.concurrent.Flow) MessageBodyStreamReader(io.helidon.media.common.MessageBodyStreamReader) ServerResponse(io.helidon.webserver.ServerResponse) Single(io.helidon.common.reactive.Single) Service(io.helidon.webserver.Service) Multi(io.helidon.common.reactive.Multi) JsonPointer(jakarta.json.JsonPointer) Set(java.util.Set) IOException(java.io.IOException) Logger(java.util.logging.Logger) GenericType(io.helidon.common.GenericType) EOFException(java.io.EOFException) Json(jakarta.json.Json) ServerRequest(io.helidon.webserver.ServerRequest) MessageBodyReaderContext(io.helidon.media.common.MessageBodyReaderContext) CompletionStage(java.util.concurrent.CompletionStage) StringReader(java.io.StringReader) Routing(io.helidon.webserver.Routing) JsonString(jakarta.json.JsonString) JsonValue(jakarta.json.JsonValue) JsonString(jakarta.json.JsonString) IOException(java.io.IOException) Flow(java.util.concurrent.Flow) GZIPInputStream(java.util.zip.GZIPInputStream) MessageBodyReaderContext(io.helidon.media.common.MessageBodyReaderContext) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) EOFException(java.io.EOFException) DataChunk(io.helidon.common.http.DataChunk)

Example 9 with GenericType

use of io.helidon.common.GenericType in project helidon by oracle.

the class CacheExtension method addBeans.

/*
     * create EmbeddedStorageManager beans
     */
private void addBeans(@Observes final AfterBeanDiscovery event, final BeanManager beanManager) {
    if (event != null && beanManager != null) {
        if (!this.cacheBeans.isEmpty()) {
            for (final Descriptor entry : this.cacheBeans) {
                assert entry != null;
                // create Microstream Cache bean
                final Set<Annotation> qualifiers = entry.getAnnotations();
                assert qualifiers != null;
                assert !qualifiers.isEmpty();
                ParameterizedType types = entry.getTypes();
                GenericType<?> keyType = GenericType.create(types.getActualTypeArguments()[0]);
                GenericType<?> valueType = GenericType.create(types.getActualTypeArguments()[1]);
                String name = getName(qualifiers);
                event.<Cache<?, ?>>addBean().qualifiers(qualifiers).scope(ApplicationScoped.class).addTransitiveTypeClosure(Cache.class).addTypes(types).createWith(cc -> {
                    return CacheBuilder.create(name, getConfigNode(qualifiers), keyType.rawType(), valueType.rawType());
                }).destroyWith((cache, context) -> cache.close());
            }
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Cache(one.microstream.cache.types.Cache) Observes(jakarta.enterprise.event.Observes) Config(io.helidon.config.Config) InjectionPoint(jakarta.enterprise.inject.spi.InjectionPoint) CacheBuilder(io.helidon.integrations.microstream.cache.CacheBuilder) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) Set(java.util.Set) ProcessInjectionPoint(jakarta.enterprise.inject.spi.ProcessInjectionPoint) GenericType(io.helidon.common.GenericType) HashSet(java.util.HashSet) Objects(java.util.Objects) ParameterizedType(java.lang.reflect.ParameterizedType) AfterBeanDiscovery(jakarta.enterprise.inject.spi.AfterBeanDiscovery) Extension(jakarta.enterprise.inject.spi.Extension) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) Priority(jakarta.annotation.Priority) PLATFORM_BEFORE(jakarta.interceptor.Interceptor.Priority.PLATFORM_BEFORE) BeanManager(jakarta.enterprise.inject.spi.BeanManager) Annotation(java.lang.annotation.Annotation) Cache(one.microstream.cache.types.Cache)

Example 10 with GenericType

use of io.helidon.common.GenericType in project helidon by oracle.

the class TestJsonBindingSupport method genericType.

@Test
public void genericType() throws Exception {
    GenericType<List<Person>> personsType = new GenericType<>() {
    };
    final Routing routing = Routing.builder().post("/foo", (req, res) -> {
        req.content().as(personsType).thenAccept(res::send);
    }).build();
    final String personsJson = "[{\"name\":\"Frank\"},{\"name\":\"John\"}]";
    final TestResponse response = TestClient.create(routing, JsonbSupport.create()).path("/foo").post(MediaPublisher.create(MediaType.APPLICATION_JSON.withCharset("UTF-8"), personsJson));
    assertThat(response.headers().first(Http.Header.CONTENT_TYPE).orElse(null), is(MediaType.APPLICATION_JSON.toString()));
    final String json = response.asString().get(10, TimeUnit.SECONDS);
    assertThat(json, is(personsJson));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) TestResponse(io.helidon.webserver.testsupport.TestResponse) TestClient(io.helidon.webserver.testsupport.TestClient) GenericType(io.helidon.common.GenericType) MediaType(io.helidon.common.http.MediaType) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) MediaPublisher(io.helidon.webserver.testsupport.MediaPublisher) Handler(io.helidon.webserver.Handler) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) Routing(io.helidon.webserver.Routing) GenericType(io.helidon.common.GenericType) TestResponse(io.helidon.webserver.testsupport.TestResponse) Routing(io.helidon.webserver.Routing) List(java.util.List) Test(org.junit.jupiter.api.Test)

Aggregations

GenericType (io.helidon.common.GenericType)12 DataChunk (io.helidon.common.http.DataChunk)8 MediaType (io.helidon.common.http.MediaType)6 Multi (io.helidon.common.reactive.Multi)6 MessageBodyWriterContext (io.helidon.media.common.MessageBodyWriterContext)5 Objects (java.util.Objects)5 Flow (java.util.concurrent.Flow)5 MessageBodyStreamWriter (io.helidon.media.common.MessageBodyStreamWriter)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Single (io.helidon.common.reactive.Single)3 Optional (java.util.Optional)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Routing (io.helidon.webserver.Routing)2 JsonStructure (jakarta.json.JsonStructure)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Test (org.junit.jupiter.api.Test)2 Prioritized (io.helidon.common.Prioritized)1