package de.jeha.kame.crawler.scheduler.resources;


import com.codahale.metrics.annotation.Timed;
import de.jeha.kame.crawler.scheduler.services.SchedulerService;
import de.jeha.kame.crawler.scheduler.api.SubmitCrawlJobRequest;
import de.jeha.kame.crawler.scheduler.api.SubmitCrawlJobResponse;
import de.jeha.kame.crawler.scheduler.model.CrawlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/")
public class CrawlJobResource {

    private static final Logger LOG = LoggerFactory.getLogger(CrawlJobResource.class);

    private final SchedulerService schedulerService;

    public CrawlJobResource(SchedulerService schedulerService) {
        this.schedulerService = schedulerService;
    }

    @POST
    @Path("/submitCrawlJob")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Timed
    public SubmitCrawlJobResponse submitCrawlJob(SubmitCrawlJobRequest request) {
        LOG.info("{}", request);

        CrawlJob crawlJob = new CrawlJob(
                request.getName(),
                request.getSeedUrl(),
                request.getAllowedDomains()
        );

        schedulerService.addJob(crawlJob);

        return new SubmitCrawlJobResponse(crawlJob.getId());
    }

}
@@ -95,73 +95,8 @@
 ed;%0A
-import de.jeha.kame.crawler.scheduler.services.SchedulerService;%0A
 impo
@@ -276,16 +276,148 @@
 awlJob;%0A
+import de.jeha.kame.crawler.scheduler.services.SchedulerException;%0Aimport de.jeha.kame.crawler.scheduler.services.SchedulerService;%0A
 import o
@@ -1335,24 +1335,42 @@
         );%0A%0A
+        try %7B%0A    
         sche
@@ -1399,16 +1399,210 @@
 awlJob);
+%0A        %7D catch (SchedulerException e) %7B%0A            LOG.error(%22Unable to add job%22, e);%0A            return new SubmitCrawlJobResponse(null); // TODO: add an appropriate error response%0A        %7D
 %0A%0A      

handle exception