Skip to content

Commit ddd79e6

Browse files
fix: Fixed placeholder handling for password defaults (#34)
* fix: Fixed placeholder handling for password defaults Signed-off-by: Nicola Timeus <nicola.timeus@eurotech.com> * Use StringUtil in core.configuration for splitting Signed-off-by: Nicola Timeus <nicola.timeus@eurotech.com> --------- Signed-off-by: Nicola Timeus <nicola.timeus@eurotech.com> Co-authored-by: sfiorani <109297780+sfiorani@users.noreply.github.com>
1 parent 571ca63 commit ddd79e6

3 files changed

Lines changed: 79 additions & 35 deletions

File tree

bundles/org.eclipse.kura.web2/META-INF/MANIFEST.MF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Import-Package: com.eclipsesource.json;version="0.9.5",
4040
org.eclipse.kura.container.orchestration;version="[1.0,2.0)",
4141
org.eclipse.kura.core.configuration;version="[2.0,3.0)",
4242
org.eclipse.kura.core.configuration.metatype;version="[1.0,2.0)",
43+
org.eclipse.kura.core.configuration.util;version="[2.0,3.0)",
4344
org.eclipse.kura.core.keystore.util;version="[1.0,2.0)",
4445
org.eclipse.kura.core.net;version="[1.0,2.0)";resolution:=optional,
4546
org.eclipse.kura.core.net.util;version="[1.0,2.0)";resolution:=optional,

bundles/org.eclipse.kura.web2/src/main/java/org/eclipse/kura/web/client/ui/AbstractServicesUi.java

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.gwtbootstrap3.client.ui.InlineRadio;
4646
import org.gwtbootstrap3.client.ui.Input;
4747
import org.gwtbootstrap3.client.ui.ListBox;
48+
import com.google.gwt.user.client.ui.Panel;
4849
import org.gwtbootstrap3.client.ui.TextArea;
4950
import org.gwtbootstrap3.client.ui.TextBox;
5051
import org.gwtbootstrap3.client.ui.base.TextBoxBase;
@@ -396,12 +397,11 @@ protected void renderPasswordField(final GwtConfigParameter param, boolean isFir
396397
} else {
397398
input.setInputPasswordText("");
398399
}
399-
400+
400401
input.setShowPasswordButtonEnabled(!PLACEHOLDER.equals(input.getInputPasswordText()));
401-
402+
402403
input.setInputPasswordClickHandler(handler -> {
403-
if (input.isInputPasswordEnabled()
404-
&& input.getInputPasswordText().equals(PLACEHOLDER)) {
404+
if (input.isInputPasswordEnabled() && input.getInputPasswordText().equals(PLACEHOLDER)) {
405405
input.setInputPasswordText("");
406406
input.validateInputPassword();
407407
input.setShowPasswordButtonEnabled(true);
@@ -419,7 +419,8 @@ protected void renderPasswordField(final GwtConfigParameter param, boolean isFir
419419
public List<EditorError> validate(Editor editor, Object value) {
420420

421421
List<EditorError> result = new ArrayList<>();
422-
if ((input.getInputPasswordText() == null || "".equals(input.getInputPasswordText().trim())) && param.isRequired()) {
422+
if ((input.getInputPasswordText() == null || "".equals(input.getInputPasswordText().trim()))
423+
&& param.isRequired()) {
423424
// null in required field
424425
result.add(new BasicEditorError(input, input.getInputPasswordText(), MSGS.formRequiredParameter()));
425426
AbstractServicesUi.this.valid.put(param.getId(), false);
@@ -591,36 +592,43 @@ protected List<EditorError> validateTextBox(final GwtConfigParameter param, fina
591592
}
592593

593594
protected void fillUpdatedConfiguration(FormGroup fg) {
594-
GwtConfigParameter param = new GwtConfigParameter();
595595
List<String> multiFieldValues = new ArrayList<>();
596-
int fgwCount = fg.getWidgetCount();
597-
for (int i = 0; i < fgwCount; i++) {
598-
logger.fine("Widget: " + fg.getClass());
599596

600-
if (fg.getWidget(i) instanceof FormLabel) {
601-
param = this.configurableComponent.getParameter(fg.getWidget(i).getTitle());
597+
final GwtConfigParameter param = fillUpdatedConfigurationInternal(fg, null, multiFieldValues);
598+
599+
if (!multiFieldValues.isEmpty() && param != null) {
600+
param.setValues(multiFieldValues.toArray(new String[] {}));
601+
}
602+
}
603+
604+
private GwtConfigParameter fillUpdatedConfigurationInternal(final Panel fg, GwtConfigParameter target,
605+
List<String> multiFieldValues) {
606+
for (final Widget w : fg) {
607+
logger.fine("Widget: " + w.getClass());
602608

603-
} else if (fg.getWidget(i) instanceof ListBox || fg.getWidget(i) instanceof Input
604-
|| fg.getWidget(i) instanceof TextBoxBase) {
609+
if (w instanceof FormLabel) {
610+
target = this.configurableComponent.getParameter(w.getTitle());
611+
} else if (w instanceof Panel) {
612+
target = fillUpdatedConfigurationInternal((Panel) w, target, multiFieldValues);
613+
} else if (w instanceof ListBox || w instanceof Input || w instanceof TextBoxBase) {
605614

606-
if (param == null) {
615+
if (target == null) {
607616
errorLogger.warning("Missing parameter");
608617
continue;
609618
}
610-
String value = getUpdatedFieldConfiguration(param, fg.getWidget(i));
619+
String value = getUpdatedFieldConfiguration(target, w);
611620
if (value == null) {
612621
continue;
613622
}
614-
if (param.getCardinality() == 0 || param.getCardinality() == 1 || param.getCardinality() == -1) {
615-
param.setValue(value);
623+
if (target.getCardinality() == 0 || target.getCardinality() == 1 || target.getCardinality() == -1) {
624+
target.setValue(value);
616625
} else {
617626
multiFieldValues.add(value);
618627
}
619628
}
620629
}
621-
if (!multiFieldValues.isEmpty() && param != null) {
622-
param.setValues(multiFieldValues.toArray(new String[] {}));
623-
}
630+
631+
return target;
624632
}
625633

626634
protected void restoreConfiguration(GwtConfigComponent originalConfig) {

bundles/org.eclipse.kura.web2/src/main/java/org/eclipse/kura/web/server/util/GwtServerUtil.java

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2016, 2025 Eurotech and/or its affiliates and others
2+
* Copyright (c) 2016, 2026 Eurotech and/or its affiliates and others
33
*
44
* This program and the accompanying materials are made
55
* available under the terms of the Eclipse Public License 2.0
@@ -41,6 +41,7 @@
4141
import org.eclipse.kura.core.configuration.XmlComponentConfigurations;
4242
import org.eclipse.kura.core.configuration.metatype.Tad;
4343
import org.eclipse.kura.core.configuration.metatype.Tocd;
44+
import org.eclipse.kura.core.configuration.util.StringUtil;
4445
import org.eclipse.kura.driver.descriptor.DriverDescriptor;
4546
import org.eclipse.kura.driver.descriptor.DriverDescriptorService;
4647
import org.eclipse.kura.identity.LoginBannerService;
@@ -252,32 +253,66 @@ public static Object[] getObjectValues(GwtConfigParameter param, String[] defaul
252253
}
253254

254255
public static Object getUserDefinedObject(GwtConfigParameter param, Object currentObjValue) {
255-
Object objValue;
256256

257257
final int cardinality = param.getCardinality();
258258
if (cardinality == 0 || cardinality == 1 || cardinality == -1) {
259-
String strValue = param.getValue();
259+
return getUserDefinedObjectScalar(param, currentObjValue);
260+
} else {
261+
return getUserDefinedObjectArray(param, currentObjValue);
262+
}
263+
}
260264

261-
if (currentObjValue instanceof Password && PASSWORD_PLACEHOLDER.equals(strValue)) {
262-
objValue = currentObjValue;
263-
} else {
264-
objValue = getObjectValue(param);
265+
private static Object getUserDefinedObjectScalar(GwtConfigParameter param, Object currentObjValue) {
266+
String strValue = param.getValue();
267+
268+
if (param.getType() == GwtConfigParameterType.PASSWORD && PASSWORD_PLACEHOLDER.equals(strValue)) {
269+
270+
if (currentObjValue instanceof Password) {
271+
return currentObjValue;
265272
}
266-
} else {
267-
String[] strValues = param.getValues();
273+
274+
if (param.isRequired()) {
275+
final String defaultValue = param.getDefault();
276+
277+
if (defaultValue != null && !defaultValue.trim().isEmpty()) {
278+
final GwtConfigParameter cloned = new GwtConfigParameter(param);
279+
cloned.setValue(defaultValue);
280+
return getObjectValue(cloned);
281+
}
282+
}
283+
}
284+
285+
return getObjectValue(param);
286+
}
287+
288+
private static Object getUserDefinedObjectArray(GwtConfigParameter param, Object currentObjValue) {
289+
String[] strValues = param.getValues();
290+
291+
if (param.getType() == GwtConfigParameterType.PASSWORD) {
292+
293+
Optional<String[]> current = Optional.empty();
268294

269295
if (currentObjValue instanceof Password[]) {
270-
Password[] currentPasswordValue = (Password[]) currentObjValue;
296+
current = Optional.of(Arrays.stream((Password[]) currentObjValue).map(p -> new String(p.getPassword()))
297+
.collect(Collectors.toList()).toArray(new String[] {}));
298+
} else if (param.isRequired()) {
299+
final String defaultValue = param.getDefault();
300+
301+
if (defaultValue != null && !defaultValue.trim().isEmpty()) {
302+
current = Optional.of(StringUtil.splitValues(defaultValue));
303+
}
304+
}
305+
306+
if (current.isPresent()) {
271307
for (int i = 0; i < strValues.length; i++) {
272-
if (PASSWORD_PLACEHOLDER.equals(strValues[i])) {
273-
strValues[i] = new String(currentPasswordValue[i].getPassword());
308+
if (PASSWORD_PLACEHOLDER.equals(strValues[i]) && i < current.get().length) {
309+
strValues[i] = current.get()[i];
274310
}
275311
}
276312
}
277-
278-
objValue = getObjectValues(param, strValues);
279313
}
280-
return objValue;
314+
315+
return getObjectValues(param, strValues);
281316
}
282317

283318
/**

0 commit comments

Comments
 (0)