How to do URL Rewrite in Zuul Proxy? - url-rewriting

One of the request that comes to my Zuul Filter is of URI /hello/World which i want to redirect to /myapp/test. This /myapp/test is a service that is registered in Eureka.
zuul:
routes:
xyz:
path: /hello/World
url: http://localhost:1234/myapp/test
stripPrefix: true
When i try the above configuration, the incoming URI is suffixed to the configured URL like http://localhost:1234/myapp/test/World . Few of the links which i came across seem to be stating that URL Rewrite feature is not yet available in Zuul.
Is there any other way this can be done at the Zuul Layer ?
Note: At this point of time, i cannot do this reverse proxying in the Webserver or any other layer since, my Zuul filter is the one that is receiving the request directly.

Using #Adelin solution, with little improvements
Use 'url' property as path to prepend for customizing the Url rewriting (I have disabled Eureka in my example) :
ribbon.eureka.enabled=false
zuul.routes.route1.path=/route1/**
zuul.routes.route1.serviceId=service1
zuul.routes.route1.url=/path/to/prepend
service1.ribbon.listOfServers=http://server1
Then implement the following filter :
/**
* Fixing missing URL rewriting when using ribbon
*/
#Component
public class CustomPathZuulFilter extends ZuulFilter {
#Autowired
private ZuulProperties zuulProperties;
#Override
public String filterType() {
return FilterConstants.PRE_TYPE;
}
#Override
public int filterOrder() {
return FilterConstants.PRE_DECORATION_FILTER_ORDER + 1;
}
#Override
public boolean shouldFilter() {
// override PreDecorationFilter only if executed previously successfully
return RequestContext.getCurrentContext().getFilterExecutionSummary().toString()
.contains("PreDecorationFilter[SUCCESS]");
}
#Override
public Object run() {
final RequestContext context = RequestContext.getCurrentContext();
if (context.get(FilterConstants.SERVICE_ID_KEY) == null || context.getRouteHost() != null) {
// not a Ribbon route
return null;
}
// get current ZuulRoute
final String proxy = (String) context.get(FilterConstants.PROXY_KEY);
final ZuulRoute zuulRoute = this.zuulProperties.getRoutes().get(proxy);
// patch URL by prefixing it with zuulRoute.url
final Object originalRequestPath = context.get(FilterConstants.REQUEST_URI_KEY);
final String modifiedRequestPath = zuulRoute.getUrl() + originalRequestPath;
context.put(FilterConstants.REQUEST_URI_KEY, modifiedRequestPath);
// patch serviceId because :
// - has been set to route.location in PreDecorationFilter
// - route.location has been set to zuulRoute.location in SimpleRouteLocator
// - zuulRoute.location return zuulRoute.url if set
context.set(FilterConstants.SERVICE_ID_KEY, zuulRoute.getServiceId());
return null;
}
}
Now calls to /route1 will be proxified to http://server1/path/to/prepend
This solution is also compatible with co-existing routes not using Ribbon.
Example of a co-existing route not using Ribbon :
zuul.routes.route2.path=/route2/**
zuul.routes.route2.url=http://server2/some/path
Calls to /route2 will be proxified to http://server2/some/path by SimpleHostRoutingFilter (if not disabled)

Here is a posted solution in the link by #Vikash
#Component
public class CustomPathZuulFilter extends ZuulFilter
{
#Override
public String filterType() {
return "pre";
}
#Override
public int filterOrder() {
return PreDecorationFilter.FILTER_ORDER + 1;
}
#Override
public boolean shouldFilter() {
return true;
}
#Override
public Object run() {
RequestContext context = RequestContext.getCurrentContext();
Object originalRequestPath = context.get(REQUEST_URI_KEY);
String modifiedRequestPath = "/api/microservicePath" + originalRequestPath;
context.put(REQUEST_URI_KEY, modifiedRequestPath);
return null;
}
}

Have you tried creating a preFilter or even a routeFilter ?
That way you can intercept the request, and change the routing.
See Zuul Filters

Related

Micrometer - WebMvcTagsContributor not adding custom tags

I'm trying to add custom tags - the path variables and their values from each request - to each metric micrometer generates. I'm using spring-boot with java 16.
From my research i've found that creating a bean of type WebMvcTagsContributor alows me to do just that.
This is the code
public class CustomWebMvcTagsContributor implements WebMvcTagsContributor {
private static int PRINT_ERROR_COUNTER = 0;
#Override
public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response,
Object handler,
Throwable exception) {
return Tags.of(getAllTags(request));
}
private static List<Tag> getAllTags(HttpServletRequest request) {
Object attributesMapObject = request.getAttribute(View.PATH_VARIABLES);
if (isNull(attributesMapObject)) {
attributesMapObject = request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
if (isNull(attributesMapObject)) {
attributesMapObject = extractPathVariablesFromURI(request);
}
}
if (nonNull(attributesMapObject)) {
return getPathVariablesTags(attributesMapObject);
}
return List.of();
}
private static Object extractPathVariablesFromURI(HttpServletRequest request) {
Long currentUserId = SecurityUtils.getCurrentUserId().orElse(null);
try {
URI uri = new URI(request.getRequestURI());
String path = uri.getPath(); //get the path
UriTemplate uriTemplate = new UriTemplate((String) request.getAttribute(
HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)); //create template
return uriTemplate.match(path); //extract values form template
} catch (Exception e) {
log.warn("[Error on 3rd attempt]", e);
}
return null;
}
private static List<Tag> getPathVariablesTags(Object attributesMapObject) {
try {
Long currentUserId = SecurityUtils.getCurrentUserId().orElse(null);
if (nonNull(attributesMapObject)) {
var attributesMap = (Map<String, Object>) attributesMapObject;
List<Tag> tags = attributesMap.entrySet().stream()
.map(stringObjectEntry -> Tag.of(stringObjectEntry.getKey(),
String.valueOf(stringObjectEntry.getValue())))
.toList();
log.warn("[CustomTags] [{}]", CommonUtils.toJson(tags));
return tags;
}
} catch (Exception e) {
if (PRINT_ERROR_COUNTER < 5) {
log.error("[Error while getting attributes map object]", e);
PRINT_ERROR_COUNTER++;
}
}
return List.of();
}
#Override
public Iterable<Tag> getLongRequestTags(HttpServletRequest request, Object handler) {
return null;
}
}
#Bean
public WebMvcTagsContributor webMvcTagsContributor() {
return new CustomWebMvcTagsContributor();
}
In order to test this, i've created a small spring boot app, added an endpoint to it. It works just fine.
The problem is when I add this code to the production app.
The metrics generates are the default ones and i can't figure out why.
What can I check to see why the tags are not added?
local test project
http_server_requests_seconds_count {exception="None", method="GET",id="123",outcome="Success",status="200",test="test",uri="/test/{id}/compute/{test}",)1.0
in prod - different (& bigger) app
http_server_requests_seconds_count {exception="None", method="GET",outcome="Success",status="200",uri="/api/{something}/test",)1.0
What i've tried and didn't work
Created a bean that implemented WebMvcTagsProvider - this one had an odd behaviour - it wasn't creating metrics for endpoints that had path variables in the path - though in my local test project it worked as expected
I added that log there in order to see what the extra tags are but doesn't seem to reach there as i don't see anything in the logs - i know, you might say that the current user id stops it, but it's not that.

Spring Cloud Zuul - Custom Error Filter

I want to add a custom error Zuul Filter and want to make sure SendErrorFilter does not execute. I have looked at few github links including Spring-cloud/spring-cloud-netflix and various stack-overflow questions:-
Customizing Zuul Exception
Overriding Zuul Filter SendErrorFilter
My code is as follows-
public class CustomErrorFilter extends ZuulFilter {
private static final Logger LOG = LoggerFactory.getLogger(CustomErrorFilter.class);
#Override
public String filterType() {
return "post";
}
#Override
public int filterOrder() {
return -1;
}
#Override
public boolean shouldFilter() {
RequestContext ctx=RequestContext.getCurrentContext();
if(ctx.getThrowable()!=null)
return true;
else
return false;
}
#Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
ctx.setThrowable(null); // response is not returned unless
throwable is set to null.
ctx.remove("error.status_code");
ctx.setResponseBody(“Error”);
ctx.getResponse().setContentType("text/plain");
ctx.setResponseStatusCode(400);
}
return null;
}
I am having the following issues-
Setting filter order to -1 does not prohibit sendErrorFilter from running.
To stop sendErrorFilter from running, I need to set
zuul.SendErrorFilter.error.disable=true in bootstrap.yml
To get a response body which is set in the custom error filter, i need to set throwable to null as mentioned in the github.
Setting a filter as type "error" does nothing, and the custom filer does not run.
I would like someone to explain, what I am doing wrong and what is the most correct way of handling custom error filters, because there is lot of conflicting information available on the web.
Dependencies-
spring cloud - Edgware.RELEASE
spring cloud netflix starter zuul- 1.4.3.RELEASE
filtertype() should return "error" if you want to handle error scenario
filterOrder() should be -1 to execute before SendErrorFilter
Add the following lines to your filter(CustomErrorFilter )
protected static final String SEND_ERROR_FILTER_RAN = "sendErrorFilter.ran";
and
#Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
ctx.set(SEND_ERROR_FILTER_RAN);
// rest of your code
return null;
}
ctx.set(SEND_ERROR_FILTER_RAN); will block the SendErrorFilter from running.
Update:
Check shouldFilter() method in SendErrorFilter
#Override
public boolean shouldFilter() {
RequestContext ctx = RequestContext.getCurrentContext();
// only forward to errorPath if it hasn't been forwarded to already
return ctx.getThrowable() != null
&& !ctx.getBoolean(SEND_ERROR_FILTER_RAN, false);
}
For every request a RequestContext gets created, but spring is not setting SEND_ERROR_FILTER_RAN in the context. May be for older version you have to configure from yaml file (zuul.SendErrorFilter.error.disable=true) for newer version(1.4.3.RELEASE) its from code like ctx.set(SEND_ERROR_FILTER_RAN).
By default !ctx.getBoolean(SEND_ERROR_FILTER_RAN, false) this will evaluates to true and run() method of SendErrorFilter will execute.
if you put ctx.set(SEND_ERROR_FILTER_RAN) in your CustomErrorFilter with filterOrder() as -1, your CustomErrorFilter will execute first and we are setting the RequestContext with SEND_ERROR_FILTER_RAN as true. Now when it goes to shouldFilter() method of SendErrorFilter evaluate to false and it won't execute run() method of SendErrorFilter.

Manually call Hibernate URL validator in spring-boot

I am using spring-boot 1.4.0 and hibernate-validator 5.2.0. I have a model which contains custom validator inside the custom validator i want to check whether the property value is a valid URL for that i need to call URLValidator in hibernate but no luck.Could anyone please guide me to resolve this issue
CustomValidator.java
#Component
public class BookValidator extends GenericValidator<Book, ConstraintValidatorContext> implements ConstraintValidator<ValidBooks, List<Book>> {
public BookValidator() {
addValidators();
}
private void addValidators() {
getValidators().add((book, context) -> {
boolean isValid = book.getUrl(); //here i want to check against Hibernate URL validator
if (!isValid) {
context.disableDefaultConstraintViolation();
context
.buildConstraintViolationWithTemplate("Book URL should be valid!")
.addConstraintViolation();
}
return isValid;
});
}
#Override
public void initialize(ValidBooks constraintAnnotation) {
}
}
How can i check whether the URL is a valid one
boolean isValid = book.getUrl(); using hibernate URLValidator?
This works:
AnnotationDescriptor<URL> descriptor = new AnnotationDescriptor<URL>( URL.class );
URL url = AnnotationFactory.create(descriptor);
URLValidator urlValidator = new URLValidator();
urlValidator.initialize(url);
boolean isValid = urlValidator.isValid(book.getUrl(), context);

Spring WS (DefaultWsdl11Definition) HTTP status code with void

We have a (working) SOAP web service based on Spring WS with DefaultWsdl11Definition.
This is basically what it looks like:
#Endpoint("name")
public class OurEndpoint {
#PayloadRoot(namespace = "somenamespace", localPart = "localpart")
public void onMessage(#RequestPayload SomePojo pojo) {
// do stuff
}
}
It is wired in Spring and it is correctly processing all of our SOAP requests. The only problem is that the method returns a 202 Accepted. This is not what the caller wants, he'd rather have us return 204 No Content (or if that is not possible an empty 200 OK).
Our other endpoints have a valid response object, and do return 200 OK. It seems void causes 202 when 204 might be more appropriate?
Is it possible to change the response code in Spring WS? We can't seem to find the correct way to do this.
Things we tried and didn't work:
Changing the return type to:
HttpStatus.NO_CONTENT
org.w3c.dom.Element <- not accepted
Adding #ResponseStatus <- this is for MVC, not WS
Any ideas?
Instead of what I wrote in the comments it is possibly the easiest to create a delegation kind of solution.
public class DelegatingMessageDispatcher extends MessageDispatcher {
private final WebServiceMessageReceiver delegate;
public DelegatingMessageDispatcher(WebServiceMessageReceiver delegate) {
this.delegate = delegate;
}
public void receive(MessageContext messageContext) throws Exception {
this.delegate.receive(messageContext);
if (!messageContext.hasResponse()) {
TransportContext tc = TransportContextHolder.getTransportContext();
if (tc != null && tc.getConnection() instanceof HttpServletConnection) {
((HttpServletConnection) tc.getConnection()).getHttpServletResponse().setStatus(200);
}
}
}
}
Then you need to configure a bean named messageDispatcher which would wrap the default SoapMessageDispatcher.
#Bean
public MessageDispatcher messageDispatcher() {
return new DelegatingMessageDispatcher(soapMessageDispatcher());
}
#Bean
public MessageDispatcher soapMessageDispatcher() {
return new SoapMessageDispatcher();
}
Something like that should do the trick. Now when response is created (In the case of a void return type), the status as you want is send back to the client.
When finding a proper solutions we've encountered some ugly problems:
Creating custom adapters/interceptors is problematic because the handleResponse method isn't called by Spring when you don't have a response (void)
Manually setting the status code doesn't work because HttpServletConnection keeps a boolean statusCodeSet which doesn't get updated
But luckily we managed to get it working with the following changes:
/**
* If a web service has no response, this handler returns: 204 No Content
*/
public class NoContentInterceptor extends EndpointInterceptorAdapter {
#Override
public void afterCompletion(MessageContext messageContext, Object o, Exception e) throws Exception {
if (!messageContext.hasResponse()) {
TransportContext tc = TransportContextHolder.getTransportContext();
if (tc != null && tc.getConnection() instanceof HttpServletConnection) {
HttpServletConnection connection = ((HttpServletConnection) tc.getConnection());
// First we force the 'statusCodeSet' boolean to true:
connection.setFaultCode(null);
// Next we can set our custom status code:
connection.getHttpServletResponse().setStatus(204);
}
}
}
}
Next we need to register this interceptor, this can be easily done using Spring's XML:
<sws:interceptors>
<bean class="com.something.NoContentInterceptor"/>
</sws:interceptors>
A big thanks to #m-deinum for pointing us in the right direction!
To override the afterCompletion method really helped me out in the exact same situation. And for those who use code based Spring configuration, here´s how one can add the interceptor for a specific endpoint.
Annotate the custom interceptor with #Component, next register the custom interceptor to a WsConfigurerAdapter like this:
#EnableWs
#Configuration
public class EndpointConfig extends WsConfigurerAdapter {
/**
* Add our own interceptor for the specified WS endpoint.
* #param interceptors
*/
#Override
public void addInterceptors(List<EndpointInterceptor> interceptors) {
interceptors.add(new PayloadRootSmartSoapEndpointInterceptor(
new NoContentInterceptor(),
"NAMESPACE",
"LOCAL_PART"
));
}
}
NAMESPACE and LOCAL_PART should correspond to the endpoint.
If someone ever wanted to set custom HTTP status when returning non-void response, here is solution:
Spring Boot WS-Server - Custom Http Status

