Search in sources :

Example 91 with MuleRuntimeException

use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.

the class ConfigurationProviderObjectFactory method createInnerInstance.

private ConfigurationProvider createInnerInstance() throws ConfigurationException {
    if (expirationPolicy == null) {
        expirationPolicy = muleContext.getConfiguration().getDynamicConfigExpiration().getExpirationPolicy();
    }
    ResolverSet resolverSet = getParametersResolver().getParametersAsHashedResolverSet(configurationModel, muleContext);
    final ConnectionProviderValueResolver connectionProviderResolver = getConnectionProviderResolver();
    return withContextClassLoader(getExtensionClassLoader(), () -> {
        connectionProviderResolver.getResolverSet().ifPresent((CheckedConsumer) resolver -> initialiseIfNeeded(resolver, true, muleContext));
        ConfigurationProvider configurationProvider;
        try {
            if (resolverSet.isDynamic() || connectionProviderResolver.isDynamic()) {
                configurationProvider = configurationProviderFactory.createDynamicConfigurationProvider(name, extensionModel, configurationModel, resolverSet, connectionProviderResolver, expirationPolicy, reflectionCache, muleContext);
            } else {
                configurationProvider = configurationProviderFactory.createStaticConfigurationProvider(name, extensionModel, configurationModel, resolverSet, connectionProviderResolver, reflectionCache, muleContext);
            }
        } catch (Exception e) {
            throw new MuleRuntimeException(createStaticMessage(format("Could not create an implicit configuration '%s' for the extension '%s'", configurationModel.getName(), extensionModel.getName())), e);
        }
        return configurationProvider;
    });
}
Also used : ResolverSet(org.mule.runtime.module.extension.internal.runtime.resolver.ResolverSet) Optional.empty(java.util.Optional.empty) Thread.currentThread(java.lang.Thread.currentThread) LifecycleUtils.initialiseIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.initialiseIfNeeded) ConnectionProviderValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ConnectionProviderValueResolver) ResolverSet(org.mule.runtime.module.extension.internal.runtime.resolver.ResolverSet) ExpirationPolicy(org.mule.runtime.extension.api.runtime.ExpirationPolicy) MuleContext(org.mule.runtime.core.api.MuleContext) DefaultConfigurationProviderFactory(org.mule.runtime.module.extension.internal.runtime.config.DefaultConfigurationProviderFactory) ClassUtils.withContextClassLoader(org.mule.runtime.core.api.util.ClassUtils.withContextClassLoader) ObjectFactory(org.mule.runtime.dsl.api.component.ObjectFactory) CheckedConsumer(org.mule.runtime.core.api.util.func.CheckedConsumer) ConfigurationException(org.mule.runtime.core.api.config.ConfigurationException) ConfigurationProvider(org.mule.runtime.extension.api.runtime.config.ConfigurationProvider) Optional.ofNullable(java.util.Optional.ofNullable) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) ImplicitConnectionProviderValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ImplicitConnectionProviderValueResolver) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ConfigurationModel(org.mule.runtime.api.meta.model.config.ConfigurationModel) ClassLoaderModelProperty(org.mule.runtime.extension.api.property.ClassLoaderModelProperty) StaticConnectionProviderResolver(org.mule.runtime.module.extension.internal.runtime.resolver.StaticConnectionProviderResolver) String.format(java.lang.String.format) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) ConnectionProviderResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ConnectionProviderResolver) AbstractExtensionObjectFactory(org.mule.runtime.module.extension.internal.config.dsl.AbstractExtensionObjectFactory) ConfigurationProviderFactory(org.mule.runtime.module.extension.internal.runtime.config.ConfigurationProviderFactory) Optional(java.util.Optional) ConfigurationProvider(org.mule.runtime.extension.api.runtime.config.ConfigurationProvider) ConnectionProviderValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ConnectionProviderValueResolver) ImplicitConnectionProviderValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ImplicitConnectionProviderValueResolver) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ConfigurationException(org.mule.runtime.core.api.config.ConfigurationException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException)

Example 92 with MuleRuntimeException

use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.

the class DefaultConnectionProviderObjectBuilder method applyExtensionClassLoaderProxy.

/**
 * Wraps the {@link ConnectionProvider} inside of a dynamic proxy which changes the current {@link ClassLoader} to the the
 * extension's {@link ClassLoader} when executing any method of this wrapped {@link ConnectionProvider}
 * <p>
 * This ensures that every time that the {@link ConnectionProvider} is used, it will work in the correct classloader.
 * <p>
 * Although if the {@link ConnectionProvider} is created with the correct classloader and then used with an incorrect one this
 * may work, due that static class references were loaded correctly, logic loading class in a dynamic way will fail.
 *
 * @param provider The {@link ConnectionProvider} to wrap
 * @return The wrapped {@link ConnectionProvider}
 */
private ConnectionProvider<C> applyExtensionClassLoaderProxy(ConnectionProvider provider) {
    Enhancer enhancer = new Enhancer();
    ClassLoader classLoader = getClassLoader(extensionModel);
    Class[] proxyInterfaces = getProxyInterfaces(provider);
    enhancer.setInterfaces(proxyInterfaces);
    MethodInterceptor returnProviderInterceptor = (obj, method, args, proxy) -> provider;
    MethodInterceptor invokerInterceptor = (obj, method, args, proxy) -> {
        Reference<Object> resultReference = new Reference<>();
        Reference<Throwable> errorReference = new Reference<>();
        withContextClassLoader(classLoader, () -> {
            try {
                resultReference.set(method.invoke(provider, args));
            } catch (InvocationTargetException e) {
                errorReference.set(e.getTargetException());
            } catch (Throwable t) {
                errorReference.set(t);
            }
        });
        if (errorReference.get() != null) {
            throw errorReference.get();
        } else {
            return resultReference.get();
        }
    };
    CallbackHelper callbackHelper = new CallbackHelper(Object.class, proxyInterfaces) {

        @Override
        protected Object getCallback(Method method) {
            if (method.getDeclaringClass().equals(HasDelegate.class) && method.getName().equals("getDelegate")) {
                return returnProviderInterceptor;
            } else {
                return invokerInterceptor;
            }
        }
    };
    enhancer.setCallbackTypes(callbackHelper.getCallbackTypes());
    enhancer.setCallbackFilter(callbackHelper);
    if (Enhancer.class.getClassLoader() != classLoader) {
        enhancer.setClassLoader(new CompositeClassLoader(DefaultConnectionProviderObjectBuilder.class.getClassLoader(), classLoader));
        enhancer.setUseCache(false);
    }
    Class<ConnectionProvider<C>> proxyClass = enhancer.createClass();
    registerStaticCallbacks(proxyClass, callbackHelper.getCallbacks());
    try {
        return proxyClass.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw new MuleRuntimeException(e);
    }
}
Also used : ErrorTypeHandlerConnectionProviderWrapper(org.mule.runtime.core.internal.connection.ErrorTypeHandlerConnectionProviderWrapper) ConnectionProviderModel(org.mule.runtime.api.meta.model.connection.ConnectionProviderModel) MuleExtensionUtils.getClassLoader(org.mule.runtime.module.extension.internal.util.MuleExtensionUtils.getClassLoader) ResolverSet(org.mule.runtime.module.extension.internal.runtime.resolver.ResolverSet) POOLING(org.mule.runtime.api.meta.model.connection.ConnectionManagementType.POOLING) ClassUtils(org.apache.commons.lang3.ClassUtils) MuleContext(org.mule.runtime.core.api.MuleContext) MuleException(org.mule.runtime.api.exception.MuleException) ReconnectableConnectionProviderWrapper(org.mule.runtime.core.internal.connection.ReconnectableConnectionProviderWrapper) PoolingProfile(org.mule.runtime.api.config.PoolingProfile) ClassUtils.withContextClassLoader(org.mule.runtime.core.api.util.ClassUtils.withContextClassLoader) Pair(org.mule.runtime.api.util.Pair) Enhancer(net.sf.cglib.proxy.Enhancer) Method(java.lang.reflect.Method) PoolingConnectionProviderWrapper(org.mule.runtime.core.internal.connection.PoolingConnectionProviderWrapper) IntrospectionUtils.injectFields(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.injectFields) Enhancer.registerStaticCallbacks(net.sf.cglib.proxy.Enhancer.registerStaticCallbacks) CallbackHelper(net.sf.cglib.proxy.CallbackHelper) ConnectionProvider(org.mule.runtime.api.connection.ConnectionProvider) ResolverSetBasedObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.ResolverSetBasedObjectBuilder) MethodInterceptor(net.sf.cglib.proxy.MethodInterceptor) CompositeClassLoader(org.mule.runtime.core.internal.util.CompositeClassLoader) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) ConnectionManagementType(org.mule.runtime.api.meta.model.connection.ConnectionManagementType) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) HasDelegate(org.mule.runtime.core.internal.connection.HasDelegate) ReconnectionConfig(org.mule.runtime.core.internal.retry.ReconnectionConfig) Reference(org.mule.runtime.api.util.Reference) ResolverSetResult(org.mule.runtime.module.extension.internal.runtime.resolver.ResolverSetResult) Enhancer(net.sf.cglib.proxy.Enhancer) CallbackHelper(net.sf.cglib.proxy.CallbackHelper) Reference(org.mule.runtime.api.util.Reference) Method(java.lang.reflect.Method) HasDelegate(org.mule.runtime.core.internal.connection.HasDelegate) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConnectionProvider(org.mule.runtime.api.connection.ConnectionProvider) MethodInterceptor(net.sf.cglib.proxy.MethodInterceptor) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) MuleExtensionUtils.getClassLoader(org.mule.runtime.module.extension.internal.util.MuleExtensionUtils.getClassLoader) ClassUtils.withContextClassLoader(org.mule.runtime.core.api.util.ClassUtils.withContextClassLoader) CompositeClassLoader(org.mule.runtime.core.internal.util.CompositeClassLoader) CompositeClassLoader(org.mule.runtime.core.internal.util.CompositeClassLoader)

