Repository: syncope
Updated Branches:
  refs/heads/2_0_X 632c5c606 -> 7bd67e918


[SYNCOPE-1091] fixed binary attribute content download


Project: http://git-wip-us.apache.org/repos/asf/syncope/repo
Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/7bd67e91
Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/7bd67e91
Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/7bd67e91

Branch: refs/heads/2_0_X
Commit: 7bd67e91821db525204ebe8b9e4b3948e91dd044
Parents: 632c5c6
Author: Andrea Patricelli <[email protected]>
Authored: Wed May 24 15:39:46 2017 +0200
Committer: Andrea Patricelli <[email protected]>
Committed: Wed May 24 15:39:46 2017 +0200

----------------------------------------------------------------------
 .../wicket/markup/html/form/AjaxDownload.java   | 62 ++++++++++++++++++++
 .../markup/html/form/BinaryFieldPanel.java      | 31 ++++++----
 2 files changed, 82 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/syncope/blob/7bd67e91/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/AjaxDownload.java
----------------------------------------------------------------------
diff --git 
a/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/AjaxDownload.java
 
b/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/AjaxDownload.java
new file mode 100644
index 0000000..20a1d25
--- /dev/null
+++ 
b/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/AjaxDownload.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.client.console.wicket.markup.html.form;
+
+import org.apache.syncope.client.console.commons.HttpResourceStream;
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.behavior.AbstractAjaxBehavior;
+import org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler;
+import org.apache.wicket.request.resource.ContentDisposition;
+
+public abstract class AjaxDownload extends AbstractAjaxBehavior {
+
+    private static final long serialVersionUID = 7203445884857810583L;
+
+    private final String name;
+
+    private final boolean addAntiCache;
+
+    public AjaxDownload(final String name, final boolean addAntiCache) {
+        super();
+        this.name = name;
+        this.addAntiCache = addAntiCache;
+    }
+
+    public void initiate(final AjaxRequestTarget target) {
+
+        String url = getCallbackUrl().toString();
+        if (addAntiCache) {
+            url = url + (url.contains("?") ? "&" : "?");
+            url = url + "antiCache=" + System.currentTimeMillis();
+        }
+        target.appendJavaScript("setTimeout(\"window.location.href='" + url + 
"'\", 100);");
+    }
+
+    @Override
+    public void onRequest() {
+        HttpResourceStream stream = getResourceStream();
+        ResourceStreamRequestHandler handler = new 
ResourceStreamRequestHandler(stream);
+        handler.setFileName(stream.getFilename() == null ? name : 
stream.getFilename());
+        handler.setContentDisposition(ContentDisposition.ATTACHMENT);
+        
getComponent().getRequestCycle().scheduleRequestHandlerAfterCurrent(handler);
+    }
+
+    protected abstract HttpResourceStream getResourceStream();
+
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/7bd67e91/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/BinaryFieldPanel.java
----------------------------------------------------------------------
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 c5504f5..f60ac2e 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
@@ -32,8 +32,8 @@ import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.syncope.client.console.SyncopeConsoleSession;
-import org.apache.syncope.client.console.commons.HttpResourceStream;
 import org.apache.syncope.client.console.commons.Constants;
+import org.apache.syncope.client.console.commons.HttpResourceStream;
 import org.apache.syncope.client.console.commons.PreviewUtils;
 import 
org.apache.syncope.client.console.wicket.markup.html.form.preview.AbstractBinaryPreviewer;
 import org.apache.wicket.Component;
@@ -53,15 +53,13 @@ import org.apache.wicket.markup.html.panel.Fragment;
 import org.apache.wicket.model.IModel;
 import org.apache.wicket.model.Model;
 import org.apache.wicket.model.util.ListModel;
-import org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler;
-import org.apache.wicket.request.resource.ContentDisposition;
 import org.apache.wicket.util.crypt.Base64;
 import org.apache.wicket.util.lang.Bytes;
 
 public class BinaryFieldPanel extends FieldPanel<String> {
 
     private static final long serialVersionUID = 6264462604183088931L;
-    
+
     private static final PreviewUtils PREVIEW_UTILS = 
PreviewUtils.getInstance();
 
     private final String mimeType;
@@ -76,6 +74,10 @@ public class BinaryFieldPanel extends FieldPanel<String> {
 
     private final BootstrapFileInputField fileUpload;
 
+    private final AjaxDownload fileDownload;
+
+    private final transient PreviewUtils previewUtils = 
PreviewUtils.getInstance();
+
     private final AbstractBinaryPreviewer previewer;
 
     public BinaryFieldPanel(final String id, final String name, final 
IModel<String> model, final String mimeType) {
@@ -125,6 +127,19 @@ public class BinaryFieldPanel extends FieldPanel<String> {
 
         uploadForm.add(new Label("preview", StringUtils.isBlank(mimeType) ? 
StringUtils.EMPTY : "(" + mimeType + ")"));
 
+        fileDownload = new AjaxDownload(name, true) {
+
+            private static final long serialVersionUID = 7203445884857810583L;
+
+            @Override
+            protected HttpResourceStream getResourceStream() {
+                return new HttpResourceStream(buildResponse());
+            }
+
+        };
+
+        add(fileDownload);
+
         downloadLink = new AjaxLink<Void>("downloadLink") {
 
             private static final long serialVersionUID = -4331619903296515985L;
@@ -132,13 +147,7 @@ public class BinaryFieldPanel extends FieldPanel<String> {
             @Override
             public void onClick(final AjaxRequestTarget target) {
                 try {
-                    HttpResourceStream stream = new 
HttpResourceStream(buildResponse());
-
-                    ResourceStreamRequestHandler rsrh = new 
ResourceStreamRequestHandler(stream);
-                    rsrh.setFileName(stream.getFilename() == null ? name : 
stream.getFilename());
-                    rsrh.setContentDisposition(ContentDisposition.ATTACHMENT);
-
-                    getRequestCycle().scheduleRequestHandlerAfterCurrent(rsrh);
+                    fileDownload.initiate(target);
                 } catch (Exception e) {
                     SyncopeConsoleSession.get().error(
                             StringUtils.isBlank(e.getMessage()) ? 
e.getClass().getName() : e.getMessage());

Reply via email to