Search in sources :

Example 96 with MuleRuntimeException

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

the class DefaultExtensionsOAuthManager method refreshToken.

/**
 * {@inheritDoc}
 */
@Override
public void refreshToken(String ownerConfigName, String resourceOwnerId, OAuthConnectionProviderWrapper connectionProvider) {
    AuthorizationCodeOAuthDancer dancer = dancers.get(ownerConfigName);
    try {
        dancer.refreshToken(resourceOwnerId).get();
        connectionProvider.updateAuthState();
    } catch (Exception e) {
        throw new MuleRuntimeException(createStaticMessage(format("Could not refresh token for resourceOwnerId '%s' using config '%s'", resourceOwnerId, ownerConfigName)), e);
    }
}
Also used : MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) AuthorizationCodeOAuthDancer(org.mule.runtime.oauth.api.AuthorizationCodeOAuthDancer) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ServerNotFoundException(org.mule.runtime.http.api.server.ServerNotFoundException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) MuleException(org.mule.runtime.api.exception.MuleException) MalformedURLException(java.net.MalformedURLException)

Example 97 with MuleRuntimeException

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

the class ReflectiveFunctionExecutorFactory method createExecutor.

@Override
public FunctionExecutor createExecutor(FunctionModel functionModel, FunctionParameterDefaultValueResolverFactory defaultResolverFactory) {
    DataType returnType = fromType(getType(functionModel.getOutput().getType()).orElseThrow(() -> new MuleRuntimeException(createStaticMessage(format("Failed to obtain the return type for function [%s]", functionModel.getName())))));
    List<FunctionParameter> functionParameters = functionModel.getAllParameterModels().stream().map(p -> {
        MetadataType paramType = p.getType();
        DataType type = isTypedValue(paramType) ? fromType(TypedValue.class) : toDataType(paramType);
        if (p.isRequired()) {
            return new FunctionParameter(p.getName(), type);
        }
        Object defaultValue = p.getDefaultValue();
        if (defaultValue == null) {
            return new FunctionParameter(p.getName(), type, context -> Defaults.defaultValue(type.getType()));
        }
        return new FunctionParameter(p.getName(), type, defaultResolverFactory.create(defaultValue, type));
    }).collect(toList());
    return new ReflectiveExpressionFunctionExecutor(functionModel, returnType, functionParameters, method, getDelegateInstance());
}
Also used : FunctionModel(org.mule.runtime.api.meta.model.function.FunctionModel) DataType(org.mule.runtime.api.metadata.DataType) Defaults(com.google.common.base.Defaults) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) ReflectiveMethodOperationExecutor(org.mule.runtime.module.extension.internal.runtime.operation.ReflectiveMethodOperationExecutor) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) String.format(java.lang.String.format) TypedValue(org.mule.runtime.api.metadata.TypedValue) Collectors.toList(java.util.stream.Collectors.toList) ExtensionMetadataTypeUtils.getType(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getType) List(java.util.List) DataType.fromType(org.mule.runtime.api.metadata.DataType.fromType) IntrospectionUtils.toDataType(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.toDataType) MetadataType(org.mule.metadata.api.model.MetadataType) FunctionParameter(org.mule.runtime.api.metadata.FunctionParameter) Method(java.lang.reflect.Method) ExtensionMetadataTypeUtils.isTypedValue(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isTypedValue) ComponentExecutorFactory(org.mule.runtime.extension.api.runtime.operation.ComponentExecutorFactory) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) MetadataType(org.mule.metadata.api.model.MetadataType) DataType(org.mule.runtime.api.metadata.DataType) IntrospectionUtils.toDataType(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.toDataType) FunctionParameter(org.mule.runtime.api.metadata.FunctionParameter)

Example 98 with MuleRuntimeException

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

the class OAuthOperationMessageProcessor method doProcess.

