Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.rajaram.bookmark.controller; import com.rajaram.bookmark.model.Bookmark; import com.rajaram.bookmark.model.BookmarkList; import com.rajaram.bookmark.model.Response; import com.rajaram.bookmark.operations.bookmark.BookmarkOperation; import com.rajaram.bookmark.service.bookmark.BookmarkService; import com.wordnik.swagger.annotations.Api; import com.wordnik.swagger.annotations.ApiOperation; import com.wordnik.swagger.annotations.ApiParam; import java.io.IOException; import java.io.InputStream; import java.net.URLDecoder; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; 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.multipart.MultipartFile; /** * <P> * Controller class to handle REST calls related to bookmarks. * </P> * @author Rajaram * @since 06-04-2014 * @version 0.1 * */ @Controller @RequestMapping(value = "/bookmark") //@Path("/bookmark") @Api(value = "bookmark", description = "Bookmark operation") public class BookmarkController { @Autowired private BookmarkService bookmarkService; private final BookmarkOperation bookmarkOperation = new BookmarkOperation(); /** * <P> * Upload single bookmark file. * </P> * * @param userName * @param file * @return */ @RequestMapping(value = "/uploadFile", method = RequestMethod.POST) @ApiOperation(httpMethod = "POST", value = "Upload and extract data " + "from the bookmark file and stores the extracted data to the" + " bookmark SQL table.", produces = ("application/json,application/xml")) public @ResponseBody Response uploadFileHandler( @ApiParam(name = "userName", value = "user name", required = true) @RequestParam String userName, @ApiParam(name = "file", value = "file", required = true) @RequestParam MultipartFile file) { userName = URLDecoder.decode(userName); Response response = new Response(); InputStream fileIn = null; try { fileIn = file.getInputStream(); } catch (IOException ex) { Logger.getLogger(BookmarkController.class.getName()).log(Level.SEVERE, null, ex); response.setMessage("Error reading bookmark file. Error message : " + ex.getMessage()); return response; } List<Bookmark> bookmarks = bookmarkOperation.extractNetscapeBookmarks(fileIn); try { bookmarkService.insertBookmarks(userName, bookmarks); response.setMessage("Bookmark data is stored in the database"); } catch (DataAccessException ex) { response.setMessage("Error storing bookmark data. Error message : " + ex.getMessage()); } return response; } @RequestMapping(value = "{userName}", method = RequestMethod.GET) @ApiOperation(httpMethod = "GET", value = "Get bookmarks from the database" + " for a given user name." , produces = ("application/json,application/xml")) public @ResponseBody BookmarkList getBookmarks( @ApiParam(name = "userName", value = "user name", required = true) @RequestParam String userName) { userName = URLDecoder.decode(userName); List<Bookmark> bookmarks = bookmarkService.getBookmarks(userName); BookmarkList bookmarkList = new BookmarkList(bookmarks); return bookmarkList; } }