Skip to content

Commit 255b689

Browse files
authored
fix: Fixed placeholder handling for password defaults [backport release-5.6.0] (#6189)
Signed-off-by: Nicola Timeus <nicola.timeus@eurotech.com>
1 parent fcfc869 commit 255b689

3 files changed

Lines changed: 166 additions & 122 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Import-Package: com.eclipsesource.json;version="0.9.5",
4343
org.eclipse.kura.container.orchestration;version="[1.0,2.0)",
4444
org.eclipse.kura.core.configuration;version="[2.0,3.0)",
4545
org.eclipse.kura.core.configuration.metatype;version="[1.0,2.0)",
46+
org.eclipse.kura.core.configuration.util;version="[2.0,3.0)",
4647
org.eclipse.kura.core.keystore.util;version="[1.0,2.0)",
4748
org.eclipse.kura.core.net;version="[1.0,2.0)",
4849
org.eclipse.kura.core.net.util;version="[1.0,2.0)",

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2016, 2021 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
@@ -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;
@@ -579,36 +580,43 @@ protected List<EditorError> validateTextBox(final GwtConfigParameter param, fina
579580
}
580581

581582
protected void fillUpdatedConfiguration(FormGroup fg) {
582-
GwtConfigParameter param = new GwtConfigParameter();
583583
List<String> multiFieldValues = new ArrayList<>();
584-
int fgwCount = fg.getWidgetCount();
585-
for (int i = 0; i < fgwCount; i++) {
586-
logger.fine("Widget: " + fg.getClass());
587584

588-
if (fg.getWidget(i) instanceof FormLabel) {
589-
param = this.configurableComponent.getParameter(fg.getWidget(i).getTitle());
585+
final GwtConfigParameter param = fillUpdatedConfigurationInternal(fg, null, multiFieldValues);
590586

591-
} else if (fg.getWidget(i) instanceof ListBox || fg.getWidget(i) instanceof Input
592-
|| fg.getWidget(i) instanceof TextBoxBase) {
587+
if (!multiFieldValues.isEmpty() && param != null) {
588+
param.setValues(multiFieldValues.toArray(new String[] {}));
589+
}
590+
}
591+
592+
private GwtConfigParameter fillUpdatedConfigurationInternal(final Panel fg, GwtConfigParameter target,
593+
List<String> multiFieldValues) {
594+
for (final Widget w : fg) {
595+
logger.fine("Widget: " + w.getClass());
593596

594-
if (param == null) {
597+
if (w instanceof FormLabel) {
598+
target = this.configurableComponent.getParameter(w.getTitle());
599+
} else if (w instanceof Panel) {
600+
target = fillUpdatedConfigurationInternal((Panel) w, target, multiFieldValues);
601+
} else if (w instanceof ListBox || w instanceof Input || w instanceof TextBoxBase) {
602+
603+
if (target == null) {
595604
errorLogger.warning("Missing parameter");
596605
continue;
597606
}
598-
String value = getUpdatedFieldConfiguration(param, fg.getWidget(i));
607+
String value = getUpdatedFieldConfiguration(target, w);
599608
if (value == null) {
600609
continue;
601610
}
602-
if (param.getCardinality() == 0 || param.getCardinality() == 1 || param.getCardinality() == -1) {
603-
param.setValue(value);
611+
if (target.getCardinality() == 0 || target.getCardinality() == 1 || target.getCardinality() == -1) {
612+
target.setValue(value);
604613
} else {
605614
multiFieldValues.add(value);
606615
}
607616
}
608617
}
609-
if (!multiFieldValues.isEmpty() && param != null) {
610-
param.setValues(multiFieldValues.toArray(new String[] {}));
611-
}
618+
619+
return target;
612620
}
613621

614622
protected void restoreConfiguration(GwtConfigComponent originalConfig) {

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

Lines changed: 141 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2016, 2024 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
@@ -45,6 +45,7 @@
4545
import org.eclipse.kura.core.configuration.XmlComponentConfigurations;
4646
import org.eclipse.kura.core.configuration.metatype.Tad;
4747
import org.eclipse.kura.core.configuration.metatype.Tocd;
48+
import org.eclipse.kura.core.configuration.util.StringUtil;
4849
import org.eclipse.kura.driver.descriptor.DriverDescriptor;
4950
import org.eclipse.kura.marshalling.Marshaller;
5051
import org.eclipse.kura.rest.configuration.api.ComponentConfigurationList;
@@ -122,35 +123,35 @@ public static Object getObjectValue(GwtConfigParameter param) {
122123
} else if (strValue != null && !strValue.trim().isEmpty()) {
123124
final String trimmedValue = strValue.trim();
124125
switch (gwtType) {
125-
case LONG:
126-
objValue = Long.parseLong(trimmedValue);
127-
break;
128-
case DOUBLE:
129-
objValue = Double.parseDouble(trimmedValue);
130-
break;
131-
case FLOAT:
132-
objValue = Float.parseFloat(trimmedValue);
133-
break;
134-
case INTEGER:
135-
objValue = Integer.parseInt(trimmedValue);
136-
break;
137-
case SHORT:
138-
objValue = Short.parseShort(trimmedValue);
139-
break;
140-
case BYTE:
141-
objValue = Byte.parseByte(trimmedValue);
142-
break;
143-
case BOOLEAN:
144-
objValue = Boolean.parseBoolean(trimmedValue);
145-
break;
146-
case PASSWORD:
147-
objValue = new Password(trimmedValue);
148-
break;
149-
case CHAR:
150-
objValue = Character.valueOf(trimmedValue.charAt(0));
151-
break;
152-
default:
153-
break;
126+
case LONG:
127+
objValue = Long.parseLong(trimmedValue);
128+
break;
129+
case DOUBLE:
130+
objValue = Double.parseDouble(trimmedValue);
131+
break;
132+
case FLOAT:
133+
objValue = Float.parseFloat(trimmedValue);
134+
break;
135+
case INTEGER:
136+
objValue = Integer.parseInt(trimmedValue);
137+
break;
138+
case SHORT:
139+
objValue = Short.parseShort(trimmedValue);
140+
break;
141+
case BYTE:
142+
objValue = Byte.parseByte(trimmedValue);
143+
break;
144+
case BOOLEAN:
145+
objValue = Boolean.parseBoolean(trimmedValue);
146+
break;
147+
case PASSWORD:
148+
objValue = new Password(trimmedValue);
149+
break;
150+
case CHAR:
151+
objValue = Character.valueOf(trimmedValue.charAt(0));
152+
break;
153+
default:
154+
break;
154155
}
155156
}
156157
return objValue;
@@ -163,117 +164,151 @@ public static Object[] getObjectValues(GwtConfigParameter param, String[] defaul
163164
List<String> trimmedValues = Stream.of(defaultValues).map(String::trim).collect(Collectors.toList());
164165

165166
switch (type) {
166-
case BOOLEAN:
167-
for (String value : trimmedValues) {
168-
if (!value.isEmpty()) {
169-
values.add(Boolean.valueOf(value));
170-
}
167+
case BOOLEAN:
168+
for (String value : trimmedValues) {
169+
if (!value.isEmpty()) {
170+
values.add(Boolean.valueOf(value));
171171
}
172-
return values.toArray(new Boolean[] {});
172+
}
173+
return values.toArray(new Boolean[] {});
173174

174-
case BYTE:
175-
for (String value : trimmedValues) {
176-
if (!value.isEmpty()) {
177-
values.add(Byte.valueOf(value));
178-
}
175+
case BYTE:
176+
for (String value : trimmedValues) {
177+
if (!value.isEmpty()) {
178+
values.add(Byte.valueOf(value));
179179
}
180-
return values.toArray(new Byte[] {});
180+
}
181+
return values.toArray(new Byte[] {});
181182

182-
case CHAR:
183-
for (String value : trimmedValues) {
184-
if (!value.isEmpty()) {
185-
values.add(new Character(value.charAt(0)));
186-
}
183+
case CHAR:
184+
for (String value : trimmedValues) {
185+
if (!value.isEmpty()) {
186+
values.add(new Character(value.charAt(0)));
187187
}
188-
return values.toArray(new Character[] {});
188+
}
189+
return values.toArray(new Character[] {});
189190

190-
case DOUBLE:
191-
for (String value : trimmedValues) {
192-
if (!value.isEmpty()) {
193-
values.add(Double.valueOf(value));
194-
}
191+
case DOUBLE:
192+
for (String value : trimmedValues) {
193+
if (!value.isEmpty()) {
194+
values.add(Double.valueOf(value));
195195
}
196-
return values.toArray(new Double[] {});
196+
}
197+
return values.toArray(new Double[] {});
197198

198-
case FLOAT:
199-
for (String value : trimmedValues) {
200-
if (!value.isEmpty()) {
201-
values.add(Float.valueOf(value));
202-
}
199+
case FLOAT:
200+
for (String value : trimmedValues) {
201+
if (!value.isEmpty()) {
202+
values.add(Float.valueOf(value));
203203
}
204-
return values.toArray(new Float[] {});
204+
}
205+
return values.toArray(new Float[] {});
205206

206-
case INTEGER:
207-
for (String value : trimmedValues) {
208-
if (!value.isEmpty()) {
209-
values.add(Integer.valueOf(value));
210-
}
207+
case INTEGER:
208+
for (String value : trimmedValues) {
209+
if (!value.isEmpty()) {
210+
values.add(Integer.valueOf(value));
211211
}
212-
return values.toArray(new Integer[] {});
212+
}
213+
return values.toArray(new Integer[] {});
213214

214-
case LONG:
215-
for (String value : trimmedValues) {
216-
if (!value.isEmpty()) {
217-
values.add(Long.valueOf(value));
218-
}
215+
case LONG:
216+
for (String value : trimmedValues) {
217+
if (!value.isEmpty()) {
218+
values.add(Long.valueOf(value));
219219
}
220-
return values.toArray(new Long[] {});
220+
}
221+
return values.toArray(new Long[] {});
221222

222-
case SHORT:
223-
for (String value : trimmedValues) {
224-
if (!value.isEmpty()) {
225-
values.add(Short.valueOf(value));
226-
}
223+
case SHORT:
224+
for (String value : trimmedValues) {
225+
if (!value.isEmpty()) {
226+
values.add(Short.valueOf(value));
227227
}
228-
return values.toArray(new Short[] {});
228+
}
229+
return values.toArray(new Short[] {});
229230

230-
case PASSWORD:
231-
for (String value : trimmedValues) {
232-
if (!value.isEmpty()) {
233-
values.add(new Password(value));
234-
}
231+
case PASSWORD:
232+
for (String value : trimmedValues) {
233+
if (!value.isEmpty()) {
234+
values.add(new Password(value));
235235
}
236-
return values.toArray(new Password[] {});
236+
}
237+
return values.toArray(new Password[] {});
237238

238-
case STRING:
239-
for (String value : trimmedValues) {
240-
if (!value.isEmpty()) {
241-
values.add(value);
242-
}
239+
case STRING:
240+
for (String value : trimmedValues) {
241+
if (!value.isEmpty()) {
242+
values.add(value);
243243
}
244-
return values.toArray(new String[] {});
245-
default:
246-
return null;
244+
}
245+
return values.toArray(new String[] {});
246+
default:
247+
return null;
247248
}
248249
}
249250

250251
public static Object getUserDefinedObject(GwtConfigParameter param, Object currentObjValue) {
251-
Object objValue;
252252

253253
final int cardinality = param.getCardinality();
254254
if (cardinality == 0 || cardinality == 1 || cardinality == -1) {
255-
String strValue = param.getValue();
255+
return getUserDefinedObjectScalar(param, currentObjValue);
256+
} else {
257+
return getUserDefinedObjectArray(param, currentObjValue);
258+
}
259+
}
256260

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

265291
if (currentObjValue instanceof Password[]) {
266-
Password[] currentPasswordValue = (Password[]) currentObjValue;
292+
current = Optional.of(Arrays.stream((Password[]) currentObjValue).map(p -> new String(p.getPassword()))
293+
.collect(Collectors.toList()).toArray(new String[] {}));
294+
} else if (param.isRequired()) {
295+
final String defaultValue = param.getDefault();
296+
297+
if (defaultValue != null && !defaultValue.trim().isEmpty()) {
298+
current = Optional.of(StringUtil.splitValues(defaultValue));
299+
}
300+
}
301+
302+
if (current.isPresent()) {
267303
for (int i = 0; i < strValues.length; i++) {
268-
if (PASSWORD_PLACEHOLDER.equals(strValues[i])) {
269-
strValues[i] = new String(currentPasswordValue[i].getPassword());
304+
if (PASSWORD_PLACEHOLDER.equals(strValues[i]) && i < current.get().length) {
305+
strValues[i] = current.get()[i];
270306
}
271307
}
272308
}
273-
274-
objValue = getObjectValues(param, strValues);
275309
}
276-
return objValue;
310+
311+
return getObjectValues(param, strValues);
277312
}
278313

279314
/**

0 commit comments

Comments
 (0)