Search in sources :

Example 1 with JSONSerializer

use of flexjson.JSONSerializer in project openhab1-addons by openhab.

the class SenseService method store.

/**
     * @{inheritDoc}
     */
@Override
public void store(Item item, String alias) {
    if (initialized) {
        JSONSerializer serializer = new JSONSerializer().transform(new SenseEventTransformer(), SenseEventBean.class);
        String serializedBean = serializer.serialize(new SenseEventBean(alias, item.getState().toString()));
        String serviceUrl = url + apiKey;
        String response = HttpUtil.executeUrl("POST", serviceUrl, IOUtils.toInputStream(serializedBean), "application/json", 5000);
        logger.debug("Stored item '{}' as '{}' in Sen.se and received response: {} ", new String[] { item.getName(), alias, response });
    }
}
Also used : JSONSerializer(flexjson.JSONSerializer)

Example 2 with JSONSerializer

use of flexjson.JSONSerializer in project openhab1-addons by openhab.

the class CosmService method store.

/**
     * @{inheritDoc}
     */
public void store(Item item, String alias) {
    if (initialized) {
        try {
            String serviceUrl = url;
            if (alias == null) {
                alias = item.getName();
            }
            serviceUrl += "/" + alias;
            URL url = new URL(serviceUrl);
            HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
            httpCon.setDoOutput(true);
            httpCon.setRequestMethod("PUT");
            httpCon.setRequestProperty("Content-type", "application/json");
            httpCon.setRequestProperty("X-ApiKey", apiKey);
            OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());
            JSONSerializer serializer = new JSONSerializer().transform(new CosmEventTransformer(), CosmEventBean.class);
            String serializedBean = serializer.serialize(new CosmEventBean(alias, item.getState().toString()));
            out.write(serializedBean);
            out.flush();
            logger.debug("Stored item '{}' as '{}' in Cosm and received response: {} ", new String[] { item.getName(), alias, httpCon.getResponseMessage() });
            out.close();
        } catch (Exception e) {
            logger.warn("Connection error");
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) OutputStreamWriter(java.io.OutputStreamWriter) URL(java.net.URL) JSONSerializer(flexjson.JSONSerializer)

Example 3 with JSONSerializer

use of flexjson.JSONSerializer in project esup-papercut by EsupPortail.

the class PayBoxResourceController method getStats.

@RequestMapping(value = "/statsPapercut")
public void getStats(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletRequestBindingException {
    HttpSession session = request.getSession();
    String sharedSessionId = request.getParameter("sharedSessionId");
    if (sharedSessionId != null) {
        EsupPapercutSessionObject objectShared = (EsupPapercutSessionObject) session.getAttribute(sharedSessionId);
        String requeteNbTransactions = objectShared.getRequeteNbTransactions();
        String requeteMontantTransactions = objectShared.getRequeteMontantTransactions();
        String requeteCumulTransactions = objectShared.getRequeteCumulTransactions();
        String requeteCumulMontants = objectShared.getRequeteCumulMontants();
        if (objectShared.isIsAdmin()) {
            String flexJsonString = "Aucune statistique à récupérer";
            try {
                JSONSerializer serializer = new JSONSerializer();
                flexJsonString = serializer.deepSerialize(statsService.getStatsPapercut(requeteNbTransactions, requeteMontantTransactions, requeteCumulTransactions, requeteCumulMontants));
            } catch (Exception e) {
                log.warn("Impossible de récupérer les statistiques", e);
            }
            InputStream jsonStream = IOUtils.toInputStream(flexJsonString, "utf-8");
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            FileCopyUtils.copy(jsonStream, response.getOutputStream());
        }
    }
}
Also used : EsupPapercutSessionObject(org.esupportail.papercut.domain.EsupPapercutSessionObject) HttpSession(javax.servlet.http.HttpSession) InputStream(java.io.InputStream) IOException(java.io.IOException) ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) JSONSerializer(flexjson.JSONSerializer) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

JSONSerializer (flexjson.JSONSerializer)3 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 HttpSession (javax.servlet.http.HttpSession)1 EsupPapercutSessionObject (org.esupportail.papercut.domain.EsupPapercutSessionObject)1 ServletRequestBindingException (org.springframework.web.bind.ServletRequestBindingException)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1