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