Skip to content
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

How to override Request.getClientIp() ? #575

Open
mhagnumdw opened this issue Dec 29, 2020 · 6 comments
Open

How to override Request.getClientIp() ? #575

mhagnumdw opened this issue Dec 29, 2020 · 6 comments

Comments

@mhagnumdw
Copy link
Member

In my current scenario I need to retrieve the client's IP from the X-Forwarded-For header.

I could override the Application.createRequestResponseFactory() method, create my custom RequestResponseFactory and in it create my instance that inherits from the Request class, overriding the getClientIp() method, but the Request class is final.

@decebals
Copy link
Member

The final word on Request was added by #95.
In many situations (my projects) I think that I need to inject somehow custom methods (helpers, utilities, ...) in Request, Response, RequestContext (especially when I use Controllers).
Maybe together we will find an elegant solution to improve this aspect.
In the end, I am not against removing the word "final" from these classes, but I think we must first see the arguments for making those classes final.

@decebals
Copy link
Member

Also see #64.

@decebals
Copy link
Member

@mhagnumdw Any resolution here? Do you see a way to improve the current API (to be easy to config and extend)?

@decebals
Copy link
Member

From what I understood from #95, the idea was to override RouteContext and not Request and Response. The RouteContext is passed as parameter in each RouteHandler and Controller's methods and each application can add an improved version of this class, with new helper methods. Until now I didn't use a custom version of RouteContext and and I haven't heard anyone do it.
I don't know if what I said make sense for you but I wanted to do a summary of what is being done.

@mhagnumdw
Copy link
Member Author

My workaround in my application is to have an abstract implementation of RouteHandler and Controller and in them I put helpers methods.

For example, to retrieve the client's IP, I have the helpers below and I use them in my abstract classes:

public static String getClientIp(RouteContext routeContext) {
    return getClientIp(routeContext.getRequest());
}

public static String getClientIp(Request request) {
    String headerXForwardForHeader = StringUtils.defaultString(request.getHeader("X-Forwarded-For"));
    String headerXForwardFor0 = headerXForwardForHeader.split(",")[0];
    String defaultRemoteAddr = request.getClientIp();
    return StringUtils.firstNonBlank(headerXForwardFor0, defaultRemoteAddr);
}

@mhagnumdw
Copy link
Member Author

With what I said above it is possible to get the client IP from two points: provided by Pippo in Request.getClientIp() and by the helpers methods in my abstractions.

Depending on the infra, they can bring different results.

It would be good if the developer has a single point to get the client's IP.

Here we are talking about client IP, but there may be other situations/scenarios.

Anyway, there is a workaround and I have no problem using it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants