package org.magnum.mobilecloud.video.controller;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.magnum.mobilecloud.video.client.VideoSvcApi;
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.ResponseBody;
/**
* This simple VideoSvc allows clients to send HTTP POST requests with
* videos that are stored in memory using a list. Clients can send HTTP GET
* requests to receive a JSON listing of the videos that have been sent to
* the controller so far. Stopping the controller will cause it to lose the history of
* videos that have been sent to it because they are stored in memory.
*
* Notice how much simpler this VideoSvc is than the original VideoServlet?
* Spring allows us to dramatically simplify our service. Another important
* aspect of this version is that we have defined a VideoSvcApi that provides
* strong typing on both the client and service interface to ensure that we
* don't send the wrong paraemters, etc.
*
* @author jules
*
*/
// Tell Spring that this class is a Controller that should
// handle certain HTTP requests for the DispatcherServlet
@Controller
public class VideoSvc implements VideoSvcApi {
// An in-memory list that the servlet uses to store the
// videos that are sent to it by clients
private List<Video> videos = new CopyOnWriteArrayList<Video>();
// Receives POST requests to /video and converts the HTTP
// request body, which should contain json, into a Video
// object before adding it to the list. The @RequestBody
// annotation on the Video parameter is what tells Spring
// to interpret the HTTP request body as JSON and convert
// it into a Video object to pass into the method. The
// @ResponseBody annotation tells Spring to conver the
// return value from the method back into JSON and put
// it into the body of the HTTP response to the client.
//
// The VIDEO_SVC_PATH is set to "/video" in the VideoSvcApi
// interface. We use this constant to ensure that the
// client and service paths for the VideoSvc are always
// in synch.
//
// For some ways to improve the validation of the data
// in the Video object, please see this Spring guide:
// http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/validation.html#validation-beanvalidation
//
@RequestMapping(value=VIDEO_SVC_PATH, method=RequestMethod.POST)
public @ResponseBody boolean addVideo(@RequestBody Video v){
return videos.add(v);
}
// Receives GET requests to /video and returns the current
// list of videos in memory. Spring automatically converts
// the list of videos to JSON because of the @ResponseBody
// annotation.
@RequestMapping(value=VIDEO_SVC_PATH, method=RequestMethod.GET)
public @ResponseBody List<Video> getVideoList(){
return videos;
}
}