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