You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2020/05/18 10:05:35 UTC

[syncope] branch 2_0_X updated: [SYNCOPE-1560] Proper use of Wicket model, removing duplicate AjaxFormSubmitBehavior

This is an automated email from the ASF dual-hosted git repository.

ilgrosso pushed a commit to branch 2_0_X
in repository https://gitbox.apache.org/repos/asf/syncope.git


The following commit(s) were added to refs/heads/2_0_X by this push:
     new 050fabd  [SYNCOPE-1560] Proper use of Wicket model, removing duplicate AjaxFormSubmitBehavior
050fabd is described below

commit 050fabde66d5645ecafa8dc44d5b232043fcaaa4
Author: Francesco Chicchiriccò <il...@apache.org>
AuthorDate: Mon May 18 11:49:12 2020 +0200

    [SYNCOPE-1560] Proper use of Wicket model, removing duplicate AjaxFormSubmitBehavior
---
 .../wicket/markup/html/form/BinaryFieldPanel.java  | 23 ++++++++--------
 .../client/console/panels/ImportMetadata.java      | 32 ++++------------------
 2 files changed, 17 insertions(+), 38 deletions(-)

diff --git a/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/BinaryFieldPanel.java b/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/BinaryFieldPanel.java
index a187f16..3ed178a 100644
--- a/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/BinaryFieldPanel.java
+++ b/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/BinaryFieldPanel.java
@@ -176,34 +176,33 @@ public class BinaryFieldPanel extends FieldPanel<String> {
         downloadLink.setOutputMarkupId(true);
         uploadForm.add(downloadLink);
 
+        final ListModel<FileUpload> fileUploadModel = new ListModel<>(new ArrayList<FileUpload>());
         FileInputConfig config = new FileInputConfig().
                 showUpload(false).showRemove(false).showPreview(false);
         String language = SyncopeConsoleSession.get().getLocale().getLanguage();
         if (!Locale.ENGLISH.getLanguage().equals(language)) {
             config.withLocale(language);
         }
-
-        fileUpload = new BootstrapFileInputField("fileUpload", new ListModel<>(new ArrayList<FileUpload>()), config);
-        fileUpload.setOutputMarkupId(true);
-
+        fileUpload = new BootstrapFileInputField("fileUpload", fileUploadModel, config);
         fileUpload.add(new AjaxFormSubmitBehavior(Constants.ON_CHANGE) {
 
             private static final long serialVersionUID = -1107858522700306810L;
 
             @Override
             protected void onSubmit(final AjaxRequestTarget target) {
-                FileUpload uploadedFile = fileUpload.getFileUpload();
-                if (uploadedFile != null) {
-                    if (maxUploadSize != null && uploadedFile.getSize() > maxUploadSize.bytes()) {
+                if (!fileUploadModel.getObject().isEmpty()) {
+                    FileUpload uploaded = fileUploadModel.getObject().get(0);
+
+                    if (maxUploadSize != null && uploaded.getSize() > maxUploadSize.bytes()) {
                         // SYNCOPE-1213 manage directly max upload file size (if set in properties file)
                         SyncopeConsoleSession.get().error(getString("tooLargeFile").
                                 replace("${maxUploadSizeB}", String.valueOf(maxUploadSize.bytes())).
                                 replace("${maxUploadSizeMB}", String.valueOf(maxUploadSize.bytes() / 1000000L)));
                         ((BasePage) getPageReference().getPage()).getNotificationPanel().refresh(target);
                     } else {
-                        byte[] uploadedBytes = uploadedFile.getBytes();
-                        String uploaded = new String(Base64.encodeBase64(uploadedBytes), StandardCharsets.UTF_8);
-                        field.setModelObject(uploaded);
+                        byte[] uploadedBytes = uploaded.getBytes();
+                        String uploadedEncoded = new String(Base64.encodeBase64(uploadedBytes), StandardCharsets.UTF_8);
+                        field.setModelObject(uploadedEncoded);
                         target.add(field);
 
                         if (previewer == null) {
@@ -215,8 +214,8 @@ public class BinaryFieldPanel extends FieldPanel<String> {
                             uploadForm.addOrReplace(fileUpload);
                         }
 
-                        setVisibleFileButtons(StringUtils.isNotBlank(uploaded));
-                        downloadLink.setEnabled(StringUtils.isNotBlank(uploaded));
+                        setVisibleFileButtons(StringUtils.isNotBlank(uploadedEncoded));
+                        downloadLink.setEnabled(StringUtils.isNotBlank(uploadedEncoded));
 
                         target.add(uploadForm);
                     }
diff --git a/ext/saml2sp/client-console/src/main/java/org/apache/syncope/client/console/panels/ImportMetadata.java b/ext/saml2sp/client-console/src/main/java/org/apache/syncope/client/console/panels/ImportMetadata.java
index 7c4c902..373bfc2 100644
--- a/ext/saml2sp/client-console/src/main/java/org/apache/syncope/client/console/panels/ImportMetadata.java
+++ b/ext/saml2sp/client-console/src/main/java/org/apache/syncope/client/console/panels/ImportMetadata.java
@@ -20,11 +20,9 @@ package org.apache.syncope.client.console.panels;
 
 import de.agilecoders.wicket.extensions.markup.html.bootstrap.form.fileinput.BootstrapFileInputField;
 import de.agilecoders.wicket.extensions.markup.html.bootstrap.form.fileinput.FileInputConfig;
-import java.io.ByteArrayInputStream;
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Locale;
-import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.syncope.client.console.SyncopeConsoleSession;
 import org.apache.syncope.client.console.commons.Constants;
@@ -32,12 +30,10 @@ import org.apache.syncope.client.console.pages.BasePage;
 import org.apache.syncope.client.console.rest.SAML2IdPsRestClient;
 import org.apache.wicket.PageReference;
 import org.apache.wicket.ajax.AjaxRequestTarget;
-import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
 import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
 import org.apache.wicket.markup.html.WebMarkupContainer;
 import org.apache.wicket.markup.html.form.Form;
 import org.apache.wicket.markup.html.form.upload.FileUpload;
-import org.apache.wicket.model.Model;
 import org.apache.wicket.model.util.ListModel;
 
 public class ImportMetadata extends TogglePanel<Serializable> {
@@ -52,30 +48,15 @@ public class ImportMetadata extends TogglePanel<Serializable> {
         Form<?> form = new Form<>("metadataForm");
         addInnerObject(form);
 
-        final Model<byte[]> metadata = new Model<>();
-
+        final ListModel<FileUpload> fileUploadModel = new ListModel<>(new ArrayList<FileUpload>());
         FileInputConfig config = new FileInputConfig().
                 showUpload(false).showRemove(false).showPreview(false);
         String language = SyncopeConsoleSession.get().getLocale().getLanguage();
         if (!Locale.ENGLISH.getLanguage().equals(language)) {
             config.withLocale(language);
         }
-        final BootstrapFileInputField fileUpload =
-                new BootstrapFileInputField("fileUpload", new ListModel<>(new ArrayList<FileUpload>()), config);
-        fileUpload.setOutputMarkupId(true);
-        fileUpload.add(new AjaxFormSubmitBehavior(Constants.ON_CHANGE) {
-
-            private static final long serialVersionUID = 5538299138211283825L;
-
-            @Override
-            protected void onSubmit(final AjaxRequestTarget target) {
-                FileUpload uploadedFile = fileUpload.getFileUpload();
-                if (uploadedFile != null) {
-                    metadata.setObject(uploadedFile.getBytes());
-                }
-            }
-        });
-        form.add(fileUpload);
+        BootstrapFileInputField fileUpload = new BootstrapFileInputField("fileUpload", fileUploadModel, config);
+        form.add(fileUpload.setOutputMarkupId(true));
 
         form.add(new AjaxSubmitLink("doUpload", form) {
 
@@ -83,10 +64,10 @@ public class ImportMetadata extends TogglePanel<Serializable> {
 
             @Override
             protected void onSubmit(final AjaxRequestTarget target, final Form<?> form) {
-                if (ArrayUtils.isNotEmpty(metadata.getObject())) {
+                if (!fileUploadModel.getObject().isEmpty()) {
+                    FileUpload uploaded = fileUploadModel.getObject().get(0);
                     try {
-                        restClient.importIdPs(new ByteArrayInputStream(metadata.getObject()));
-                        metadata.setObject(null);
+                        restClient.importIdPs(uploaded.getInputStream());
 
                         SyncopeConsoleSession.get().info(getString(Constants.OPERATION_SUCCEEDED));
                         toggle(target, false);
@@ -107,5 +88,4 @@ public class ImportMetadata extends TogglePanel<Serializable> {
             }
         });
     }
-
 }