Search in sources :

Example 96 with HTTPResponse

use of org.wso2.mdm.qsg.dto.HTTPResponse in project product-iots by wso2.

the class PolicyOperations method createPasscodePolicy.

public static boolean createPasscodePolicy(String policyName, String deviceType) {
    HashMap<String, String> headers = new HashMap<String, String>();
    String policyEndpoint = EMMQSGConfig.getInstance().getEmmHost() + "/api/device-mgt/v1.0/policies";
    // Set the policy payload
    JSONObject policyData = new JSONObject();
    policyData.put("policyName", policyName);
    policyData.put("description", "Passcode Policy");
    policyData.put("compliance", "enforce");
    policyData.put("ownershipType", "ANY");
    policyData.put("active", false);
    JSONObject profile = new JSONObject();
    profile.put("profileName", "passcode");
    profile.put("deviceType", deviceType);
    JSONArray featureList = new JSONArray();
    JSONObject feature = new JSONObject();
    feature.put("featureCode", "PASSCODE_POLICY");
    feature.put("deviceType", deviceType);
    JSONObject featureContent = new JSONObject();
    featureContent.put("allowSimple", false);
    featureContent.put("requireAlphanumeric", true);
    featureContent.put("minLength", "5");
    featureContent.put("minComplexChars", "2");
    featureContent.put("maxPINAgeInDays", 7);
    featureContent.put("pinHistory", 7);
    featureContent.put("maxFailedAttempts", null);
    feature.put("content", featureContent);
    featureList.add(feature);
    profile.put("profileFeaturesList", featureList);
    JSONArray roles = new JSONArray();
    roles.add(Constants.EMM_USER_ROLE);
    policyData.put("profile", profile);
    policyData.put("roles", roles);
    // Set the headers
    headers.put(Constants.Header.CONTENT_TYPE, Constants.ContentType.APPLICATION_JSON);
    HTTPResponse httpResponse = HTTPInvoker.sendHTTPPostWithOAuthSecurity(policyEndpoint, policyData.toJSONString(), headers);
    if (httpResponse.getResponseCode() == Constants.HTTPStatus.CREATED) {
        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 97 with HTTPResponse

use of org.wso2.mdm.qsg.dto.HTTPResponse in project product-iots by wso2.

the class UserOperations method changePassword.

public static boolean changePassword(String username, String pwd) {
    HashMap<String, String> headers = new HashMap<String, String>();
    String pwdEndpoint = EMMQSGConfig.getInstance().getEmmHost() + "/api/device-mgt/v1.0/admin/users/" + username + "/credentials";
    // Set the password payload
    JSONObject pwdData = new JSONObject();
    pwdData.put("newPassword", pwd);
    // Set the headers
    headers.put(Constants.Header.CONTENT_TYPE, Constants.ContentType.APPLICATION_JSON);
    HTTPResponse httpResponse = HTTPInvoker.sendHTTPPostWithOAuthSecurity(pwdEndpoint, pwdData.toJSONString(), headers);
    if (httpResponse.getResponseCode() == Constants.HTTPStatus.OK) {
        return true;
    }
    return false;
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) HTTPResponse(org.wso2.mdm.qsg.dto.HTTPResponse)

Example 98 with HTTPResponse

use of org.wso2.mdm.qsg.dto.HTTPResponse in project product-iots by wso2.

the class UserOperations method createRole.

public static boolean createRole(String roleName, String[] users) {
    HashMap<String, String> headers = new HashMap<String, String>();
    String roleEndpoint = EMMQSGConfig.getInstance().getEmmHost() + "/api/device-mgt/v1.0/roles";
    // Set the role payload
    JSONObject roleData = new JSONObject();
    roleData.put("roleName", roleName);
    JSONArray perms = new JSONArray();
    String[] permissions = getUserPermissions();
    for (String perm : permissions) {
        perms.add(perm);
    }
    roleData.put("permissions", perms);
    JSONArray usrs = new JSONArray();
    for (String usr : users) {
        usrs.add(usr);
    }
    roleData.put("permissions", perms);
    roleData.put("users", usrs);
    // Set the headers
    headers.put(Constants.Header.CONTENT_TYPE, Constants.ContentType.APPLICATION_JSON);
    HTTPResponse httpResponse = HTTPInvoker.sendHTTPPostWithOAuthSecurity(roleEndpoint, roleData.toJSONString(), headers);
    if (httpResponse.getResponseCode() == Constants.HTTPStatus.CREATED) {
        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 99 with HTTPResponse

use of org.wso2.mdm.qsg.dto.HTTPResponse in project product-iots by wso2.

the class UserOperations method createUser.

public static boolean createUser(String username, String email, boolean isAdmin) {
    HashMap<String, String> headers = new HashMap<String, String>();
    String userEndpoint = EMMQSGConfig.getInstance().getEmmHost() + "/api/device-mgt/v1.0/users";
    // Set the user payload
    JSONObject userData = new JSONObject();
    userData.put("username", username);
    userData.put("emailAddress", email);
    JSONArray roles = new JSONArray();
    if (isAdmin) {
        roles.add("admin");
        userData.put("firstname", "Chris");
        userData.put("lastname", "Admin");
    } else {
        userData.put("password", "kimemmtrial");
        userData.put("firstname", "Alex");
        userData.put("lastname", "User");
    }
    userData.put("roles", roles);
    // Set the headers
    headers.put(Constants.Header.CONTENT_TYPE, Constants.ContentType.APPLICATION_JSON);
    HTTPResponse httpResponse = HTTPInvoker.sendHTTPPostWithOAuthSecurity(userEndpoint, userData.toJSONString(), headers);
    if (httpResponse.getResponseCode() == Constants.HTTPStatus.CREATED) {
        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 100 with HTTPResponse

use of org.wso2.mdm.qsg.dto.HTTPResponse in project product-iots by wso2.

the class HTTPInvoker method uploadFile.

public static HTTPResponse uploadFile(String url, String fileName, String fileContentType) {
    HttpPost post = null;
    HttpResponse response = null;
    HTTPResponse httpResponse = new HTTPResponse();
    CloseableHttpClient httpclient = null;
    try {
        httpclient = (CloseableHttpClient) createHttpClient();
        post = new HttpPost(url);
        File file = new File(fileName);
        MultipartEntity mpEntity = new MultipartEntity();
        ContentBody cbFile = new FileBody(file, fileContentType);
        mpEntity.addPart("file", cbFile);
        post.setEntity(mpEntity);
        post.setHeader(Constants.Header.AUTH, OAUTH_BEARER + oAuthToken);
        // post.setHeader(Constants.Header.CONTENT_TYPE, "multipart/form-data");
        post.setHeader("Accept", Constants.ContentType.APPLICATION_JSON);
        response = httpclient.execute(post);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyStoreException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    BufferedReader rd = null;
    try {
        rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    } catch (IOException e) {
        e.printStackTrace();
    }
    StringBuffer result = new StringBuffer();
    String line = "";
    try {
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    httpResponse.setResponseCode(response.getStatusLine().getStatusCode());
    httpResponse.setResponse(result.toString());
    try {
        httpclient.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return httpResponse;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) FileBody(org.apache.http.entity.mime.content.FileBody) HTTPResponse(org.wso2.mdm.qsg.dto.HTTPResponse) HttpResponse(org.apache.http.HttpResponse) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) ClientProtocolException(org.apache.http.client.ClientProtocolException) ContentBody(org.apache.http.entity.mime.content.ContentBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity)

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