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

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

Introduction

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

Prototype

int SC_NOT_IMPLEMENTED

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

Click Source Link

Document

<tt>501 Not Implemented</tt> (HTTP/1.0 - RFC 1945)

Usage

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

@Path("/fan/{state}")
@POST//from ww w .ja  v a2 s  .  co  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  ww .  j a  v  a  2 s. c om*/
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 .  j  av a2 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:semantic.diversification.semantic.OpenCalaisRestCaller.java

private InputStream doRequest(PostMethod method) throws IOException {
    try {/* w  w w  .j av  a2  s  . c  o m*/
        int returnCode = client.executeMethod(method);

        switch (returnCode) {
        case HttpStatus.SC_OK:
            InputStream responseStream = method.getResponseBodyAsStream();
            return new ByteArrayInputStream(IOUtils.toByteArray(responseStream));

        case HttpStatus.SC_NOT_IMPLEMENTED:
            throw new RuntimeException(
                    "The Post method is not implemented by this URI: " + method.getResponseBodyAsString());

        default:
            throw new RuntimeException(
                    "Got code: <" + returnCode + "> and response: <" + method.getResponseBodyAsString() + ">");
        }
    } finally {
        method.releaseConnection();
    }
}

From source file:servlets.ResponseServlet.java

public String getResponseInfo(String access_token) throws ServletException, IOException {
    HttpClient httpclient = new HttpClient();
    BufferedReader bufferedreader = null;
    String result = null;//from   ww w .j av  a 2s  .c  o m

    GetMethod getmethod = new GetMethod(String.format(INSTAGRAM_GET_SELF_INFO, access_token));

    int rCode = 0;
    try {
        rCode = httpclient.executeMethod(getmethod);

        StringBuffer sb = new StringBuffer();

        if (rCode == HttpStatus.SC_NOT_IMPLEMENTED) {
            throw new ServletException("The Post postmethod is not implemented by this URI");
        } else {
            bufferedreader = new BufferedReader(new InputStreamReader(getmethod.getResponseBodyAsStream()));
            String readLine;
            while (((readLine = bufferedreader.readLine()) != null))
                sb.append(readLine);
            System.out.println(sb.toString());

            System.out.println(sb.toString());

            result = sb.toString();
        }

    } catch (IOException e) {
        throw e;
    } finally {
        getmethod.releaseConnection();
        bufferedreader.close();
    }

    return result;
}