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 java.io.Serializable;
22  import java.util.List;
23  import java.util.stream.Collectors;
24  import org.apache.syncope.client.console.SyncopeConsoleSession;
25  import org.apache.syncope.client.console.pages.BasePage;
26  import org.apache.syncope.client.console.rest.WAConfigRestClient;
27  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
28  import org.apache.syncope.client.ui.commons.Constants;
29  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoicePanel;
30  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxPalettePanel;
31  import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
32  import org.apache.syncope.common.rest.api.service.wa.WAConfigService;
33  import org.apache.wicket.PageReference;
34  import org.apache.wicket.ajax.AjaxRequestTarget;
35  import org.apache.wicket.model.Model;
36  import org.apache.wicket.model.util.ListModel;
37  import org.apache.wicket.spring.injection.annot.SpringBean;
38  
39  public class WAPushModalPanel extends AbstractModalPanel<Serializable> {
40  
41      private static final long serialVersionUID = -8589310598889871801L;
42  
43      @SpringBean
44      protected WAConfigRestClient waConfigRestClient;
45  
46      protected final Model<WAConfigService.PushSubject> subjectModel = new Model<>(WAConfigService.PushSubject.conf);
47  
48      protected final ListModel<String> servicesModel = new ListModel<>();
49  
50      public WAPushModalPanel(
51              final BaseModal<Serializable> modal,
52              final List<NetworkService> instances,
53              final PageReference pageRef) {
54  
55          super(modal, pageRef);
56  
57          List<String> addresses = instances.stream().
58                  map(NetworkService::getAddress).distinct().sorted().collect(Collectors.toList());
59          servicesModel.setObject(addresses);
60  
61          add(new AjaxPalettePanel.Builder<String>().setName("services").setAllowMoveAll(true).build(
62                  "services",
63                  servicesModel,
64                  new ListModel<>(addresses)).addRequiredLabel());
65  
66          add(new AjaxDropDownChoicePanel<>(
67                  "subject", getString("subject"), subjectModel).
68                  setChoices(List.of(WAConfigService.PushSubject.values())).
69                  setChoiceRenderer(s -> getString(s.name(), Model.of(), s.name())).setNullValid(false));
70      }
71  
72      @Override
73      public void onSubmit(final AjaxRequestTarget target) {
74          try {
75              waConfigRestClient.push(subjectModel.getObject(), servicesModel.getObject());
76  
77              SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
78              this.modal.close(target);
79          } catch (Exception e) {
80              LOG.error("While pushing to WA", e);
81              SyncopeConsoleSession.get().onException(e);
82          }
83          ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
84      }
85  }