Search in sources :

Example 1 with UserCreateRequest

use of com.bwssystems.HABridge.api.UserCreateRequest in project ha-bridge by bwssytems.

the class HueMulator method userAdd.

private String userAdd(String body, String ipAddress, boolean followingSlash) {
    UserCreateRequest aNewUser = null;
    String newUser = null;
    String aDeviceType = null;
    boolean toContinue = false;
    if (bridgeSettings.isTraceupnp())
        log.info("Traceupnp: hue api user create requested: " + body + " from " + ipAddress);
    if (bridgeSettingMaster.getBridgeSecurity().isUseLinkButton() && bridgeSettingMaster.getBridgeControl().isLinkButton())
        toContinue = true;
    else if (!bridgeSettingMaster.getBridgeSecurity().isUseLinkButton())
        toContinue = true;
    if (toContinue) {
        log.debug("hue api user create requested: " + body + " from " + ipAddress);
        if (body != null && !body.isEmpty()) {
            try {
                aNewUser = aGsonHandler.fromJson(body, UserCreateRequest.class);
            } catch (Exception e) {
                log.warn("Could not add user. Request garbled: " + body);
                return aGsonHandler.toJson(HueErrorResponse.createResponse("2", "/", "Could not add user.", null, null, null).getTheErrors(), HueError[].class);
            }
            newUser = aNewUser.getUsername();
            aDeviceType = aNewUser.getDevicetype();
        }
        if (aDeviceType == null)
            aDeviceType = "<not given>";
        if (newUser == null) {
            newUser = bridgeSettingMaster.getBridgeSecurity().createWhitelistUser(aDeviceType);
        } else {
            bridgeSettingMaster.getBridgeSecurity().validateWhitelistUser(newUser, aDeviceType, false);
        }
        if (bridgeSettingMaster.getBridgeSecurity().isSettingsChanged())
            bridgeSettingMaster.updateConfigFile();
        if (bridgeSettings.isTraceupnp())
            log.info("Traceupnp: hue api user create requested for device type: " + aDeviceType + " and username: " + newUser + (followingSlash ? " /api/ called" : ""));
        log.debug("hue api user create requested for device type: " + aDeviceType + " and username: " + newUser + (followingSlash ? " /api/ called" : ""));
        return "[{\"success\":{\"username\":\"" + newUser + "\"}}]";
    }
    return aGsonHandler.toJson(HueErrorResponse.createResponse("1", "/api/", "unauthorized user", null, null, null).getTheErrors());
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) UserCreateRequest(com.bwssystems.HABridge.api.UserCreateRequest)

Example 2 with UserCreateRequest

use of com.bwssystems.HABridge.api.UserCreateRequest in project ha-bridge by bwssytems.

the class HueInfo method registerWithHue.

public String registerWithHue() {
    UserCreateRequest theLogin = new UserCreateRequest();
    theLogin.setDevicetype("HABridge#MyMachine");
    HttpPost postRequest = new HttpPost("http://" + hueAddress.getIp() + HUE_REQUEST);
    ContentType parsedContentType = ContentType.parse("application/json");
    StringEntity requestBody = new StringEntity(new Gson().toJson(theLogin), parsedContentType);
    HttpResponse response = null;
    postRequest.setEntity(requestBody);
    HttpClient anHttpClient = httpClient.getHttpClient();
    try {
        response = anHttpClient.execute(postRequest);
        log.debug("registerWithHue - POST execute on " + hueAddress.getName() + "URL responded: " + response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300) {
            String theBody = EntityUtils.toString(response.getEntity());
            log.debug("registerWithHue response data: " + theBody);
            if (theBody.contains("[{\"error\":")) {
                if (theBody.contains("link button not")) {
                    log.warn("registerWithHue needs link button pressed on HUE bridge: " + hueAddress.getName());
                } else
                    log.warn("registerWithHue returned an unexpected error: " + theBody);
            } else {
                //read content for data, SuccessUserResponse[].class);
                SuccessUserResponse[] theResponses = new Gson().fromJson(theBody, SuccessUserResponse[].class);
                hueAddress.setUsername(theResponses[0].getSuccess().getUsername());
            }
        }
        //close out inputstream ignore content
        EntityUtils.consume(response.getEntity());
    } catch (IOException e) {
        log.warn("Error logging into HUE: IOException in log", e);
    }
    return hueAddress.getUsername();
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) SuccessUserResponse(com.bwssystems.HABridge.api.SuccessUserResponse) ContentType(org.apache.http.entity.ContentType) HttpClient(org.apache.http.client.HttpClient) Gson(com.google.gson.Gson) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) UserCreateRequest(com.bwssystems.HABridge.api.UserCreateRequest)

Example 3 with UserCreateRequest

use of com.bwssystems.HABridge.api.UserCreateRequest in project ha-bridge by bwssytems.

the class HueUtil method registerWithHue.

public static final String registerWithHue(HTTPHandler anHttpHandler, String ipAddress, String aName, String theUser) {
    UserCreateRequest theLogin = new UserCreateRequest();
    theLogin.setDevicetype("HABridge#MyMachine");
    HttpPost postRequest = new HttpPost("http://" + ipAddress + HUE_REQUEST);
    ContentType parsedContentType = ContentType.parse("application/json");
    StringEntity requestBody = new StringEntity(new Gson().toJson(theLogin), parsedContentType);
    HttpResponse response = null;
    postRequest.setEntity(requestBody);
    HttpClient anHttpClient = anHttpHandler.getHttpClient();
    try {
        response = anHttpClient.execute(postRequest);
        log.debug("POST execute on URL responded: " + response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300) {
            String theBody = EntityUtils.toString(response.getEntity());
            log.debug("registerWithHue response data: " + theBody);
            if (theBody.contains("[{\"error\":")) {
                if (theBody.contains("link button not")) {
                    log.warn("registerWithHue needs link button pressed on HUE bridge: " + aName);
                } else
                    log.warn("registerWithHue returned an unexpected error: " + theBody);
            } else {
                //read content for data, SuccessUserResponse[].class);
                SuccessUserResponse[] theResponses = new Gson().fromJson(theBody, SuccessUserResponse[].class);
                theUser = theResponses[0].getSuccess().getUsername();
            }
        }
        //close out inputstream ignore content
        EntityUtils.consume(response.getEntity());
    } catch (IOException e) {
        log.warn("Error logging into HUE: IOException in log", e);
    }
    return theUser;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) SuccessUserResponse(com.bwssystems.HABridge.api.SuccessUserResponse) ContentType(org.apache.http.entity.ContentType) HttpClient(org.apache.http.client.HttpClient) Gson(com.google.gson.Gson) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) UserCreateRequest(com.bwssystems.HABridge.api.UserCreateRequest)

Aggregations

UserCreateRequest (com.bwssystems.HABridge.api.UserCreateRequest)3 SuccessUserResponse (com.bwssystems.HABridge.api.SuccessUserResponse)2 Gson (com.google.gson.Gson)2 IOException (java.io.IOException)2 HttpResponse (org.apache.http.HttpResponse)2 HttpClient (org.apache.http.client.HttpClient)2 HttpPost (org.apache.http.client.methods.HttpPost)2 ContentType (org.apache.http.entity.ContentType)2 StringEntity (org.apache.http.entity.StringEntity)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1