Search in sources :

Example 16 with HttpHelper

use of com.centurylink.mdw.common.utilities.HttpHelper in project mdw-designer by CenturyLinkCloud.

the class GroovyTestCaseRun method http.

TestCaseResponse http(TestCaseHttp http) throws TestException {
    if (verbose) {
        log.println("http " + http.getMethod() + " request to " + http.getUri());
    }
    try {
        String url;
        if (http.getUri().startsWith("http://") || http.getUri().startsWith("https://")) {
            url = http.getUri();
        } else {
            url = dao.getCurrentServer().getServerUrl();
            if (http.getUri().startsWith("/"))
                url += http.getUri();
            else
                url += "/" + http.getUri();
        }
        HttpHelper helper;
        if (http.getMessage() != null && http.getMessage().getUser() != null)
            helper = new HttpHelper(new URL(url), http.getMessage().getUser(), http.getMessage().getPassword());
        else
            helper = new HttpHelper(new URL(url));
        Map<String, String> headers = new HashMap<String, String>();
        if (http.getMessage() != null) {
            if (http.getMessage().getPayload() != null && http.getMessage().getPayload().startsWith("{"))
                headers.put("Content-Type", "application/json");
            if (http.getMessage().getHeaders() != null)
                headers.putAll(http.getMessage().getHeaders());
        }
        if (!headers.isEmpty())
            helper.setHeaders(headers);
        if (http.getConnectTimeout() > 0)
            helper.setConnectTimeout(http.getConnectTimeout());
        if (http.getReadTimeout() > 0)
            helper.setReadTimeout(http.getReadTimeout());
        String actual;
        if (http.getMethod().equalsIgnoreCase("get")) {
            try {
                actual = helper.get();
            } catch (IOException ex) {
                actual = helper.getResponse();
            }
        } else {
            if (http.getMessage() == null) {
                if (!http.getMethod().equalsIgnoreCase("delete"))
                    throw new TestException("Missing payload for HTTP: " + http.getMethod());
            }
            try {
                if (http.getMethod().equalsIgnoreCase("post"))
                    actual = helper.post(http.getMessage().getPayload());
                else if (http.getMethod().equalsIgnoreCase("put"))
                    actual = helper.put(http.getMessage().getPayload());
                else if (http.getMethod().equalsIgnoreCase("patch"))
                    actual = helper.patch(http.getMessage().getPayload());
                else if (http.getMethod().equalsIgnoreCase("delete"))
                    actual = helper.delete(http.getMessage() == null ? null : http.getMessage().getPayload());
                else
                    throw new TestException("Unsupported http method: " + http.getMethod());
            } catch (IOException ex) {
                actual = helper.getResponse();
            }
        }
        if (verbose) {
            log.println("http response:");
            log.println(actual);
        }
        TestCaseResponse response = new TestCaseResponse();
        response.setHeaders(helper.getHeaders());
        response.setCode(helper.getResponseCode());
        response.setActual(actual);
        return response;
    } catch (Exception ex) {
        throw new TestException("Failed to send http " + http.getMethod() + " request to " + http.getUri(), ex);
    }
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) HttpHelper(com.centurylink.mdw.common.utilities.HttpHelper) URL(java.net.URL) JSONException(org.json.JSONException) DataAccessException(com.centurylink.mdw.common.exception.DataAccessException) IOException(java.io.IOException) XmlException(org.apache.xmlbeans.XmlException)

Example 17 with HttpHelper

use of com.centurylink.mdw.common.utilities.HttpHelper in project mdw-designer by CenturyLinkCloud.

the class RestfulServer method invokeService.

public MDWStatusMessageDocument invokeService(String request) throws DataAccessException, RemoteException {
    String response = null;
    try {
        // append to Services context root since sometimes only Services/* are excluded from CT auth
        HttpHelper httpHelper = getHttpHelper(getMdwWebUrl() + "/Services/REST");
        httpHelper.setConnectTimeout(getConnectTimeout());
        httpHelper.setReadTimeout(getReadTimeout());
        response = httpHelper.post(request);
        MDWStatusMessageDocument statusMessageDoc;
        if (response.startsWith("{")) {
            StatusMessage statusMessage = new StatusMessage(new JSONObject(response));
            statusMessageDoc = statusMessage.getStatusDocument();
        } else {
            statusMessageDoc = MDWStatusMessageDocument.Factory.parse(response, Compatibility.namespaceOptions());
        }
        MDWStatusMessage statusMessage = statusMessageDoc.getMDWStatusMessage();
        if (statusMessage.getStatusCode() == -3) {
            // event handler not registered
            throw new RemoteException("No event handler is registered for instance-level actions on: " + getMdwWebUrl());
        } else if (statusMessage.getStatusCode() != 0) {
            throw new RemoteException("Error response from server: " + statusMessage.getStatusMessage());
        }
        return statusMessageDoc;
    } catch (RemoteException ex) {
        // don't fall through to IOException catch block
        throw ex;
    } catch (SocketTimeoutException ex) {
        throw new DataAccessOfflineException("Timeout after " + getReadTimeout() + " ms", ex);
    } catch (IOException ex) {
        throw new DataAccessOfflineException("Unable to connect to " + getMdwWebUrl(), ex);
    } catch (JSONException ex) {
        throw new DataAccessException("Unparsable JSON response:\n" + response);
    } catch (XmlException ex) {
        throw new DataAccessException("Unparsable XML response:\n" + response);
    }
}
Also used : DataAccessOfflineException(com.centurylink.mdw.dataaccess.DataAccessOfflineException) JSONException(org.json.JSONException) IOException(java.io.IOException) MDWStatusMessage(com.centurylink.mdw.bpm.MDWStatusMessageDocument.MDWStatusMessage) StatusMessage(com.centurylink.mdw.common.service.types.StatusMessage) SocketTimeoutException(java.net.SocketTimeoutException) JSONObject(org.json.JSONObject) MDWStatusMessage(com.centurylink.mdw.bpm.MDWStatusMessageDocument.MDWStatusMessage) XmlException(org.apache.xmlbeans.XmlException) MDWStatusMessageDocument(com.centurylink.mdw.bpm.MDWStatusMessageDocument) RemoteException(java.rmi.RemoteException) HttpHelper(com.centurylink.mdw.common.utilities.HttpHelper) DataAccessException(com.centurylink.mdw.common.exception.DataAccessException)

