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();
}
}
}
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());
}
}
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);
}
}
}
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;
}
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) {
}
}
}
Aggregations