Skip to content

Commit

Permalink
reduce network load
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
  • Loading branch information
J-N-K committed Jan 4, 2021
1 parent 25947bf commit a921aee
Showing 1 changed file with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.openhab.binding.tr064.internal.dto.config.ParameterType;
import org.openhab.binding.tr064.internal.dto.scpd.root.SCPDServiceType;
import org.openhab.binding.tr064.internal.dto.scpd.service.*;
import org.openhab.core.cache.ExpiringCacheMap;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.binding.builder.ChannelBuilder;
Expand All @@ -67,6 +68,12 @@
@NonNullByDefault
public class Util {
private static final Logger LOGGER = LoggerFactory.getLogger(Util.class);
private static final int HTTP_REQUEST_TIMEOUT = 5; // in s
private static final int XML_OBJECT_CACHE_EXPIRY_TIME = 3000; // in ms

// cache XML content for 5s
private static final ExpiringCacheMap<String, Object> XML_OBJECT_CACHE = new ExpiringCacheMap<>(
XML_OBJECT_CACHE_EXPIRY_TIME);

/**
* read the channel config from the resource file (static initialization)
Expand Down Expand Up @@ -317,23 +324,37 @@ public static Optional<String> getSOAPElement(SOAPMessage soapMessage, String el
* @param clazz the class describing the XML file
* @return unmarshalling result
*/
@SuppressWarnings("unchecked")
public static <T> @Nullable T getAndUnmarshalXML(HttpClient httpClient, String uri, Class<T> clazz) {
try {
ContentResponse contentResponse = httpClient.newRequest(uri).timeout(2, TimeUnit.SECONDS)
.method(HttpMethod.GET).send();
byte[] response = contentResponse.getContent();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("XML = {}", new String(response));
}
InputStream xml = new ByteArrayInputStream(response);
T returnValue = (T) XML_OBJECT_CACHE.putIfAbsentAndGet(uri, () -> {
try {
LOGGER.trace("Refreshing cache for '{}'", uri);
ContentResponse contentResponse = httpClient.newRequest(uri)
.timeout(HTTP_REQUEST_TIMEOUT, TimeUnit.SECONDS).method(HttpMethod.GET).send();
byte[] response = contentResponse.getContent();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("XML = {}", new String(response));
}
InputStream xml = new ByteArrayInputStream(response);

JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller um = context.createUnmarshaller();
return um.unmarshal(new StreamSource(xml), clazz).getValue();
} catch (ExecutionException | InterruptedException | TimeoutException e) {
LOGGER.debug("HTTP Failed to GET uri '{}': {}", uri, e.getMessage());
} catch (JAXBException e) {
LOGGER.debug("Unmarshalling failed: {}", e.getMessage());
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller um = context.createUnmarshaller();
T newValue = um.unmarshal(new StreamSource(xml), clazz).getValue();
LOGGER.trace("Storing in cache {}", newValue);
return newValue;
} catch (ExecutionException | InterruptedException | TimeoutException e) {
LOGGER.debug("HTTP Failed to GET uri '{}': {}", uri, e.getMessage());
throw new IllegalArgumentException();
} catch (JAXBException e) {
LOGGER.debug("Unmarshalling failed: {}", e.getMessage());
throw new IllegalArgumentException();
}
});
LOGGER.trace("Returning from cache: {}", returnValue);
return returnValue;
} catch (IllegalArgumentException e) {
// already logged
}
return null;
}
Expand Down

0 comments on commit a921aee

Please sign in to comment.