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.delivery.testreg.rest; import static org.springframework.util.StringUtils.hasText; import java.io.ByteArrayInputStream; import java.io.IOException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.time.StopWatch; import org.opentestsystem.delivery.testreg.domain.Student; import org.opentestsystem.delivery.testreg.service.StudentPackageService; import org.opentestsystem.delivery.testreg.service.StudentService; import org.opentestsystem.shared.mna.client.service.MetricClient; 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.security.access.annotation.Secured; import org.springframework.stereotype.Controller; 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; @Controller public class StudentPackageController extends AbstractRestController { @Autowired private StudentPackageService studentPackageService; @Autowired private StudentService studentService; @Autowired private MetricClient metricClient; @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/studentpackage", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE) @Secured({ "ROLE_Entity Read" }) @ResponseBody public void extractStudentPackage(@RequestParam(value = "ssid", required = false) final String studentId, @RequestParam(value = "externalId", required = false) final String externalSsid, @RequestParam("stateabbreviation") final String stateAbbreviation, final HttpServletResponse response) throws IOException { StopWatch sw = new StopWatch(); sw.start(); Student student = null; if (hasText(studentId) && hasText(externalSsid)) { response.setStatus(HttpServletResponse.SC_CONFLICT); } else if (hasText(studentId)) { student = studentService.findByStudentIdAndStateAbbreviation(studentId, stateAbbreviation); } else if (hasText(externalSsid)) { student = studentService.findByExternalSsidAndStateAbbreviation(externalSsid, stateAbbreviation); } if (student != null) { String studentPackage = studentPackageService.exportStudentPackage(student); response.setContentType(MediaType.APPLICATION_XML_VALUE); ServletOutputStream out = response.getOutputStream(); IOUtils.copy(new ByteArrayInputStream(studentPackage.getBytes()), out); out.flush(); } else { response.setStatus(HttpServletResponse.SC_NO_CONTENT); } sw.stop(); this.metricClient.sendPerformanceMetricToMna("StudentPackage for " + externalSsid + " (ms) ", sw.getTime()); } }