Search in sources :

Example 1 with JsonFeatures

use of io.micronaut.json.JsonFeatures in project micronaut-core by micronaut-projects.

the class DefaultNettyHttpClientRegistry method getClientKey.

private ClientKey getClientKey(AnnotationMetadata metadata) {
    final HttpVersion httpVersion = metadata.enumValue(Client.class, "httpVersion", HttpVersion.class).orElse(null);
    String clientId = metadata.stringValue(Client.class).orElse(null);
    String path = metadata.stringValue(Client.class, "path").orElse(null);
    List<String> filterAnnotation = metadata.getAnnotationNamesByStereotype(FilterMatcher.class);
    final Class configurationClass = metadata.classValue(Client.class, "configuration").orElse(null);
    JsonFeatures jsonFeatures = beanContext.getBean(JsonMapper.class).detectFeatures(metadata).orElse(null);
    return new ClientKey(httpVersion, clientId, filterAnnotation, path, configurationClass, jsonFeatures);
}
Also used : JsonFeatures(io.micronaut.json.JsonFeatures) ProxyHttpClient(io.micronaut.http.client.ProxyHttpClient) SseClient(io.micronaut.http.client.sse.SseClient) HttpClient(io.micronaut.http.client.HttpClient) StreamingHttpClient(io.micronaut.http.client.StreamingHttpClient) Client(io.micronaut.http.client.annotation.Client) WebSocketClient(io.micronaut.websocket.WebSocketClient) HttpVersion(io.micronaut.http.HttpVersion)

Example 2 with JsonFeatures

use of io.micronaut.json.JsonFeatures in project micronaut-core by micronaut-projects.

the class DefaultNettyHttpClientRegistry method getClient.

private DefaultHttpClient getClient(ClientKey key, BeanContext beanContext, AnnotationMetadata annotationMetadata) {
    return clients.computeIfAbsent(key, clientKey -> {
        DefaultHttpClient clientBean = null;
        final String clientId = clientKey.clientId;
        final Class<?> configurationClass = clientKey.configurationClass;
        if (clientId != null) {
            clientBean = (DefaultHttpClient) this.beanContext.findBean(HttpClient.class, Qualifiers.byName(clientId)).orElse(null);
        }
        if (configurationClass != null && !HttpClientConfiguration.class.isAssignableFrom(configurationClass)) {
            throw new IllegalStateException("Referenced HTTP client configuration class must be an instance of HttpClientConfiguration for injection point: " + configurationClass);
        }
        final List<String> filterAnnotations = clientKey.filterAnnotations;
        final String path = clientKey.path;
        if (clientBean != null && path == null && configurationClass == null && filterAnnotations.isEmpty()) {
            return clientBean;
        }
        LoadBalancer loadBalancer = null;
        List<String> clientIdentifiers = null;
        final HttpClientConfiguration configuration;
        if (configurationClass != null) {
            configuration = (HttpClientConfiguration) this.beanContext.getBean(configurationClass);
        } else if (clientId != null) {
            configuration = this.beanContext.findBean(HttpClientConfiguration.class, Qualifiers.byName(clientId)).orElse(defaultHttpClientConfiguration);
        } else {
            configuration = defaultHttpClientConfiguration;
        }
        if (clientId != null) {
            loadBalancer = loadBalancerResolver.resolve(clientId).orElseThrow(() -> new HttpClientException("Invalid service reference [" + clientId + "] specified to @Client"));
            clientIdentifiers = Collections.singletonList(clientId);
        }
        String contextPath = null;
        if (StringUtils.isNotEmpty(path)) {
            contextPath = path;
        } else if (StringUtils.isNotEmpty(clientId) && clientId.startsWith("/")) {
            contextPath = clientId;
        } else {
            if (loadBalancer != null) {
                contextPath = loadBalancer.getContextPath().orElse(null);
            }
        }
        final DefaultHttpClient client = buildClient(loadBalancer, clientKey.httpVersion, configuration, clientIdentifiers, contextPath, beanContext, annotationMetadata);
        final JsonFeatures jsonFeatures = clientKey.jsonFeatures;
        if (jsonFeatures != null) {
            List<MediaTypeCodec> codecs = new ArrayList<>(2);
            MediaTypeCodecRegistry codecRegistry = client.getMediaTypeCodecRegistry();
            for (MediaTypeCodec codec : codecRegistry.getCodecs()) {
                if (codec instanceof MapperMediaTypeCodec) {
                    codecs.add(((MapperMediaTypeCodec) codec).cloneWithFeatures(jsonFeatures));
                } else {
                    codecs.add(codec);
                }
            }
            if (!codecRegistry.findCodec(MediaType.APPLICATION_JSON_TYPE).isPresent()) {
                codecs.add(createNewJsonCodec(this.beanContext, jsonFeatures));
            }
            client.setMediaTypeCodecRegistry(MediaTypeCodecRegistry.of(codecs));
        }
        return client;
    });
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) LoadBalancer(io.micronaut.http.client.LoadBalancer) MapperMediaTypeCodec(io.micronaut.json.codec.MapperMediaTypeCodec) HttpClientException(io.micronaut.http.client.exceptions.HttpClientException) ProxyHttpClient(io.micronaut.http.client.ProxyHttpClient) HttpClient(io.micronaut.http.client.HttpClient) StreamingHttpClient(io.micronaut.http.client.StreamingHttpClient) JsonFeatures(io.micronaut.json.JsonFeatures) MediaTypeCodecRegistry(io.micronaut.http.codec.MediaTypeCodecRegistry) HttpClientConfiguration(io.micronaut.http.client.HttpClientConfiguration) MapperMediaTypeCodec(io.micronaut.json.codec.MapperMediaTypeCodec) MediaTypeCodec(io.micronaut.http.codec.MediaTypeCodec)

Aggregations

HttpClient (io.micronaut.http.client.HttpClient)2 ProxyHttpClient (io.micronaut.http.client.ProxyHttpClient)2 StreamingHttpClient (io.micronaut.http.client.StreamingHttpClient)2 JsonFeatures (io.micronaut.json.JsonFeatures)2 HttpVersion (io.micronaut.http.HttpVersion)1 HttpClientConfiguration (io.micronaut.http.client.HttpClientConfiguration)1 LoadBalancer (io.micronaut.http.client.LoadBalancer)1 Client (io.micronaut.http.client.annotation.Client)1 HttpClientException (io.micronaut.http.client.exceptions.HttpClientException)1 SseClient (io.micronaut.http.client.sse.SseClient)1 MediaTypeCodec (io.micronaut.http.codec.MediaTypeCodec)1 MediaTypeCodecRegistry (io.micronaut.http.codec.MediaTypeCodecRegistry)1 MapperMediaTypeCodec (io.micronaut.json.codec.MapperMediaTypeCodec)1 WebSocketClient (io.micronaut.websocket.WebSocketClient)1 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1