Example 93 with MuleRuntimeException

use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.

the class ComponentMessageProcessor method createComponentExecutor.

private ComponentExecutor<T> createComponentExecutor() {
    Map<String, Object> params = new HashMap<>();
    LazyValue<Optional<ConfigurationInstance>> staticConfiguration = new LazyValue<>(this::getStaticConfiguration);
    LazyValue<ValueResolvingContext> resolvingContext = new LazyValue<>(() -> {
        CoreEvent initialiserEvent = null;
        try {
            initialiserEvent = getInitialiserEvent();
            return from(initialiserEvent, staticConfiguration.get());
        } finally {
            if (initialiserEvent != null) {
                ((BaseEventContext) initialiserEvent.getContext()).success();
            }
        }
    });
    componentModel.getParameterGroupModels().stream().forEach(group -> {
        if (group.getName().equals(DEFAULT_GROUP_NAME)) {
            group.getParameterModels().stream().filter(p -> p.getModelProperty(FieldOperationParameterModelProperty.class).isPresent()).forEach(p -> {
                ValueResolver<?> resolver = resolverSet.getResolvers().get(p.getName());
                if (resolver != null) {
                    try {
                        params.put(getMemberName(p), resolveValue(resolver, resolvingContext.get()));
                    } catch (MuleException e) {
                        throw new MuleRuntimeException(e);
                    }
                }
            });
        } else {
            ParameterGroupDescriptor groupDescriptor = group.getModelProperty(ParameterGroupModelProperty.class).map(g -> g.getDescriptor()).orElse(null);
            if (groupDescriptor == null) {
                return;
            }
            List<ParameterModel> fieldParameters = getGroupsOfFieldParameters(group);
            if (fieldParameters.isEmpty()) {
                return;
            }
            ObjectBuilder groupBuilder = createFieldParameterGroupBuilder(groupDescriptor, fieldParameters);
            try {
                params.put(((Field) groupDescriptor.getContainer()).getName(), groupBuilder.build(resolvingContext.get()));
            } catch (MuleException e) {
                throw new MuleRuntimeException(e);
            }
        }
    });
    return getOperationExecutorFactory(componentModel).createExecutor(componentModel, params);
}
Also used : IntrospectionUtils.getMemberName(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.getMemberName) FieldOperationParameterModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.FieldOperationParameterModelProperty) Optional.of(java.util.Optional.of) Mono.subscriberContext(reactor.core.publisher.Mono.subscriberContext) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ResolverUtils.resolveValue(org.mule.runtime.module.extension.internal.runtime.resolver.ResolverUtils.resolveValue) ParameterValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ParameterValueResolver) PROCESSOR_SCHEDULER_CONTEXT_KEY(org.mule.runtime.core.internal.processor.strategy.AbstractProcessingStrategy.PROCESSOR_SCHEDULER_CONTEXT_KEY) Scheduler(org.mule.runtime.api.scheduler.Scheduler) IMMEDIATE_SCHEDULER(org.mule.runtime.core.internal.util.rx.ImmediateScheduler.IMMEDIATE_SCHEDULER) MuleExtensionUtils.getOperationExecutorFactory(org.mule.runtime.module.extension.internal.util.MuleExtensionUtils.getOperationExecutorFactory) Map(java.util.Map) TARGET_PARAMETER_NAME(org.mule.runtime.extension.api.ExtensionConstants.TARGET_PARAMETER_NAME) OperationArgumentResolverFactory(org.mule.runtime.module.extension.internal.runtime.execution.OperationArgumentResolverFactory) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) LifecycleUtils.stopIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.stopIfNeeded) ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel) Exceptions(org.mule.runtime.core.api.rx.Exceptions) PolicyManager(org.mule.runtime.core.internal.policy.PolicyManager) ParametersResolverProcessor(org.mule.runtime.core.internal.processor.ParametersResolverProcessor) ParameterGroupDescriptor(org.mule.runtime.module.extension.internal.loader.ParameterGroupDescriptor) ConfigurationProvider(org.mule.runtime.extension.api.runtime.config.ConfigurationProvider) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ReflectionCache(org.mule.runtime.module.extension.internal.util.ReflectionCache) ConfigurationInstance(org.mule.runtime.extension.api.runtime.config.ConfigurationInstance) String.format(java.lang.String.format) OperationPolicy(org.mule.runtime.core.internal.policy.OperationPolicy) MuleExtensionUtils.getInitialiserEvent(org.mule.runtime.module.extension.api.util.MuleExtensionUtils.getInitialiserEvent) ObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.ObjectBuilder) Lifecycle(org.mule.runtime.api.lifecycle.Lifecycle) List(java.util.List) ComponentExecutorModelProperty(org.mule.runtime.module.extension.api.loader.java.property.ComponentExecutorModelProperty) Optional(java.util.Optional) CursorProviderFactory(org.mule.runtime.core.api.streaming.CursorProviderFactory) ValueResolvingContext.from(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolvingContext.from) ComponentExecutorFactory(org.mule.runtime.extension.api.runtime.operation.ComponentExecutorFactory) Optional.empty(java.util.Optional.empty) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) RetryPolicyTemplate(org.mule.runtime.core.api.retry.policy.RetryPolicyTemplate) Flux.from(reactor.core.publisher.Flux.from) ComponentModel(org.mule.runtime.api.meta.model.ComponentModel) ComponentExecutor(org.mule.runtime.extension.api.runtime.operation.ComponentExecutor) HashMap(java.util.HashMap) LifecycleUtils.initialiseIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.initialiseIfNeeded) ResolverSet(org.mule.runtime.module.extension.internal.runtime.resolver.ResolverSet) Processor(org.mule.runtime.core.api.processor.Processor) TARGET_VALUE_PARAMETER_NAME(org.mule.runtime.extension.api.ExtensionConstants.TARGET_VALUE_PARAMETER_NAME) DefaultObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder) MuleException(org.mule.runtime.api.exception.MuleException) Flux.error(reactor.core.publisher.Flux.error) BiConsumer(java.util.function.BiConsumer) DEFAULT_GROUP_NAME(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel.DEFAULT_GROUP_NAME) ParameterGroupModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ParameterGroupModelProperty) Mono.fromCallable(reactor.core.publisher.Mono.fromCallable) ExecutionContext(org.mule.runtime.extension.api.runtime.operation.ExecutionContext) ExecutionContextAdapter(org.mule.runtime.module.extension.api.runtime.privileged.ExecutionContextAdapter) DefaultExecutionContext(org.mule.runtime.module.extension.internal.runtime.DefaultExecutionContext) Logger(org.slf4j.Logger) ValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver) MessageProcessors.processToApply(org.mule.runtime.core.privileged.processor.MessageProcessors.processToApply) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) Exceptions.checkedFunction(org.mule.runtime.core.api.rx.Exceptions.checkedFunction) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) ComponentLocationProvider.resolveProcessorRepresentation(org.mule.runtime.api.util.ComponentLocationProvider.resolveProcessorRepresentation) LifecycleUtils.startIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.startIfNeeded) Mono(reactor.core.publisher.Mono) Field(java.lang.reflect.Field) ExtensionComponent(org.mule.runtime.module.extension.internal.runtime.ExtensionComponent) LazyExecutionContext(org.mule.runtime.module.extension.internal.runtime.LazyExecutionContext) Interceptor(org.mule.runtime.extension.api.runtime.operation.Interceptor) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) LifecycleUtils.disposeIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.disposeIfNeeded) INTERCEPTION_RESOLVED_CONTEXT(org.mule.runtime.core.internal.interception.DefaultInterceptionEvent.INTERCEPTION_RESOLVED_CONTEXT) Collectors.toList(java.util.stream.Collectors.toList) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) LazyValue(org.mule.runtime.api.util.LazyValue) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) ExtensionManager(org.mule.runtime.core.api.extension.ExtensionManager) IntrospectionUtils.isVoid(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.isVoid) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) DefaultFlowCallStack(org.mule.runtime.core.internal.context.notification.DefaultFlowCallStack) ValueResolvingContext(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolvingContext) OperationExecutionFunction(org.mule.runtime.core.internal.policy.OperationExecutionFunction) InternalEvent(org.mule.runtime.core.internal.message.InternalEvent) FieldOperationParameterModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.FieldOperationParameterModelProperty) LazyValue(org.mule.runtime.api.util.LazyValue) Optional(java.util.Optional) HashMap(java.util.HashMap) ParameterGroupDescriptor(org.mule.runtime.module.extension.internal.loader.ParameterGroupDescriptor) ObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.ObjectBuilder) DefaultObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ValueResolvingContext(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolvingContext) MuleException(org.mule.runtime.api.exception.MuleException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException)

