View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.console.wicket.ajax.form;
20  
21  import java.time.Duration;
22  import org.apache.syncope.client.ui.commons.HttpResourceStream;
23  import org.apache.syncope.client.ui.commons.rest.ResponseHolder;
24  import org.apache.wicket.ajax.AjaxRequestTarget;
25  import org.apache.wicket.behavior.AbstractAjaxBehavior;
26  import org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  public class AjaxDownloadBehavior extends AbstractAjaxBehavior {
31  
32      private static final long serialVersionUID = 6833760760338614245L;
33  
34      protected static final Logger LOG = LoggerFactory.getLogger(AjaxDownloadBehavior.class);
35  
36      protected ResponseHolder responseHolder;
37  
38      public void setResponse(final ResponseHolder response) {
39          this.responseHolder = response;
40      }
41  
42      /**
43       * Call this method to initiate the download.
44       *
45       * @param target request target.
46       */
47      public void initiate(final AjaxRequestTarget target) {
48          CharSequence url = getCallbackUrl();
49          target.appendJavaScript("window.location.href='" + url + "'");
50      }
51  
52      protected HttpResourceStream getResourceStream() {
53          HttpResourceStream stream = null;
54  
55          if (responseHolder != null) {
56              stream = new HttpResourceStream(responseHolder);
57              responseHolder = null;
58          }
59  
60          return stream;
61      }
62  
63      @Override
64      public void onRequest() {
65          try {
66              HttpResourceStream resourceStream = getResourceStream();
67              if (resourceStream != null) {
68                  getComponent().getRequestCycle().scheduleRequestHandlerAfterCurrent(
69                          new ResourceStreamRequestHandler(
70                                  resourceStream, resourceStream.getFilename()).setCacheDuration(Duration.ZERO));
71              }
72          } catch (Exception e) {
73              // cannot be notifies beacause the use of scheduleRequestHandlerAfterCurrent
74              LOG.error("Error downloading file", e);
75          }
76      }
77  }