Java tutorial
package com.its.web.controllers; import java.io.IOException; import java.io.InputStream; import java.util.List; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; 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 com.its.web.db.mappings.License; import com.its.web.db.mappings.Product; import com.its.web.services.ProductsService; @Controller public class ProductsManagerController { private Logger log = Logger.getLogger(this.getClass()); @Autowired ProductsService productsService; @RequestMapping(value = "listProducts.do") public @ResponseBody List<Product> listProducts() { List<Product> list = productsService.readAll(); log.debug("products list size:" + list.size()); return list; } @RequestMapping(value = "saveOrUpdateProduct.do", method = RequestMethod.POST) public @ResponseBody Boolean saveOrUpdate(@RequestBody Product product) { log.debug("id------------->" + product.getId()); log.debug("name------------->" + product.getName()); log.debug("description------------->" + product.getDescription()); boolean operation = productsService.saveOrUpdate(product); return operation; } @RequestMapping(value = "deleteProduct.do", method = RequestMethod.POST) public @ResponseBody Boolean deleteProduct(@RequestBody Product product) { log.debug("product id to delete:" + product.getId()); boolean operation = productsService.remove(product.getId()); return operation; } @RequestMapping(value = "generateProductXmlFile.do", method = RequestMethod.GET) public void generateLicenseFile(@RequestParam("productName") String productName, @RequestParam("selectedFullType") String selectedFullType, @RequestParam("selectedTrialType") String selectedTrialType, HttpServletResponse response) { log.debug("productName------------->" + productName); log.debug("selectedFullType------------->" + selectedFullType); log.debug("selectedTrialType------------->" + selectedTrialType); InputStream is = productsService.generateXml(productName, selectedFullType, selectedTrialType); String fileName = "product.xml"; response.setContentType("application/force-download"); response.setHeader("Content-Disposition", "attachment; filename=" + fileName); try { // copy it to response's OutputStream IOUtils.copy(is, response.getOutputStream()); response.flushBuffer(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }