package qzui.domain;

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;
        private String contentType;

        public String getUrl() {
            return url;
        }

        public String getMethod() {
            return method;
        }

        public String getBody() {
            return body;
        }

        public String getContentType() {
            return contentType;
        }

        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;
        }

        public HttpJobDescriptor setContentType(final String contentType) {
            this.contentType = contentType;
            return this;
        }

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

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

    }

    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");
            }
            String contentType = jobDataMap.getString("contentType");
            if (!isNullOrEmpty(contentType)) {
                request.contentType(contentType);
            }
            request.send(body);

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

}

@@ -1358,16 +1358,78 @@
 entType;
+%0A        private String login;%0A        private String pwdHash;
 %0A%0A      
@@ -1710,32 +1710,180 @@
 ype;%0A        %7D%0A%0A
+        public String getLogin() %7B%0A            return login;%0A        %7D%0A%0A        public String getPwdHash() %7B%0A            return pwdHash;%0A        %7D%0A%0A
         public H
@@ -2402,32 +2402,304 @@
 his;%0A        %7D%0A%0A
+        public HttpJobDescriptor setPwdHash(final String pwdHash) %7B%0A            this.pwdHash = pwdHash;%0A            return this;%0A        %7D%0A%0A        public HttpJobDescriptor setLogin(final String login) %7B%0A            this.login = login;%0A            return this;%0A        %7D%0A%0A
         @Overrid
@@ -2968,32 +2968,114 @@
 , contentType);%0A
+            dataMap.put(%22login%22, login);%0A            dataMap.put(%22pwd%22, pwdHash);%0A
             retu
@@ -3731,24 +3731,25 @@
 Exception %7B%0A
+%0A
             
@@ -3982,32 +3982,33 @@
 t(url, method);%0A
+%0A
             Stri
@@ -4144,32 +4144,33 @@
 ;%0A            %7D%0A
+%0A
             Stri
@@ -4165,24 +4165,20 @@
         
-String c
+setC
 ontentTy
@@ -4179,19 +4179,17 @@
 tentType
- = 
+(
 jobDataM
@@ -4194,145 +4194,70 @@
 aMap
-.getString(%22contentType%22);%0A            if (!isNullOrEmpty(contentType)) %7B%0A                request.contentType(contentType);%0A            %7D
+, request);%0A            setCrendentials(jobDataMap, request);%0A
 %0A   
@@ -4285,17 +4285,16 @@
 (body);%0A
-%0A
         
@@ -4370,24 +4370,25 @@
 est.body();%0A
+%0A
             
@@ -4461,16 +4461,617 @@
        %7D
+%0A%0A        private void setCrendentials(JobDataMap jobDataMap, HttpRequest request) %7B%0A            String login = jobDataMap.getString(%22login%22);%0A            String pwd = jobDataMap.getString(%22pwd%22);%0A            if (!isNullOrEmpty(login) && !isNullOrEmpty(pwd)) %7B%0A                request.basic(login, pwd);%0A            %7D%0A        %7D%0A%0A        private void setContentType(JobDataMap jobDataMap, HttpRequest request) %7B%0A            String contentType = jobDataMap.getString(%22contentType%22);%0A            if (!isNullOrEmpty(contentType)) %7B%0A                request.contentType(contentType);%0A            %7D%0A        %7D
 %0A    %7D%0A%0A

Support http basic authentication concerning http jobs