Java tutorial
/******************************************************************************* * Educational Online Test Delivery System * Copyright (c) 2013 American Institutes for Research * * Distributed under the AIR Open Source License, Version 1.0 * See accompanying file AIR-License-1_0.txt or at * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf ******************************************************************************/ package org.opentestsystem.authoring.testauth.rest; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import org.opentestsystem.authoring.testauth.domain.FileGroup; import org.opentestsystem.authoring.testauth.domain.PsychometricRecord; import org.opentestsystem.authoring.testauth.domain.SimulationRecord; import org.opentestsystem.authoring.testauth.service.FileGroupService; import org.opentestsystem.shared.exception.LocalizedException; import org.opentestsystem.shared.search.domain.SearchResponse; import org.opentestsystem.shared.web.AbstractRestController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.security.access.annotation.Secured; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.multipart.MultipartFile; import com.fasterxml.jackson.databind.ObjectMapper; import com.mongodb.gridfs.GridFSDBFile; import com.mongodb.gridfs.GridFSFile; /** * Used to retrieve help messages */ @Controller public class FileGroupController extends AbstractRestController { @Autowired private FileGroupService fileGroupService; @Autowired private ObjectMapper objectMapper; @RequestMapping(value = "/fileGroup/{fileGroupId}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE }) @Secured({ "ROLE_Result Upload Read" }) @ResponseBody public FileGroup getFileGroup(@PathVariable final String fileGroupId) { return fileGroupService.getFileGroup(fileGroupId); } @ResponseStatus(HttpStatus.CREATED) @RequestMapping(value = "/fileGroup", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = { MediaType.APPLICATION_JSON_VALUE }) @Secured({ "ROLE_Result Upload Modify" }) @ResponseBody public FileGroup saveFileGroup(@RequestBody @Valid final FileGroup fileGroup, final HttpServletResponse response) { return fileGroupService.saveFileGroup(fileGroup); } @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/fileGroup/{fileGroupId}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = { MediaType.APPLICATION_JSON_VALUE }) @Secured({ "ROLE_Result Upload Modify" }) @ResponseBody public FileGroup updateFileGroup(@PathVariable final String fileGroupId, @RequestBody @Valid final FileGroup fileGroup, final HttpServletResponse response) { return fileGroupService.saveFileGroup(fileGroup); } @ResponseStatus(HttpStatus.NO_CONTENT) @RequestMapping(value = "/fileGroup/{fileGroupId}", method = RequestMethod.DELETE) @Secured({ "ROLE_Result Upload Modify" }) public void removeFileGroupById(@PathVariable final String fileGroupId) { this.fileGroupService.removeFileGroup(fileGroupId); } @RequestMapping(value = "/fileGroup", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @Secured({ "ROLE_Result Upload Read" }) @ResponseBody public SearchResponse<FileGroup> searchFileGroup(final HttpServletRequest request, final HttpServletResponse response) { return this.fileGroupService.searchFileGroups(request.getParameterMap()); } /******************************** Grid FS Endpoints ********************************/ @RequestMapping(value = "/fileGroup/gridFsFile/{fileGridId}") @Secured({ "ROLE_Result Upload Read" }) public ResponseEntity<byte[]> getGridFsFile(@PathVariable final String fileGridId) { final ByteArrayOutputStream ret = new ByteArrayOutputStream(); HttpHeaders responseHeaders; try { final GridFSDBFile grid = this.fileGroupService.getGridFsFile(fileGridId); grid.writeTo(ret); responseHeaders = buildResponseHeaders(ret.toByteArray().length, grid.getFilename(), grid.getContentType()); ret.flush(); } catch (final IOException e) { throw new LocalizedException("document.file.notfound", new String[] { fileGridId }, e); } return new ResponseEntity<byte[]>(ret.toByteArray(), responseHeaders, HttpStatus.OK); } @ResponseStatus(HttpStatus.CREATED) @RequestMapping(value = "/fileGroup/gridFsFile", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.TEXT_PLAIN_VALUE) @Secured({ "ROLE_Result Upload Modify" }) @ResponseBody public String uploadGridFsFile(@RequestParam("gridFsFile") final MultipartFile gridFsFile, final HttpServletRequest request, final HttpServletResponse response) throws IOException { String jsonAsStringForIE = null; try { final GridFSFile savedFile = this.fileGroupService.saveGridFsFile(gridFsFile.getOriginalFilename(), gridFsFile.getBytes(), gridFsFile.getContentType()); jsonAsStringForIE = savedFile.toString(); } catch (final LocalizedException e) { // return a 201 here - // IE9 and browsers which require iframe transport must receive an OK status to get the response result after file upload jsonAsStringForIE = this.objectMapper.writeValueAsString(super.handleException(e)); } catch (final IOException e) { // return a 201 here - // IE9 and browsers which require iframe transport must receive an OK status to get the response result after file upload jsonAsStringForIE = this.objectMapper.writeValueAsString(super.handleException(e)); } return jsonAsStringForIE; } @ResponseStatus(HttpStatus.NO_CONTENT) @RequestMapping(value = "/fileGroup/gridFsFile/{gridFsId}", method = RequestMethod.DELETE) @Secured({ "ROLE_Result Upload Modify" }) public void deleteGridFsFile(@PathVariable final String gridFsId) { this.fileGroupService.deleteGridFsFile(gridFsId); } private static HttpHeaders buildResponseHeaders(final int contentLength, final String filename, final String contentType) { final HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.clear(); responseHeaders.add(org.apache.http.HttpHeaders.CONTENT_TYPE, contentType); responseHeaders.setPragma("public"); responseHeaders.setCacheControl("no-store, must-revalidate"); responseHeaders.setExpires(Long.valueOf("-1")); responseHeaders.setContentDispositionFormData("attachment", filename); responseHeaders.setContentLength(contentLength); responseHeaders.add(org.apache.http.HttpHeaders.ACCEPT_RANGES, "bytes"); return responseHeaders; } /***********************************************************************************/ /************************************ Simulation Record ************************************/ @RequestMapping(value = "/fileGroup/simulationRecord/{assessmentId}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE }) @Secured({ "ROLE_Test Read" }) @ResponseBody public SimulationRecord findSimulationRecordById(@PathVariable final String assessmentId) { return this.fileGroupService.getSimulationRecordByAssessmentId(assessmentId); } @ResponseStatus(HttpStatus.CREATED) @RequestMapping(value = "/fileGroup/simulationRecord", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = { MediaType.APPLICATION_JSON_VALUE }) @Secured({ "ROLE_Test Modify" }) @ResponseBody public SimulationRecord saveSimulationRecord(@RequestBody @Valid final SimulationRecord simulationRecord, final HttpServletResponse response) { return this.fileGroupService.saveSimulationRecord(simulationRecord); } @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/fileGroup/simulationRecord/{simulationRecordId}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = { MediaType.APPLICATION_JSON_VALUE }) @Secured({ "ROLE_Test Modify" }) @ResponseBody public SimulationRecord updateSimulationRecord(@PathVariable final String simulationRecordId, @RequestBody @Valid final SimulationRecord simulationRecord, final HttpServletResponse response) { return this.fileGroupService.saveSimulationRecord(simulationRecord); } /***********************************************************************************/ /************************************ Psychometric Record ************************************/ @RequestMapping(value = "/fileGroup/psychometricRecord/{publishingRecordId}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE }) @Secured({ "ROLE_Test Read" }) @ResponseBody public PsychometricRecord findPsychometricRecordById(@PathVariable final String publishingRecordId) { return this.fileGroupService.getPsychometricRecordByPublishingRecordId(publishingRecordId); } @ResponseStatus(HttpStatus.CREATED) @RequestMapping(value = "/fileGroup/psychometricRecord", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = { MediaType.APPLICATION_JSON_VALUE }) @Secured({ "ROLE_Test Modify" }) @ResponseBody public PsychometricRecord savePsychometricRecord( @RequestBody @Valid final PsychometricRecord psychometricRecord, final HttpServletResponse response) { return this.fileGroupService.savePsychometricRecord(psychometricRecord); } @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/fileGroup/psychometricRecord/{psychometricRecordId}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = { MediaType.APPLICATION_JSON_VALUE }) @Secured({ "ROLE_Test Modify" }) @ResponseBody public PsychometricRecord updatePsychometricRecord(@PathVariable final String psychometricRecordId, @RequestBody @Valid final PsychometricRecord psychometricRecord, final HttpServletResponse response) { return this.fileGroupService.savePsychometricRecord(psychometricRecord); } @RequestMapping(value = "/fileGroup/psychometricRecordVersions/{publishingRecordId}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE }) @Secured({ "ROLE_Test Read" }) @ResponseBody public List<String> getPsychometricRecordVersions(@PathVariable final String publishingRecordId) { return this.fileGroupService.getPsychometricRecordVersions(publishingRecordId); } @RequestMapping(value = "/fileGroup/searchByPsychometricVersion", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @Secured({ "ROLE_Result Upload Read" }) @ResponseBody public SearchResponse<FileGroup> searchPsychometricRecordFileGroupsByVersion(final HttpServletRequest request, final HttpServletResponse response) { return this.fileGroupService.searchPsychometricRecordFileGroupsByVersion(request.getParameterMap()); } /***********************************************************************************/ }