Example usage for org.apache.commons.httpclient HttpStatus SC_UNAUTHORIZED

List of usage examples for org.apache.commons.httpclient HttpStatus SC_UNAUTHORIZED

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpStatus SC_UNAUTHORIZED.

Prototype

int SC_UNAUTHORIZED

To view the source code for org.apache.commons.httpclient HttpStatus SC_UNAUTHORIZED.

Click Source Link

Document

<tt>401 Unauthorized</tt> (HTTP/1.0 - RFC 1945)

Usage

From source file:org.wso2.carbon.device.mgt.iot.firealarm.api.FireAlarmControllerService.java

@Path("/bulb/{state}")
@POST/*w w w. j a v a2 s .c  o m*/
public void switchBulb(@HeaderParam("owner") String owner, @HeaderParam("deviceId") String deviceId,
        @HeaderParam("protocol") String protocol, @PathParam("state") String state,
        @Context HttpServletResponse response) {

    try {
        DeviceValidator deviceValidator = new DeviceValidator();
        if (!deviceValidator.isExist(owner, new DeviceIdentifier(deviceId, FireAlarmConstants.DEVICE_TYPE))) {
            response.setStatus(HttpStatus.SC_UNAUTHORIZED);
            return;
        }
    } catch (DeviceManagementException e) {
        log.error("DeviceValidation Failed for deviceId: " + deviceId + " of user: " + owner);
        response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        return;
    }

    String switchToState = state.toUpperCase();

    if (!switchToState.equals(FireAlarmConstants.STATE_ON)
            && !switchToState.equals(FireAlarmConstants.STATE_OFF)) {
        log.error("The requested state change shoud be either - 'ON' or 'OFF'");
        response.setStatus(HttpStatus.SC_BAD_REQUEST);
        return;
    }

    String deviceIP = deviceToIpMap.get(deviceId);
    if (deviceIP == null) {
        response.setStatus(HttpStatus.SC_PRECONDITION_FAILED);
        return;
    }

    String protocolString = protocol.toUpperCase();
    String callUrlPattern = BULB_CONTEXT + switchToState;

    log.info("Sending command: '" + callUrlPattern + "' to firealarm at: " + deviceIP + " " + "via" + " "
            + protocol);

    try {
        switch (protocolString) {
        case HTTP_PROTOCOL:
            sendCommandViaHTTP(deviceIP, 80, callUrlPattern, true);
            break;
        case MQTT_PROTOCOL:
            callUrlPattern = BULB_CONTEXT.replace("/", "");
            sendCommandViaMQTT(owner, deviceId, callUrlPattern, switchToState);
            break;
        default:
            if (protocolString == null) {
                sendCommandViaHTTP(deviceIP, 80, callUrlPattern, true);
            } else {
                response.setStatus(HttpStatus.SC_NOT_IMPLEMENTED);
                return;
            }
            break;
        }
    } catch (DeviceManagementException e) {
        log.error("Failed to send command '" + callUrlPattern + "' to: " + deviceIP + " via" + " " + protocol);
        response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        return;
    }

    response.setStatus(HttpStatus.SC_OK);
}

From source file:org.wso2.carbon.device.mgt.iot.firealarm.api.FireAlarmControllerService.java

@Path("/fan/{state}")
@POST//from   w w  w.j  a va  2  s  .  c  o  m
public void switchFan(@HeaderParam("owner") String owner, @HeaderParam("deviceId") String deviceId,
        @HeaderParam("protocol") String protocol, @PathParam("state") String state,
        @Context HttpServletResponse response) {

    try {
        DeviceValidator deviceValidator = new DeviceValidator();
        if (!deviceValidator.isExist(owner, new DeviceIdentifier(deviceId, FireAlarmConstants.DEVICE_TYPE))) {
            response.setStatus(HttpStatus.SC_UNAUTHORIZED);
            return;
        }
    } catch (DeviceManagementException e) {
        log.error("DeviceValidation Failed for deviceId: " + deviceId + " of user: " + owner);
        response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        return;
    }

    String switchToState = state.toUpperCase();

    if (!switchToState.equals(FireAlarmConstants.STATE_ON)
            && !switchToState.equals(FireAlarmConstants.STATE_OFF)) {
        log.error("The requested state change shoud be either - 'ON' or 'OFF'");
        response.setStatus(HttpStatus.SC_BAD_REQUEST);
        return;
    }

    String deviceIP = deviceToIpMap.get(deviceId);

    if (deviceIP == null) {
        response.setStatus(HttpStatus.SC_PRECONDITION_FAILED);
        return;
    }

    String protocolString = protocol.toUpperCase();
    String callUrlPattern = FAN_CONTEXT + switchToState;

    log.info("Sending command: '" + callUrlPattern + "' to firealarm at: " + deviceIP + " " + "via" + " "
            + protocol);

    try {
        switch (protocolString) {
        case HTTP_PROTOCOL:
            sendCommandViaHTTP(deviceIP, 80, callUrlPattern, true);
            break;
        case MQTT_PROTOCOL:
            callUrlPattern = FAN_CONTEXT.replace("/", "");
            sendCommandViaMQTT(owner, deviceId, callUrlPattern, switchToState);
            break;
        default:
            if (protocolString == null) {
                sendCommandViaHTTP(deviceIP, 80, callUrlPattern, true);
            } else {
                response.setStatus(HttpStatus.SC_NOT_IMPLEMENTED);
                return;
            }
            break;
        }
    } catch (DeviceManagementException e) {
        log.error("Failed to send command '" + callUrlPattern + "' to: " + deviceIP + " via" + " " + protocol);
        response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        return;
    }

    response.setStatus(HttpStatus.SC_OK);
}

