package com.github.dhavalmehta1997.savetogoogledrive.controller.drive;

import com.github.dhavalmehta1997.savetogoogledrive.controller.BaseController;
import com.github.dhavalmehta1997.savetogoogledrive.exception.HttpResponseException;
import com.github.dhavalmehta1997.savetogoogledrive.model.User;
import com.github.dhavalmehta1997.savetogoogledrive.uploader.UploadManager;
import com.github.dhavalmehta1997.savetogoogledrive.uploader.UploadTask;
import com.github.dhavalmehta1997.savetogoogledrive.uploader.Uploader;
import com.github.dhavalmehta1997.savetogoogledrive.uploader.drive.DriveUploaderBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URL;
import java.util.List;

@RestController
public class DriveUploadRequestHandler extends BaseController {

    private final HttpSession session;

    @Autowired
    public DriveUploadRequestHandler(HttpSession session) {
        this.session = session;
    }

    @RequestMapping("/drive/upload")
    public ResponseEntity handleUploadRequest(@RequestParam("url") URL url, @RequestParam(value = "filename", required = false) String filename) {
        User user = (User) session.getAttribute("user");
        try {
            Uploader uploader = new DriveUploaderBuilder()
                    .setUploadUrl(url)
                    .setFileName(filename)
                    .setUser(user)
                    .build();

            UploadTask uploadTask = new UploadTask(uploader);
            UploadManager.getUploadManager().add(uploadTask);
            List<String> uplaods = (List<String>) session.getAttribute("uploads");
            uplaods.add(uploadTask.getId());
            return new ResponseEntity(uploadTask.getId(), HttpStatus.OK);

        } catch (IOException ex) {
            throw new HttpResponseException(400, ex);
        }
    }
}

@@ -1354,16 +1354,24 @@
 seEntity
+%3CString%3E
  handleU
@@ -1914,18 +1914,18 @@
 ing%3E upl
-a
 o
+a
 ds = (Li
@@ -1988,10 +1988,10 @@
  upl
-a
 o
+a
 ds.a
@@ -2051,16 +2051,18 @@
 seEntity
+%3C%3E
 (uploadT
@@ -2087,24 +2087,191 @@
 tatus.OK);%0A%0A
+        %7D catch (java.net.UnknownHostException ex) %7B%0A            throw new HttpResponseException(HttpStatus.BAD_REQUEST, url.getHost() + %22: website not found. %22, ex);%0A
         %7D ca
@@ -2341,11 +2341,57 @@
 ion(
-400
+HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage()
 , ex

fix code break caused by change of HttpResponseException