use of io.micronaut.context.exceptions.ConfigurationException in project micronaut-gcp by micronaut-projects.
the class GoogleCredentialsFactory method defaultGoogleCredentials.
/**
* Method used to return the default {@link GoogleCredentials} and provide it as a bean.
*
* It will determine which credential in the following way:
* <ol>
* <li>If <pre>gcp.credentials.location</pre> is specified, use its location</li>
* <li>Otherwise, if <pre>gcp.credentials.encodedKey</pre> is specified, decode it and use its content</li>
* <li>None of the 2 properties were specified, use Application Default credential resolution. See
* <a href="https://github.com/googleapis/google-cloud-java#authentication">Google Cloud Java authentication</a>.
* This will resolve credential in the following order:
* <ol>
* <li>The credentials file pointed to by the <pre>GOOGLE_APPLICATION_CREDENTIALS</pre> environment variable</li>
* <li>Credentials provided by the Google Cloud SDK <pre>gcloud auth application-default login</pre> command</li>
* <li>Google App Engine built-in credentials when running inside of Google App Engine</li>
* <li>Google Cloud Shell built-in credentials when running inside of Google Cloud Shell</li>
* <li>Google Compute Engine built-in credentials when running inside of Google Compute Engine or Kubernetes Engine</li>
* </ol>
* </li>
* </ol>
*
* @return The {@link GoogleCredentials}
* @throws IOException An exception if an error occurs
*/
@Requires(missingBeans = GoogleCredentials.class)
@Requires(classes = com.google.auth.oauth2.GoogleCredentials.class)
@Primary
@Singleton
protected GoogleCredentials defaultGoogleCredentials() throws IOException {
final List<String> scopes = configuration.getScopes().stream().map(URI::toString).collect(Collectors.toList());
GoogleCredentials credentials;
if (configuration.getLocation().isPresent() && configuration.getEncodedKey().isPresent()) {
throw new ConfigurationException("Please specify only one of gcp.credentials.location or gcp.credentials.encodedKey");
} else if (configuration.getLocation().isPresent()) {
LOG.info("Google Credentials from gcp.credentials.location = " + configuration.getLocation());
FileInputStream fis = new FileInputStream(configuration.getLocation().get());
credentials = GoogleCredentials.fromStream(fis);
fis.close();
} else if (configuration.getEncodedKey().isPresent()) {
LOG.info("Google Credentials from gcp.credentials.encodedKey");
Base64.Decoder decoder = Base64.getDecoder();
byte[] bytes = decoder.decode(configuration.getEncodedKey().get());
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
credentials = GoogleCredentials.fromStream(is);
is.close();
} else {
LOG.info("Google Credentials from Application Default Credentials");
credentials = GoogleCredentials.getApplicationDefault();
}
return credentials.createScoped(scopes);
}
use of io.micronaut.context.exceptions.ConfigurationException in project micronaut-jms by micronaut-projects.
the class JMSProducerMethodInterceptor method intercept.
@Override
public Object intercept(MethodInvocationContext<Object, Object> context) {
if (!context.hasAnnotation(JMSProducer.class)) {
return context.proceed();
}
ExecutableMethod<?, ?> method = context.getExecutableMethod();
Class<? extends Annotation> annotationType;
JMSDestinationType destinationType;
if (method.hasAnnotation(Queue.class)) {
annotationType = Queue.class;
destinationType = QUEUE;
} else if (method.hasAnnotation(Topic.class)) {
annotationType = Topic.class;
destinationType = TOPIC;
} else {
return context.proceed();
}
String connectionFactory = method.stringValue(JMSProducer.class).orElseThrow(() -> new ConfigurationException("@JMSProducer must specify a connection factory."));
String destinationName = method.stringValue(annotationType).orElseThrow(() -> new ConfigurationException("@" + annotationType.getSimpleName() + " must specify a destination."));
Map<String, Object> parameterValueMap = context.getParameterValueMap();
Object body = Arrays.stream(method.getArguments()).filter(arg -> arg.isDeclaredAnnotationPresent(MessageBody.class)).map(arg -> parameterValueMap.get(arg.getName())).findFirst().orElseThrow(() -> new ConfigurationException("One method argument must be annotated with @Body"));
String serializerName = method.stringValue(annotationType, "serializer").orElse(null);
Serializer serializer = serializerName == null ? beanContext.getBean(DefaultSerializerDeserializer.class) : beanContext.getBean(Serializer.class, Qualifiers.byName(serializerName));
MessageHeader[] headers = Arrays.stream(method.getArguments()).filter(arg -> arg.isDeclaredAnnotationPresent(io.micronaut.messaging.annotation.MessageHeader.class)).map(arg -> {
String argName = arg.getName();
String headerName = arg.getAnnotationMetadata().stringValue(io.micronaut.messaging.annotation.MessageHeader.class).orElseThrow(() -> new IllegalArgumentException("@Header annotation on argument '" + argName + "' must have a name"));
return new MessageHeader(headerName, parameterValueMap.get(argName));
}).toArray(MessageHeader[]::new);
JMSConnectionPool pool = beanContext.getBean(JMSConnectionPool.class, Qualifiers.byName(connectionFactory));
JmsProducer producer = new JmsProducer(destinationType, pool, serializer);
producer.send(destinationName, body, headers);
return null;
}
Aggregations