Java tutorial
/* * * Copyright 2014 Jules White * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package org.magnum.mobilecloud.video; import java.security.Principal; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.List; import javax.servlet.http.HttpServletResponse; import org.magnum.mobilecloud.video.client.VideoSvcApi; import org.magnum.mobilecloud.video.repository.Video; import org.magnum.mobilecloud.video.repository.VideoRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.repository.CrudRepository; import org.springframework.data.rest.webmvc.ResourceNotFoundException; import org.springframework.security.access.prepost.PreAuthorize; 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 com.google.common.collect.Lists; @Controller public class VideoSvcCtrl { // ALM - Autowire in an implementation of the repo @Autowired private VideoRepository videoRepo; @PreAuthorize("hasRole(USER)") @RequestMapping(method = RequestMethod.GET, value = VideoSvcApi.VIDEO_SVC_PATH) public @ResponseBody Collection<Video> getVideoList() { return Lists.newArrayList(videoRepo.findAll()); } @PreAuthorize("hasRole(USER)") @RequestMapping(method = RequestMethod.GET, value = VideoSvcApi.VIDEO_SVC_PATH + "/{id}") public @ResponseBody Video getVideoById(@PathVariable("id") long id, HttpServletResponse response) { Video ret = videoRepo.findOne(id); if (ret == null) response.setStatus(HttpServletResponse.SC_NOT_FOUND); else response.setStatus(HttpServletResponse.SC_OK); return ret; } @PreAuthorize("hasRole(USER)") @RequestMapping(method = RequestMethod.POST, value = VideoSvcApi.VIDEO_SVC_PATH) public @ResponseBody Video addVideo(@RequestBody Video v) { v.getLikers().clear(); return videoRepo.save(v); } @PreAuthorize("hasRole(USER)") @RequestMapping(method = RequestMethod.POST, value = VideoSvcApi.VIDEO_SVC_PATH + "/{id}/like") public @ResponseBody void likeVideo(@PathVariable("id") long id, Principal principal, HttpServletResponse response) { Video v = videoRepo.findOne(id); if (v != null) { HashSet<String> likers = v.getLikers(); if (likers.contains(principal.getName())) response.setStatus(HttpServletResponse.SC_BAD_REQUEST); else { likers.add(principal.getName()); videoRepo.save(v); response.setStatus(HttpServletResponse.SC_OK); } } else response.setStatus(HttpServletResponse.SC_NOT_FOUND); } @PreAuthorize("hasRole(USER)") @RequestMapping(method = RequestMethod.POST, value = VideoSvcApi.VIDEO_SVC_PATH + "/{id}/unlike") public @ResponseBody void unlikeVideo(@PathVariable("id") long id, Principal principal, HttpServletResponse response) { Video v = videoRepo.findOne(id); if (v != null) { HashSet<String> likers = v.getLikers(); if (likers.contains(principal.getName())) { likers.remove(principal.getName()); videoRepo.save(v); response.setStatus(HttpServletResponse.SC_OK); } else response.setStatus(HttpServletResponse.SC_BAD_REQUEST); } else response.setStatus(HttpServletResponse.SC_NOT_FOUND); } @PreAuthorize("hasRole(USER)") @RequestMapping(method = RequestMethod.GET, value = VideoSvcApi.VIDEO_TITLE_SEARCH_PATH) public @ResponseBody Collection<Video> findByTitle(@RequestParam(VideoSvcApi.TITLE_PARAMETER) String title) { return videoRepo.findByName(title); } @PreAuthorize("hasRole(USER)") @RequestMapping(method = RequestMethod.GET, value = VideoSvcApi.VIDEO_DURATION_SEARCH_PATH) public @ResponseBody Collection<Video> findByDurationLessThan( @RequestParam(VideoSvcApi.DURATION_PARAMETER) long duration) { ArrayList<Video> matches = new ArrayList<Video>(); Iterator<Video> iter = videoRepo.findAll().iterator(); while (iter.hasNext()) { Video v = iter.next(); if (v.getDuration() < duration) matches.add(v); } return matches; } @PreAuthorize("hasRole(USER)") @RequestMapping(method = RequestMethod.GET, value = VideoSvcApi.VIDEO_SVC_PATH + "/{id}/likedby") public @ResponseBody Collection<String> getUsersWhoLikedVideo(@PathVariable("id") long id, HttpServletResponse response) { Collection<String> ret = null; Video v = videoRepo.findOne(id); if (v != null) { ret = Lists.newArrayList(v.getLikers()); response.setStatus(HttpServletResponse.SC_OK); } else response.setStatus(HttpServletResponse.SC_NOT_FOUND); return ret; } }