// Path: java/src/org/drupal/project/computing/exception/DConfigException.java
// public class DConfigException extends Exception {
//     public DConfigException() {
//     }
// 
//     public DConfigException(String s) {
//         super(s);
//     }
// 
//     public DConfigException(String s, Throwable throwable) {
//         super(s, throwable);
//     }
// 
//     public DConfigException(Throwable throwable) {
//         super(throwable);
//     }
// }
// 
// Path: java/src/org/drupal/project/computing/exception/DSiteException.java
// public class DSiteException extends Exception {
//     public DSiteException() {
//         super();
//     }
// 
//     public DSiteException(String s) {
//         super(s);
//     }
// 
//     public DSiteException(String s, Throwable throwable) {
//         super(s, throwable);
//     }
// 
//     public DSiteException(Throwable throwable) {
//         super(throwable);
//     }
// 
//     /**
//      * The error code is arbitrary by the caller which sets it.
//      */
//     private int errorCode = 0;
// 
//     public int getErrorCode() {
//         return errorCode;
//     }
// 
//     public void setErrorCode(int errorCode) {
//         this.errorCode = errorCode;
//     }
// 
// }


import com.google.gson.JsonParseException;
import org.apache.commons.lang3.StringUtils;
import org.drupal.project.computing.exception.DConfigException;
import org.drupal.project.computing.exception.DSiteException;
import javax.script.Bindings;
import javax.script.SimpleBindings;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.util.Properties;
import java.util.logging.Logger;
package org.drupal.project.computing;



/**
 * This class allows accessing Drupal using the services.module. It requires Drupal REST Sever module, and both HTTP
 * Request and HTTP Response will use Content-Type = application/json. See Drupal Computing documentation about how to
 * configure at the Drupal end.
 */
public class DRestfulJsonServices {

    protected String baseUrl;
    protected String endpoint;
    protected String userName;
    protected String userPass;
    protected String httpUserAgent = "DrupalComputingAgent";
    protected String httpContentType = "application/json";

    protected URL servicesEndpoint;
    protected String servicesSessionToken;

    protected Logger logger = DUtils.getInstance().getPackageLogger();


    public DRestfulJsonServices(String baseUrl, String endpoint, String userName, String userPass) throws IllegalArgumentException {
        assert StringUtils.isNotBlank(baseUrl) && StringUtils.isNotBlank(endpoint) && StringUtils.isNotBlank(userName) && StringUtils.isNotBlank(userPass);

        this.baseUrl = baseUrl;
        this.endpoint = endpoint;
        this.userName = userName;
        this.userPass = userPass;

        try {
            this.servicesEndpoint = new URL(new URL(baseUrl), endpoint);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Malformed URL: " + baseUrl + " and endpoint: " + endpoint, e);
        }

        // set cookie support.
        // this is required to maintain sessions among different calls.
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
    }

    /**
     * Use the settings in config file and create access to Drupal services.
     *
     * @return the DRestfulJsonServices object.
     * @throws DConfigException
     */
// Path: java/src/org/drupal/project/computing/exception/DConfigException.java
// public class DConfigException extends Exception {
//     public DConfigException() {
//     }
// 
//     public DConfigException(String s) {
//         super(s);
//     }
// 
//     public DConfigException(String s, Throwable throwable) {
//         super(s, throwable);
//     }
// 
//     public DConfigException(Throwable throwable) {
//         super(throwable);
//     }
// }
// 
// Path: java/src/org/drupal/project/computing/exception/DSiteException.java
// public class DSiteException extends Exception {
//     public DSiteException() {
//         super();
//     }
// 
//     public DSiteException(String s) {
//         super(s);
//     }
// 
//     public DSiteException(String s, Throwable throwable) {
//         super(s, throwable);
//     }
// 
//     public DSiteException(Throwable throwable) {
//         super(throwable);
//     }
// 
//     /**
//      * The error code is arbitrary by the caller which sets it.
//      */
//     private int errorCode = 0;
// 
//     public int getErrorCode() {
//         return errorCode;
//     }
// 
//     public void setErrorCode(int errorCode) {
//         this.errorCode = errorCode;
//     }
// 
// }


// Path: java/src/org/drupal/project/computing/DRestfulJsonServices.java
import com.google.gson.JsonParseException;
import org.apache.commons.lang3.StringUtils;
import org.drupal.project.computing.exception.DConfigException;
import org.drupal.project.computing.exception.DSiteException;
import javax.script.Bindings;
import javax.script.SimpleBindings;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.util.Properties;
import java.util.logging.Logger;

package org.drupal.project.computing;



/**
 * This class allows accessing Drupal using the services.module. It requires Drupal REST Sever module, and both HTTP
 * Request and HTTP Response will use Content-Type = application/json. See Drupal Computing documentation about how to
 * configure at the Drupal end.
 */
public class DRestfulJsonServices {

    protected String baseUrl;
    protected String endpoint;
    protected String userName;
    protected String userPass;
    protected String httpUserAgent = "DrupalComputingAgent";
    protected String httpContentType = "application/json";

    protected URL servicesEndpoint;
    protected String servicesSessionToken;

    protected Logger logger = DUtils.getInstance().getPackageLogger();


    public DRestfulJsonServices(String baseUrl, String endpoint, String userName, String userPass) throws IllegalArgumentException {
        assert StringUtils.isNotBlank(baseUrl) && StringUtils.isNotBlank(endpoint) && StringUtils.isNotBlank(userName) && StringUtils.isNotBlank(userPass);

        this.baseUrl = baseUrl;
        this.endpoint = endpoint;
        this.userName = userName;
        this.userPass = userPass;

        try {
            this.servicesEndpoint = new URL(new URL(baseUrl), endpoint);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Malformed URL: " + baseUrl + " and endpoint: " + endpoint, e);
        }

        // set cookie support.
        // this is required to maintain sessions among different calls.
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
    }

    /**
     * Use the settings in config file and create access to Drupal services.
     *
     * @return the DRestfulJsonServices object.
     * @throws DConfigException
     */
    public static DRestfulJsonServices loadDefault() throws DConfigException {