use of org.wso2.carbon.automation.test.utils.http.client.HttpResponse in project product-iots by wso2.
the class QSGUtils method getClientCredentials.
private static ClientCredentials getClientCredentials() {
ClientCredentials clientCredentials = null;
HashMap<String, String> headers = new HashMap<String, String>();
EMMQSGConfig emmqsgConfig = EMMQSGConfig.getInstance();
String dcrEndPoint = emmqsgConfig.getDcrEndPoint();
// Set the DCR payload
JSONObject obj = new JSONObject();
JSONArray arr = new JSONArray();
arr.add("android");
arr.add("device_management");
obj.put("applicationName", "qsg");
obj.put("tags", arr);
obj.put("isAllowedToAllDomains", false);
obj.put("isMappingAnExistingOAuthApp", false);
String authorizationStr = emmqsgConfig.getUsername() + ":" + emmqsgConfig.getPassword();
String authHeader = "Basic " + new String(Base64.encodeBase64(authorizationStr.getBytes()));
headers.put(Constants.Header.AUTH, authHeader);
// Set the headers
headers.put(Constants.Header.CONTENT_TYPE, Constants.ContentType.APPLICATION_JSON);
HTTPResponse httpResponse = HTTPInvoker.sendHTTPPost(dcrEndPoint, obj.toJSONString(), headers);
if (httpResponse.getResponseCode() == Constants.HTTPStatus.CREATED) {
try {
JSONObject jsonObject = (JSONObject) new JSONParser().parse(httpResponse.getResponse());
clientCredentials = new ClientCredentials();
clientCredentials.setClientKey((String) jsonObject.get("client_id"));
clientCredentials.setClientSecret((String) jsonObject.get("client_secret"));
} catch (ParseException e) {
e.printStackTrace();
}
}
return clientCredentials;
}
use of org.wso2.carbon.automation.test.utils.http.client.HttpResponse in project product-iots by wso2.
the class QSGUtils method getOAuthToken.
public static String getOAuthToken() {
QSGUtils.initConfig();
ClientCredentials clientCredentials = getClientCredentials();
String authorizationStr = clientCredentials.getClientKey() + ":" + clientCredentials.getClientSecret();
String authHeader = "Basic " + new String(Base64.encodeBase64(authorizationStr.getBytes()));
HashMap<String, String> headers = new HashMap<String, String>();
// Set the form params
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("username", EMMQSGConfig.getInstance().getUsername()));
urlParameters.add(new BasicNameValuePair("password", EMMQSGConfig.getInstance().getPassword()));
urlParameters.add(new BasicNameValuePair("grant_type", "password"));
urlParameters.add(new BasicNameValuePair("scope", "user:view user:manage user:admin:reset-password role:view role:manage policy:view policy:manage " + "application:manage appm:administration appm:create appm:publish appm:update appm:read"));
// Set the headers
headers.put(Constants.Header.CONTENT_TYPE, Constants.ContentType.APPLICATION_URL_ENCODED);
headers.put(Constants.Header.AUTH, authHeader);
HTTPResponse httpResponse = HTTPInvoker.sendHTTPPostWithURLParams(EMMQSGConfig.getInstance().getOauthEndPoint(), urlParameters, headers);
if (httpResponse.getResponseCode() == Constants.HTTPStatus.OK) {
try {
JSONObject jsonObject = (JSONObject) new JSONParser().parse(httpResponse.getResponse());
return (String) jsonObject.get("access_token");
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
}
use of org.wso2.carbon.automation.test.utils.http.client.HttpResponse in project product-iots by wso2.
the class AppOperations method addApplication.
public static boolean addApplication(String name, MobileApplication mblApp, boolean isEnterpriseApp) {
HashMap<String, String> headers = new HashMap<String, String>();
String appEndpoint = EMMQSGConfig.getInstance().getEmmHost() + appmPublisherAppsUrl;
// Set the application payload
JSONObject application = new JSONObject();
application.put("name", name);
application.put("description", "Sample application");
application.put("type", "enterprise");
// Set appMeta data
JSONObject appMeta = new JSONObject();
appMeta.put("package", mblApp.getPackageId());
appMeta.put("version", mblApp.getVersion());
if (isEnterpriseApp) {
application.put("marketType", "enterprise");
appMeta.put("path", mblApp.getAppId());
} else {
application.put("marketType", "public");
}
application.put("provider", "admin");
application.put("displayName", name);
application.put("category", "Business");
application.put("thumbnailUrl", mblApp.getIcon());
application.put("version", mblApp.getVersion());
application.put("banner", mblApp.getBanner());
application.put("platform", mblApp.getPlatform());
application.put("appType", mblApp.getPlatform());
// application.put("appUrL", mblApp.getAppId());
application.put("mediaType", "application/vnd.wso2-mobileapp+xml");
// Set screenshots
JSONArray screenshots = new JSONArray();
screenshots.add(mblApp.getScreenshot1());
screenshots.add(mblApp.getScreenshot2());
screenshots.add(mblApp.getScreenshot3());
application.put("appmeta", appMeta);
application.put("screenshots", screenshots);
// Set the headers
headers.put(Constants.Header.CONTENT_TYPE, Constants.ContentType.APPLICATION_JSON);
HTTPResponse httpResponse = HTTPInvoker.sendHTTPPostWithOAuthSecurity(appEndpoint, application.toJSONString(), headers);
if (Constants.HTTPStatus.OK == httpResponse.getResponseCode()) {
return true;
}
return false;
}
use of org.wso2.carbon.automation.test.utils.http.client.HttpResponse in project carbon-apimgt by wso2.
the class RestCallUtilImpl method getRequest.
/**
* {@inheritDoc}
*/
@Override
public HttpResponse getRequest(URI uri, MediaType acceptContentType, List<String> cookies) throws APIManagementException {
if (uri == null) {
throw new IllegalArgumentException("The URI must not be null");
}
HttpURLConnection httpConnection = null;
try {
httpConnection = (HttpURLConnection) uri.toURL().openConnection();
httpConnection.setRequestMethod(APIMgtConstants.FunctionsConstants.GET);
httpConnection.setDoOutput(true);
if (acceptContentType != null) {
httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.ACCEPT, acceptContentType.toString());
}
if (cookies != null && !cookies.isEmpty()) {
for (String cookie : cookies) {
httpConnection.addRequestProperty(APIMgtConstants.FunctionsConstants.COOKIE, cookie.split(";", 2)[0]);
}
}
return getResponse(httpConnection);
} catch (IOException e) {
throw new APIManagementException("Connection not established properly ", e);
} finally {
if (httpConnection != null) {
httpConnection.disconnect();
}
}
}
use of org.wso2.carbon.automation.test.utils.http.client.HttpResponse in project carbon-apimgt by wso2.
the class RestCallUtilImpl method loginRequest.
/**
* {@inheritDoc}
*/
@Override
public HttpResponse loginRequest(URI uri, String username, String password, MediaType acceptContentType) throws APIManagementException {
if (uri == null) {
throw new IllegalArgumentException("The URI must not be null");
}
if (username == null) {
throw new IllegalArgumentException("Username must not be null");
}
if (password == null) {
throw new IllegalArgumentException("Password must not be null");
}
HttpURLConnection httpConnection = null;
try {
httpConnection = (HttpURLConnection) uri.toURL().openConnection();
httpConnection.setRequestMethod(APIMgtConstants.FunctionsConstants.POST);
httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON);
httpConnection.setDoOutput(true);
if (acceptContentType != null) {
httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.ACCEPT, acceptContentType.toString());
}
JSONObject loginInfoJsonObj = new JSONObject();
loginInfoJsonObj.put(APIMgtConstants.FunctionsConstants.USERNAME, username);
loginInfoJsonObj.put(APIMgtConstants.FunctionsConstants.PASSWORD, password);
OutputStream outputStream = httpConnection.getOutputStream();
outputStream.write(loginInfoJsonObj.toString().getBytes(StandardCharsets.UTF_8));
outputStream.flush();
outputStream.close();
return getResponse(httpConnection);
} catch (IOException e) {
throw new APIManagementException("Connection not established properly ", e);
} finally {
if (httpConnection != null) {
httpConnection.disconnect();
}
}
}
Aggregations