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      protected static final Logger LOG = LoggerFactory.getLogger(Details.class);
49  
50      private static final long serialVersionUID = -8995647450549098844L;
51  
52      @SpringBean
53      protected RealmRestClient realmRestClient;
54  
55      protected final PageReference pageRef;
56  
57      protected final FieldPanel<String> realm;
58  
59      public Details(
60              final AnyWrapper<T> wrapper,
61              final boolean templateMode,
62              final boolean includeStatusPanel,
63              final PageReference pageRef) {
64  
65          this.pageRef = pageRef;
66          final List<String> authRealms = SyncopeConsoleSession.get().getAuthRealms();
67          final T inner = wrapper.getInnerObject();
68          final Fragment fragment;
69  
70          if (templateMode) {
71              realm = new AjaxTextFieldPanel(
72                      "destinationRealm", "destinationRealm", new PropertyModel<>(inner, "realm"), false);
73              AjaxTextFieldPanel.class.cast(realm).enableJexlHelp();
74              fragment = new Fragment("realmsFragment", "realmsTemplateFragment", this);
75          } else {
76              boolean fullRealmsTree = SyncopeWebApplication.get().fullRealmsTree(realmRestClient);
77              AutoCompleteSettings settings = new AutoCompleteSettings();
78              settings.setShowCompleteListOnFocusGain(fullRealmsTree);
79              settings.setShowListOnEmptyInput(fullRealmsTree);
80  
81              realm = new AjaxSearchFieldPanel("destinationRealm", "destinationRealm",
82                      new PropertyModel<>(inner, "realm"), settings) {
83  
84                  private static final long serialVersionUID = -6390474600233486704L;
85  
86                  @Override
87                  protected Iterator<String> getChoices(final String input) {
88                      return (pageRef.getPage() instanceof Realms
89                              ? getRealmsFromLinks(Realms.class.cast(pageRef.getPage()).getRealmChoicePanel().getLinks())
90                              : (fullRealmsTree
91                                      ? realmRestClient.search(RealmsUtils.buildRootQuery())
92                                      : realmRestClient.search(RealmsUtils.buildKeywordQuery(input))).getResult()).
93                              stream().filter(realm -> authRealms.stream().anyMatch(
94                              authRealm -> realm.getFullPath().startsWith(authRealm))).
95                              map(RealmTO::getFullPath).collect(Collectors.toList()).iterator();
96                  }
97              };
98  
99              fragment = new Fragment("realmsFragment", "realmsSearchFragment", this);
100         }
101         fragment.addOrReplace(realm);
102         addOrReplace(fragment);
103         add(getGeneralStatusInformation("generalStatusInformation", inner).
104                 setEnabled(includeStatusPanel).setVisible(includeStatusPanel).setRenderBodyOnly(true));
105 
106     }
107 
108     public Details<T> disableRealmSpecification() {
109         this.realm.setReadOnly(true);
110         return this;
111     }
112 
113     protected AnnotatedBeanPanel getGeneralStatusInformation(final String id, final T anyTO) {
114         return new AnnotatedBeanPanel(id, anyTO);
115     }
116 
117     private static List<RealmTO> getRealmsFromLinks(final List<AbstractLink> realmLinks) {
118         return realmLinks.stream().
119                 map(Component::getDefaultModelObject).
120                 filter(RealmTO.class::isInstance).
121                 map(RealmTO.class::cast).
122                 collect(Collectors.toList());
123     }
124 }