Skip to content

Commit

Permalink
Use Util method for phonebook retrieval
Browse files Browse the repository at this point in the history
Also-By: Christoph Weitkamp <github@christophweitkamp.de>
Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
  • Loading branch information
J-N-K committed Apr 4, 2021
1 parent 962cd7e commit 468af09
Showing 1 changed file with 13 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,17 @@
*/
package org.smarthomej.binding.tr064.internal.phonebook;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
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 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.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.http.HttpMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smarthomej.binding.tr064.internal.dto.additions.PhonebooksType;
import org.smarthomej.binding.tr064.internal.util.Util;

/**
* The {@link Tr064PhonebookImpl} class implements a phonebook
Expand All @@ -62,31 +48,19 @@ public Tr064PhonebookImpl(HttpClient httpClient, String phonebookUrl) {
}

private void getPhonebook() {
try {
ContentResponse contentResponse = httpClient.newRequest(phonebookUrl).method(HttpMethod.GET)
.timeout(5, 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();

phonebook = phonebooksType.getPhonebook().getContact().stream().map(contact -> {
String contactName = contact.getPerson().getRealName();
return contact.getTelephony().getNumber().stream()
.collect(Collectors.toMap(number -> normalizeNumber(number.getValue()), number -> contactName,
this::mergeSameContactNames));
}).collect(HashMap::new, HashMap::putAll, HashMap::putAll);
logger.debug("Downloaded phonebook {}: {}", phonebookName, phonebook);
} catch (JAXBException | InterruptedException | ExecutionException | TimeoutException | XMLStreamException e) {
logger.warn("Failed to get phonebook with URL {}:", phonebookUrl, e);
PhonebooksType phonebooksType = Util.getAndUnmarshalXML(httpClient, phonebookUrl, PhonebooksType.class, 5);
if (phonebooksType == null) {
logger.warn("Failed to get phonebook with URL '{}'", phonebookUrl);
return;
}
phonebookName = phonebooksType.getPhonebook().getName();

phonebook = phonebooksType.getPhonebook().getContact().stream().map(contact -> {
String contactName = contact.getPerson().getRealName();
return contact.getTelephony().getNumber().stream().collect(Collectors.toMap(
number -> normalizeNumber(number.getValue()), number -> contactName, this::mergeSameContactNames));
}).collect(HashMap::new, HashMap::putAll, HashMap::putAll);
logger.debug("Downloaded phonebook {}: {}", phonebookName, phonebook);
}

// in case there are multiple phone entries with same number -> name mapping, i.e. in phonebooks exported from
Expand Down

0 comments on commit 468af09

Please sign in to comment.