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.core.markup.html.bootstrap.dialog.Modal;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Iterator;
25  import java.util.List;
26  import org.apache.syncope.client.console.commons.AMConstants;
27  import org.apache.syncope.client.console.commons.DirectoryDataProvider;
28  import org.apache.syncope.client.console.commons.SortableDataProviderComparator;
29  import org.apache.syncope.client.console.panels.SAML2IdPEntityDirectoryPanel.SAML2IdPEntityProvider;
30  import org.apache.syncope.client.console.rest.SAML2IdPEntityRestClient;
31  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
32  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
33  import org.apache.syncope.client.console.wizards.SAML2IdPEntityWizardBuilder;
34  import org.apache.syncope.client.ui.commons.Constants;
35  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
36  import org.apache.syncope.common.lib.to.SAML2IdPEntityTO;
37  import org.apache.syncope.common.lib.types.AMEntitlement;
38  import org.apache.wicket.PageReference;
39  import org.apache.wicket.ajax.AjaxRequestTarget;
40  import org.apache.wicket.event.Broadcast;
41  import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
42  import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
43  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
44  import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
45  import org.apache.wicket.markup.ComponentTag;
46  import org.apache.wicket.markup.html.link.ExternalLink;
47  import org.apache.wicket.markup.repeater.Item;
48  import org.apache.wicket.model.CompoundPropertyModel;
49  import org.apache.wicket.model.IModel;
50  import org.apache.wicket.model.Model;
51  import org.apache.wicket.model.StringResourceModel;
52  
53  public class SAML2IdPEntityDirectoryPanel extends DirectoryPanel<
54          SAML2IdPEntityTO, SAML2IdPEntityTO, SAML2IdPEntityProvider, SAML2IdPEntityRestClient> {
55  
56      private static final long serialVersionUID = -6535332920023200166L;
57  
58      private final String metadataURL;
59  
60      public SAML2IdPEntityDirectoryPanel(
61              final String id,
62              final SAML2IdPEntityRestClient restClient,
63              final String waPrefix,
64              final PageReference pageRef) {
65  
66          super(id, restClient, pageRef);
67          this.metadataURL = waPrefix + "/idp/metadata";
68  
69          disableCheckBoxes();
70  
71          modal.size(Modal.Size.Large);
72          modal.addSubmitButton();
73  
74          modal.setWindowClosedCallback(target -> {
75              updateResultTable(target);
76              modal.show(false);
77          });
78  
79          addNewItemPanelBuilder(new SAML2IdPEntityWizardBuilder(new SAML2IdPEntityTO(), restClient, pageRef), false);
80  
81          initResultTable();
82      }
83  
84      @Override
85      protected List<IColumn<SAML2IdPEntityTO, String>> getColumns() {
86          List<IColumn<SAML2IdPEntityTO, String>> columns = new ArrayList<>();
87  
88          columns.add(new PropertyColumn<>(
89                  new StringResourceModel(Constants.KEY_FIELD_NAME, this),
90                  Constants.KEY_FIELD_NAME, Constants.KEY_FIELD_NAME));
91          columns.add(new AbstractColumn<>(Model.of("URL")) {
92  
93              private static final long serialVersionUID = -7226955670801277153L;
94  
95              @Override
96              public void populateItem(
97                      final Item<ICellPopulator<SAML2IdPEntityTO>> cellItem,
98                      final String componentId,
99                      final IModel<SAML2IdPEntityTO> rowModel) {
100 
101                 cellItem.add(new ExternalLink(
102                         componentId,
103                         Model.of(metadataURL),
104                         Model.of(metadataURL)) {
105 
106                     private static final long serialVersionUID = -1919646533527005367L;
107 
108                     @Override
109                     protected void onComponentTag(final ComponentTag tag) {
110                         super.onComponentTag(tag);
111 
112                         tag.setName("a");
113                         if (metadataURL.startsWith("http")) {
114                             tag.put("href", getDefaultModelObject().toString());
115                             tag.put("target", "_blank");
116                         }
117                     }
118                 });
119             }
120         });
121 
122         return columns;
123     }
124 
125     @Override
126     protected ActionsPanel<SAML2IdPEntityTO> getActions(final IModel<SAML2IdPEntityTO> model) {
127         ActionsPanel<SAML2IdPEntityTO> panel = super.getActions(model);
128 
129         panel.add(new ActionLink<>() {
130 
131             private static final long serialVersionUID = -3722207913631435501L;
132 
133             @Override
134             public void onClick(final AjaxRequestTarget target, final SAML2IdPEntityTO ignore) {
135                 send(SAML2IdPEntityDirectoryPanel.this, Broadcast.EXACT,
136                         new AjaxWizard.EditItemActionEvent<>(
137                                 restClient.get(model.getObject().getKey()), target));
138             }
139         }, ActionLink.ActionType.EDIT, AMEntitlement.SAML2_IDP_ENTITY_SET);
140 
141         return panel;
142     }
143 
144     @Override
145     protected Collection<ActionLink.ActionType> getBatches() {
146         return List.of();
147     }
148 
149     @Override
150     protected SAML2IdPEntityProvider dataProvider() {
151         return new SAML2IdPEntityProvider(rows);
152     }
153 
154     @Override
155     protected String paginatorRowsKey() {
156         return AMConstants.PREF_SAML2_IDP_ENTITY_PAGINATOR_ROWS;
157     }
158 
159     protected final class SAML2IdPEntityProvider extends DirectoryDataProvider<SAML2IdPEntityTO> {
160 
161         private static final long serialVersionUID = 5282134321828253058L;
162 
163         private final SortableDataProviderComparator<SAML2IdPEntityTO> comparator;
164 
165         public SAML2IdPEntityProvider(final int paginatorRows) {
166             super(paginatorRows);
167             comparator = new SortableDataProviderComparator<>(this);
168         }
169 
170         @Override
171         public Iterator<? extends SAML2IdPEntityTO> iterator(final long first, final long count) {
172             List<SAML2IdPEntityTO> idps = restClient.list();
173             idps.sort(comparator);
174             return idps.subList((int) first, (int) first + (int) count).iterator();
175         }
176 
177         @Override
178         public long size() {
179             return restClient.list().size();
180         }
181 
182         @Override
183         public IModel<SAML2IdPEntityTO> model(final SAML2IdPEntityTO metadata) {
184             return new CompoundPropertyModel<>(metadata);
185         }
186     }
187 }