Skip to content

Commit 62b795b

Browse files
dragonpooludomikula
authored andcommitted
Add localeContextResolver using lang query param
Add error handler & retry logic in flow endpoint Add chinese locale file
1 parent a5c1aad commit 62b795b

File tree

7 files changed

+348
-6
lines changed

7 files changed

+348
-6
lines changed

server/api-service/lowcoder-sdk/src/main/java/org/lowcoder/sdk/exception/BizError.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,12 @@ public enum BizError {
153153
BUNDLE_NAME_CONFLICT(500, 6403),
154154
ILLEGAL_BUNDLE_PERMISSION_ID(500, 6404),
155155

156-
//slug 6501 - 6501
156+
//slug 6501 - 6502
157157
SLUG_DUPLICATE_ENTRY(403, 6501),
158-
SLUG_INVALID(403, 6502);
158+
SLUG_INVALID(403, 6502),
159+
160+
//flow 6601 - 6601
161+
FLOW_ERROR(500, 6601);
159162

160163
static {
161164
checkDuplicates(values(), BizError::getBizErrorCode);

server/api-service/lowcoder-sdk/src/main/resources/locale_en.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,5 @@ USER_NOT_EXIST=User not exist.
284284
DUPLICATE_AUTH_CONFIG_ADDITION=Provider auth type already added to organization
285285
EMAIL_PROVIDER_DISABLED=Email provider is disabled.
286286
SLUG_DUPLICATE_ENTRY=Slug already exists
287-
SLUG_INVALID=Slug format is invalid
287+
SLUG_INVALID=Slug format is invalid
288+
FLOW_ERROR=Flow error message: {0}

server/api-service/lowcoder-sdk/src/main/resources/locale_zh.properties

Lines changed: 288 additions & 0 deletions
Large diffs are not rendered by default.

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/framework/configuration/CustomWebFluxConfiguration.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@
77
import org.springframework.beans.factory.annotation.Value;
88
import org.springframework.context.annotation.Bean;
99
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.context.i18n.LocaleContext;
11+
import org.springframework.context.i18n.SimpleLocaleContext;
1012
import org.springframework.http.codec.ServerCodecConfigurer;
1113
import org.springframework.http.codec.json.Jackson2JsonDecoder;
1214
import org.springframework.http.codec.json.Jackson2JsonEncoder;
1315
import org.springframework.util.unit.DataSize;
1416
import org.springframework.web.reactive.config.DelegatingWebFluxConfiguration;
1517
import org.springframework.web.reactive.function.server.support.RouterFunctionMapping;
18+
import org.springframework.web.server.ServerWebExchange;
19+
import org.springframework.web.server.i18n.LocaleContextResolver;
20+
21+
import java.util.List;
22+
import java.util.Locale;
1623

1724
@RequiredArgsConstructor
1825
@Configuration
@@ -43,4 +50,28 @@ protected void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
4350
.jackson2JsonEncoder(new Jackson2JsonEncoder(objectMapper()));
4451
}
4552

53+
@Bean
54+
public LocaleContextResolver localeContextResolver() {
55+
return new LocaleContextResolver() {
56+
@Override
57+
public LocaleContext resolveLocaleContext(ServerWebExchange exchange) {
58+
List<String> language = exchange.getRequest().getQueryParams().getOrDefault("lang", List.of("en_US"));
59+
String localeStr = language.get(0);
60+
String[] parts = localeStr.split("_");
61+
if(parts.length == 2) {
62+
Locale locale = new Locale(parts[0], parts[1]);
63+
return new SimpleLocaleContext(locale);
64+
} else {
65+
Locale locale = new Locale(parts[0]);
66+
return new SimpleLocaleContext(locale);
67+
}
68+
}
69+
70+
@Override
71+
public void setLocaleContext(ServerWebExchange exchange, LocaleContext localeContext) {
72+
throw new UnsupportedOperationException("Cannot change HTTP accept header - use a different locale context resolution strategy");
73+
}
74+
};
75+
}
76+
4677
}

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/framework/filter/GlobalContextFilter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.lowcoder.infra.util.NetworkUtils;
1212
import org.lowcoder.sdk.util.CookieHelper;
1313
import org.lowcoder.sdk.util.UriUtils;
14+
import org.springframework.context.i18n.LocaleContext;
1415
import org.springframework.core.Ordered;
1516
import org.springframework.http.HttpMethod;
1617
import org.springframework.http.server.reactive.ServerHttpRequest;
@@ -20,6 +21,7 @@
2021
import org.springframework.web.server.WebFilterChain;
2122
import reactor.core.publisher.Mono;
2223

