package org.datacite.mds.web.api.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ValidationException;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.datacite.mds.service.DoiService;
import org.datacite.mds.service.HandleException;
import org.datacite.mds.service.SecurityException;
import org.datacite.mds.web.api.ApiController;
import org.datacite.mds.web.api.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.RequestParam;

@Controller
@RequestMapping("/*")
public class DoiApiController implements ApiController {

    private static Logger log4j = Logger.getLogger(DoiApiController.class);

    @Autowired
    DoiService doiService;

    @RequestMapping(value = "doi", method = { RequestMethod.PUT, RequestMethod.POST })
    public ResponseEntity<String> createOrUpdate(@RequestBody String body,
            @RequestParam(required = false) Boolean testMode, HttpServletRequest httpRequest)
            throws ValidationException, HandleException, SecurityException, NotFoundException {
        String method = httpRequest.getMethod();
        
        if (testMode == null)
            testMode = false;
        
        String[] lines = getBodyLines(body);
        String doi = lines[0];
        String url = lines[1];

        log4j.debug("*****" + method + " doi (testMode=" + testMode + ") doi: " + doi + ", url: " + url);

        HttpStatus httpStatus; 
        if (method.equals("POST")) {
            doiService.create(doi, url, testMode);
            httpStatus = HttpStatus.CREATED;
        } else { // PUT
            doiService.update(doi, url, testMode);
            httpStatus = HttpStatus.OK;
        }

        HttpHeaders headers = new HttpHeaders();
        return new ResponseEntity<String>("OK", headers, httpStatus);
    }
    
    private String[] getBodyLines(String body) throws ValidationException {
        String[] lines = body.split("\\r?\\n",3);
        
        boolean hasTwoLines = lines.length == 2 || (lines.length == 3 && StringUtils.isEmpty(lines[2]));
        boolean areFieldsEmpty = lines.length < 2 || StringUtils.isEmpty(lines[0]) || StringUtils.isEmpty(lines[1]);
        
        if (!hasTwoLines || areFieldsEmpty)
            throw new ValidationException("request body must contain exactly two lines: DOI and URL");
        return lines;
    }
}

@@ -292,32 +292,79 @@
 andleException;%0A
+import org.datacite.mds.service.HandleService;%0A
 import org.datac
@@ -390,32 +390,32 @@
 urityException;%0A
-
 import org.datac
@@ -1023,17 +1023,19 @@
 pping(%22/
-*
+doi
 %22)%0Apubli
@@ -1206,16 +1206,650 @@
 ervice;%0A
+    %0A    @Autowired%0A    HandleService handleService;%0A    %0A    @RequestMapping(value = %22**%22, method = %7B RequestMethod.GET, RequestMethod.HEAD %7D)%0A    public ResponseEntity%3CString%3E resolve(HttpServletRequest httpRequest) throws HandleException, NotFoundException %7B%0A        String doi = getDoiFromRequest(httpRequest);%0A        String url = handleService.resolve(doi);%0A        return new ResponseEntity%3CString%3E(url, HttpStatus.OK);%0A    %7D%0A    %0A    private String getDoiFromRequest(HttpServletRequest request) %7B%0A        String uri = request.getServletPath();%0A        String doi = uri.replaceFirst(%22/doi/%22, %22%22);%0A        return doi;%0A    %7D%0A    
 %0A    @Re
@@ -1870,19 +1870,16 @@
 alue = %22
-doi
 %22, metho

implement GET for DOI API