Add camel route at runtime in Java

How can I add a camel route at run-time in Java? I have found a Grails example but I have implement it in Java.
My applicationContext.xml already has some predefined static routes and I want to add some dynamic routes to it at run time.
Is it possible?
Because the only way to include dynamic route is to write the route.xml and then load the route definition to context. How will it work on existing static routes?
Route at runtime
you can simply call a few different APIs on the CamelContext to add routes...something like this
context.addRoutes(new MyDynamcRouteBuilder(context, "direct:foo", "mock:foo"));
....
private static final class MyDynamcRouteBuilder extends RouteBuilder {
private final String from;
private final String to;
private MyDynamcRouteBuilder(CamelContext context, String from, String to) {
super(context);
this.from = from;
this.to = to;
}
#Override
public void configure() throws Exception {
from(from).to(to);
}
}
see this unit test for the complete example...
https://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/AddRoutesAtRuntimeTest.java
#Himanshu,
Please take a look at dynamicroute options (in other words routing slip) that may help you dynamically route to different 'destinations' based on certain condition.
Check the dynamic router help link in camel site;
http://camel.apache.org/dynamic-router.html
from("direct:start")
// use a bean as the dynamic router
.dynamicRouter(method(DynamicRouterTest.class, "slip"));
And within the slip method;
/**
* Use this method to compute dynamic where we should route next.
*
* #param body the message body
* #return endpoints to go, or <tt>null</tt> to indicate the end
*/
public String slip(String body) {
bodies.add(body);
invoked++;
if (invoked == 1) {
return "mock:a";
} else if (invoked == 2) {
return "mock:b,mock:c";
} else if (invoked == 3) {
return "direct:foo";
} else if (invoked == 4) {
return "mock:result";
}
// no more so return null
return null;
}
Hope it helps...
Thanks.
One such solution could be:
Define route:
private RouteDefinition buildRouteDefinition() {
RouteDefinition routeDefinition = new RouteDefinition();
routeDefinition.from(XX).to(ZZ); // define any route you want
return routeDefinition;
}
Get Model Context and create route:
CamelContext context = getContext();
ModelCamelContext modelContext = context.adapt(ModelCamelContext.class);
modelContext.addRouteDefinition(routeDefinition);
There are more way of getting camel context. To name few:
In processor, you can use exchange.getContext()
Through RouteBuilder reference, you can use routeBuilder.getContext()

Resources