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.shared.progman.rest; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import org.apache.commons.lang.StringUtils; import org.opentestsystem.shared.exception.LocalizedException; import org.opentestsystem.shared.progman.domain.Asset; import org.opentestsystem.shared.progman.domain.AssetPool; import org.opentestsystem.shared.progman.service.AssetPoolService; import org.opentestsystem.shared.web.AbstractRestController; import org.springframework.beans.factory.annotation.Autowired; 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; @Controller public class AssetPoolController extends AbstractRestController { @Autowired private AssetPoolService assetPoolService; @Autowired private ObjectMapper objectMapper; /** * Retrieve the /assetPool with query parameter or with JSON (assetPoolId). * * @param request HttpServletRequest * @return AssetPool */ @RequestMapping(value = "/assetPool/{assetPoolId}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE }) @Secured({ "ROLE_Progman Read" }) @ResponseBody public AssetPool getAssetPool(@PathVariable final String assetPoolId) { return assetPoolService.getAssetPool(assetPoolId); } /** * Retrieve the /assetPool/tenantId with query parameter or with JSON (assetPoolId). * * @param request HttpServletRequest * @return SearchResponse<AssetGroup> */ @RequestMapping(value = "/assetPool/tenant/{tenantId}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE }) @Secured({ "ROLE_Progman Read" }) @ResponseBody public AssetPool getAssetPoolByTenant(@PathVariable final String tenantId) { return assetPoolService.getAssetPoolByTenantId(tenantId); } /** * Retrieve the /assetFile with query parameter or with JSON (assetPoolId). * * @param request HttpServletRequest * @return byte[] asset file */ @RequestMapping(value = "/assetPool/assetFile/{assetGridFsId}/{filename}", method = RequestMethod.GET) @ResponseBody public ResponseEntity<byte[]> getAssetFile(@PathVariable final String assetGridFsId, final HttpServletResponse response) { ByteArrayOutputStream ret = new ByteArrayOutputStream(); try { GridFSDBFile grid = assetPoolService.getAssetFile(assetGridFsId); grid.writeTo(ret); ret.flush(); } catch (IOException e) { throw new LocalizedException("assetFile.notfound", new String[] { assetGridFsId }, e); } return new ResponseEntity<byte[]>(ret.toByteArray(), HttpStatus.CREATED); } /** * Creates AssetPool. * * @param assetPool to be saved. * @param response HttpServletResponse. * @return AssetPool newly created assetPool object. */ @ResponseStatus(HttpStatus.CREATED) @RequestMapping(value = "/assetPool", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = { MediaType.APPLICATION_JSON_VALUE }) @Secured({ "ROLE_Progman Admin" }) @ResponseBody public AssetPool createAssetPool(@RequestBody @Valid final AssetPool assetPool, final HttpServletResponse response) { if (!StringUtils.isEmpty(assetPool.getId())) { throw new LocalizedException("assetPool.exists", new String[] { assetPool.getId() }); } AssetPool savedAssetPool = assetPoolService.saveAssetPool(assetPool); response.setHeader("Location", savedAssetPool.getUrl()); return savedAssetPool; } /** * Updates AssetPool. * * @param assetPool to be updated. * @param response HttpServletResponse. * @return AssetPool newly created assetPool object. */ @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/assetPool/{assetPoolId}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = { MediaType.APPLICATION_JSON_VALUE }) @Secured({ "ROLE_Progman Admin" }) @ResponseBody public AssetPool updateAssetPool(@PathVariable final String assetPoolId, @RequestBody @Valid final AssetPool assetPool, final HttpServletResponse response) { if (assetPool == null || StringUtils.isEmpty(assetPool.getId()) || !assetPoolId.equals(assetPool.getId())) { throw new LocalizedException("assetPool.invalid.id", new String[] {}); } AssetPool savedAssetPool = assetPoolService.saveAssetPool(assetPool); response.setHeader("Location", savedAssetPool.getUrl()); return savedAssetPool; } @ResponseStatus(HttpStatus.CREATED) @RequestMapping(value = "/assetPool/{assetPoolId}/assetFile", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = { MediaType.TEXT_PLAIN_VALUE }) @Secured({ "ROLE_Progman Admin" }) @ResponseBody public String uploadAssetFile(@RequestParam("assetFile") final MultipartFile assetFile, @PathVariable final String assetPoolId, final HttpServletRequest request, final HttpServletResponse response) throws IOException { String jsonAsStringForIE = null; Asset savedFile; try { savedFile = assetPoolService.saveAssetFile(assetPoolId, assetFile.getOriginalFilename(), assetFile.getBytes(), assetFile.getContentType()); jsonAsStringForIE = objectMapper.writeValueAsString(savedFile); } catch (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 = objectMapper.writeValueAsString(super.handleException(e)); } catch (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 = objectMapper.writeValueAsString(super.handleException(e)); } return jsonAsStringForIE; } @ResponseStatus(HttpStatus.NO_CONTENT) @RequestMapping(value = "/assetPool/{assetPoolId}/assetFile/{fileGridId}", method = RequestMethod.DELETE) @Secured({ "ROLE_Progman Admin" }) public void deleteAssetFile(@PathVariable final String assetPoolId, @PathVariable final String fileGridId, final HttpServletResponse response) throws IOException { assetPoolService.deleteAssetFile(assetPoolId, fileGridId); } }