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);
}
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;
});
}
Aggregations