@Override
protected Mono<CoreEvent> doProcess(CoreEvent event, ExecutionContextAdapter<OperationModel> operationContext) {
    return super.doProcess(event, operationContext).onErrorResume(AccessTokenExpiredException.class, e -> {
        OAuthConnectionProviderWrapper connectionProvider = getOAuthConnectionProvider(operationContext);
        if (connectionProvider == null) {
            return error(e);
        }
        AccessTokenExpiredException expiredException = getTokenExpirationException(e);
        if (expiredException != null) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(format("AccessToken for resourceOwner '%s' expired while executing operation '%s:%s' using config '%s'. " + "Will attempt to refresh token and retry operation", connectionProvider.getResourceOwnerId(), getExtensionModel().getName(), operationContext.getComponentModel().getName(), operationContext.getConfiguration().get().getName()));
            }
            String ownerConfigName = operationContext.getConfiguration().get().getName();
            try {
                oauthManager.refreshToken(ownerConfigName, expiredException.getResourceOwnerId(), getOAuthConnectionProvider(operationContext));
            } catch (Exception refreshException) {
                return error(new MuleRuntimeException(createStaticMessage(format("AccessToken for resourceOwner '%s' expired while executing operation '%s:%s' using config '%s'. Refresh token " + "workflow was attempted but failed with the following exception", connectionProvider.getResourceOwnerId(), getExtensionModel().getName(), operationContext.getComponentModel().getName(), operationContext.getConfiguration().get().getName())), refreshException));
            }
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(format("Access Token successfully refreshed for resourceOwnerId '%s' on config '%s'", connectionProvider.getResourceOwnerId(), operationContext.getConfiguration().get().getName()));
            }
            return super.doProcess(event, operationContext);
        } else {
            return error(e);
        }
    });
}
Also used : AccessTokenExpiredException(org.mule.runtime.extension.api.connectivity.oauth.AccessTokenExpiredException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) OAuthConnectionProviderWrapper(org.mule.runtime.module.extension.internal.runtime.connectivity.oauth.OAuthConnectionProviderWrapper) AccessTokenExpiredException(org.mule.runtime.extension.api.connectivity.oauth.AccessTokenExpiredException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException)

Example 99 with MuleRuntimeException

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

the class ConnectionArgumentResolver method resolve.

/**
 * Returns the connection previously set on the {@code executionContext} under the key
 * {@link ExtensionProperties#CONNECTION_PARAM}
 *
 * @param executionContext an {@link ExecutionContext}
 * @return the connection
 * @throws IllegalArgumentException if the connection was not set
 * @throws ClassCastException if {@code executionContext} is not an {@link ExecutionContextAdapter}
 */
@Override
public LazyValue<Object> resolve(ExecutionContext executionContext) {
    return new LazyValue<>(() -> {
        ConnectionHandler connectionHandler = ((ExecutionContextAdapter<ComponentModel>) executionContext).getVariable(CONNECTION_PARAM);
        checkArgument(connectionHandler != null, "No connection was provided for the component [" + executionContext.getComponentModel().getName() + "]");
        try {
            return connectionHandler.getConnection();
        } catch (ConnectionException e) {
            throw new MuleRuntimeException(I18nMessageFactory.createStaticMessage(String.format("Error was found trying to obtain a connection to execute %s '%s' of extension '%s'", getComponentModelTypeName(executionContext.getComponentModel()), executionContext.getComponentModel().getName(), executionContext.getExtensionModel().getName())), e);
        }
    });
}
Also used : LazyValue(org.mule.runtime.api.util.LazyValue) ConnectionHandler(org.mule.runtime.api.connection.ConnectionHandler) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ExecutionContextAdapter(org.mule.runtime.module.extension.api.runtime.privileged.ExecutionContextAdapter) ConnectionException(org.mule.runtime.api.connection.ConnectionException)

Example 100 with MuleRuntimeException

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

the class OAuthConnectionProviderObjectBuilder method getCustomParameters.

private Map<String, String> getCustomParameters(CoreEvent event) {
    Map<String, String> oauthParams = new HashMap<>();
    withCustomParameters((parameter, property) -> {
        String alias = property.getRequestAlias();
        if (StringUtils.isBlank(alias)) {
            alias = parameter.getName();
        }
        ValueResolver resolver = resolverSet.getResolvers().get(alias);
        if (resolver != null) {
            try {
                oauthParams.put(alias, resolveString(event, resolver));
            } catch (MuleException e) {
                throw new MuleRuntimeException(e);
            }
        }
    });
    return oauthParams;
}
Also used : HashMap(java.util.HashMap) MapValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.MapValueResolver) ValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) MuleException(org.mule.runtime.api.exception.MuleException)

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