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

import javax.servlet.http.HttpServletRequest;

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.validation.ValidationException;
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;

        if (body.indexOf("\n") == -1 || body.indexOf("\n") != body.lastIndexOf("\n")
                || body.indexOf("\n") == body.length() - 1 || body.indexOf("\n") == 0)
            throw new ValidationException("request body must contain exactly two lines: DOI and URL");

        String doi = body.substring(0, body.indexOf("\n"));
        String url = body.substring(body.indexOf("\n") + 1, body.length());

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

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

        HttpHeaders headers = new HttpHeaders();
        return new ResponseEntity<String>("OK", headers, method.equals("POST") ? HttpStatus.CREATED : HttpStatus.OK);
    }
}

@@ -39,16 +39,42 @@
 oller;%0A%0A
+import java.util.Arrays;%0A%0A
 import j
@@ -112,16 +112,60 @@
 quest;%0A%0A
+import org.apache.commons.lang.StringUtils;%0A
 import o
@@ -1656,25 +1656,24 @@
  false;%0A
-%0A
         
 if (body
@@ -1668,276 +1668,54 @@
     
-if (body.indexOf(%22%5Cn%22) == -1 %7C%7C body.indexOf(%22%5Cn%22) != body.lastIndexOf(%22%5Cn%22)%0A                %7C%7C body.indexOf(%22%5Cn%22) == body.length() - 1 %7C%7C body.indexOf(%22%5Cn%22) == 0)%0A            throw new ValidationException(%22request body must contain exactly two lines: DOI and URL%22
+%0A        String%5B%5D lines = getBodyLines(body
 );%0A
-%0A
     
@@ -1735,121 +1735,47 @@
 i = 
-body.substring(0, body.indexOf(%22%5Cn%22));%0A        String url = body.substring(body.indexOf(%22%5Cn%22) + 1, body.length())
+lines%5B0%5D;%0A        String url = lines%5B1%5D
 ;%0A%0A 
@@ -2225,10 +2225,536 @@
 ;%0A    %7D%0A
+    %0A    private String%5B%5D getBodyLines(String body) throws ValidationException %7B%0A        String%5B%5D lines = body.split(%22%5C%5Cr?%5C%5Cn%22,3);%0A        %0A        boolean hasTwoLines = lines.length == 2 %7C%7C (lines.length == 3 && StringUtils.isEmpty(lines%5B2%5D));%0A        boolean areFieldsEmpty = StringUtils.isEmpty(lines%5B0%5D) %7C%7C StringUtils.isEmpty(lines%5B1%5D);%0A        %0A        if (!hasTwoLines %7C%7C areFieldsEmpty)%0A            throw new ValidationException(%22request body must contain exactly two lines: DOI and URL%22);%0A        return lines;%0A    %7D%0A
 %7D%0A

allow CR LF as line separation (closes #54)