Skip to content

Add queryParams method to ProxyExchange #2833

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.ClassUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
Expand Down Expand Up @@ -133,6 +134,7 @@
* </p>
*
* @author Dave Syer
* @author Stefan Berger
*
*/
public class ProxyExchange<T> {
Expand Down Expand Up @@ -251,11 +253,23 @@ public ProxyExchange<T> uri(String uri) {
}
}

/**
* Returns the request path without any query parameters.
* @return the URI path
* @see ProxyExchange#path(String)
* @see ProxyExchange#queryParams
*/
public String path() {
return (String) this.webRequest.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE,
WebRequest.SCOPE_REQUEST);
return exchange.getRequest().getPath().pathWithinApplication().value();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no exchange in spring mvc, this doesn't even compile

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was at the time I opened this MR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Server Web exchange is from spring webflux

}

/**
* Returns the request path without the provided path prefix and without any query parameters.
* @param prefix the path prefix to remove
* @return the URI path
* @see ProxyExchange#path(String)
* @see ProxyExchange#queryParams
*/
public String path(String prefix) {
String path = path();
if (!path.startsWith(prefix)) {
Expand All @@ -264,6 +278,14 @@ public String path(String prefix) {
return path.substring(prefix.length());
}

/**
* Returns the original query parameters.
* @return the query parameters
*/
public MultiValueMap<String, String> queryParams() {
return exchange.getRequest().getQueryParams();
}

public void forward(String path) {
HttpServletRequest request = this.webRequest.getNativeRequest(HttpServletRequest.class);
HttpServletResponse response = this.webRequest.getNativeResponse(HttpServletResponse.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.springframework.http.RequestEntity.BodyBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -107,6 +108,7 @@
* </p>
*
* @author Dave Syer
* @author Stefan Berger
*
*/
public class ProxyExchange<T> {
Expand Down Expand Up @@ -229,10 +231,23 @@ public ProxyExchange<T> uri(String uri) {
return this;
}

/**
* Returns the request path without any query parameters.
* @return the URI path
* @see ProxyExchange#path(String)
* @see ProxyExchange#queryParams
*/
public String path() {
return exchange.getRequest().getPath().pathWithinApplication().value();
}

/**
* Returns the request path without the provided path prefix and without any query parameters.
* @param prefix the path prefix to remove
* @return the URI path
* @see ProxyExchange#path(String)
* @see ProxyExchange#queryParams
*/
public String path(String prefix) {
String path = path();
if (!path.startsWith(prefix)) {
Expand All @@ -241,6 +256,14 @@ public String path(String prefix) {
return path.substring(prefix.length());
}

/**
* Returns the original query parameters.
* @return the query parameters
*/
public MultiValueMap<String, String> queryParams() {
return exchange.getRequest().getQueryParams();
}

public Mono<ResponseEntity<T>> get() {
RequestEntity<?> requestEntity = headers((BodyBuilder) RequestEntity.get(uri)).build();
return exchange(requestEntity);
Expand Down