Search in sources :

Example 46 with HttpResponse

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;
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) HTTPResponse(org.wso2.mdm.qsg.dto.HTTPResponse) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) EMMQSGConfig(org.wso2.mdm.qsg.dto.EMMQSGConfig) ParseException(org.json.simple.parser.ParseException) ClientCredentials(org.wso2.mdm.qsg.dto.ClientCredentials)

Example 47 with HttpResponse

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;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HTTPResponse(org.wso2.mdm.qsg.dto.HTTPResponse) ArrayList(java.util.ArrayList) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) ClientCredentials(org.wso2.mdm.qsg.dto.ClientCredentials)

Example 48 with HttpResponse

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;
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) HTTPResponse(org.wso2.mdm.qsg.dto.HTTPResponse) JSONArray(org.json.simple.JSONArray)

Example 49 with HttpResponse

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();
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) IOException(java.io.IOException)

Example 50 with HttpResponse

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();
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Aggregations

HttpResponse (org.wso2.carbon.automation.test.utils.http.client.HttpResponse)75 Test (org.testng.annotations.Test)72 JsonObject (com.google.gson.JsonObject)15 HTTPResponse (org.wso2.mdm.qsg.dto.HTTPResponse)15 JsonParser (com.google.gson.JsonParser)14 JSONObject (org.json.simple.JSONObject)11 HashMap (java.util.HashMap)9 JsonArray (com.google.gson.JsonArray)8 IOException (java.io.IOException)8 HttpURLConnection (java.net.HttpURLConnection)8 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)7 KeyManagementException (java.security.KeyManagementException)6 KeyStoreException (java.security.KeyStoreException)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 HttpResponse (org.apache.http.HttpResponse)6 ClientProtocolException (org.apache.http.client.ClientProtocolException)6 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 HttpPost (org.apache.http.client.methods.HttpPost)5 JSONArray (org.json.simple.JSONArray)5 JsonElement (com.google.gson.JsonElement)4