Skip to content

Commit a501bd8

Browse files
Merge branch 'develop' into translate-with-ai
~ Conflicts: ~ src/main/java/org/wise/portal/presentation/web/controllers/author/project/AuthorAPIController.java ~ src/test/java/org/wise/portal/presentation/web/controllers/author/project/AuthorAPIControllerTest.java
2 parents 982c274 + 209e230 commit a501bd8

8 files changed

Lines changed: 35 additions & 20 deletions

File tree

src/main/java/org/wise/portal/domain/user/User.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,6 @@ public interface User extends Persistable, Comparable<User> {
5555
boolean isTrustedAuthor();
5656

5757
List<String> getRoles();
58+
59+
boolean isEnabled();
5860
}

src/main/java/org/wise/portal/domain/user/impl/UserImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,9 @@ public boolean equals(Object obj) {
158158
public int compareTo(User o) {
159159
return getId().compareTo(o.getId());
160160
}
161+
162+
@Override
163+
public boolean isEnabled() {
164+
return this.getUserDetails().isEnabled();
165+
}
161166
}

src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java renamed to src/main/java/org/wise/portal/presentation/web/controllers/ChatGptController.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
import java.io.OutputStreamWriter;
77
import java.net.HttpURLConnection;
88
import java.net.URL;
9-
import org.springframework.beans.factory.annotation.Autowired;
10-
import org.springframework.core.env.Environment;
9+
import org.springframework.beans.factory.annotation.Value;
1110
import org.springframework.security.access.annotation.Secured;
1211
import org.springframework.web.bind.annotation.PostMapping;
1312
import org.springframework.web.bind.annotation.RequestBody;
@@ -16,33 +15,36 @@
1615
import org.springframework.web.bind.annotation.RestController;
1716

1817
@RestController
19-
@RequestMapping("/api")
20-
public class ChatGptcontroller {
18+
@RequestMapping("/api/chat-gpt")
19+
public class ChatGptController {
2120

22-
@Autowired
23-
Environment appProperties;
21+
@Value("${openai.api.key:}")
22+
private String openAiApiKey;
23+
24+
@Value("${openai.chat.api.url:https://api.openai.com/v1/chat/completions}")
25+
private String openAiChatApiUrl;
2426

2527
@ResponseBody
2628
@Secured("ROLE_USER")
27-
@PostMapping("/chat-gpt")
29+
@PostMapping
2830
protected String sendChatMessage(@RequestBody String body) {
29-
String openaiApiKey = appProperties.getProperty("OPENAI_API_KEY");
30-
if (openaiApiKey == null || openaiApiKey.isEmpty()) {
31-
throw new RuntimeException("OPENAI_API_KEY is not set");
31+
if (openAiApiKey == null || openAiApiKey.isEmpty()) {
32+
throw new RuntimeException("openai.api.key is not set");
3233
}
3334
try {
34-
URL url = new URL("https://api.openai.com/v1/chat/completions");
35+
URL url = new URL(openAiChatApiUrl);
3536
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
3637
connection.setRequestMethod("POST");
37-
connection.setRequestProperty("Authorization", "Bearer " + openaiApiKey);
38+
connection.setRequestProperty("Authorization", "Bearer " + openAiApiKey);
3839
connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
3940
connection.setRequestProperty("Accept-Charset", "UTF-8");
4041
connection.setDoOutput(true);
4142
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
4243
writer.write(body);
4344
writer.flush();
4445
writer.close();
45-
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(),"ISO-8859-1"));
46+
BufferedReader br = new BufferedReader(
47+
new InputStreamReader(connection.getInputStream(), "UTF-8"));
4648
String line;
4749
StringBuffer response = new StringBuffer();
4850
while ((line = br.readLine()) != null) {

src/main/java/org/wise/portal/presentation/web/controllers/InformationController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ public void handleGetConfigWISE5Preview(HttpServletRequest request, HttpServletR
194194
addCommonConfigParameters(request, config, project, rawProjectUrl);
195195
} else {
196196
Project project = projectService.getById(Long.parseLong(projectIdStr));
197+
if (!project.getOwner().isEnabled()) {
198+
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
199+
return;
200+
}
197201
addCommonConfigParameters(request, config, project);
198202
}
199203

src/main/java/org/wise/portal/presentation/web/controllers/author/project/AuthorAPIController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ protected HashMap<String, Object> getAuthorProjectConfig(Authentication auth,
399399
config.put("projectAssetURL", contextPath + "/api/author/project/asset/" + project.getId());
400400
config.put("projectBaseURL", projectBaseURL);
401401
config.put("previewProjectURL", contextPath + "/preview/unit/" + project.getId());
402-
config.put("chatGptEnabled", !StringUtils.isEmpty(appProperties.getProperty("OPENAI_API_KEY")));
402+
config.put("chatGptEnabled", !StringUtils.isEmpty(appProperties.getProperty("openai.api.key")));
403403
config.put("translationServiceEnabled", this.awsPropertiesConfigured());
404404
config.put("cRaterRequestURL", contextPath + "/api/c-rater");
405405
config.put("importStepsURL",
@@ -426,9 +426,9 @@ protected HashMap<String, Object> getAuthorProjectConfig(Authentication auth,
426426
}
427427

428428
private boolean awsPropertiesConfigured() {
429-
return !(StringUtils.isEmpty(appProperties.getProperty("aws.accessKeyId"))
430-
|| StringUtils.isEmpty(appProperties.getProperty("aws.secretAccessKey"))
431-
|| StringUtils.isEmpty(appProperties.getProperty("aws.region")));
429+
return !(StringUtils.isEmpty(appProperties.getProperty("aws.accessKeyId"))
430+
|| StringUtils.isEmpty(appProperties.getProperty("aws.secretAccessKey"))
431+
|| StringUtils.isEmpty(appProperties.getProperty("aws.region")));
432432
}
433433

434434
/**

src/main/resources/application-dockerdev-sample.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ aws.secretAccessKey=
224224
aws.region=
225225

226226
# OpenAI and AWS Bedrock Chat endpoints (optional)
227-
#OPENAI_API_KEY=
227+
#openai.api.key=
228+
#openai.chat.api.url=
228229
#aws.bedrock.api.key=
229230
#aws.bedrock.runtime.endpoint=
230231

src/main/resources/application_sample.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ aws.secretAccessKey=
224224
aws.region=
225225

226226
# OpenAI and AWS Bedrock Chat endpoints (optional)
227-
#OPENAI_API_KEY=
227+
#openai.api.key=
228+
#openai.chat.api.url=
228229
#aws.bedrock.api.key=
229230
#aws.bedrock.runtime.endpoint=

src/test/java/org/wise/portal/presentation/web/controllers/author/project/AuthorAPIControllerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void getAuthorProjectConfig_HasProjectRun_ReturnCanGradeStudentWork() thr
8080
replay(request);
8181
expect(appProperties.getProperty("curriculum_base_www"))
8282
.andReturn("http://localhost:8080/curriculum");
83-
expect(appProperties.getProperty("OPENAI_API_KEY")).andReturn("OPENAPIKEY");
83+
expect(appProperties.getProperty("openai.api.key")).andReturn("OPENAPIKEY");
8484
expect(appProperties.getProperty("aws.accessKeyId")).andReturn("ACCESSKEY");
8585
expect(appProperties.getProperty("aws.secretAccessKey")).andReturn("SECRETKEY");
8686
expect(appProperties.getProperty("aws.region")).andReturn("us-west-1");

0 commit comments

Comments
 (0)