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

Fix #151: Add RequestContextConverter (backport) #243

Closed
wants to merge 2 commits into from
Closed
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,17 @@ Auditing with parameters and type of audit message:
param.put("operation_id", operationId);
audit.info("an access message", AuditDetail.builder().type("ACCESS").params(param).build());
```


## Wultra HTTP Common

The `http-common` project provides common functionality for HTTP stack.


### Features


#### RequestContextConverter

`RequestContextConverter` converts `HttpServletRequest` to a Wultra specific class `RequestContext`.
This context object contains _user agent_ and best-effort guess of the _client IP address_.
36 changes: 36 additions & 0 deletions http-common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.getlime.core</groupId>
<artifactId>lime-java-core-parent</artifactId>
<version>1.6.2-SNAPSHOT</version>
</parent>

<artifactId>http-common</artifactId>
<description>Common HTTP functionality</description>

<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2023 Wultra s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wultra.core.http.common.request;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

/**
* Context of the HTTP request.
*
* @author Petr Dvorak, [email protected]
* @author Lubos Racansky, [email protected]
*/
@Builder
@Getter
@ToString
@EqualsAndHashCode
public class RequestContext {

private String ipAddress;
private String userAgent;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2023 Wultra s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wultra.core.http.common.request;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* Converter for HTTP request context information.
*
* @author Petr Dvorak, [email protected]
* @author Lubos Racansky, [email protected]
*/
public final class RequestContextConverter {

/**
* List of HTTP headers that may contain the actual IP address
* when hidden behind a proxy component.
*/
private static final List<String> HTTP_HEADERS_IP_ADDRESS = Collections.unmodifiableList(Arrays.asList(
"X-Forwarded-For",
"Proxy-Client-IP",
"WL-Proxy-Client-IP",
"HTTP_X_FORWARDED_FOR",
"HTTP_X_FORWARDED",
"HTTP_X_CLUSTER_CLIENT_IP",
"HTTP_CLIENT_IP",
"HTTP_FORWARDED_FOR",
"HTTP_FORWARDED",
"HTTP_VIA",
"REMOTE_ADDR"
));

private static final String HTTP_HEADER_USER_AGENT = "User-Agent";

private RequestContextConverter() {
throw new IllegalStateException("Should not be instantiated");
}

/**
* Convert HTTP Servlet Request to request context representation.
*
* @param source HttpServletRequest instance.
* @return Request context data.
*/
public static RequestContext convert(final HttpServletRequest source) {
if (source == null) {
return null;
}
return RequestContext.builder()
.userAgent(source.getHeader(HTTP_HEADER_USER_AGENT))
.ipAddress(getClientIpAddress(source))
.build();
}

/**
* Obtain the best-effort guess of the client IP address.
*
* @param request HttpServletRequest instance.
* @return Best-effort information about the client IP address.
*/
private static String getClientIpAddress(final HttpServletRequest request) {
for (String header : HTTP_HEADERS_IP_ADDRESS) {
final String ip = request.getHeader(header);
if (isNotBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
return ip;
}
}
return request.getRemoteAddr();
}

private static boolean isNotBlank(final String value) {
return !(value == null || value.trim().isEmpty());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2023 Wultra s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wultra.core.http.common.request;

import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockHttpServletRequest;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

/**
* Test for {@link RequestContextConverter}.
*
* @author Lubos Racansky, [email protected]
*/
class RequestContextConverterTest {

@Test
void testNullRequest() {
final RequestContext result = RequestContextConverter.convert(null);

assertNull(result);
}

@Test
void testUserAgentNull() {
final MockHttpServletRequest request = new MockHttpServletRequest();
final RequestContext result = RequestContextConverter.convert(request);

assertNull(result.getUserAgent());
}

@Test
void testUserAgent() {
final MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0");

final RequestContext result = RequestContextConverter.convert(request);

assertEquals("Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0", result.getUserAgent());
}

@Test
void testIpAddress() {
final MockHttpServletRequest request = new MockHttpServletRequest();
final RequestContext result = RequestContextConverter.convert(request);

assertEquals("127.0.0.1", result.getIpAddress());
}

@Test
void testIpAddressProxy() {
final MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Proxy-Client-IP", "\t");
request.addHeader("HTTP_X_FORWARDED_FOR", "unKNOWN");
request.addHeader("HTTP_X_FORWARDED", "192.168.1.134");

final RequestContext result = RequestContextConverter.convert(request);

assertEquals("192.168.1.134", result.getIpAddress());
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

<modules>
<module>audit-base</module>
<module>http-common</module>
<module>rest-model-base</module>
<module>rest-client-base</module>
</modules>
Expand Down