-
Notifications
You must be signed in to change notification settings - Fork 1.1k
GH-3154: Support UriBuilderFactory.EncodingMode
#3162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
473bbb4
4002501
615c36d
6a30f1c
a884203
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2017-2019 the original author or authors. | ||
* Copyright 2017-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -17,7 +17,6 @@ | |
package org.springframework.integration.http.outbound; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
|
@@ -27,7 +26,6 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.function.Supplier; | ||
|
||
import javax.xml.transform.Source; | ||
|
||
|
@@ -56,14 +54,13 @@ | |
import org.springframework.integration.support.MessageBuilderFactory; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.messaging.MessageHandlingException; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.ClassUtils; | ||
import org.springframework.util.CollectionUtils; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.util.UriComponents; | ||
import org.springframework.web.util.DefaultUriBuilderFactory; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
/** | ||
|
@@ -85,6 +82,8 @@ public abstract class AbstractHttpRequestExecutingMessageHandler extends Abstrac | |
private static final List<HttpMethod> NO_BODY_HTTP_METHODS = | ||
Arrays.asList(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.TRACE); | ||
|
||
DefaultUriBuilderFactory uriFactory = new DefaultUriBuilderFactory(); | ||
|
||
private final Map<String, Expression> uriVariableExpressions = new HashMap<>(); | ||
|
||
private final Expression uriExpression; | ||
|
@@ -95,8 +94,6 @@ public abstract class AbstractHttpRequestExecutingMessageHandler extends Abstrac | |
|
||
private boolean trustedSpel; | ||
|
||
private boolean encodeUri = true; | ||
|
||
private Expression httpMethodExpression = new ValueExpression<>(HttpMethod.POST); | ||
|
||
private boolean expectReply = true; | ||
|
@@ -118,6 +115,7 @@ public abstract class AbstractHttpRequestExecutingMessageHandler extends Abstrac | |
public AbstractHttpRequestExecutingMessageHandler(Expression uriExpression) { | ||
Assert.notNull(uriExpression, "URI Expression is required"); | ||
this.uriExpression = uriExpression; | ||
this.uriFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.TEMPLATE_AND_VALUES); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this needed? This is the default. |
||
} | ||
|
||
/** | ||
|
@@ -127,9 +125,25 @@ public AbstractHttpRequestExecutingMessageHandler(Expression uriExpression) { | |
* <code>true</code>. | ||
* @param encodeUri true if the URI should be encoded. | ||
* @see UriComponentsBuilder | ||
* @deprecated since 5.3 in favor of {@link #setEncodingMode} | ||
*/ | ||
@Deprecated | ||
public void setEncodeUri(boolean encodeUri) { | ||
this.encodeUri = encodeUri; | ||
setEncodingMode( | ||
encodeUri | ||
? DefaultUriBuilderFactory.EncodingMode.TEMPLATE_AND_VALUES | ||
: DefaultUriBuilderFactory.EncodingMode.NONE); | ||
} | ||
|
||
/** | ||
* Set the encoding mode to use. | ||
* By default this is set to {@link DefaultUriBuilderFactory.EncodingMode#TEMPLATE_AND_VALUES}. | ||
* @param encodingMode the mode to use for uri encoding | ||
* @since 5.3 | ||
*/ | ||
public void setEncodingMode(DefaultUriBuilderFactory.EncodingMode encodingMode) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. I'm going to document that for more complex scenarios it would be better to configure an external I would even go with all the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, makes sense; proceed with the remaining work 😄 |
||
Assert.notNull(encodingMode, "'encodingMode' must not be null"); | ||
this.uriFactory.setEncodingMode(encodingMode); | ||
} | ||
|
||
/** | ||
|
@@ -291,32 +305,23 @@ protected Object handleRequestMessage(Message<?> requestMessage) { | |
Object expectedResponseType = determineExpectedResponseType(requestMessage); | ||
|
||
HttpEntity<?> httpRequest = generateHttpRequest(requestMessage, httpMethod); | ||
return exchange(() -> generateUri(requestMessage), httpMethod, httpRequest, expectedResponseType, | ||
requestMessage); | ||
} | ||
|
||
protected abstract Object exchange(Supplier<URI> uriSupplier, HttpMethod httpMethod, HttpEntity<?> httpRequest, | ||
Object expectedResponseType, Message<?> requestMessage); | ||
|
||
private URI generateUri(Message<?> requestMessage) { | ||
Object uri = this.uriExpression.getValue(this.evaluationContext, requestMessage); | ||
Assert.state(uri instanceof String || uri instanceof URI, | ||
() -> "'uriExpression' evaluation must result in a 'String' or 'URI' instance, not: " | ||
+ (uri == null ? "null" : uri.getClass())); | ||
Map<String, ?> uriVariables = determineUriVariables(requestMessage); | ||
UriComponentsBuilder uriComponentsBuilder = | ||
uri instanceof String | ||
? UriComponentsBuilder.fromUriString((String) uri) | ||
: UriComponentsBuilder.fromUri((URI) uri); | ||
UriComponents uriComponents = uriComponentsBuilder.buildAndExpand(uriVariables); | ||
try { | ||
return this.encodeUri ? uriComponents.encode().toUri() : new URI(uriComponents.toUriString()); | ||
} | ||
catch (URISyntaxException e) { | ||
throw new MessageHandlingException(requestMessage, "Invalid URI [" + uri + "] in the [" + this + ']', e); | ||
|
||
Map<String, ?> uriVariables = null; | ||
|
||
if (uri instanceof String) { | ||
uriVariables = determineUriVariables(requestMessage); | ||
} | ||
|
||
return exchange(uri, httpMethod, httpRequest, expectedResponseType, requestMessage, uriVariables); | ||
} | ||
|
||
protected abstract Object exchange(Object uri, HttpMethod httpMethod, HttpEntity<?> httpRequest, | ||
Object expectedResponseType, Message<?> requestMessage, Map<String, ?> uriVariables); | ||
|
||
protected Object getReply(ResponseEntity<?> httpResponse) { | ||
if (this.expectReply) { | ||
HttpHeaders httpHeaders = httpResponse.getHeaders(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why package visibility?
Why not
UriBuilderFactory
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
UriBuilderFactory
doesn't have asetEncodingMode()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and package?