Skip to content

Commit

Permalink
Merge pull request #248 from buerokratt/247-add-default-values-to-cookie
Browse files Browse the repository at this point in the history
247 Add default value to return cookie
  • Loading branch information
RayDNoper authored Dec 22, 2023
2 parents 7dba3ac + f87e3fb commit a042dbd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void addCorsMappings(CorsRegistry registry) {
String[] allowedMethods = properties.getIncomingRequests().getAllowedMethodTypes().toArray(new String[0]);
boolean allowCredentials = Optional.ofNullable(properties.getCors().getAllowCredentials()).orElse(false);
registry.addMapping("/**")
.allowedOrigins(allowedOrigins)
.allowedOriginPatterns(allowedOrigins)
.allowCredentials(allowCredentials)
.allowedMethods(allowedMethods);
}
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/ee/buerokratt/ruuter/domain/steps/ReturnStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

Expand Down Expand Up @@ -41,7 +42,27 @@ public String getType() {

private Map<String, String> formatHeaders(DslInstance di) {
Map<String, Object> evaluatedMap = di.getScriptingHelper().evaluateScripts(headers, di.getContext(), di.getRequestBody(), di.getRequestQuery(), di.getRequestHeaders());
return evaluatedMap.entrySet().stream().collect(toMap(Entry::getKey, this::entryValueToHeaderString));
return evaluatedMap.entrySet().stream()
.map(e -> addDefaultCookies(e, di))
.collect(toMap(Entry::getKey, this::entryValueToHeaderString));
}

private void addToCookie(LinkedHashMap<String, Object> cookie, String key, Object value) {
if (!cookie.containsKey(key))
cookie.put(key, value);
}
private Map.Entry<String, Object> addDefaultCookies(Map.Entry<String, Object> entry, DslInstance di) {
if ("Set-Cookie".equals(entry.getKey())) {
LinkedHashMap<String, Object> cookie = new LinkedHashMap<>((HashMap<String, Object>) entry.getValue());

addToCookie(cookie, "Path", "/");
addToCookie(cookie, "HttpOnly", true);
addToCookie(cookie, "Secure", true);
addToCookie(cookie, "Max-Age", 28800);

entry.setValue(cookie);
}
return entry;
}

private String entryValueToHeaderString(Entry<String, Object> entry) {
Expand Down

0 comments on commit a042dbd

Please sign in to comment.