Search in sources :

Example 1 with Result

use of org.mule.runtime.extension.api.runtime.operation.Result in project mule by mulesoft.

the class ExportedPackagesValidatorTestCase method setUpInvalidExtension.

private void setUpInvalidExtension() {
    OutputModel appleList = mockOutputModel(InternalTestClass.class);
    OperationModel firstOperation = mockOperationModel(appleList, mockOutputModel(InternalTestClass.class));
    withMethod(firstOperation, getApiMethods(InternalTestClass.class).stream().filter(m -> m.getName().equals("someOperation")).findFirst());
    ParameterModel parameter = mockParameterModel(Shape.class);
    OutputModel resultList = mockOutputModel(new TypeToken<List<Result<Apple, VeganAttributes>>>() {
    }.getType());
    OperationModel secondOperation = mockOperationModel(resultList, mockOutputModel(List.class), parameter);
    withMethod(secondOperation, empty());
    when(extensionModel.getOperationModels()).thenReturn(asList(firstOperation, secondOperation));
    visitableMock(firstOperation, secondOperation);
}
Also used : InternalTestClass(org.mule.test.module.extension.internal.InternalTestClass) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) TypeToken(com.google.common.reflect.TypeToken) Arrays.asList(java.util.Arrays.asList) List(java.util.List) OutputModel(org.mule.runtime.api.meta.model.OutputModel) OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel) Result(org.mule.runtime.extension.api.runtime.operation.Result)

Example 2 with Result

use of org.mule.runtime.extension.api.runtime.operation.Result in project mule by mulesoft.

the class DefaultExtensionsClient method executeAsync.

/**
 * {@inheritDoc}
 */
@Override
public <T, A> CompletableFuture<Result<T, A>> executeAsync(String extension, String operation, OperationParameters parameters) {
    OperationMessageProcessor processor = createProcessor(extension, operation, parameters);
    Mono<Result<T, A>> resultMono = process(processor).map(event -> Result.<T, A>builder(event.getMessage()).build()).onErrorMap(Exceptions::unwrap).doAfterTerminate(() -> disposeProcessor(processor));
    return resultMono.toFuture();
}
Also used : OperationMessageProcessor(org.mule.runtime.module.extension.internal.runtime.operation.OperationMessageProcessor) Result(org.mule.runtime.extension.api.runtime.operation.Result)

Example 3 with Result

use of org.mule.runtime.extension.api.runtime.operation.Result in project mule by mulesoft.

the class AbstractReturnDelegate method resolveMediaType.

/**
 * If provided, mimeType and encoding configured as operation parameters will take precedence over what comes with the message's
 * {@link DataType}.
 *
 * @param value
 * @param operationContext
 * @return
 */
protected MediaType resolveMediaType(Object value, ExecutionContextAdapter<ComponentModel> operationContext) {
    Charset existingEncoding = getDefaultEncoding(muleContext);
    MediaType mediaType = defaultMediaType;
    if (value instanceof Result) {
        final Optional<MediaType> optionalMediaType = ((Result) value).getMediaType();
        if (optionalMediaType.isPresent()) {
            mediaType = optionalMediaType.get();
            if (mediaType.getCharset().isPresent()) {
                existingEncoding = mediaType.getCharset().get();
            }
        }
    }
    if (mediaType == null) {
        mediaType = ANY;
    }
    if (operationContext.hasParameter(MIME_TYPE_PARAMETER_NAME)) {
        mediaType = MediaType.parse(operationContext.getParameter(MIME_TYPE_PARAMETER_NAME));
    }
    if (operationContext.hasParameter(ENCODING_PARAMETER_NAME)) {
        mediaType = mediaType.withCharset(parseCharset(operationContext.getParameter(ENCODING_PARAMETER_NAME)));
    } else {
        mediaType = mediaType.withCharset(existingEncoding);
    }
    return mediaType;
}
Also used : Charset(java.nio.charset.Charset) MediaTypeUtils.parseCharset(org.mule.runtime.api.metadata.MediaTypeUtils.parseCharset) MediaType(org.mule.runtime.api.metadata.MediaType) Result(org.mule.runtime.extension.api.runtime.operation.Result)

Example 4 with Result

use of org.mule.runtime.extension.api.runtime.operation.Result in project mule by mulesoft.

the class ExportedArtifactsCollectorTestCase method setup.

@Before
public void setup() {
    ClassLoaderModelProperty classLoaderModelProperty = new ClassLoaderModelProperty(getClass().getClassLoader());
    when(extensionModel.getModelProperty(ClassLoaderModelProperty.class)).thenReturn(of(classLoaderModelProperty));
    OutputModel appleList = mockOutputModel(new TypeToken<List<Apple>>() {
    }.getType());
    OperationModel firstOperation = mockOperationModel(appleList, mockOutputModel(List.class));
    withMethod(firstOperation, getApiMethods(HeisenbergOperations.class).stream().filter(m -> m.getName().equals("callGusFring")).findFirst());
    ParameterModel parameter = mockParameterModel(Shape.class);
    OutputModel resultList = mockOutputModel(new TypeToken<List<Result<Apple, VeganAttributes>>>() {
    }.getType());
    OperationModel secondOperation = mockOperationModel(resultList, mockOutputModel(List.class), parameter);
    withMethod(secondOperation, empty());
    when(extensionModel.getOperationModels()).thenReturn(asList(firstOperation, secondOperation));
    visitableMock(firstOperation, secondOperation);
    collector = new ExportedArtifactsCollector(extensionModel);
}
Also used : ClassLoaderModelProperty(org.mule.runtime.extension.api.property.ClassLoaderModelProperty) Apple(org.mule.tck.testmodels.fruit.Apple) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) TypeToken(com.google.common.reflect.TypeToken) Arrays.asList(java.util.Arrays.asList) List(java.util.List) OutputModel(org.mule.runtime.api.meta.model.OutputModel) OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel) Result(org.mule.runtime.extension.api.runtime.operation.Result) Before(org.junit.Before)

Example 5 with Result

use of org.mule.runtime.extension.api.runtime.operation.Result in project mule by mulesoft.

the class ReflectiveHttpConfigBasedRequester method getHttpHeaders.

/**
 * Reflectively introspects the result to find the HTTP Headers.
 *
 * @param result the {@link Result} returned by the http request operation
 */
private Map<String, String> getHttpHeaders(Result<Object, Object> result) {
    try {
        Optional httpAttributes = result.getAttributes();
        if (!httpAttributes.isPresent()) {
            throw new IllegalStateException("No Http Attributes found on the response, cannot get response headers.");
        }
        Object headers = httpAttributes.get().getClass().getMethod("getHeaders").invoke(httpAttributes.get());
        Map<String, List<String>> map = (Map<String, List<String>>) headers.getClass().getMethod("toListValuesMap").invoke(headers);
        return map.entrySet().stream().collect(toMap(e -> e.getKey(), e -> e.getValue().stream().collect(joining(" "))));
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        throw new IllegalStateException("Something went wrong when introspecting the http response attributes.", e);
    }
}
Also used : POST(org.mule.runtime.http.api.HttpConstants.Method.POST) DefaultOperationParametersBuilder(org.mule.runtime.extension.api.client.DefaultOperationParametersBuilder) CursorStreamProvider(org.mule.runtime.api.streaming.bytes.CursorStreamProvider) GET(org.mule.runtime.http.api.HttpConstants.Method.GET) Collectors.joining(java.util.stream.Collectors.joining) Result(org.mule.runtime.extension.api.runtime.operation.Result) DefaultOperationParameters.builder(org.mule.runtime.extension.api.client.DefaultOperationParameters.builder) InvocationTargetException(java.lang.reflect.InvocationTargetException) TypedValue(org.mule.runtime.api.metadata.TypedValue) List(java.util.List) Collectors.toMap(java.util.stream.Collectors.toMap) DispatchingException(org.mule.runtime.soap.api.exception.DispatchingException) Map(java.util.Map) MultiMap(org.mule.runtime.api.util.MultiMap) ExtensionsClient(org.mule.runtime.extension.api.client.ExtensionsClient) Optional(java.util.Optional) Pair(org.mule.runtime.api.util.Pair) INPUT_STREAM(org.mule.runtime.api.metadata.DataType.INPUT_STREAM) InputStream(java.io.InputStream) Optional(java.util.Optional) List(java.util.List) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) MultiMap(org.mule.runtime.api.util.MultiMap) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Result (org.mule.runtime.extension.api.runtime.operation.Result)10 List (java.util.List)6 TypeToken (com.google.common.reflect.TypeToken)3 Map (java.util.Map)3 InputStream (java.io.InputStream)2 Charset (java.nio.charset.Charset)2 ArrayList (java.util.ArrayList)2 Arrays.asList (java.util.Arrays.asList)2 Collectors.toMap (java.util.stream.Collectors.toMap)2 MuleException (org.mule.runtime.api.exception.MuleException)2 Disposable (org.mule.runtime.api.lifecycle.Disposable)2 OutputModel (org.mule.runtime.api.meta.model.OutputModel)2 OperationModel (org.mule.runtime.api.meta.model.operation.OperationModel)2 ParameterModel (org.mule.runtime.api.meta.model.parameter.ParameterModel)2 TypedValue (org.mule.runtime.api.metadata.TypedValue)2 Optional (org.mule.runtime.extension.api.annotation.param.Optional)2 Summary (org.mule.runtime.extension.api.annotation.param.display.Summary)2 DefaultOperationParameters.builder (org.mule.runtime.extension.api.client.DefaultOperationParameters.builder)2 DefaultOperationParametersBuilder (org.mule.runtime.extension.api.client.DefaultOperationParametersBuilder)2 ExtensionsClient (org.mule.runtime.extension.api.client.ExtensionsClient)2