Search in sources :

Example 6 with ConfigurationException

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);
}
Also used : Base64(java.util.Base64) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) ByteArrayInputStream(java.io.ByteArrayInputStream) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) FileInputStream(java.io.FileInputStream) Requires(io.micronaut.context.annotation.Requires) Singleton(jakarta.inject.Singleton) Primary(io.micronaut.context.annotation.Primary)

Example 7 with ConfigurationException

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;
}
Also used : ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) Arrays(java.util.Arrays) TOPIC(io.micronaut.jms.model.JMSDestinationType.TOPIC) BeanContext(io.micronaut.context.BeanContext) JMSConnectionPool(io.micronaut.jms.pool.JMSConnectionPool) QUEUE(io.micronaut.jms.model.JMSDestinationType.QUEUE) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) Singleton(jakarta.inject.Singleton) Queue(io.micronaut.jms.annotations.Queue) DefaultSerializerDeserializer(io.micronaut.jms.serdes.DefaultSerializerDeserializer) Topic(io.micronaut.jms.annotations.Topic) ExecutableMethod(io.micronaut.inject.ExecutableMethod) JmsProducer(io.micronaut.jms.templates.JmsProducer) JMSDestinationType(io.micronaut.jms.model.JMSDestinationType) JMSProducer(io.micronaut.jms.annotations.JMSProducer) MethodInterceptor(io.micronaut.aop.MethodInterceptor) MethodInvocationContext(io.micronaut.aop.MethodInvocationContext) Map(java.util.Map) Annotation(java.lang.annotation.Annotation) MessageHeader(io.micronaut.jms.model.MessageHeader) Serializer(io.micronaut.jms.serdes.Serializer) MessageBody(io.micronaut.messaging.annotation.MessageBody) DefaultSerializerDeserializer(io.micronaut.jms.serdes.DefaultSerializerDeserializer) JmsProducer(io.micronaut.jms.templates.JmsProducer) JMSProducer(io.micronaut.jms.annotations.JMSProducer) JMSDestinationType(io.micronaut.jms.model.JMSDestinationType) JMSConnectionPool(io.micronaut.jms.pool.JMSConnectionPool) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) MessageHeader(io.micronaut.jms.model.MessageHeader) Topic(io.micronaut.jms.annotations.Topic) Serializer(io.micronaut.jms.serdes.Serializer)

Aggregations

ConfigurationException (io.micronaut.context.exceptions.ConfigurationException)7 JMSConnectionPool (io.micronaut.jms.pool.JMSConnectionPool)2 Singleton (jakarta.inject.Singleton)2 Properties (java.util.Properties)2 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)1 ConnectionString (com.mongodb.ConnectionString)1 MethodInterceptor (io.micronaut.aop.MethodInterceptor)1 MethodInvocationContext (io.micronaut.aop.MethodInvocationContext)1 KafkaClient (io.micronaut.configuration.kafka.annotation.KafkaClient)1 DefaultMongoConfiguration (io.micronaut.configuration.mongo.core.DefaultMongoConfiguration)1 BeanContext (io.micronaut.context.BeanContext)1 Any (io.micronaut.context.annotation.Any)1 Bean (io.micronaut.context.annotation.Bean)1 Primary (io.micronaut.context.annotation.Primary)1 Prototype (io.micronaut.context.annotation.Prototype)1 Requires (io.micronaut.context.annotation.Requires)1 ArgumentInjectionPoint (io.micronaut.inject.ArgumentInjectionPoint)1 ExecutableMethod (io.micronaut.inject.ExecutableMethod)1 FieldInjectionPoint (io.micronaut.inject.FieldInjectionPoint)1 Qualifiers (io.micronaut.inject.qualifiers.Qualifiers)1