Search in sources :

Example 21 with JAXBException

use of javax.xml.bind.JAXBException in project openhab1-addons by openhab.

the class OpenWebIfCommunicator method executeRequest.

/**
     * Executes the http request and parses the returned stream.
     */
@SuppressWarnings("unchecked")
private <T> T executeRequest(OpenWebIfConfig config, String url, Class<T> clazz) throws IOException {
    HttpURLConnection con = null;
    try {
        logger.trace("Request [{}]: {}", config.getName(), url);
        con = (HttpURLConnection) new URL(url).openConnection();
        con.setConnectTimeout(CONNECTION_TIMEOUT);
        con.setReadTimeout(10000);
        if (config.hasLogin()) {
            String userpass = config.getUser() + ":" + config.getPassword();
            String basicAuth = "Basic " + DatatypeConverter.printBase64Binary(userpass.getBytes());
            con.setRequestProperty("Authorization", basicAuth);
        }
        if (con instanceof HttpsURLConnection) {
            HttpsURLConnection sCon = (HttpsURLConnection) con;
            TrustManager[] trustManager = new TrustManager[] { new SimpleTrustManager() };
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(new KeyManager[0], trustManager, new SecureRandom());
            sCon.setSSLSocketFactory(context.getSocketFactory());
            sCon.setHostnameVerifier(new AllowAllHostnameVerifier());
        }
        StringWriter sw = new StringWriter();
        IOUtils.copy(con.getInputStream(), sw);
        con.disconnect();
        if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
            String response = sw.toString();
            logger.trace("Response: [{}]: {}", config.getName(), response);
            Unmarshaller um = JAXBContext.newInstance(clazz).createUnmarshaller();
            return (T) um.unmarshal(new StringReader(response));
        } else {
            throw new IOException(con.getResponseMessage());
        }
    } catch (JAXBException ex) {
        throw new IOException(ex.getMessage(), ex);
    } catch (GeneralSecurityException ex) {
        throw new IOException(ex.getMessage(), ex);
    } finally {
        if (con != null) {
            con.disconnect();
        }
    }
}
Also used : AllowAllHostnameVerifier(org.openhab.action.openwebif.internal.impl.ssl.AllowAllHostnameVerifier) JAXBException(javax.xml.bind.JAXBException) GeneralSecurityException(java.security.GeneralSecurityException) SimpleTrustManager(org.openhab.action.openwebif.internal.impl.ssl.SimpleTrustManager) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) URL(java.net.URL) TrustManager(javax.net.ssl.TrustManager) SimpleTrustManager(org.openhab.action.openwebif.internal.impl.ssl.SimpleTrustManager) HttpURLConnection(java.net.HttpURLConnection) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 22 with JAXBException

use of javax.xml.bind.JAXBException in project openhab1-addons by openhab.

the class ConfigParser method marshal.

/**
     * This method saves List of Request objects into xml file
     *
     * @param requests
     *            object to be saved
     * @param xmlFileLocation
     *            file object to save the object into
     */
