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.wizards.any;
20  
21  import java.util.Iterator;
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.SyncopeWebApplication;
26  import org.apache.syncope.client.console.commons.RealmsUtils;
27  import org.apache.syncope.client.console.pages.Realms;
28  import org.apache.syncope.client.console.rest.RealmRestClient;
29  import org.apache.syncope.client.console.wicket.markup.html.form.AjaxSearchFieldPanel;
30  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTextFieldPanel;
31  import org.apache.syncope.client.ui.commons.markup.html.form.FieldPanel;
32  import org.apache.syncope.client.ui.commons.wizards.any.AnyWrapper;
33  import org.apache.syncope.common.lib.to.AnyTO;
34  import org.apache.syncope.common.lib.to.RealmTO;
35  import org.apache.wicket.Component;
36  import org.apache.wicket.PageReference;
37  import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteSettings;
38  import org.apache.wicket.extensions.wizard.WizardStep;
39  import org.apache.wicket.markup.html.link.AbstractLink;
40  import org.apache.wicket.markup.html.panel.Fragment;
41  import org.apache.wicket.model.PropertyModel;
42  import org.apache.wicket.spring.injection.annot.SpringBean;
43  import org.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  
46  public class Details<T extends AnyTO> extends WizardStep {
47  
48      private static final long serialVersionUID = -8995647450549098844L;
49  
50      protected static final Logger LOG = LoggerFactory.getLogger(Details.class);
51  
52      protected static List<RealmTO> getRealmsFromLinks(final List<AbstractLink> realmLinks) {
53          return realmLinks.stream().
54                  map(Component::getDefaultModelObject).
55                  filter(RealmTO.class::isInstance).
56                  map(RealmTO.class::cast).
57                  collect(Collectors.toList());
58      }
59  
60      @SpringBean
61      protected RealmRestClient realmRestClient;
62  
63      protected final PageReference pageRef;
64  
65      protected final FieldPanel<String> realm;
66  
67      public Details(
68              final AnyWrapper<T> wrapper,
69              final boolean templateMode,
70              final boolean includeStatusPanel,
71              final PageReference pageRef) {
72  
73          this.pageRef = pageRef;
74          final List<String> authRealms = SyncopeConsoleSession.get().getAuthRealms();
75          final T inner = wrapper.getInnerObject();
76          final Fragment fragment;
77  
78          if (templateMode) {
79              realm = new AjaxTextFieldPanel(
80                      "destinationRealm", "destinationRealm", new PropertyModel<>(inner, "realm"), false);
81              AjaxTextFieldPanel.class.cast(realm).enableJexlHelp();
82              fragment = new Fragment("realmsFragment", "realmsTemplateFragment", this);
83          } else {
84              boolean fullRealmsTree = SyncopeWebApplication.get().fullRealmsTree(realmRestClient);
85              AutoCompleteSettings settings = new AutoCompleteSettings();
86              settings.setShowCompleteListOnFocusGain(fullRealmsTree);
87              settings.setShowListOnEmptyInput(fullRealmsTree);
88  
89              realm = new AjaxSearchFieldPanel("destinationRealm", "destinationRealm",
90                      new PropertyModel<>(inner, "realm"), settings) {
91  
92                  private static final long serialVersionUID = -6390474600233486704L;
93  
94                  @Override
95                  protected Iterator<String> getChoices(final String input) {
96                      return (pageRef.getPage() instanceof Realms
97                              ? getRealmsFromLinks(Realms.class.cast(pageRef.getPage()).getRealmChoicePanel().getLinks())
98                              : (fullRealmsTree
99                                      ? realmRestClient.search(RealmsUtils.buildRootQuery())
100                                     : realmRestClient.search(RealmsUtils.buildKeywordQuery(input))).getResult()).
101                             stream().filter(realm -> authRealms.stream().anyMatch(
102                             authRealm -> realm.getFullPath().startsWith(authRealm))).
103                             map(RealmTO::getFullPath).collect(Collectors.toList()).iterator();
104                 }
105             };
106 
107             fragment = new Fragment("realmsFragment", "realmsSearchFragment", this);
108         }
109         fragment.addOrReplace(realm);
110         addOrReplace(fragment);
111         add(getGeneralStatusInformation("generalStatusInformation", inner).
112                 setEnabled(includeStatusPanel).setVisible(includeStatusPanel).setRenderBodyOnly(true));
113 
114     }
115 
116     public Details<T> disableRealmSpecification() {
117         this.realm.setReadOnly(true);
118         return this;
119     }
120 
121     protected AnnotatedBeanPanel getGeneralStatusInformation(final String id, final T anyTO) {
122         return new AnnotatedBeanPanel(id, anyTO);
123     }
124 }