From source file:org.wso2.carbon.device.mgt.iot.firealarm.api.FireAlarmControllerService.java

@Path("/readtemperature")
@GET/*from  w w w .  j  a va 2s .c o  m*/
public String requestTemperature(@HeaderParam("owner") String owner, @HeaderParam("deviceId") String deviceId,
        @HeaderParam("protocol") String protocol, @Context HttpServletResponse response) {
    String replyMsg = "";

    DeviceValidator deviceValidator = new DeviceValidator();
    try {
        if (!deviceValidator.isExist(owner, new DeviceIdentifier(deviceId, FireAlarmConstants.DEVICE_TYPE))) {
            response.setStatus(HttpStatus.SC_UNAUTHORIZED);
            return "Unauthorized Access";
        }
    } catch (DeviceManagementException e) {
        replyMsg = e.getErrorMessage();
        response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        return replyMsg;
    }

    String deviceIp = deviceToIpMap.get(deviceId);

    if (deviceIp == null) {
        replyMsg = "IP not registered for device: " + deviceId + " of owner: " + owner;
        response.setStatus(HttpStatus.SC_PRECONDITION_FAILED);
        return replyMsg;
    }

    try {
        switch (protocol) {
        case HTTP_PROTOCOL:
            log.info(
                    "Sending request to read firealarm-temperature at : " + deviceIp + " via " + HTTP_PROTOCOL);

            replyMsg = sendCommandViaHTTP(deviceIp, 80, TEMPERATURE_CONTEXT, false);
            break;

        case XMPP_PROTOCOL:
            log.info(
                    "Sending request to read firealarm-temperature at : " + deviceIp + " via " + XMPP_PROTOCOL);
            replyMsg = requestTemperatureViaXMPP(deviceIp, response);
            break;

        default:
            if (protocol == null) {
                log.info("Sending request to read firealarm-temperature at : " + deviceIp + " via "
                        + HTTP_PROTOCOL);

                replyMsg = sendCommandViaHTTP(deviceIp, 80, TEMPERATURE_CONTEXT, false);
            } else {
                replyMsg = "Requested protocol '" + protocol + "' is not supported";
                response.setStatus(HttpStatus.SC_NOT_IMPLEMENTED);
                return replyMsg;
            }
            break;
        }
    } catch (DeviceManagementException e) {
        replyMsg = e.getErrorMessage();
        response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        return replyMsg;
    }

    response.setStatus(HttpStatus.SC_OK);
    replyMsg = "The current temperature of the device is " + replyMsg;
    return replyMsg;
}

From source file:org.wso2.carbon.device.mgt.iot.firealarm.api.FireAlarmControllerService.java

