-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7718ac
commit f9a537b
Showing
8 changed files
with
221 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...ackend/src/main/java/com/comet/opik/infrastructure/ratelimit/RateLimitResponseFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.comet.opik.infrastructure.ratelimit; | ||
|
||
import com.comet.opik.infrastructure.auth.RequestContext; | ||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.container.ContainerRequestContext; | ||
import jakarta.ws.rs.container.ContainerResponseContext; | ||
import jakarta.ws.rs.container.ContainerResponseFilter; | ||
import jakarta.ws.rs.ext.Provider; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@Provider | ||
@RequiredArgsConstructor(onConstructor_ = @Inject) | ||
public class RateLimitResponseFilter implements ContainerResponseFilter { | ||
|
||
@Override | ||
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { | ||
List<Object> userLimit = getValueFromHeader(requestContext, RequestContext.USER_LIMIT); | ||
List<Object> remainingLimit = getValueFromHeader(requestContext, RequestContext.USER_REMAINING_LIMIT); | ||
List<Object> remainingTtl = getValueFromHeader(requestContext, RequestContext.USER_LIMIT_REMAINING_TTL); | ||
|
||
responseContext.getHeaders().put(RequestContext.USER_LIMIT, userLimit); | ||
responseContext.getHeaders().put(RequestContext.USER_REMAINING_LIMIT, remainingLimit); | ||
responseContext.getHeaders().put(RequestContext.USER_LIMIT_REMAINING_TTL, remainingTtl); | ||
} | ||
|
||
private List<Object> getValueFromHeader(ContainerRequestContext requestContext, String key) { | ||
return requestContext.getHeaders().getOrDefault(key, List.of()) | ||
.stream() | ||
.map(Object.class::cast) | ||
.toList(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.