Я учу весну MVC онлайн. В рамках моего курса я должен разработать облачный видеосервис. Технические характеристики упомянуты здесь. https://github.com/juleswhite/mobile-cloud-asgn1
Ниже приведен мой класс контроллера.
@Controller
publi c класс VideosController {private final AtomicLong currentId = new AtomicLong (1L);
//A Map to hold incoming Video meta data
private HashMap<Long, Video> videoMap = new HashMap<Long, Video>();
//Receives GET requests to /video and returns the current list
// list of videos in memory
@RequestMapping(value = "/video", method = RequestMethod.GET)
public @ResponseBody List<Video> getVideoList() throws IOException{
List<Video> resultList = new ArrayList<Video>();
for(Long id : videoMap.keySet()) {
resultList.add(videoMap.get(id));
}
return resultList;
}
//Receives POST requests to /video and adds the video object
//created from request data to the Map
@RequestMapping(value = "/video", method = RequestMethod.POST)
public @ResponseBody() Video addVideoMetadata(@RequestBody Video data)
{
//create a Video object
Video video = Video.create().withContentType(data.getContentType())
.withDuration(data.getDuration())
.withSubject(data.getSubject())
.withTitle(data.getTitle()).build();
//set the id for the video
long videoId = currentId.incrementAndGet();
video.setId(videoId);
//set the URL for this Video
String videoURL = getDataUrl(videoId);
video.setDataUrl(videoURL);
//save the Video metadata object to map
Video v = save(video);
return v;
}
// Receives POST requests to /video/{id}/data e.g. /videoa/2/data
// uploads the video file sent as MultipartFile
// and writes it to the disc
@RequestMapping(value = "/video/{id}/data", method = RequestMethod.POST)
public @ResponseBody ResponseEntity<VideoStatus> uploadVideo
(@RequestParam("data") MultipartFile data,
@PathVariable("id") long id,
HttpServletResponse response
) throws IOException
{
// if video with id not present
if(!videoMap.containsKey(id)) {
System.out.println(" this id not present");
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
//return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
}
InputStream in = null;
try {
//read the input stream
in = data.getInputStream();
}
catch(IOException ie){
System.out.println("Exception reading inputstream");
}
finally {
in.close();
}
//get the video
Video v = videoMap.get(id);
//write it to disk
VideoFileManager.get().saveVideoData(v, in);
VideoStatus vs = new VideoStatus(VideoStatus.VideoState.READY);
return new ResponseEntity<>(vs, HttpStatus.OK);
//response.setStatus(200);
//return new ResponseEntity<>(vs, HttpStatus.OK);
}
//Reads GET request to /vide/{id}/data and returns the video
//binary data as output stream
@RequestMapping(value = "/video/{id}/data", method = RequestMethod.GET)
public @ResponseBody ResponseEntity<OutputStream> getBinaryData(
@PathVariable("id") long videoId,
HttpServletResponse response) throws IOException {
//if id is incorrect
if(!videoMap.containsKey(videoId)) {
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
}
//get the video from Map
Video outVideo = videoMap.get(videoId);
VideoFileManager vm = VideoFileManager.get();
//write the binary data to OutputStream
OutputStream os = response.getOutputStream();
vm.copyVideoData(outVideo, os);
return new ResponseEntity<>(os, HttpStatus.OK);
}
//save incoming video metadata to a Map
public Video save(Video v) {
checkAndSetId(v);
if(!videoMap.containsKey(v.getId())) {
videoMap.put(v.getId(), v);
}
return v;
}
//helper method to generate url for video
private String getDataUrl(long videoId){
String url = getUrlBaseForLocalServer() + "/video/" + videoId + "/data";
return url;
}
private String getUrlBaseForLocalServer() {
HttpServletRequest request =
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String base =
"http://"+request.getServerName()
+ ((request.getServerPort() != 80) ? ":"+request.getServerPort() : "");
return base;
}
private void checkAndSetId(Video entity) {
if(entity.getId() == 0){
entity.setId(currentId.incrementAndGet());
}
}
}
Теперь я прохожу все тесты в AutoGradingTest. java Код модуля, но не testAddVideoData (). Он выдает ошибку Socket Time out, за которой следует бесконечная ошибка рекурсии, указывающая на строку с номером 159 в AutoGradingTest. java Логически мой код выглядит нормально. Многие другие ученики тоже сталкиваются с проблемой, связанной с самым сложным делом, но инструкторы курса не помогают. Может кто-нибудь помочь мне здесь? Большое спасибо.