|
|
@@ -20,6 +20,9 @@ import org.springframework.data.domain.*;
|
|
|
import javax.validation.constraints.NotBlank;
|
|
|
import javax.validation.constraints.NotNull;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.text.MessageFormat;
|
|
|
import java.util.*;
|
|
|
import java.util.function.Function;
|
|
|
@@ -90,21 +93,21 @@ public class SeecoderBokClient
|
|
|
|
|
|
@Override
|
|
|
public Optional<QuestionVO> getQuestionById(@NotBlank String questionId) {
|
|
|
- HttpUrl httpUrl = createUrlBuilder(MessageFormat.format("/api/question/{0}", questionId)).build();
|
|
|
+ HttpUrl httpUrl = createUrlBuilder(formatUrl("/api/question/{0}", encodeValue(questionId))).build();
|
|
|
Response response = get(httpUrl);
|
|
|
return getResponseBody(response, QuestionVO.class);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public QuestionVO updateQuestion(@NotNull QuestionVO question) {
|
|
|
- HttpUrl httpUrl = createUrlBuilder(MessageFormat.format("/api/question/{0}", question.getId())).build();
|
|
|
+ HttpUrl httpUrl = createUrlBuilder(formatUrl("/api/question/{0}", encodeValue(question.getId()))).build();
|
|
|
Response response = put(httpUrl, writeRequestBody(question));
|
|
|
return getResponseBody(response, QuestionVO.class).orElseThrow(() -> SeecoderBokException.UNWRAP_EXCEPTION);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void deleteQuestionById(@NotBlank String questionId) {
|
|
|
- HttpUrl httpUrl = createUrlBuilder(MessageFormat.format("/api/question/{0}", questionId)).build();
|
|
|
+ HttpUrl httpUrl = createUrlBuilder(formatUrl("/api/question/{0}", questionId)).build();
|
|
|
delete(httpUrl);
|
|
|
}
|
|
|
|
|
|
@@ -145,7 +148,7 @@ public class SeecoderBokClient
|
|
|
@Override
|
|
|
public RecordQuestionVO getUserRecordQuestions(
|
|
|
@NotNull String userId, Pageable pageable) {
|
|
|
- HttpUrl.Builder urlBuilder = createUrlBuilder(MessageFormat.format("/api/record/{0}/question", userId));
|
|
|
+ HttpUrl.Builder urlBuilder = createUrlBuilder(formatUrl("/api/record/{0}/question", userId));
|
|
|
HttpUrl httpUrl = addPageableToParams(urlBuilder, pageable).build();
|
|
|
Response response = get(httpUrl);
|
|
|
return getResponseBody(response, RecordQuestionVO.class)
|
|
|
@@ -156,7 +159,7 @@ public class SeecoderBokClient
|
|
|
public void collectQuestion(
|
|
|
@NotBlank String userId, @NotBlank String questionId, @NotNull CollectDTO collectDTO) {
|
|
|
HttpUrl httpUrl = createUrlBuilder(
|
|
|
- MessageFormat.format("/api/user/{0}/question/{1}/collection", userId, questionId)).build();
|
|
|
+ formatUrl("/api/user/{0}/question/{1}/collection", userId, questionId)).build();
|
|
|
post(httpUrl, writeRequestBody(collectDTO));
|
|
|
}
|
|
|
|
|
|
@@ -164,14 +167,14 @@ public class SeecoderBokClient
|
|
|
public void rateQuestion(
|
|
|
@NotBlank String userId, @NotBlank String questionId, @NotNull RateDTO rateDTO) {
|
|
|
HttpUrl httpUrl = createUrlBuilder(
|
|
|
- MessageFormat.format("/api/user/{0}/question/{1}/rate", userId, questionId)).build();
|
|
|
+ formatUrl("/api/user/{0}/question/{1}/rate", userId, questionId)).build();
|
|
|
post(httpUrl, writeRequestBody(rateDTO));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Page<QuestionVO> getUserInteractiveQuestions(
|
|
|
@NotBlank String userId, Boolean collect, Boolean rate, Pageable pageable) {
|
|
|
- HttpUrl.Builder urlBuilder = createUrlBuilder(MessageFormat.format("/api/user/{0}/question", userId))
|
|
|
+ HttpUrl.Builder urlBuilder = createUrlBuilder(formatUrl("/api/user/{0}/question", userId))
|
|
|
.addQueryParameter("collection", String.valueOf(collect))
|
|
|
.addQueryParameter("rate", String.valueOf(rate));
|
|
|
return getQuestionVOS(pageable, urlBuilder);
|
|
|
@@ -181,7 +184,7 @@ public class SeecoderBokClient
|
|
|
public Optional<InteractionVO> getUserInteractiveQuestion(
|
|
|
@NotBlank String userId, @NotBlank String questionId) {
|
|
|
HttpUrl httpUrl = createUrlBuilder(
|
|
|
- MessageFormat.format("/api/user/{0}/question/{1}", userId, questionId)).build();
|
|
|
+ formatUrl("/api/user/{0}/question/{1}", userId, questionId)).build();
|
|
|
Response response = get(httpUrl);
|
|
|
return getResponseBody(response, InteractionVO.class);
|
|
|
}
|
|
|
@@ -190,7 +193,7 @@ public class SeecoderBokClient
|
|
|
public List<InteractionVO> getUserInteractiveQuestionIn(
|
|
|
@NotBlank String userId, List<String> questionIdList) {
|
|
|
HttpUrl httpUrl = createUrlBuilder(
|
|
|
- MessageFormat.format("/api/user/{0}/questionInList", userId)).build();
|
|
|
+ formatUrl("/api/user/{0}/questionInList", userId)).build();
|
|
|
Response response = get(httpUrl);
|
|
|
return getResponseBody(response, new TypeReference<List<InteractionVO>>() {})
|
|
|
.orElse(Collections.emptyList());
|
|
|
@@ -305,4 +308,18 @@ public class SeecoderBokClient
|
|
|
return String.join("|",
|
|
|
sort.map(order -> order.getProperty() + "," + order.getDirection()));
|
|
|
}
|
|
|
+
|
|
|
+ private static String encodeValue(String value) {
|
|
|
+ try {
|
|
|
+ return URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatUrl(String pattern, String... arguments) {
|
|
|
+ MessageFormat temp = new MessageFormat(pattern);
|
|
|
+ return temp.format(Arrays.stream(arguments).map(SeecoderBokClient::encodeValue).toArray());
|
|
|
+ }
|
|
|
}
|