Skip to content

Commit

Permalink
fix(StudentData): Send response as UTF-8 (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
hirokiterashima authored Aug 12, 2022
1 parent 51bd8e6 commit be980fb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package org.wise.vle.web.wise5.student;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.wise.portal.dao.ObjectNotFoundException;
import org.wise.portal.domain.authentication.impl.StudentUserDetails;
import org.wise.portal.domain.run.Run;
Expand All @@ -23,11 +18,8 @@
import org.wise.portal.service.user.UserService;
import org.wise.portal.service.vle.wise5.VLEService;
import org.wise.portal.service.workgroup.WorkgroupService;
import org.wise.vle.domain.annotation.wise5.Annotation;
import org.wise.vle.domain.work.Event;
import org.wise.vle.domain.work.StudentWork;

@Controller
@RestController
@Secured("ROLE_STUDENT")
public class StudentGetDataController {

Expand All @@ -44,7 +36,7 @@ public class StudentGetDataController {
private WorkgroupService workgroupService;

@GetMapping("/api/student/data")
public void getStudentData(HttpServletResponse response, Authentication authentication,
public HashMap<String, Object> getStudentData(Authentication authentication,
@RequestParam(value = "getStudentWork", defaultValue = "false") boolean getStudentWork,
@RequestParam(value = "getEvents", defaultValue = "false") boolean getEvents,
@RequestParam(value = "getAnnotations", defaultValue = "false") boolean getAnnotations,
Expand All @@ -68,42 +60,24 @@ public void getStudentData(HttpServletResponse response, Authentication authenti
@RequestParam(value = "annotationType", required = false) String annotationType,
@RequestParam(value = "components", required = false) List<JSONObject> components,
@RequestParam(value = "onlyGetLatest", required = false) Boolean onlyGetLatest)
throws ObjectNotFoundException, IOException, JSONException {
JSONObject result = new JSONObject();
throws ObjectNotFoundException {
HashMap<String, Object> data = new HashMap<String, Object>();
User user = userService.retrieveUser((StudentUserDetails) authentication.getPrincipal());
Run run = runService.retrieveById(Long.valueOf(runId));
if (getStudentWork && isMemberOfWorkgroupId(user, run, workgroupId)) {
try {
result.put("studentWorkList", getStudentWork(id, runId, periodId, workgroupId,
isAutoSave, isSubmit, nodeId, componentId, componentType, components, onlyGetLatest));
} catch (JSONException e) {
e.printStackTrace();
}
data.put("studentWorkList", vleService.getStudentWorkList(id, runId, periodId, workgroupId,
isAutoSave, isSubmit, nodeId, componentId, componentType, components, onlyGetLatest));
}
if (getEvents && isMemberOfWorkgroupId(user, run, workgroupId)) {
try {
result.put("events", getEvents(id, runId, periodId, workgroupId, nodeId, componentId,
componentType, context, category, event, components));
} catch (JSONException e) {
e.printStackTrace();
}
data.put("events", vleService.getEvents(id, runId, periodId, workgroupId, nodeId, componentId,
componentType, context, category, event, components));
}
if (getAnnotations && isAllowedToGetAnnotations(user, run, fromWorkgroupId, toWorkgroupId)) {
try {
result.put("annotations", getAnnotations(id, runId, periodId, fromWorkgroupId,
toWorkgroupId, nodeId, componentId, studentWorkId, localNotebookItemId,
notebookItemId, annotationType));
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
PrintWriter writer = response.getWriter();
writer.write(result.toString());
writer.close();
} catch (IOException e) {
e.printStackTrace();
data.put("annotations",
vleService.getAnnotations(id, runId, periodId, fromWorkgroupId, toWorkgroupId, nodeId,
componentId, studentWorkId, localNotebookItemId, notebookItemId, annotationType));
}
return data;
}

private boolean isMemberOfWorkgroupId(User user, Run run, Integer workgroupId)
Expand All @@ -112,48 +86,9 @@ private boolean isMemberOfWorkgroupId(User user, Run run, Integer workgroupId)
workgroupService.retrieveById(Long.valueOf(workgroupId)));
}

private JSONArray getStudentWork(Integer id, Integer runId, Integer periodId, Integer workgroupId,
Boolean isAutoSave, Boolean isSubmit, String nodeId, String componentId, String componentType,
List<JSONObject> components, Boolean onlyGetLatest) {
List<StudentWork> studentWorkList = vleService.getStudentWorkList(id, runId, periodId,
workgroupId, isAutoSave, isSubmit, nodeId, componentId, componentType, components,
onlyGetLatest);
JSONArray studentWorkJSONArray = new JSONArray();
for (StudentWork studentWork : studentWorkList) {
studentWorkJSONArray.put(studentWork.toJSON());
}
return studentWorkJSONArray;
}

private JSONArray getEvents(Integer id, Integer runId, Integer periodId,
Integer workgroupId, String nodeId, String componentId, String componentType, String context,
String category, String event, List<JSONObject> components) {
List<Event> events = vleService.getEvents(id, runId, periodId, workgroupId, nodeId,
componentId, componentType, context, category, event, components);
JSONArray eventsJSONArray = new JSONArray();
for (Event eventObject : events) {
eventsJSONArray.put(eventObject.toJSON());
}
return eventsJSONArray;
}

private boolean isAllowedToGetAnnotations(User user, Run run, Integer fromWorkgroupId,
Integer toWorkgroupId) throws ObjectNotFoundException {
return isMemberOfWorkgroupId(user, run, fromWorkgroupId) ||
isMemberOfWorkgroupId(user, run, toWorkgroupId);
}

private JSONArray getAnnotations(Integer id, Integer runId, Integer periodId,
Integer fromWorkgroupId, Integer toWorkgroupId, String nodeId, String componentId,
Integer studentWorkId, String localNotebookItemId, Integer notebookItemId,
String annotationType) {
List<Annotation> annotations = vleService.getAnnotations(id, runId, periodId, fromWorkgroupId,
toWorkgroupId, nodeId, componentId, studentWorkId, localNotebookItemId, notebookItemId,
annotationType);
JSONArray annotationsJSONArray = new JSONArray();
for (Annotation annotation : annotations) {
annotationsJSONArray.put(annotation.toJSON());
}
return annotationsJSONArray;
return isMemberOfWorkgroupId(user, run, fromWorkgroupId)
|| isMemberOfWorkgroupId(user, run, toWorkgroupId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@
import static org.easymock.EasyMock.*;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.easymock.EasyMockRunner;
import org.easymock.TestSubject;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.wise.portal.dao.ObjectNotFoundException;
import org.wise.portal.domain.authentication.impl.StudentUserDetails;
import org.wise.portal.domain.run.Run;
Expand All @@ -33,7 +28,6 @@ public class StudentGetDataControllerTest extends APIControllerTest {
@TestSubject
private StudentGetDataController controller = new StudentGetDataController();

private HttpServletResponse response;
private boolean getStudentWork;
private boolean getEvents;
private boolean getAnnotations;
Expand All @@ -60,7 +54,6 @@ public class StudentGetDataControllerTest extends APIControllerTest {

@Before
public void init() {
response = new MockHttpServletResponse();
getStudentWork = false;
getEvents = false;
getAnnotations = false;
Expand Down Expand Up @@ -97,12 +90,11 @@ public void getWISE5StudentData_NotAllowedToGetData_DoesNotRetrieveStudentWork()
expectRetrieveWorkgroup(workgroup2Id, workgroup2);
expectIsUserInWorkgroupForRun(student1, run1, workgroup2, false);
replayAll();
controller.getStudentData(response, studentAuth, getStudentWork, getEvents,
getAnnotations, id, runId, periodId, workgroupId, isAutoSave, isSubmit, nodeId,
componentId, componentType, context, category, event, fromWorkgroupId, toWorkgroupId,
studentWorkId, localNotebookItemId, notebookItemId, annotationType, components,
onlyGetLatest);
} catch(ObjectNotFoundException | IOException | JSONException e) {
controller.getStudentData(studentAuth, getStudentWork, getEvents, getAnnotations, id, runId,
periodId, workgroupId, isAutoSave, isSubmit, nodeId, componentId, componentType, context,
category, event, fromWorkgroupId, toWorkgroupId, studentWorkId, localNotebookItemId,
notebookItemId, annotationType, components, onlyGetLatest);
} catch (ObjectNotFoundException e) {
fail(SHOULD_NOT_HAVE_THROWN_EXCEPTION);
}
verifyAll();
Expand All @@ -122,12 +114,11 @@ public void getWISE5StudentData_AllowedToGetData_RetrievesStudentWork() {
expectGetStudentWorkList(id, runId, periodId, workgroupId, isAutoSave, isSubmit, nodeId,
componentId, componentType, components, onlyGetLatest, studentWorkList);
replayAll();
controller.getStudentData(response, studentAuth, getStudentWork, getEvents,
getAnnotations, id, runId, periodId, workgroupId, isAutoSave, isSubmit, nodeId,
componentId, componentType, context, category, event, fromWorkgroupId, toWorkgroupId,
studentWorkId, localNotebookItemId, notebookItemId, annotationType, components,
onlyGetLatest);
} catch(ObjectNotFoundException | IOException | JSONException e) {
controller.getStudentData(studentAuth, getStudentWork, getEvents, getAnnotations, id, runId,
periodId, workgroupId, isAutoSave, isSubmit, nodeId, componentId, componentType, context,
category, event, fromWorkgroupId, toWorkgroupId, studentWorkId, localNotebookItemId,
notebookItemId, annotationType, components, onlyGetLatest);
} catch (ObjectNotFoundException e) {
fail(SHOULD_NOT_HAVE_THROWN_EXCEPTION);
}
verifyAll();
Expand All @@ -142,7 +133,7 @@ private void expectRetrieveRun(Long runId, Run run) throws ObjectNotFoundExcepti
}

private void expectRetrieveWorkgroup(Long workgroupId, Workgroup workgroup)
throws ObjectNotFoundException{
throws ObjectNotFoundException {
expect(workgroupService.retrieveById(workgroupId)).andReturn(workgroup);
}

Expand Down

0 comments on commit be980fb

Please sign in to comment.