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

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

Introduction

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

Prototype

int SC_PRECONDITION_FAILED

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

Click Source Link

Document

<tt>412 Precondition Failed</tt> (HTTP/1.1 - RFC 2616)

Usage

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

@Path("/push_temperature")
@POST/*from w w  w.  j  a  va2  s.co m*/
@Consumes(MediaType.APPLICATION_JSON)
public void pushTemperatureData(final DeviceJSON dataMsg, @Context HttpServletResponse response) {
    boolean result;
    String deviceId = dataMsg.deviceId;
    String deviceIp = dataMsg.reply;
    String temperature = dataMsg.value;

    String registeredIp = deviceToIpMap.get(deviceId);

    if (registeredIp == null) {
        log.warn("Unregistered IP: Temperature Data Received from an un-registered IP " + deviceIp
                + " for device ID - " + deviceId);
        response.setStatus(HttpStatus.SC_PRECONDITION_FAILED);
        return;
    } else if (!registeredIp.equals(deviceIp)) {
        log.warn("Conflicting IP: Received IP is " + deviceIp + ". Device with ID " + deviceId
                + " is already registered under some other IP. Re-registration " + "required");
        response.setStatus(HttpStatus.SC_CONFLICT);
        return;
    }

    try {
        DeviceController deviceController = new DeviceController();
        result = deviceController.pushBamData(dataMsg.owner, FireAlarmConstants.DEVICE_TYPE, dataMsg.deviceId,
                System.currentTimeMillis(), "DeviceData", temperature,
                DataStreamDefinitions.StreamTypeLabel.TEMPERATURE);

        if (!result) {
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        }
    } catch (UnauthorizedException e) {
        response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        log.error("Data Push Attempt Failed for BAM Publisher: " + e.getMessage());
    }

    try {
        DeviceController deviceController = new DeviceController();
        result = deviceController.pushCepData(dataMsg.owner, FireAlarmConstants.DEVICE_TYPE, dataMsg.deviceId,
                System.currentTimeMillis(), "DeviceData", temperature,
                DataStreamDefinitions.StreamTypeLabel.TEMPERATURE);

        if (!result) {
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        }
    } catch (UnauthorizedException e) {
        response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        log.error("Data Push Attempt Failed for CEP Publisher: " + e.getMessage());
    }
}