mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-02-04 19:34:05 +01:00
[tr064] Use Util.getAndUnmarshalXML method to retrieve phonebook (supports timeout 5s and caching) (#10447)
Fixes #10436 Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
parent
8ab37ce285
commit
223a745946
@ -12,29 +12,15 @@
|
|||||||
*/
|
*/
|
||||||
package org.openhab.binding.tr064.internal.phonebook;
|
package org.openhab.binding.tr064.internal.phonebook;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.TimeoutException;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import javax.xml.bind.JAXBContext;
|
|
||||||
import javax.xml.bind.JAXBException;
|
|
||||||
import javax.xml.bind.Unmarshaller;
|
|
||||||
import javax.xml.stream.XMLInputFactory;
|
|
||||||
import javax.xml.stream.XMLStreamException;
|
|
||||||
import javax.xml.stream.XMLStreamReader;
|
|
||||||
import javax.xml.transform.stream.StreamSource;
|
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jetty.client.HttpClient;
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
import org.eclipse.jetty.client.api.ContentResponse;
|
|
||||||
import org.eclipse.jetty.http.HttpMethod;
|
|
||||||
import org.openhab.binding.tr064.internal.dto.additions.PhonebooksType;
|
import org.openhab.binding.tr064.internal.dto.additions.PhonebooksType;
|
||||||
|
import org.openhab.binding.tr064.internal.util.Util;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -61,21 +47,9 @@ public class Tr064PhonebookImpl implements Phonebook {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void getPhonebook() {
|
private void getPhonebook() {
|
||||||
try {
|
PhonebooksType phonebooksType = Util.getAndUnmarshalXML(httpClient, phonebookUrl, PhonebooksType.class);
|
||||||
ContentResponse contentResponse = httpClient.newRequest(phonebookUrl).method(HttpMethod.GET)
|
if (phonebooksType != null) {
|
||||||
.timeout(2, TimeUnit.SECONDS).send();
|
|
||||||
InputStream xml = new ByteArrayInputStream(contentResponse.getContent());
|
|
||||||
|
|
||||||
JAXBContext context = JAXBContext.newInstance(PhonebooksType.class);
|
|
||||||
XMLInputFactory xif = XMLInputFactory.newFactory();
|
|
||||||
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
|
|
||||||
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
|
|
||||||
XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(xml));
|
|
||||||
Unmarshaller um = context.createUnmarshaller();
|
|
||||||
PhonebooksType phonebooksType = um.unmarshal(xsr, PhonebooksType.class).getValue();
|
|
||||||
|
|
||||||
phonebookName = phonebooksType.getPhonebook().getName();
|
phonebookName = phonebooksType.getPhonebook().getName();
|
||||||
|
|
||||||
phonebook = phonebooksType.getPhonebook().getContact().stream().map(contact -> {
|
phonebook = phonebooksType.getPhonebook().getContact().stream().map(contact -> {
|
||||||
String contactName = contact.getPerson().getRealName();
|
String contactName = contact.getPerson().getRealName();
|
||||||
return contact.getTelephony().getNumber().stream()
|
return contact.getTelephony().getNumber().stream()
|
||||||
@ -83,8 +57,6 @@ public class Tr064PhonebookImpl implements Phonebook {
|
|||||||
this::mergeSameContactNames));
|
this::mergeSameContactNames));
|
||||||
}).collect(HashMap::new, HashMap::putAll, HashMap::putAll);
|
}).collect(HashMap::new, HashMap::putAll, HashMap::putAll);
|
||||||
logger.debug("Downloaded phonebook {}: {}", phonebookName, phonebook);
|
logger.debug("Downloaded phonebook {}: {}", phonebookName, phonebook);
|
||||||
} catch (JAXBException | InterruptedException | ExecutionException | TimeoutException | XMLStreamException e) {
|
|
||||||
logger.warn("Failed to get phonebook with URL {}:", phonebookUrl, e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user