Skip to content

Commit

Permalink
remove dependency on o.a.c.l (openhab#9835)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <[email protected]>
Signed-off-by: John Marshall <[email protected]>
  • Loading branch information
J-N-K authored and themillhousegroup committed May 10, 2021
1 parent ecffe08 commit 325ae95
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 54 deletions.
9 changes: 9 additions & 0 deletions bundles/org.openhab.binding.amazonechocontrol/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@

<name>openHAB Add-ons :: Bundles :: Amazon Echo Control Binding</name>

<dependencies>
<dependency>
<groupId>org.unbescape</groupId>
<artifactId>unbescape</artifactId>
<version>1.1.6.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringEscapeUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.amazonechocontrol.internal.handler.AccountHandler;
Expand All @@ -46,6 +45,7 @@
import org.osgi.service.http.NamespaceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unbescape.html.HtmlEscape;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
Expand Down Expand Up @@ -365,27 +365,27 @@ private void handleChangeDomain(HttpServletResponse resp, Connection connection)
private void handleDefaultPageResult(HttpServletResponse resp, String message, Connection connection)
throws IOException {
StringBuilder html = createPageStart("");
html.append(StringEscapeUtils.escapeHtml(message));
html.append(HtmlEscape.escapeHtml4(message));
// logout link
html.append(" <a href='" + servletUrl + "/logout' >");
html.append(StringEscapeUtils.escapeHtml("Logout"));
html.append(HtmlEscape.escapeHtml4("Logout"));
html.append("</a>");
// newdevice link
html.append(" | <a href='" + servletUrl + "/newdevice' >");
html.append(StringEscapeUtils.escapeHtml("Logout and create new device id"));
html.append(HtmlEscape.escapeHtml4("Logout and create new device id"));
html.append("</a>");
// customer id
html.append("<br>Customer Id: ");
html.append(StringEscapeUtils.escapeHtml(connection.getCustomerId()));
html.append(HtmlEscape.escapeHtml4(connection.getCustomerId()));
// customer name
html.append("<br>Customer Name: ");
html.append(StringEscapeUtils.escapeHtml(connection.getCustomerName()));
html.append(HtmlEscape.escapeHtml4(connection.getCustomerName()));
// device name
html.append("<br>App name: ");
html.append(StringEscapeUtils.escapeHtml(connection.getDeviceName()));
html.append(HtmlEscape.escapeHtml4(connection.getDeviceName()));
// connection
html.append("<br>Connected to: ");
html.append(StringEscapeUtils.escapeHtml(connection.getAlexaServer()));
html.append(HtmlEscape.escapeHtml4(connection.getAlexaServer()));
// domain
html.append(" <a href='");
html.append(servletUrl);
Expand All @@ -394,7 +394,7 @@ private void handleDefaultPageResult(HttpServletResponse resp, String message, C
// Main UI link
html.append("<br><a href='/#!/settings/things/" + BINDING_ID + ":"
+ URLEncoder.encode(THING_TYPE_ACCOUNT.getId(), "UTF8") + ":" + URLEncoder.encode(id, "UTF8") + "'>");
html.append(StringEscapeUtils.escapeHtml("Check Thing in Main UI"));
html.append(HtmlEscape.escapeHtml4("Check Thing in Main UI"));
html.append("</a><br><br>");

// device list
Expand All @@ -403,28 +403,28 @@ private void handleDefaultPageResult(HttpServletResponse resp, String message, C
for (Device device : this.account.getLastKnownDevices()) {

html.append("<tr><td>");
html.append(StringEscapeUtils.escapeHtml(nullReplacement(device.accountName)));
html.append(HtmlEscape.escapeHtml4(nullReplacement(device.accountName)));
html.append("</td><td>");
html.append(StringEscapeUtils.escapeHtml(nullReplacement(device.serialNumber)));
html.append(HtmlEscape.escapeHtml4(nullReplacement(device.serialNumber)));
html.append("</td><td>");
html.append(StringEscapeUtils.escapeHtml(device.online ? "Online" : "Offline"));
html.append(HtmlEscape.escapeHtml4(device.online ? "Online" : "Offline"));
html.append("</td><td>");
Thing accountHandler = account.findThingBySerialNumber(device.serialNumber);
if (accountHandler != null) {
html.append("<a href='" + servletUrl + "/ids/?serialNumber="
+ URLEncoder.encode(device.serialNumber, "UTF8") + "'>"
+ StringEscapeUtils.escapeHtml(accountHandler.getLabel()) + "</a>");
+ HtmlEscape.escapeHtml4(accountHandler.getLabel()) + "</a>");
} else {
html.append("<a href='" + servletUrl + "/ids/?serialNumber="
+ URLEncoder.encode(device.serialNumber, "UTF8") + "'>"
+ StringEscapeUtils.escapeHtml("Not defined") + "</a>");
+ URLEncoder.encode(device.serialNumber, "UTF8") + "'>" + HtmlEscape.escapeHtml4("Not defined")
+ "</a>");
}
html.append("</td><td>");
html.append(StringEscapeUtils.escapeHtml(nullReplacement(device.deviceFamily)));
html.append(HtmlEscape.escapeHtml4(nullReplacement(device.deviceFamily)));
html.append("</td><td>");
html.append(StringEscapeUtils.escapeHtml(nullReplacement(device.deviceType)));
html.append(HtmlEscape.escapeHtml4(nullReplacement(device.deviceType)));
html.append("</td><td>");
html.append(StringEscapeUtils.escapeHtml(nullReplacement(device.deviceOwnerCustomerId)));
html.append(HtmlEscape.escapeHtml4(nullReplacement(device.deviceOwnerCustomerId)));
html.append("</td>");
html.append("</tr>");
}
Expand All @@ -434,8 +434,7 @@ private void handleDefaultPageResult(HttpServletResponse resp, String message, C

private void handleDevices(HttpServletResponse resp, Connection connection)
throws IOException, URISyntaxException, InterruptedException {
returnHtml(connection, resp,
"<html>" + StringEscapeUtils.escapeHtml(connection.getDeviceListJson()) + "</html>");
returnHtml(connection, resp, "<html>" + HtmlEscape.escapeHtml4(connection.getDeviceListJson()) + "</html>");
}

private String nullReplacement(@Nullable String text) {
Expand All @@ -448,16 +447,16 @@ private String nullReplacement(@Nullable String text) {
StringBuilder createPageStart(String title) {
StringBuilder html = new StringBuilder();
html.append("<html><head><title>"
+ StringEscapeUtils.escapeHtml(BINDING_NAME + " - " + this.account.getThing().getLabel()));
+ HtmlEscape.escapeHtml4(BINDING_NAME + " - " + this.account.getThing().getLabel()));
if (!title.isEmpty()) {
html.append(" - ");
html.append(StringEscapeUtils.escapeHtml(title));
html.append(HtmlEscape.escapeHtml4(title));
}
html.append("</title><head><body>");
html.append("<h1>" + StringEscapeUtils.escapeHtml(BINDING_NAME + " - " + this.account.getThing().getLabel()));
html.append("<h1>" + HtmlEscape.escapeHtml4(BINDING_NAME + " - " + this.account.getThing().getLabel()));
if (!title.isEmpty()) {
html.append(" - ");
html.append(StringEscapeUtils.escapeHtml(title));
html.append(HtmlEscape.escapeHtml4(title));
}
html.append("</h1>");
return html;
Expand All @@ -466,7 +465,7 @@ StringBuilder createPageStart(String title) {
private void createPageEndAndSent(HttpServletResponse resp, StringBuilder html) {
// account overview link
html.append("<br><a href='" + servletUrl + "/../' >");
html.append(StringEscapeUtils.escapeHtml("Account overview"));
html.append(HtmlEscape.escapeHtml4("Account overview"));
html.append("</a><br>");

html.append("</body></html>");
Expand Down Expand Up @@ -497,14 +496,13 @@ private void handleIds(HttpServletResponse resp, Connection connection, Device d
private void renderCapabilities(Connection connection, Device device, StringBuilder html) {
html.append("<h2>Capabilities</h2>");
html.append("<table><tr><th align='left'>Name</th></tr>");
device.getCapabilities().forEach(capability -> html.append("<tr><td>")
.append(StringEscapeUtils.escapeHtml(capability)).append("</td></tr>"));
device.getCapabilities().forEach(
capability -> html.append("<tr><td>").append(HtmlEscape.escapeHtml4(capability)).append("</td></tr>"));
html.append("</table>");
}

private void renderMusicProviderIdChannel(Connection connection, StringBuilder html) {
html.append("<h2>").append(StringEscapeUtils.escapeHtml("Channel " + CHANNEL_MUSIC_PROVIDER_ID))
.append("</h2>");
html.append("<h2>").append(HtmlEscape.escapeHtml4("Channel " + CHANNEL_MUSIC_PROVIDER_ID)).append("</h2>");
html.append("<table><tr><th align='left'>Name</th><th align='left'>Value</th></tr>");
List<JsonMusicProvider> musicProviders = connection.getMusicProviders();
for (JsonMusicProvider musicProvider : musicProviders) {
Expand All @@ -515,17 +513,17 @@ private void renderMusicProviderIdChannel(Connection connection, StringBuilder h
&& !providerId.isEmpty() && "AVAILABLE".equals(musicProvider.availability) && displayName != null
&& !displayName.isEmpty()) {
html.append("<tr><td>");
html.append(StringEscapeUtils.escapeHtml(displayName));
html.append(HtmlEscape.escapeHtml4(displayName));
html.append("</td><td>");
html.append(StringEscapeUtils.escapeHtml(providerId));
html.append(HtmlEscape.escapeHtml4(providerId));
html.append("</td></tr>");
}
}
html.append("</table>");
}

private void renderPlayAlarmSoundChannel(Connection connection, Device device, StringBuilder html) {
html.append("<h2>").append(StringEscapeUtils.escapeHtml("Channel " + CHANNEL_PLAY_ALARM_SOUND)).append("</h2>");
html.append("<h2>").append(HtmlEscape.escapeHtml4("Channel " + CHANNEL_PLAY_ALARM_SOUND)).append("</h2>");
List<JsonNotificationSound> notificationSounds = List.of();
String errorMessage = "No notifications sounds found";
try {
Expand All @@ -542,20 +540,20 @@ private void renderPlayAlarmSoundChannel(Connection connection, Device device, S
String providerSoundId = notificationSound.providerId + ":" + notificationSound.id;

html.append("<tr><td>");
html.append(StringEscapeUtils.escapeHtml(notificationSound.displayName));
html.append(HtmlEscape.escapeHtml4(notificationSound.displayName));
html.append("</td><td>");
html.append(StringEscapeUtils.escapeHtml(providerSoundId));
html.append(HtmlEscape.escapeHtml4(providerSoundId));
html.append("</td></tr>");
}
}
html.append("</table>");
} else {
html.append(StringEscapeUtils.escapeHtml(errorMessage));
html.append(HtmlEscape.escapeHtml4(errorMessage));
}
}

private void renderAmazonMusicPlaylistIdChannel(Connection connection, Device device, StringBuilder html) {
html.append("<h2>").append(StringEscapeUtils.escapeHtml("Channel " + CHANNEL_AMAZON_MUSIC_PLAY_LIST_ID))
html.append("<h2>").append(HtmlEscape.escapeHtml4("Channel " + CHANNEL_AMAZON_MUSIC_PLAY_LIST_ID))
.append("</h2>");

JsonPlaylists playLists = null;
Expand All @@ -578,23 +576,23 @@ private void renderAmazonMusicPlaylistIdChannel(Connection connection, Device de
PlayList playList = innerLists[0];
if (playList != null && playList.playlistId != null && playList.title != null) {
html.append("<tr><td>");
html.append(StringEscapeUtils.escapeHtml(nullReplacement(playList.title)));
html.append(HtmlEscape.escapeHtml4(nullReplacement(playList.title)));
html.append("</td><td>");
html.append(StringEscapeUtils.escapeHtml(nullReplacement(playList.playlistId)));
html.append(HtmlEscape.escapeHtml4(nullReplacement(playList.playlistId)));
html.append("</td></tr>");
}
}
}
}
html.append("</table>");
} else {
html.append(StringEscapeUtils.escapeHtml(errorMessage));
html.append(HtmlEscape.escapeHtml4(errorMessage));
}
}
}

private void renderBluetoothMacChannel(Connection connection, Device device, StringBuilder html) {
html.append("<h2>").append(StringEscapeUtils.escapeHtml("Channel " + CHANNEL_BLUETOOTH_MAC)).append("</h2>");
html.append("<h2>").append(HtmlEscape.escapeHtml4("Channel " + CHANNEL_BLUETOOTH_MAC)).append("</h2>");
JsonBluetoothStates bluetoothStates = connection.getBluetoothConnectionStates();
if (bluetoothStates == null) {
return;
Expand All @@ -615,14 +613,14 @@ private void renderBluetoothMacChannel(Connection connection, Device device, Str
html.append("<table><tr><th align='left'>Name</th><th align='left'>Value</th></tr>");
for (PairedDevice pairedDevice : pairedDeviceList) {
html.append("<tr><td>");
html.append(StringEscapeUtils.escapeHtml(nullReplacement(pairedDevice.friendlyName)));
html.append(HtmlEscape.escapeHtml4(nullReplacement(pairedDevice.friendlyName)));
html.append("</td><td>");
html.append(StringEscapeUtils.escapeHtml(nullReplacement(pairedDevice.address)));
html.append(HtmlEscape.escapeHtml4(nullReplacement(pairedDevice.address)));
html.append("</td></tr>");
}
html.append("</table>");
} else {
html.append(StringEscapeUtils.escapeHtml("No bluetooth devices paired"));
html.append(HtmlEscape.escapeHtml4("No bluetooth devices paired"));
}
}
}
Expand Down Expand Up @@ -712,7 +710,7 @@ private void returnHtml(Connection connection, HttpServletResponse resp, String
void returnError(HttpServletResponse resp, @Nullable String errorMessage) {
try {
String message = errorMessage != null ? errorMessage : "null";
resp.getWriter().write("<html>" + StringEscapeUtils.escapeHtml(message) + "<br><a href='" + servletUrl
resp.getWriter().write("<html>" + HtmlEscape.escapeHtml4(message) + "<br><a href='" + servletUrl
+ "'>Try again</a></html>");
} catch (IOException e) {
logger.info("Returning error message failed", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringEscapeUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.thing.Thing;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unbescape.html.HtmlEscape;

/**
* This servlet provides the base navigation page, with hyperlinks for the defined account things
Expand Down Expand Up @@ -104,16 +104,16 @@ protected void doGet(@Nullable HttpServletRequest req, @Nullable HttpServletResp
}

StringBuilder html = new StringBuilder();
html.append("<html><head><title>" + StringEscapeUtils.escapeHtml(BINDING_NAME) + "</title><head><body>");
html.append("<h1>" + StringEscapeUtils.escapeHtml(BINDING_NAME) + "</h1>");
html.append("<html><head><title>" + HtmlEscape.escapeHtml4(BINDING_NAME) + "</title><head><body>");
html.append("<h1>" + HtmlEscape.escapeHtml4(BINDING_NAME) + "</h1>");

synchronized (accountHandlers) {
if (accountHandlers.isEmpty()) {
html.append("No Account thing created.");
} else {
for (Thing accountHandler : accountHandlers) {
String url = URLEncoder.encode(accountHandler.getUID().getId(), "UTF8");
html.append("<a href='./" + url + " '>" + StringEscapeUtils.escapeHtml(accountHandler.getLabel())
html.append("<a href='./" + url + " '>" + HtmlEscape.escapeHtml4(accountHandler.getLabel())
+ "</a><br>");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,6 @@ public void smartHomeCommand(String entityId, String action, @Nullable String pr
.collect(Collectors.joining(" / ")));
}
}

} catch (URISyntaxException e) {
logger.warn("URL '{}' has invalid format for request '{}': {}", url, requestBody, e.getMessage());
}
Expand Down Expand Up @@ -1223,7 +1222,6 @@ public List<DeviceNotificationState> getDeviceNotificationStates() {
String json = makeRequestAndReturnString(alexaServer + "/api/device-notification-state");
JsonDeviceNotificationState result = parseJson(json, JsonDeviceNotificationState.class);
return Objects.requireNonNullElse(result.deviceNotificationStates, List.of());

} catch (IOException | URISyntaxException | InterruptedException e) {
logger.info("Error getting device notification states", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
import java.net.URISyntaxException;
import java.util.Objects;

import org.apache.commons.lang.StringEscapeUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.amazonechocontrol.internal.Connection;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDevices.Device;
import org.openhab.core.library.types.StringType;
import org.openhab.core.types.Command;
import org.unbescape.xml.XmlEscape;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
Expand Down Expand Up @@ -72,7 +72,7 @@ public boolean tryHandleCommand(Device device, Connection connection, String cha
Boolean sound = request.sound;
if (sound != null) {
if (!sound && !speak.startsWith("<speak>")) {
speak = "<speak>" + StringEscapeUtils.escapeXml(speak) + "</speak>";
speak = "<speak>" + XmlEscape.escapeXml10(speak) + "</speak>";
}
if (sound && speak.startsWith("<speak>")) {
body = "Error: The combination of sound and speak in SSML syntax is not allowed";
Expand All @@ -87,8 +87,7 @@ public boolean tryHandleCommand(Device device, Connection connection, String cha
} catch (JsonSyntaxException e) {
body = "Invalid Json." + e.getLocalizedMessage();
title = "Error";
speak = "<speak><lang xml:lang=\"en-US\">" + StringEscapeUtils.escapeXml(body)
+ "</lang></speak>";
speak = "<speak><lang xml:lang=\"en-US\">" + XmlEscape.escapeXml10(body) + "</lang></speak>";
body = e.getLocalizedMessage();
}
}
Expand Down

0 comments on commit 325ae95

Please sign in to comment.