@SuppressWarnings("resource")
public void marshal(List<Request> requests, File xmlFileLocation) throws StiebelHeatPumpException {
    JAXBContext context;
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(xmlFileLocation), "UTF-8"));
    } catch (IOException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
    try {
        context = JAXBContext.newInstance(Requests.class);
    } catch (JAXBException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
    Marshaller m;
    try {
        m = context.createMarshaller();
    } catch (JAXBException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
    try {
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    } catch (PropertyException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
    try {
        m.marshal(new Requests(requests), writer);
    } catch (JAXBException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
    try {
        writer.close();
    } catch (IOException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) PropertyException(javax.xml.bind.PropertyException) FileOutputStream(java.io.FileOutputStream) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) Requests(org.openhab.binding.stiebelheatpump.protocol.Requests) BufferedWriter(java.io.BufferedWriter)

Example 23 with JAXBException

use of javax.xml.bind.JAXBException in project openhab1-addons by openhab.

the class FritzahaWebserviceUpdateXmlCallback method execute.

/**
     * {@inheritDoc}
     */
@Override
public void execute(int status, String response) {
    super.execute(status, response);
    if (validRequest) {
        logger.trace("Received State response " + response + " for item " + itemName);
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(DevicelistModel.class);
            Unmarshaller jaxbUM = jaxbContext.createUnmarshaller();
            DevicelistModel model = (DevicelistModel) jaxbUM.unmarshal(new StringReader(response));
            ArrayList<DeviceModel> list = model.getDevicelist();
            for (DeviceModel device : list) {
                if (device.getIdentifier().equals(this.deviceAin)) {
                    BigDecimal meterValueScaled = new BigDecimal(0);
                    switch(type) {
                        case POWER:
                            meterValueScaled = device.getPowermeter().getPower().scaleByPowerOfTen(-3);
                            break;
                        case ENERGY:
                            meterValueScaled = device.getPowermeter().getEnergy();
                            break;
                        case TEMPERATURE:
                            meterValueScaled = device.getTemperature().getCelsius().scaleByPowerOfTen(-1);
                            break;
                        default:
                            logger.warn("unknown meter type: " + type);
                            break;
                    }
                    logger.debug(device.toString());
                    webIface.postUpdate(itemName, new DecimalType(meterValueScaled));
                } else {
                    logger.trace("device " + device.getIdentifier() + " was not requested");
                }
            }
        } catch (JAXBException e) {
            logger.error(e.getLocalizedMessage(), e);
        }
    }
}
Also used : DeviceModel(org.openhab.binding.fritzaha.internal.model.DeviceModel) JAXBException(javax.xml.bind.JAXBException) StringReader(java.io.StringReader) DecimalType(org.openhab.core.library.types.DecimalType) JAXBContext(javax.xml.bind.JAXBContext) DevicelistModel(org.openhab.binding.fritzaha.internal.model.DevicelistModel) Unmarshaller(javax.xml.bind.Unmarshaller) BigDecimal(java.math.BigDecimal)

Example 24 with JAXBException

use of javax.xml.bind.JAXBException in project openhab1-addons by openhab.

the class SmarthomaticBinding method activate.

/**
     * activate binding
     *
     */
@Override
public void activate() {
    // log activate of binding
    if (baseStation != null) {
        logger.info("Smarthomatic Binding activated. BaseStation= {}", baseStation.toString());
    }
    Bundle bundle = SmarthomaticActivator.getContext().getBundle();
    URL fileURL = bundle.getEntry("packet_layout.xml");
    Packet packet = null;
    try {
        InputStream inputStream = fileURL.openConnection().getInputStream();
        JAXBContext jaxbContext = JAXBContext.newInstance(Packet.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        packet = (Packet) jaxbUnmarshaller.unmarshal(inputStream);
    } catch (IOException e1) {
        e1.printStackTrace();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    this.packet = packet;
}
Also used : Packet(org.openhab.binding.smarthomatic.internal.packetData.Packet) Bundle(org.osgi.framework.Bundle) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException) Unmarshaller(javax.xml.bind.Unmarshaller) URL(java.net.URL)

Example 25 with JAXBException

use of javax.xml.bind.JAXBException in project openhab1-addons by openhab.

the class LgTvChannelSet method savetofile.

/**
     * Save Channel List to File f
     * 
     * @param f
     */
public void savetofile(String f) {
    Writer writer = null;
    JAXBContext jc;
    try {
        jc = JAXBContext.newInstance(envelope.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "utf-8"));
        marshaller.marshal(envel, writer);
    } catch (PropertyException e) {
        logger.error("error in savetofile", e);
    } catch (JAXBException e) {
        logger.error("error in savetofile", e);
    } catch (IOException ex) {
        logger.error("error in savetofile", ex);
    } finally {
        try {
            writer.close();
        } catch (Exception ex) {
        }
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) PropertyException(javax.xml.bind.PropertyException) FileOutputStream(java.io.FileOutputStream) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) PropertyException(javax.xml.bind.PropertyException) JAXBException(javax.xml.bind.JAXBException) BufferedWriter(java.io.BufferedWriter)

Aggregations

JAXBException (javax.xml.bind.JAXBException)402 JAXBContext (javax.xml.bind.JAXBContext)126 IOException (java.io.IOException)93 Unmarshaller (javax.xml.bind.Unmarshaller)91 Marshaller (javax.xml.bind.Marshaller)69 ArrayList (java.util.ArrayList)38 StringWriter (java.io.StringWriter)35 List (java.util.List)35 Map (java.util.Map)33 SAXException (org.xml.sax.SAXException)32 File (java.io.File)29 InputStream (java.io.InputStream)29 AMConsoleException (com.sun.identity.console.base.model.AMConsoleException)28 HashSet (java.util.HashSet)28 JAXBElement (javax.xml.bind.JAXBElement)24 XMLStreamException (javax.xml.stream.XMLStreamException)23 SAML2MetaException (com.sun.identity.saml2.meta.SAML2MetaException)22 StringReader (java.io.StringReader)21 HashMap (java.util.HashMap)21 SAML2MetaManager (com.sun.identity.saml2.meta.SAML2MetaManager)20