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.panels;
20  
21  import de.agilecoders.wicket.extensions.markup.html.bootstrap.form.fileinput.BootstrapFileInputField;
22  import de.agilecoders.wicket.extensions.markup.html.bootstrap.form.fileinput.FileInputConfig;
23  import java.io.Serializable;
24  import java.util.ArrayList;
25  import java.util.Locale;
26  import org.apache.syncope.client.console.SyncopeConsoleSession;
27  import org.apache.syncope.client.console.pages.BasePage;
28  import org.apache.syncope.client.console.rest.SAML2IdPsRestClient;
29  import org.apache.syncope.client.ui.commons.Constants;
30  import org.apache.wicket.PageReference;
31  import org.apache.wicket.ajax.AjaxRequestTarget;
32  import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
33  import org.apache.wicket.markup.html.WebMarkupContainer;
34  import org.apache.wicket.markup.html.form.Form;
35  import org.apache.wicket.markup.html.form.upload.FileUpload;
36  import org.apache.wicket.model.util.ListModel;
37  import org.apache.wicket.spring.injection.annot.SpringBean;
38  
39  public class ImportMetadata extends TogglePanel<Serializable> {
40  
41      private static final long serialVersionUID = 6959177759869415782L;
42  
43      @SpringBean
44      protected SAML2IdPsRestClient saml2IdPsRestClient;
45      
46      public ImportMetadata(final String id, final WebMarkupContainer container, final PageReference pageRef) {
47          super(id, pageRef);
48  
49          Form<?> form = new Form<>("metadataForm");
50          addInnerObject(form);
51  
52          FileInputConfig config = new FileInputConfig().
53                  showUpload(false).showRemove(false).showPreview(false).
54                  browseClass("btn btn-success").browseIcon("<i class=\"fas fa-folder-open\"></i> &nbsp;");
55          String language = SyncopeConsoleSession.get().getLocale().getLanguage();
56          if (!Locale.ENGLISH.getLanguage().equals(language)) {
57              config.withLocale(language);
58          }
59          BootstrapFileInputField fileUpload =
60                  new BootstrapFileInputField("fileUpload", new ListModel<>(new ArrayList<>()), config);
61          form.add(fileUpload.setOutputMarkupId(true));
62  
63          form.add(new AjaxSubmitLink("doUpload", form) {
64  
65              private static final long serialVersionUID = -7978723352517770644L;
66  
67              @Override
68              protected void onSubmit(final AjaxRequestTarget target) {
69                  FileUpload uploaded = fileUpload.getFileUpload();
70                  if (uploaded != null) {
71                      try {
72                          saml2IdPsRestClient.importIdPs(uploaded.getInputStream());
73  
74                          SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
75                          toggle(target, false);
76                          target.add(container);
77                      } catch (Exception e) {
78                          LOG.error("While importing SAML 2.0 IdP metadata", e);
79                          SyncopeConsoleSession.get().onException(e);
80                      }
81                      ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
82                  }
83              }
84  
85              @Override
86              protected void onError(final AjaxRequestTarget target) {
87                  ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
88              }
89          });
90      }
91  }