Search in sources :

Example 46 with JsonMappingException

use of org.codehaus.jackson.map.JsonMappingException in project OpenAttestation by OpenAttestation.

the class DemoPortalDataController method saveNewHostInfo.

public ModelAndView saveNewHostInfo(HttpServletRequest req, HttpServletResponse res) {
    log.info("WLMDataController.saveNewHostInfo >>");
    ModelAndView responseView = new ModelAndView(new JSONView());
    String hostObject = null;
    boolean newhost = false;
    try {
        hostObject = req.getParameter("hostObject");
        newhost = Boolean.parseBoolean(req.getParameter("newhost"));
    } catch (Exception e1) {
        responseView.addObject("result", false);
        responseView.addObject("message", e1.getMessage());
    }
    System.out.println(hostObject);
    ObjectMapper mapper = new ObjectMapper();
    HostDetailsEntityVO dataVO = new HostDetailsEntityVO();
    try {
        dataVO = mapper.readValue(hostObject, HostDetailsEntityVO.class);
    } catch (JsonParseException e) {
        log.error("Error While Parsing request parameters Data. " + e.getMessage());
        responseView.addObject("result", false);
        responseView.addObject("message", "Error While Parsing request parameters Data.");
        return responseView;
    } catch (JsonMappingException e) {
        log.error("Error While Mapping request parameters to Mle Data Object. " + e.getMessage());
        responseView.addObject("result", false);
        responseView.addObject("message", "Error While Mapping request parameters to Mle Data Object.");
        return responseView;
    } catch (IOException e) {
        log.error("IO Exception " + e.getMessage());
        responseView.addObject("result", false);
        responseView.addObject("message", "Error While Mapping request parameters to Mle Data Object.");
        return responseView;
    }
    dataVO.setUpdatedOn(new Date(System.currentTimeMillis()));
    try {
        if (newhost) {
            System.err.println("dataForNew : " + dataVO);
            responseView.addObject("result", demoPortalServices.saveNewHostData(dataVO, getAttestationService(req, AttestationService.class)));
        } else {
            System.err.println("dataForOLD : " + dataVO);
            responseView.addObject("result", demoPortalServices.updateHostData(dataVO, getAttestationService(req, AttestationService.class)));
        }
    } catch (DemoPortalException e) {
        log.error(e.getMessage());
        responseView.addObject("result", false);
        responseView.addObject("message", e.getMessage());
        return responseView;
    }
    log.info("WLMDataController.saveNewHostInfo <<<");
    return responseView;
}
Also used : JSONView(com.intel.mountwilson.util.JSONView) HostDetailsEntityVO(com.intel.mountwilson.datamodel.HostDetailsEntityVO) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) ModelAndView(org.springframework.web.servlet.ModelAndView) IOException(java.io.IOException) DemoPortalException(com.intel.mountwilson.common.DemoPortalException) JsonParseException(org.codehaus.jackson.JsonParseException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) DemoPortalException(com.intel.mountwilson.common.DemoPortalException) JsonParseException(org.codehaus.jackson.JsonParseException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Date(java.util.Date)

Example 47 with JsonMappingException

use of org.codehaus.jackson.map.JsonMappingException in project openhab1-addons by openhab.

the class FritzahaQueryscriptUpdateNumberCallback method execute.

/**
     * {@inheritDoc}
     */
@Override
public void execute(int status, String response) {
    super.execute(status, response);
    if (validRequest) {
        logger.debug("Received State response " + response + " for item " + itemName);
        String valueType;
        if (type == MeterType.VOLTAGE) {
            valueType = "MM_Value_Volt";
        } else if (type == MeterType.CURRENT) {
            valueType = "MM_Value_Amp";
        } else if (type == MeterType.POWER) {
            valueType = "MM_Value_Power";
        } else if (type == MeterType.ENERGY) {
            valueType = "";
        } else {
            return;
        }
        ObjectMapper jsonReader = new ObjectMapper();
        Map<String, String> deviceData;
        try {
            deviceData = jsonReader.readValue(response, Map.class);
        } catch (JsonParseException e) {
            logger.error("Error parsing JSON:\n" + response);
            return;
        } catch (JsonMappingException e) {
            logger.error("Error mapping JSON:\n" + response);
            return;
        } catch (IOException e) {
            logger.error("An I/O error occured while decoding JSON:\n" + response);
            return;
        }
        if (type == MeterType.ENERGY) {
            String ValIdent = "EnStats_watt_value_";
            long valCount = Long.parseLong(deviceData.get("EnStats_count"));
            BigDecimal meterValue = new BigDecimal(0);
            BigDecimal meterValueScaled;
            long tmplong;
            BigDecimal tmpBD;
            for (int tmpcnt = 1; tmpcnt <= valCount; tmpcnt++) {
                tmplong = Long.parseLong(deviceData.get(ValIdent + tmpcnt));
                meterValue = meterValue.add(new BigDecimal(tmplong));
            }
            if (Long.parseLong(deviceData.get("EnStats_timer_type")) == 10) {
                // 10 Minute values are given in mWh, so scale to Wh
                meterValueScaled = meterValue.scaleByPowerOfTen(-6);
            } else {
                // Other values are given in Wh, so scale to kWh
                meterValueScaled = meterValue.scaleByPowerOfTen(-3);
            }
            webIface.postUpdate(itemName, new DecimalType(meterValueScaled));
        } else if (deviceData.containsKey(valueType)) {
            BigDecimal meterValue = new BigDecimal(deviceData.get(valueType));
            BigDecimal meterValueScaled;
            switch(type) {
                case VOLTAGE:
                    meterValueScaled = meterValue.scaleByPowerOfTen(-3);
                    break;
                case CURRENT:
                    meterValueScaled = meterValue.scaleByPowerOfTen(-4);
                    break;
                case POWER:
                    meterValueScaled = meterValue.scaleByPowerOfTen(-2);
                    break;
                default:
                    meterValueScaled = meterValue;
            }
            webIface.postUpdate(itemName, new DecimalType(meterValueScaled));
        } else {
            logger.error("Response did not contain " + valueType);
        }
    }
}
Also used : JsonMappingException(org.codehaus.jackson.map.JsonMappingException) DecimalType(org.openhab.core.library.types.DecimalType) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException) Map(java.util.Map) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) BigDecimal(java.math.BigDecimal)

