All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jelastic.api.AbstractService Maven / Gradle / Ivy

The newest version!
package com.jelastic.api;

import com.jelastic.api.core.utils.AfterRequestAction;
import com.jelastic.api.core.utils.BeforeRequestAction;
import com.jelastic.api.core.utils.Transport;
import com.jelastic.api.development.Applications;
import com.jelastic.api.development.response.ApplicationHostsResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Ruslan Sinitskiy
 */
public abstract class AbstractService {

    public static String SERVER_URL = "http://app.jelastic.com/1.0/";
    private final static Map APPID_HOSTS = new HashMap();
    private Transport transport = new Transport();

    public AbstractService() {
        setFollowRedirects(true);
    }

    protected Object _call(final String appid, String serviceUrl, Map params, Class clazz) {
        return _call(appid, serviceUrl, params, null, clazz);
    }

    protected Object _call(final String appid, String serviceUrl, Map params, Map headers, Class clazz) {
        if (serviceUrl.indexOf("/app.jelastic.com/") == -1) {
            return transport.request(serviceUrl, params, headers, clazz);
        }

        Object host = APPID_HOSTS.get(appid);
        if (host == null) {
            APPID_HOSTS.put(appid, Boolean.FALSE);
            ApplicationHostsResponse resp = new Applications(appid).getAppHosts();
            if (resp.getResult() == 0 && resp.getOptimal() != null) {
                APPID_HOSTS.put(appid, resp.getOptimal().getDomain());
            }

            return _call(appid, serviceUrl, params, clazz);

        } else if (host.equals(Boolean.FALSE)) {
            return transport.request(serviceUrl, params, headers, clazz);
        } else {
            String mark = "/app.jelastic.com/";
            int ind = serviceUrl.indexOf(mark);
            if (ind > -1) {
                serviceUrl = serviceUrl.substring(0, ind + 1) + host + serviceUrl.substring(ind + mark.length() - 1);
            }
            return transport.request(serviceUrl, params, headers, clazz);
        }
    }


    public void addBeforeRequestAction(BeforeRequestAction beforeRequestAction) {
        transport.addBeforeRequestAction(beforeRequestAction);
    }

    public void removeBeforeRequestAction(BeforeRequestAction beforeRequestAction) {
        transport.removeBeforeRequestAction(beforeRequestAction);
    }

    public void addAfterRequestAction(AfterRequestAction afterRequestAction) {
        transport.addAfterRequestAction(afterRequestAction);
    }

    public void removeAfterRequestAction(AfterRequestAction afterRequestAction) {
        transport.removeAfterRequestAction(afterRequestAction);
    }

    public boolean isFollowRedirects() {
        return this.transport.isFollowRedirects();
    }

    public void setFollowRedirects(boolean followRedirects) {
        this.transport.setFollowRedirects(followRedirects);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy