-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
some change on smartthings customer onboarding Signed-off-by: Laurent ARNAL <[email protected]>
- Loading branch information
Showing
12 changed files
with
462 additions
and
330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...things/src/main/java/org/openhab/binding/smartthings/internal/SmartthingsBaseServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* Copyright (c) 2010-2024 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.smartthings.internal; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import javax.servlet.http.HttpServlet; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.openhab.binding.smartthings.internal.api.SmartthingsNetworkConnector; | ||
import org.openhab.binding.smartthings.internal.handler.SmartthingsBridgeHandler; | ||
import org.osgi.service.http.HttpService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* The {@link SpotifyAuthServlet} manages the authorization with the Smartthings Web API. The servlet implements the | ||
* Authorization Code flow and saves the resulting refreshToken with the bridge. | ||
* | ||
* @author Laurent Arnal - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class SmartthingsBaseServlet extends HttpServlet { | ||
|
||
private static final long serialVersionUID = -4719613645562518231L; | ||
private final Logger logger = LoggerFactory.getLogger(SmartthingsAuthServlet.class); | ||
|
||
protected final SmartthingsBridgeHandler bridgeHandler; | ||
protected final HttpService httpService; | ||
protected final SmartthingsNetworkConnector networkConnector; | ||
protected final String token; | ||
|
||
private static final Pattern MESSAGE_KEY_PATTERN = Pattern.compile("\\$\\{([^\\}]+)\\}"); | ||
|
||
private static final String TEMPLATE_PATH = "templates/"; | ||
|
||
public SmartthingsBaseServlet(SmartthingsBridgeHandler bridgeHandler, HttpService httpService, | ||
SmartthingsNetworkConnector networkConnector, String token) { | ||
|
||
this.bridgeHandler = bridgeHandler; | ||
this.httpService = httpService; | ||
this.networkConnector = networkConnector; | ||
this.token = token; | ||
|
||
} | ||
|
||
/** | ||
* Replaces all keys from the map found in the template with values from the map. If the key is not found the key | ||
* will be kept in the template. | ||
* | ||
* @param template template to replace keys with values | ||
* @param map map with key value pairs to replace in the template | ||
* @return a template with keys replaced | ||
*/ | ||
protected String replaceKeysFromMap(String template, Map<String, String> map) { | ||
final Matcher m = MESSAGE_KEY_PATTERN.matcher(template); | ||
final StringBuffer sb = new StringBuffer(); | ||
|
||
while (m.find()) { | ||
try { | ||
final String key = m.group(1); | ||
m.appendReplacement(sb, Matcher.quoteReplacement(map.getOrDefault(key, "${" + key + '}'))); | ||
} catch (RuntimeException e) { | ||
logger.debug("Error occurred during template filling, cause ", e); | ||
} | ||
} | ||
m.appendTail(sb); | ||
return sb.toString(); | ||
} | ||
|
||
/** | ||
* Reads a template from file and returns the content as String. | ||
* | ||
* @param templateName name of the template file to read | ||
* @return The content of the template file | ||
* @throws IOException thrown when an HTML template could not be read | ||
*/ | ||
protected String readTemplate(String templateName) throws IOException { | ||
final URL url = bridgeHandler.getBundleContext().getBundle().getEntry(TEMPLATE_PATH + templateName); | ||
|
||
if (url == null) { | ||
throw new FileNotFoundException( | ||
String.format("Cannot find {}' - failed to initialize Linky servlet".formatted(templateName))); | ||
} else { | ||
try (InputStream inputStream = url.openStream()) { | ||
return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.