Search in sources :

Example 1 with EMMQSGConfig

use of org.wso2.mdm.qsg.dto.EMMQSGConfig 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 2 with EMMQSGConfig

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

the class QSGUtils method initConfig.

public static EMMQSGConfig initConfig() {
    Properties props = new Properties();
    InputStream input = null;
    EMMQSGConfig emmConfig = null;
    try {
        input = new FileInputStream("config.properties");
        // load a properties file and set the properties
        props.load(input);
        emmConfig = EMMQSGConfig.getInstance();
        emmConfig.setEmmHost(props.getProperty("emm-host"));
        emmConfig.setDcrEndPoint(props.getProperty("dcr-endpoint"));
        emmConfig.setOauthEndPoint(props.getProperty("oauth-endpoint"));
        emmConfig.setUsername(props.getProperty("username"));
        emmConfig.setPassword(props.getProperty("password"));
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return emmConfig;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) EMMQSGConfig(org.wso2.mdm.qsg.dto.EMMQSGConfig) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream)

Aggregations

EMMQSGConfig (org.wso2.mdm.qsg.dto.EMMQSGConfig)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 JSONArray (org.json.simple.JSONArray)1 JSONObject (org.json.simple.JSONObject)1 JSONParser (org.json.simple.parser.JSONParser)1 ParseException (org.json.simple.parser.ParseException)1 ClientCredentials (org.wso2.mdm.qsg.dto.ClientCredentials)1 HTTPResponse (org.wso2.mdm.qsg.dto.HTTPResponse)1