Skip to content

Commit

Permalink
Java: Fetch user data based on yaml/properties configuration in Sprin…
Browse files Browse the repository at this point in the history
…g Boot applications (#1147)
  • Loading branch information
serhii-vydiuk-kevychsolutions committed Dec 25, 2024
1 parent f606ac9 commit e29887a
Show file tree
Hide file tree
Showing 29 changed files with 878 additions and 267 deletions.
5 changes: 5 additions & 0 deletions packages/java/examples/OwlTestApp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
</properties>

<dependencies>
<dependency>
<groupId>com.readme</groupId>
<artifactId>metrics-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.readme.example;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
Expand All @@ -13,6 +14,9 @@
@RestController
public class OwlController {

@Value("${readme.readmeApiKey}")
private String readmeApiKey;

private final Map<String, String> owlStorage = new HashMap<>();

public OwlController() {
Expand All @@ -21,7 +25,7 @@ public OwlController() {

@GetMapping("/owl/{id}")
public String getOwlById(@PathVariable String id) {
return "Owl with id " + id;
return "Owl with id " + id + " and key is " + readmeApiKey;
}

@GetMapping("/owls")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
readme:
readmeApiKey: ${README_API_KEY}
userdata:
apiKey:
source: header
fieldName: X-User-Name
email:
source: header
fieldName: X-User-Email
label:
source: header
fieldName: X-User-Id

#readme:
# readmeApiKey: ${README_API_KEY}
# userdata:
# apiKey:
# source: jsonBody
# fieldName: /owl-creator/name
# email:
# source: jsonBody
# fieldName: /owl-creator/contacts/email
# label:
# source: jsonBody
# fieldName: owl-creator/label

#readme:
# readmeApiKey: ${README_API_KEY}
# userdata:
# apiKey:
# source: jwt
# fieldName: name
# email:
# source: jwt
# fieldName: aud
# label:
# source: jwt
# fieldName: user_id



This file was deleted.

4 changes: 2 additions & 2 deletions packages/java/readme-metrics-spring-boot-starter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ Each field (`apiKey`, `email`, `label`) requires two sub-properties:
### Example Configuration (YAML)
```yaml
readme:
readmeApiKey: a11b33b2c44de78f7a
readmeApiKey: ${readmeApiKey}
userdata:
apiKey:
source: header
fieldName: X-User-Id
email:
source: jwtClaim
source: jwt
fieldName: aud
label:
source: jsonBody
Expand Down
38 changes: 15 additions & 23 deletions packages/java/readme-metrics-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,33 @@

<dependencies>
<dependency>
<groupId>com.readme</groupId>
<artifactId>readme-metrics</artifactId>
<version>${readme-metrics.version}</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
<optional>true</optional>
<groupId>com.readme</groupId>
<artifactId>readme-metrics</artifactId>
<version>${readme-metrics.version}</version>
</dependency>

<!-- HANDLING JWT TOKENS-->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>4.4.0</version>
</dependency>

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

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.readme.starter.config;

import com.readme.dataextraction.RequestDataCollector;
import com.readme.dataextraction.UserDataCollector;
import com.readme.starter.datacollection.DataCollectionFilter;
import com.readme.starter.datacollection.ServletDataPayloadAdapter;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

/**
* Configuration class for registering and initializing the JakartaDataCollectionFilter
* along with its dependencies in a Spring Boot application.
* <p>
* This configuration provides the following:
* <ul>
* <li>Instantiates the {@link DataCollectionFilter} with required collectors.</li>
* <li>Registers the filter using {@link FilterRegistrationBean} for servlet-based applications.</li>
* <li>Sets up default implementations for collecting request and user data.</li>
* </ul>
*/
@Configuration
@ConditionalOnClass({UserDataProperties.class})
@ComponentScan(basePackages = {"com.readme.starter"})
@AllArgsConstructor
public class DataCollectionAutoConfiguration {

@Bean
public FilterRegistrationBean<DataCollectionFilter> metricsFilter(
RequestDataCollector<ServletDataPayloadAdapter> requestDataCollector,
UserDataCollector<ServletDataPayloadAdapter> userDataCollector) {
FilterRegistrationBean<DataCollectionFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new DataCollectionFilter(requestDataCollector, userDataCollector));
registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
registrationBean.addUrlPatterns("/*");
return registrationBean;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.readme.starter.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "readme")
public class ReadmeConfigurationProperties {

private String readmeApiKey;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.readme.config.FieldMapping;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
* Configuration properties for monitoring library.
Expand All @@ -14,12 +14,12 @@
* (e.g., header, jwtClaim, or jsonBody) and its corresponding value.
* </p>
*/
@Configuration
@ConfigurationProperties(prefix = "readme.userdata")

@Data
public class MonitoringProperties {
@Component
@ConfigurationProperties(prefix = "readme.userdata")
public class UserDataProperties {

private String readmeApiKey;
private FieldMapping apiKey;
private FieldMapping email;
private FieldMapping label;
Expand Down
Loading

0 comments on commit e29887a

Please sign in to comment.