package qzui;

import com.github.kevinsawicki.http.HttpRequest;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import restx.factory.Component;

import java.util.List;

import static com.google.common.base.Strings.isNullOrEmpty;
import static org.quartz.JobBuilder.newJob;

/**
 * Date: 19/2/14
 * Time: 06:34
 */
@Component
public class HttpJobDefinition extends AbstractJobDefinition {
    private static final Logger logger = LoggerFactory.getLogger(HttpJobDefinition.class);

    @Override
    public boolean acceptJobClass(Class<? extends Job> jobClass) {
        return jobClass.getName() == HttpJob.class.getName();
    }

    @Override
    public JobDescriptor buildDescriptor(JobDetail jobDetail, List<? extends Trigger> triggersOfJob) {
        HttpJobDescriptor jobDescriptor = setupDescriptorFromDetail(new HttpJobDescriptor(), jobDetail, triggersOfJob);

        return jobDescriptor
                .setUrl((String) jobDescriptor.getData().remove("url"))
                .setMethod((String) jobDescriptor.getData().remove("method"))
                .setBody((String) jobDescriptor.getData().remove("body"));
    }

    public static class HttpJobDescriptor extends JobDescriptor {

        private String url;
        private String method = "POST";
        private String body;

        public String getUrl() {
            return url;
        }

        public String getMethod() {
            return method;
        }

        public String getBody() {
            return body;
        }

        public HttpJobDescriptor setBody(final String body) {
            this.body = body;
            return this;
        }

        public HttpJobDescriptor setMethod(final String method) {
            this.method = method;
            return this;
        }

        public HttpJobDescriptor setUrl(final String url) {
            this.url = url;
            return this;
        }

        @Override
        public JobDetail buildJobDetail() {
            JobDataMap dataMap = new JobDataMap(getData());
            dataMap.put("url", url);
            dataMap.put("method", method);
            dataMap.put("body", body);
            return newJob(HttpJob.class)
                    .withIdentity(getName(), getGroup())
                    .usingJobData(dataMap)
                    .build();
        }

        @Override
        public String toString() {
            return "HttpJobDescriptor{" +
                    "url='" + url + '\'' +
                    ", method='" + method + '\'' +
                    ", body='" + body + '\'' +
                    '}';
        }

    }

    public static class HttpJob implements Job {
        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
            JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
            String url = jobDataMap.getString("url");
            String method = jobDataMap.getString("method");
            HttpRequest request = new HttpRequest(url, method);
            String body = "";
            if (!isNullOrEmpty(jobDataMap.getString("body"))) {
                body = jobDataMap.getString("body");
            }
            request.send(body);

            int code = request.code();
            String responseBody = request.body();
            logger.info("{} {} => {}\n{}", method, url, code, responseBody);
        }
    }

}

@@ -1315,16 +1315,52 @@
 ng body;
+%0A        private String contentType;
 %0A%0A      
@@ -1557,32 +1557,116 @@
 ody;%0A        %7D%0A%0A
+        public String getContentType() %7B%0A            return contentType;%0A        %7D%0A%0A
         public H
@@ -2029,32 +2029,188 @@
 his;%0A        %7D%0A%0A
+        public HttpJobDescriptor setContentType(final String contentType) %7B%0A            this.contentType = contentType;%0A            return this;%0A        %7D%0A%0A
         @Overrid
@@ -2430,24 +2430,77 @@
 dy%22, body);%0A
+            dataMap.put(%22contentType%22, contentType);%0A
             
@@ -2897,32 +2897,93 @@
 + body + '%5C'' +%0A
+                    %22, contentType='%22 + contentType + '%5C'' +%0A
                 
@@ -3508,24 +3508,24 @@
 %22body%22))) %7B%0A
-
             
@@ -3575,24 +3575,205 @@
           %7D%0A
+            String contentType = jobDataMap.getString(%22contentType%22);%0A            if (!isNullOrEmpty(contentType)) %7B%0A                request.contentType(contentType);%0A            %7D%0A
             

Add contentType support concerning HttpJob definition