Example 94 with MuleRuntimeException

use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.

the class ComponentMessageProcessorBuilder method build.

public P build() {
    return withContextClassLoader(getClassLoader(extensionModel), () -> {
        try {
            final ExtensionManager extensionManager = muleContext.getExtensionManager();
            final ResolverSet operationArguments = getArgumentsResolverSet();
            P processor = createMessageProcessor(extensionManager, operationArguments);
            // TODO: MULE-5002 this should not be necessary but lifecycle issues when injecting message processors automatically
            muleContext.getInjector().inject(processor);
            return processor;
        } catch (Exception e) {
            throw new MuleRuntimeException(e);
        }
    });
}
Also used : ResolverSet(org.mule.runtime.module.extension.internal.runtime.resolver.ResolverSet) ExtensionManager(org.mule.runtime.core.api.extension.ExtensionManager) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ConfigurationException(org.mule.runtime.core.api.config.ConfigurationException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException)

Example 95 with MuleRuntimeException

use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.

the class DefaultExtensionsOAuthManager method createDancer.

private AuthorizationCodeOAuthDancer createDancer(OAuthConfig config) throws MuleException {
    OAuthAuthorizationCodeDancerBuilder dancerBuilder = oauthService.get().authorizationCodeGrantTypeDancerBuilder(lockId -> lockFactory.createLock(lockId), new LazyObjectStoreToMapAdapter(getObjectStoreSupplier(config)), expressionEvaluator);
    final AuthCodeConfig authCodeConfig = config.getAuthCodeConfig();
    final AuthorizationCodeGrantType grantType = config.getGrantType();
    final OAuthCallbackConfig callbackConfig = config.getCallbackConfig();
    dancerBuilder.encoding(getDefaultEncoding(muleContext)).clientCredentials(authCodeConfig.getConsumerKey(), authCodeConfig.getConsumerSecret()).tokenUrl(authCodeConfig.getAccessTokenUrl()).responseExpiresInExpr(grantType.getExpirationRegex()).responseRefreshTokenExpr(grantType.getRefreshTokenExpr()).responseAccessTokenExpr(grantType.getAccessTokenExpr()).resourceOwnerIdTransformer(ownerId -> ownerId + "-" + config.getOwnerConfigName());
    String scopes = authCodeConfig.getScope().orElseGet(() -> grantType.getDefaultScope().orElse(null));
    if (scopes != null) {
        dancerBuilder.scopes(scopes);
    }
    HttpServer httpServer;
    try {
        httpServer = httpService.get().getServerFactory().lookup(callbackConfig.getListenerConfig());
    } catch (ServerNotFoundException e) {
        throw new MuleRuntimeException(createStaticMessage(format("Connector '%s' defines '%s' as the http:listener-config to use for provisioning callbacks, but no such definition " + "exists in the application configuration", config.getOwnerConfigName(), callbackConfig.getListenerConfig())), e);
    }
    dancerBuilder.localCallback(httpServer, callbackConfig.getCallbackPath()).externalCallbackUrl(getExternalCallback(httpServer, callbackConfig)).authorizationUrl(authCodeConfig.getAuthorizationUrl()).localAuthorizationUrlPath(callbackConfig.getLocalAuthorizePath()).localAuthorizationUrlResourceOwnerId("#[attributes.queryParams.resourceOwnerId]").state("#[attributes.queryParams.state]").customParameters(config.getCustomParameters()).customParametersExtractorsExprs(getParameterExtractors(config));
    Pair<Optional<Flow>, Optional<Flow>> listenerFlows = getListenerFlows(config);
    listenerFlows.getFirst().ifPresent(flow -> dancerBuilder.beforeDanceCallback(beforeCallback(config, flow)));
    listenerFlows.getSecond().ifPresent(flow -> dancerBuilder.afterDanceCallback(afterCallback(config, flow)));
    AuthorizationCodeOAuthDancer dancer = dancerBuilder.build();
    if (started) {
        start(dancer);
    }
    return dancer;
}
Also used : LazyObjectStoreToMapAdapter(org.mule.runtime.module.extension.internal.store.LazyObjectStoreToMapAdapter) Optional(java.util.Optional) HttpServer(org.mule.runtime.http.api.server.HttpServer) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) AuthorizationCodeGrantType(org.mule.runtime.extension.api.connectivity.oauth.AuthorizationCodeGrantType) ServerNotFoundException(org.mule.runtime.http.api.server.ServerNotFoundException) AuthorizationCodeOAuthDancer(org.mule.runtime.oauth.api.AuthorizationCodeOAuthDancer) OAuthAuthorizationCodeDancerBuilder(org.mule.runtime.oauth.api.builder.OAuthAuthorizationCodeDancerBuilder)

Aggregations

MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)123 IOException (java.io.IOException)22 List (java.util.List)22 MuleException (org.mule.runtime.api.exception.MuleException)22 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)22 ExtensionModel (org.mule.runtime.api.meta.model.ExtensionModel)22 Map (java.util.Map)20 Optional (java.util.Optional)20 I18nMessageFactory.createStaticMessage (org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage)18 ArrayList (java.util.ArrayList)17 String.format (java.lang.String.format)16 File (java.io.File)15 HashMap (java.util.HashMap)15 HashSet (java.util.HashSet)13 Set (java.util.Set)13 Collectors.toList (java.util.stream.Collectors.toList)12 ConfigurationException (org.mule.runtime.core.api.config.ConfigurationException)12 ComponentIdentifier (org.mule.runtime.api.component.ComponentIdentifier)10 Collections.emptyMap (java.util.Collections.emptyMap)9 Optional.empty (java.util.Optional.empty)9