24+
import java.util.Locale;
2325
import java.util.Map;
2426
import java.util.Map.Entry;
2527
import java.util.Optional;
@@ -98,7 +100,10 @@ private Map<String, Object> buildContextMap(ServerWebExchange serverWebExchange,
98100
contextMap.put(REQUEST_PATH, request.getPath().pathWithinApplication().value());
99101
contextMap.put(REQUEST, request);
100102
contextMap.put(REQUEST_METHOD, ofNullable(request.getMethod()).map(HttpMethod::name).orElse(""));
101-
contextMap.put(CLIENT_LOCALE, globalContextService.getClientLocale(request));
103+
104+
LocaleContext localeContext = serverWebExchange.getLocaleContext();
105+
Locale currentLocale = localeContext.getLocale() != null ? localeContext.getLocale() : Locale.ENGLISH;
106+
contextMap.put(CLIENT_LOCALE, currentLocale);
102107
contextMap.put(CURRENT_ORG_MEMBER, orgMemberService.getCurrentOrgMember(visitorId).cache());
103108
contextMap.put(VISITOR_TOKEN, cookieHelper.getCookieToken(serverWebExchange));
104109
contextMap.put(DOMAIN, UriUtils.getRefererDomainFromRequest(serverWebExchange));

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/misc/ApiFlowController.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,24 @@
44
import lombok.RequiredArgsConstructor;
55
import org.apache.commons.lang.StringUtils;
66
import org.lowcoder.api.authentication.request.AuthException;
7+
import org.lowcoder.sdk.exception.BizError;
8+
import org.lowcoder.sdk.exception.BizException;
79
import org.lowcoder.sdk.util.JsonUtils;
810
import org.lowcoder.sdk.webclient.WebClientBuildHelper;
911
import org.springframework.http.HttpHeaders;
1012
import org.springframework.http.HttpMethod;
13+
import org.springframework.http.HttpStatusCode;
1114
import org.springframework.http.MediaType;
1215
import org.springframework.web.bind.annotation.RestController;
1316
import org.springframework.web.reactive.function.BodyInserters;
17+
import org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver;
1418
import reactor.core.publisher.Mono;
19+
import reactor.util.retry.Retry;
1520

21+
import java.time.Duration;
22+
import java.time.temporal.ChronoUnit;
1623
import java.util.Map;
24+
import java.util.Objects;
1725
import java.util.function.Consumer;
1826

1927
import static org.lowcoder.api.authentication.util.AuthenticationUtils.mapToAuthToken;
@@ -38,7 +46,11 @@ public Mono<String> flow(FlowRequest flowRequest) {
3846
.body(BodyInserters.fromValue(jsonBody))
3947
.headers(httpHeaders -> flowRequest.headers().forEach(httpHeaders::add))
4048
.retrieve()
41-
.bodyToMono(String.class);
49+
.bodyToMono(String.class)
50+
.onErrorResume(Mono::error)
51+
.retryWhen(Retry.backoff(Objects.requireNonNullElse(flowRequest.retries(), 0),
52+
Duration.of(Objects.requireNonNullElse(flowRequest.timeout(), HTTP_TIMEOUT), ChronoUnit.SECONDS))
53+
.onRetryExhaustedThrow(((retryBackoffSpec, retrySignal) -> new BizException(BizError.FLOW_ERROR, "FLOW_ERROR", retrySignal.failure().getMessage()))));
4254
} catch (Exception e) {
4355
return Mono.error(e);
4456
}

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/misc/ApiFlowEndpoints.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public interface ApiFlowEndpoints
2121
public record FlowRequest(String path,
2222
String method,
2323
Map<String, Object> data,
24-
Map<String, String> headers) {
24+
Map<String, String> headers,
25+
Integer timeout,
26+
Integer retries) {
2527
}
2628
}

0 commit comments

Comments
 (0)