forked from openhab/openhab-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New method to build a valid consumer name for a web client (openhab#3359
- Loading branch information
Showing
2 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
.../org.openhab.core.thing/src/main/java/org/openhab/core/thing/util/ThingWebClientUtil.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,67 @@ | ||
/** | ||
* Copyright (c) 2010-2023 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.core.thing.util; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.thing.ThingUID; | ||
|
||
/** | ||
* {@link ThingWebClientUtil} provides an utility method to create a valid consumer name for web clients. | ||
* | ||
* @author Laurent Garnier - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class ThingWebClientUtil { | ||
|
||
private static final int MAX_CONSUMER_NAME_LENGTH = 20; | ||
|
||
/** | ||
* Build a valid consumer name for HTTP or WebSocket client. | ||
* | ||
* @param uid thing UID for which to associate HTTP or WebSocket client | ||
* @param prefix a prefix to consider for the name; can be null | ||
* @return a valid consumer name | ||
*/ | ||
public static String buildWebClientConsumerName(ThingUID uid, @Nullable String prefix) { | ||
String pref = prefix == null ? "" : prefix; | ||
String name = pref + uid.getAsString().replace(':', '-'); | ||
if (name.length() > MAX_CONSUMER_NAME_LENGTH) { | ||
// Try to use only prefix + binding ID + thing ID | ||
name = pref + uid.getBindingId(); | ||
if (name.length() > (MAX_CONSUMER_NAME_LENGTH / 2)) { | ||
// Truncate the binding ID to keep enough place for thing ID | ||
name = name.substring(0, MAX_CONSUMER_NAME_LENGTH / 2); | ||
} | ||
// Add the thing ID | ||
String id = uid.getId(); | ||
int maxIdLength = MAX_CONSUMER_NAME_LENGTH - 1 - name.length(); | ||
if (id.length() > maxIdLength) { | ||
// If thing ID is too big, use a hash code of the thing UID instead of thing id | ||
// and truncate it if necessary | ||
id = buildHashCode(uid, maxIdLength); | ||
} | ||
name += "-" + id; | ||
} | ||
return name; | ||
} | ||
|
||
// Make the method public just to be able to call it inside the tests | ||
static String buildHashCode(ThingUID uid, int maxLength) { | ||
String result = Integer.toHexString(uid.hashCode()); | ||
if (result.length() > maxLength) { | ||
result = result.substring(result.length() - maxLength); | ||
} | ||
return result; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
....openhab.core.thing/src/test/java/org/openhab/core/thing/util/ThingWebClientUtilTest.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,69 @@ | ||
/** | ||
* Copyright (c) 2010-2023 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.core.thing.util; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.junit.jupiter.api.Test; | ||
import org.openhab.core.thing.ThingUID; | ||
|
||
/** | ||
* @author Laurent Garnier - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class ThingWebClientUtilTest { | ||
|
||
private ThingUID uid1 = new ThingUID("mycroft", "mycroft", "yy"); | ||
private ThingUID uid2 = new ThingUID("mycroft", "mycroft", "myInstance"); | ||
private ThingUID uid3 = new ThingUID("mycroft", "mycroft", "myPersonalInstance"); | ||
private ThingUID uid4 = new ThingUID("amazonechocontrol", "account", "myAccount"); | ||
|
||
@Test | ||
public void testBuildWebClientConsumerNameWhenThingUidSizeIsOk() { | ||
String name = ThingWebClientUtil.buildWebClientConsumerName(uid1, null); | ||
assertThat(name, is("mycroft-mycroft-yy")); | ||
} | ||
|
||
@Test | ||
public void testBuildWebClientConsumerNameWhenPrefixAndThingUidSizeIsOk() { | ||
String name = ThingWebClientUtil.buildWebClientConsumerName(uid1, "x-"); | ||
assertThat(name, is("x-mycroft-mycroft-yy")); | ||
} | ||
|
||
@Test | ||
public void testBuildWebClientConsumerNameWhenPrefixIsTooBig() { | ||
String name = ThingWebClientUtil.buildWebClientConsumerName(uid1, "xxxx-"); | ||
assertThat(name, is("xxxx-mycro-yy")); | ||
} | ||
|
||
@Test | ||
public void testBuildWebClientConsumerNameWhenThingUidIsTooBig() { | ||
String name = ThingWebClientUtil.buildWebClientConsumerName(uid2, null); | ||
assertThat(name, is("mycroft-myInstance")); | ||
} | ||
|
||
@Test | ||
public void testBuildWebClientConsumerNameWhenThingIdIsTooBig() { | ||
String name = ThingWebClientUtil.buildWebClientConsumerName(uid3, null); | ||
String hashCode = ThingWebClientUtil.buildHashCode(uid3, 12); | ||
assertThat(name, is("mycroft-" + hashCode)); | ||
} | ||
|
||
@Test | ||
public void testBuildWebClientConsumerNameWhenBindingIdIsTooBig() { | ||
String name = ThingWebClientUtil.buildWebClientConsumerName(uid4, null); | ||
assertThat(name, is("amazonecho-myAccount")); | ||
} | ||
} |