@Path("/readsonar")
@GET//  w w  w  .ja v a  2  s  .co m
public String requestSonar(@HeaderParam("owner") String owner, @HeaderParam("deviceId") String deviceId,
        @HeaderParam("protocol") String protocol, @Context HttpServletResponse response) {
    String replyMsg = "";

    DeviceValidator deviceValidator = new DeviceValidator();
    try {
        if (!deviceValidator.isExist(owner, new DeviceIdentifier(deviceId, FireAlarmConstants.DEVICE_TYPE))) {
            response.setStatus(HttpStatus.SC_UNAUTHORIZED);
            return "Unauthorized Access";
        }
    } catch (DeviceManagementException e) {
        replyMsg = e.getErrorMessage();
        response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        return replyMsg;
    }

    String deviceIp = deviceToIpMap.get(deviceId);

    if (deviceIp == null) {
        replyMsg = "IP not registered for device: " + deviceId + " of owner: " + owner;
        response.setStatus(HttpStatus.SC_PRECONDITION_FAILED);
        return replyMsg;
    }

    try {
        switch (protocol) {
        case HTTP_PROTOCOL:
            log.info("Sending request to read firealarm-sonar at : " + deviceIp + " via " + HTTP_PROTOCOL);

            replyMsg = sendCommandViaHTTP(deviceIp, 80, SONAR_CONTEXT, false);
            break;

        //            case XMPP_PROTOCOL:
        //               log.info("Sending request to read firealarm-sonar at : " + deviceIp +
        //                            " via " +
        //                            XMPP_PROTOCOL);
        //               replyMsg = requestTemperatureViaXMPP(deviceIp, response);
        //               break;

        default:
            if (protocol == null) {
                log.info("Sending request to read firealarm-sonar at : " + deviceIp + " via " + HTTP_PROTOCOL);

                replyMsg = sendCommandViaHTTP(deviceIp, 80, SONAR_CONTEXT, false);
            } else {
                replyMsg = "Requested protocol '" + protocol + "' is not supported";
                response.setStatus(HttpStatus.SC_NOT_IMPLEMENTED);
                return replyMsg;
            }
            break;
        }
    } catch (DeviceManagementException e) {
        replyMsg = e.getErrorMessage();
        response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        return replyMsg;
    }

    response.setStatus(HttpStatus.SC_OK);
    replyMsg = "The current sonar reading of the device is " + replyMsg;
    return replyMsg;
}

From source file:org.wso2.carbon.device.mgt.iot.sensebot.api.SensebotControllerService.java

@Path("/pushsensordata")
@POST//from  w ww.  j a v a  2s  . co m
@Consumes(MediaType.APPLICATION_JSON)
public void pushAlarmData(final DeviceJSON dataMsg, @Context HttpServletResponse response) {
    boolean result = false;

    String sensorValues = dataMsg.value; //TEMP-PIR-SONAR-LDR
    log.info("Recieved Sensor Data Values: " + sensorValues);

    String sensors[] = sensorValues.split(":");
    try {
        if (sensors.length == 4) {
            String temperature = sensors[0];
            String motion = sensors[1];
            String sonar = sensors[2];
            String light = sensors[3];

            if (sonar.equals("-1")) {
                sonar = "No Object";
            }

            sensorValues = "Temperature:" + temperature + "C\t\tMotion:" + motion + "\tSonar:" + sonar
                    + "\tLight:" + light;

            if (log.isDebugEnabled())
                log.debug(sensorValues);

            result = DeviceController.pushData(dataMsg.owner, "SenseBot", dataMsg.deviceId,
                    System.currentTimeMillis(), "DeviceData", temperature, "TEMP");

            if (!result) {
                response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                return;
            }

            result = DeviceController.pushData(dataMsg.owner, "SenseBot", dataMsg.deviceId,
                    System.currentTimeMillis(), "DeviceData", motion, "MOTION");

            if (!result) {
                response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                return;
            }

            if (!sonar.equals("No Object")) {
                result = DeviceController.pushData(dataMsg.owner, "SenseBot", dataMsg.deviceId,
                        System.currentTimeMillis(), "DeviceData", sonar, "SONAR");

                if (!result) {
                    response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                    return;
                }

            }

            result = DeviceController.pushData(dataMsg.owner, "SenseBot", dataMsg.deviceId,
                    System.currentTimeMillis(), "DeviceData", light, "LIGHT");

            if (!result) {
                response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                return;
            }

        } else {
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            return;
        }

    } catch (UnauthorizedException e) {
        response.setStatus(HttpStatus.SC_UNAUTHORIZED);

    }

}

From source file:org.wso2.carbon.device.mgt.iot.sensebot.api.SensebotControllerService.java

@Path("/pushtempdata")
@POST/*w  w  w  .ja  v a2 s  .com*/
@Consumes(MediaType.APPLICATION_JSON)
public void pushTempData(final DeviceJSON dataMsg, @Context HttpServletResponse response) {

    String temperature = dataMsg.value; //TEMP-PIR-SONAR-LDR
    if (log.isDebugEnabled())
        log.debug("Recieved Tenperature Data Value: " + temperature + " degrees C");
    try {
        boolean result = DeviceController.pushData(dataMsg.owner, "SenseBot", dataMsg.deviceId,
                System.currentTimeMillis(), "DeviceData", temperature, "TEMP");

        if (!result) {
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            return;
        }

    } catch (UnauthorizedException e) {
        response.setStatus(HttpStatus.SC_UNAUTHORIZED);

    }
}

From source file:org.wso2.carbon.device.mgt.iot.sensebot.api.SensebotControllerService.java