Example 48 with JsonMappingException

use of org.codehaus.jackson.map.JsonMappingException in project openhab1-addons by openhab.

the class FritzahaQueryscriptUpdateSwitchCallback method execute.

/**
     * {@inheritDoc}
     */
@Override
public void execute(int status, String response) {
    super.execute(status, response);
    if (validRequest) {
        logger.debug("Received State response " + response + " for item " + itemName);
        ObjectMapper jsonReader = new ObjectMapper();
        Map<String, String> deviceData;
        try {
            deviceData = jsonReader.readValue(response, Map.class);
        } catch (JsonParseException e) {
            logger.error("Error parsing JSON:\n" + response);
            return;
        } catch (JsonMappingException e) {
            logger.error("Error mapping JSON:\n" + response);
            return;
        } catch (IOException e) {
            logger.error("An I/O error occured while decoding JSON:\n" + response);
            return;
        }
        if (deviceData.containsKey("DeviceSwitchState")) {
            webIface.postUpdate(itemName, "1".equals(deviceData.get("DeviceSwitchState")) ? OnOffType.ON : OnOffType.OFF);
        }
    }
}
Also used : JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException) Map(java.util.Map) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 49 with JsonMappingException

use of org.codehaus.jackson.map.JsonMappingException in project neo4j by neo4j.

the class StatementDeserializer method fetchNextOrNull.

@Override
protected Statement fetchNextOrNull() {
    try {
        if (errors != null) {
            return null;
        }
        switch(state) {
            case BEFORE_OUTER_ARRAY:
                if (!beginsWithCorrectTokens()) {
                    return null;
                }
                state = State.IN_BODY;
            case IN_BODY:
                String statement = null;
                Map<String, Object> parameters = null;
                List<Object> resultsDataContents = null;
                boolean includeStats = false;
                JsonToken tok;
                while ((tok = input.nextToken()) != null && tok != END_OBJECT) {
                    if (tok == END_ARRAY) {
                        // No more statements
                        state = State.FINISHED;
                        return null;
                    }
                    input.nextValue();
                    String currentName = input.getCurrentName();
                    switch(currentName) {
                        case "statement":
                            statement = input.readValueAs(String.class);
                            break;
                        case "parameters":
                            parameters = readMap(input);
                            break;
                        case "resultDataContents":
                            resultsDataContents = readArray(input);
                            break;
                        case "includeStats":
                            includeStats = input.getBooleanValue();
                            break;
                        default:
                            discardValue(input);
                    }
                }
                if (statement == null) {
                    addError(new Neo4jError(Status.Request.InvalidFormat, new DeserializationException("No statement provided.")));
                    return null;
                }
                return new Statement(statement, parameters == null ? NO_PARAMETERS : parameters, includeStats, ResultDataContent.fromNames(resultsDataContents));
            case FINISHED:
                return null;
            default:
                break;
        }
        return null;
    } catch (JsonParseException | JsonMappingException e) {
        addError(new Neo4jError(Status.Request.InvalidFormat, new DeserializationException("Unable to deserialize request", e)));
        return null;
    } catch (IOException e) {
        addError(new Neo4jError(Status.Network.CommunicationError, e));
        return null;
    } catch (Exception e) {
        addError(new Neo4jError(Status.General.UnknownError, e));
        return null;
    }
}
Also used : IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException) Neo4jError(org.neo4j.server.rest.transactional.error.Neo4jError) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) JsonToken(org.codehaus.jackson.JsonToken)

Example 50 with JsonMappingException

use of org.codehaus.jackson.map.JsonMappingException in project ranger by apache.

the class JSONUtil method writeObjectAsString.

public String writeObjectAsString(Serializable obj) {
    ObjectMapper mapper = new ObjectMapper();
    String jsonStr;
    try {
        jsonStr = mapper.writeValueAsString(obj);
        return jsonStr;
    } catch (JsonParseException e) {
        throw restErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA);
    } catch (JsonMappingException e) {
        throw restErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA);
    } catch (IOException e) {
        throw restErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA);
    }
}
Also used : JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

JsonMappingException (org.codehaus.jackson.map.JsonMappingException)88 IOException (java.io.IOException)79 JsonParseException (org.codehaus.jackson.JsonParseException)53 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)36 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)27 ArrayList (java.util.ArrayList)24 HashMap (java.util.HashMap)19 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)15 Response (javax.ws.rs.core.Response)13 Map (java.util.Map)11 List (java.util.List)10 GET (javax.ws.rs.GET)10 Path (javax.ws.rs.Path)10 Produces (javax.ws.rs.Produces)10 WebApplicationException (javax.ws.rs.WebApplicationException)10 TypeReference (org.codehaus.jackson.type.TypeReference)10 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)7 ClientResponse (com.sun.jersey.api.client.ClientResponse)6 StringWriter (java.io.StringWriter)5 Enterprise (com.itrus.portal.db.Enterprise)4