Example 18 with HttpHelper

use of com.centurylink.mdw.common.utilities.HttpHelper in project mdw-designer by CenturyLinkCloud.

the class RestfulServer method invokeResourceServiceRaw.

/**
 * Used for retrieving document values (does not interpret response at all).
 */
public String invokeResourceServiceRaw(String path) throws DataAccessException, IOException {
    String url = getMdwWebUrl() + (path.startsWith("/") ? "Services/" + path : "/Services/" + path);
    try {
        HttpHelper httpHelper = getHttpHelper(url);
        httpHelper.setConnectTimeout(getConnectTimeout());
        httpHelper.setReadTimeout(getReadTimeout());
        return httpHelper.get();
    } catch (SocketTimeoutException ex) {
        throw new IOException("Timeout after " + getReadTimeout() + " ms", ex);
    } catch (IOException ex) {
        throw new IOException("Unable to connect to " + getMdwWebUrl(), ex);
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) HttpHelper(com.centurylink.mdw.common.utilities.HttpHelper)

Example 19 with HttpHelper

use of com.centurylink.mdw.common.utilities.HttpHelper in project mdw-designer by CenturyLinkCloud.

the class RestfulServer method getAppSummary.

public AppSummary getAppSummary() throws IOException {
    String url = appSummaryUrl + "/Services/AppSummary?format=json";
    HttpHelper httpHelper = new HttpHelper(new URL(url));
    httpHelper.setConnectTimeout(getConnectTimeout());
    httpHelper.setReadTimeout(getReadTimeout());
    try {
        return new AppSummary(new JSONObject(httpHelper.get()));
    } catch (JSONException ex) {
        throw new IOException("Unable to get app summary: " + url);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) IOException(java.io.IOException) AppSummary(com.centurylink.mdw.designer.model.AppSummary) HttpHelper(com.centurylink.mdw.common.utilities.HttpHelper) URL(java.net.URL)

Example 20 with HttpHelper

use of com.centurylink.mdw.common.utilities.HttpHelper in project mdw-designer by CenturyLinkCloud.

the class DesignerDataAccess method callTaskManager.

public String callTaskManager(String taskManagerUrl, String request) throws DataAccessException {
    try {
        HttpHelper httpHelper = currentServer.getHttpHelper(taskManagerUrl);
        String response = httpHelper.post(request);
        httpHelper.setConnectTimeout(getConnectTimeout());
        httpHelper.setReadTimeout(getReadTimeout());
        return response;
    } catch (IOException ex) {
        throw new DataAccessException(0, IOEXCEPTION, ex);
    }
}
Also used : IOException(java.io.IOException) HttpHelper(com.centurylink.mdw.common.utilities.HttpHelper) DataAccessException(com.centurylink.mdw.common.exception.DataAccessException)

Aggregations

HttpHelper (com.centurylink.mdw.common.utilities.HttpHelper)21 IOException (java.io.IOException)17 DataAccessException (com.centurylink.mdw.common.exception.DataAccessException)10 URL (java.net.URL)9 JSONException (org.json.JSONException)6 XmlException (org.apache.xmlbeans.XmlException)5 SocketTimeoutException (java.net.SocketTimeoutException)4 RemoteException (java.rmi.RemoteException)4 JSONObject (org.json.JSONObject)4 MdwSecurityException (com.centurylink.mdw.auth.MdwSecurityException)3 MDWStatusMessageDocument (com.centurylink.mdw.bpm.MDWStatusMessageDocument)3 HashMap (java.util.HashMap)3 CoreException (org.eclipse.core.runtime.CoreException)3 ActionCancelledException (com.centurylink.mdw.common.utilities.timer.ActionCancelledException)2 DataAccessOfflineException (com.centurylink.mdw.dataaccess.DataAccessOfflineException)2 AppSummary (com.centurylink.mdw.designer.model.AppSummary)2 DesignerHttpHelper (com.centurylink.mdw.designer.utils.DesignerHttpHelper)2 File (com.centurylink.mdw.plugin.designer.model.File)2 FileNotFoundException (java.io.FileNotFoundException)2 GeneralSecurityException (java.security.GeneralSecurityException)2