Search in sources :

Example 1 with MultiMap

use of org.mule.runtime.api.util.MultiMap in project mule by mulesoft.

the class HttpRequestBuilderTestCase method headerManipulation.

@Test
public void headerManipulation() {
    builder.uri(URI_VALUE);
    assertThat(builder.build().getHeaderNames(), empty());
    String otherValue = "otherValue";
    MultiMap<String, String> multiMap = new MultiMap<>();
    multiMap.put(name, value);
    multiMap.put(name, otherValue);
    // add multiple valued header through parameter map and individually
    builder.headers(multiMap);
    builder.addHeader(name, value);
    assertThat(builder.getHeaderValues(name), hasItems(value, otherValue, value));
    assertThat(builder.build().getHeaderValues(name), hasItems(value, otherValue, value));
    // remove header
    builder.removeHeader(name);
    assertThat(builder.build().getHeaderNames(), empty());
}
Also used : MultiMap(org.mule.runtime.api.util.MultiMap) UriCache.getUriFromString(org.mule.runtime.http.api.utils.UriCache.getUriFromString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 2 with MultiMap

use of org.mule.runtime.api.util.MultiMap in project mule by mulesoft.

the class MapToMultiMap method doTransform.

@Override
protected Object doTransform(Object src, Charset enc) throws TransformerException {
    Map map = (Map) src;
    MultiMap multiMap = new MultiMap(map);
    return multiMap;
}
Also used : MultiMap(org.mule.runtime.api.util.MultiMap) Map(java.util.Map) MultiMap(org.mule.runtime.api.util.MultiMap)

Example 3 with MultiMap

use of org.mule.runtime.api.util.MultiMap in project mule by mulesoft.

the class DefaultHttpMessageDispatcher method dispatch.

/**
 * {@inheritDoc}
 * <p>
 * Dispatches a Soap message through http adding the SoapAction header, if required, and the content-type.
 */
@Override
public DispatchingResponse dispatch(DispatchingRequest context) {
    InputStream content = logIfNeeded("Soap Request to [" + context.getAddress() + "]", context.getContent());
    HttpRequest request = HttpRequest.builder().uri(context.getAddress()).method(POST).entity(new InputStreamHttpEntity(content)).headers(new MultiMap<>(context.getHeaders())).build();
    try {
        HttpResponse response = client.send(request, DEFAULT_TIMEOUT_MILLIS, false, null);
        return new DispatchingResponse(logIfNeeded("Soap Response", response.getEntity().getContent()), toHeadersMap(response));
    } catch (IOException e) {
        throw new DispatchingException("An error occurred while sending the SOAP request");
    } catch (TimeoutException e) {
        throw new DispatchingException("The SOAP request timed out", e);
    }
}
Also used : HttpRequest(org.mule.runtime.http.api.domain.message.request.HttpRequest) MultiMap(org.mule.runtime.api.util.MultiMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InputStreamHttpEntity(org.mule.runtime.http.api.domain.entity.InputStreamHttpEntity) HttpResponse(org.mule.runtime.http.api.domain.message.response.HttpResponse) IOException(java.io.IOException) DispatchingException(org.mule.runtime.soap.api.exception.DispatchingException) DispatchingResponse(org.mule.runtime.extension.api.soap.message.DispatchingResponse) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with MultiMap

use of org.mule.runtime.api.util.MultiMap in project mule by mulesoft.

the class ReflectiveHttpConfigBasedRequester method request.

private Pair<InputStream, Map<String, String>> request(String method, String url, Map<String, String> headers, InputStream body) {
    DefaultOperationParametersBuilder params = builder().configName(configName).addParameter("method", method).addParameter("url", url).addParameter("headers", new MultiMap<>(headers)).addParameter("targetValue", "#[payload]");
    if (body != null) {
        params.addParameter("body", new TypedValue<>(body, INPUT_STREAM));
    }
    try {
        Result<Object, Object> result = client.executeAsync("HTTP", "request", params.build()).get();
        Map<String, String> httpHeaders = getHttpHeaders(result);
        InputStream content = getContent(result);
        return new Pair<>(content, httpHeaders);
    } catch (Exception e) {
        throw new DispatchingException("Could not dispatch soap message using the [" + configName + "] HTTP configuration", e);
    }
}
Also used : MultiMap(org.mule.runtime.api.util.MultiMap) InputStream(java.io.InputStream) InvocationTargetException(java.lang.reflect.InvocationTargetException) DispatchingException(org.mule.runtime.soap.api.exception.DispatchingException) DispatchingException(org.mule.runtime.soap.api.exception.DispatchingException) DefaultOperationParametersBuilder(org.mule.runtime.extension.api.client.DefaultOperationParametersBuilder) Pair(org.mule.runtime.api.util.Pair)

Example 5 with MultiMap

use of org.mule.runtime.api.util.MultiMap in project mule by mulesoft.

the class HttpRequestBuilderTestCase method complexResponse.

@Test
public void complexResponse() {
    MultiMap<String, String> paramMap = new MultiMap<>();
    paramMap.put(name, value);
    HttpRequest request = builder.entity(new ByteArrayHttpEntity("test".getBytes())).uri(URI_VALUE).method(POST).queryParams(paramMap).headers(paramMap).addHeader(name.toUpperCase(), value.toUpperCase()).build();
    assertThat(request.getUri(), is(URI.create(URI_VALUE)));
    assertThat(request.getMethod(), is(POST.name()));
    assertThat(request.getEntity(), is(instanceOf(ByteArrayHttpEntity.class)));
    assertThat(request.getHeaderNames(), hasItems(name));
    assertThat(request.getHeaderValues(name), hasItems(value, value.toUpperCase()));
    MultiMap<String, String> requestQueryParams = request.getQueryParams();
    assertThat(requestQueryParams.keySet(), hasItems(name));
    assertThat(requestQueryParams.getAll(name), hasItems(value));
}
Also used : HttpRequest(org.mule.runtime.http.api.domain.message.request.HttpRequest) MultiMap(org.mule.runtime.api.util.MultiMap) UriCache.getUriFromString(org.mule.runtime.http.api.utils.UriCache.getUriFromString) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayHttpEntity(org.mule.runtime.http.api.domain.entity.ByteArrayHttpEntity) Test(org.junit.Test)

Aggregations

MultiMap (org.mule.runtime.api.util.MultiMap)5 InputStream (java.io.InputStream)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Test (org.junit.Test)2 HttpRequest (org.mule.runtime.http.api.domain.message.request.HttpRequest)2 UriCache.getUriFromString (org.mule.runtime.http.api.utils.UriCache.getUriFromString)2 DispatchingException (org.mule.runtime.soap.api.exception.DispatchingException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Map (java.util.Map)1 TimeoutException (java.util.concurrent.TimeoutException)1 Pair (org.mule.runtime.api.util.Pair)1 DefaultOperationParametersBuilder (org.mule.runtime.extension.api.client.DefaultOperationParametersBuilder)1 DispatchingResponse (org.mule.runtime.extension.api.soap.message.DispatchingResponse)1 ByteArrayHttpEntity (org.mule.runtime.http.api.domain.entity.ByteArrayHttpEntity)1 InputStreamHttpEntity (org.mule.runtime.http.api.domain.entity.InputStreamHttpEntity)1 HttpResponse (org.mule.runtime.http.api.domain.message.response.HttpResponse)1