@Path("/pushpirdata")
@POST/*from w  w w  .j av  a  2  s.com*/
@Consumes(MediaType.APPLICATION_JSON)
public void pushPIRData(final DeviceJSON dataMsg, @Context HttpServletResponse response) {

    String motion = dataMsg.value; //TEMP-PIR-SONAR-LDR
    if (log.isDebugEnabled())
        log.debug("Recieved PIR (Motion) Sensor Data Value: " + motion);
    try {
        boolean result = DeviceController.pushData(dataMsg.owner, "SenseBot", dataMsg.deviceId,
                System.currentTimeMillis(), "DeviceData", motion, "MOTION");

        if (!result) {
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            return;
        }

    } catch (UnauthorizedException e) {
        response.setStatus(HttpStatus.SC_UNAUTHORIZED);

    }

}

From source file:org.wso2.carbon.device.mgt.iot.sensebot.api.SensebotControllerService.java

@Path("/pushsonardata")
@POST//w  ww.  ja v a  2  s .  c o m
@Consumes(MediaType.APPLICATION_JSON)
public void pushSonarData(final DeviceJSON dataMsg, @Context HttpServletResponse response) {

    String sonar = dataMsg.value; //TEMP-PIR-SONAR-LDR

    if (sonar.equals("-1")) {
        if (log.isDebugEnabled())
            log.debug("Recieved a 'No Obstacle' Sonar value. (Means there are no abstacles within 30cm)");
    } else {
        if (log.isDebugEnabled())
            log.debug("Recieved Sonar Sensor Data Value: " + sonar + " cm");
        try {
            boolean result = DeviceController.pushData(dataMsg.owner, "SenseBot", dataMsg.deviceId,
                    System.currentTimeMillis(), "DeviceData", sonar, "SONAR");

            if (!result) {
                response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                return;
            }

        } catch (UnauthorizedException e) {
            response.setStatus(HttpStatus.SC_UNAUTHORIZED);

        }

    }

}

From source file:org.wso2.carbon.device.mgt.iot.sensebot.api.SensebotControllerService.java

@Path("/pushlightdata")
@POST// www. j  av a2 s  . com
@Consumes(MediaType.APPLICATION_JSON)
public void pushlightData(final DeviceJSON dataMsg, @Context HttpServletResponse response) {

    String light = dataMsg.value; //TEMP-PIR-SONAR-LDR
    if (log.isDebugEnabled())
        log.debug("Recieved LDR (Light) Sensor Data Value: " + light);

    try {
        boolean result = DeviceController.pushData(dataMsg.owner, "SenseBot", dataMsg.deviceId,
                System.currentTimeMillis(), "DeviceData", light, "LIGHT");

        if (!result) {
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            return;
        }

    } catch (UnauthorizedException e) {
        response.setStatus(HttpStatus.SC_UNAUTHORIZED);

    }

}

From source file:org.xwiki.test.escaping.framework.AbstractEscapingTest.java

/**
 * Download a page from the server and return its content. Throws a {@link RuntimeException} on connection problems
 * etc./*  w  ww  .  j a va 2 s  .  co m*/
 * 
 * @param url URL of the page
 * @return content of the page
 */
protected static URLContent getUrlContent(String url) {
    GetMethod get = new GetMethod(url);
    get.setFollowRedirects(true);
    if (isLoggedIn()) {
        get.setDoAuthentication(true);
        get.addRequestHeader("Authorization",
                "Basic " + new String(Base64.encodeBase64("Admin:admin".getBytes())));
    }

    try {
        int statusCode = AbstractEscapingTest.getClient().executeMethod(get);
        switch (statusCode) {
        case HttpStatus.SC_OK:
            // everything is fine
            break;
        case HttpStatus.SC_UNAUTHORIZED:
            // do not fail on 401 (unauthorized), used in some tests
            System.out.println("WARNING, Ignoring status 401 (unauthorized) for URL: " + url);
            break;
        case HttpStatus.SC_CONFLICT:
            // do not fail on 409 (conflict), used in some templates
            System.out.println("WARNING, Ignoring status 409 (conflict) for URL: " + url);
            break;
        case HttpStatus.SC_NOT_FOUND:
            // ignore 404 (the page is still rendered)
            break;
        case HttpStatus.SC_INTERNAL_SERVER_ERROR:
            // ignore 500 (internal server error), which is used by the standard exception.vm error display
            break;
        default:
            throw new RuntimeException("HTTP GET request returned status " + statusCode + " ("
                    + get.getStatusText() + ") for URL: " + url);
        }

        return new URLContent(get.getResponseHeader("Content-Type").getValue(), get.getResponseBody());
    } catch (IOException exception) {
        throw new RuntimeException("Error retrieving URL: " + url, exception);
    } finally {
        get.releaseConnection();
    }
}