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 com.nimbusds.jwt.SignedJWT;
22  import de.agilecoders.wicket.core.markup.html.bootstrap.dialog.Modal;
23  import java.text.ParseException;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Iterator;
27  import java.util.List;
28  import org.apache.commons.lang3.StringUtils;
29  import org.apache.syncope.client.console.SyncopeConsoleSession;
30  import org.apache.syncope.client.console.commons.DirectoryDataProvider;
31  import org.apache.syncope.client.console.commons.IdRepoConstants;
32  import org.apache.syncope.client.console.pages.BasePage;
33  import org.apache.syncope.client.console.panels.AccessTokenDirectoryPanel.AccessTokenDataProvider;
34  import org.apache.syncope.client.console.rest.AccessTokenRestClient;
35  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.DatePropertyColumn;
36  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.KeyPropertyColumn;
37  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
38  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
39  import org.apache.syncope.client.console.wizards.WizardMgtPanel;
40  import org.apache.syncope.client.ui.commons.Constants;
41  import org.apache.syncope.common.lib.SyncopeClientException;
42  import org.apache.syncope.common.lib.to.AccessTokenTO;
43  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
44  import org.apache.wicket.PageReference;
45  import org.apache.wicket.ajax.AjaxRequestTarget;
46  import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
47  import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
48  import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
49  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
50  import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
51  import org.apache.wicket.markup.html.basic.Label;
52  import org.apache.wicket.markup.repeater.Item;
53  import org.apache.wicket.model.CompoundPropertyModel;
54  import org.apache.wicket.model.IModel;
55  import org.apache.wicket.model.ResourceModel;
56  import org.apache.wicket.model.StringResourceModel;
57  
58  public class AccessTokenDirectoryPanel
59          extends DirectoryPanel<AccessTokenTO, AccessTokenTO, AccessTokenDataProvider, AccessTokenRestClient> {
60  
61      private static final long serialVersionUID = -6903586269155682961L;
62  
63      protected AccessTokenDirectoryPanel(final String id, final Builder builder) {
64          super(id, builder);
65  
66          setShowResultPanel(true);
67  
68          modal.size(Modal.Size.Large);
69          initResultTable();
70      }
71  
72      @Override
73      protected AccessTokenDataProvider dataProvider() {
74          return new AccessTokenDataProvider(rows);
75      }
76  
77      @Override
78      protected String paginatorRowsKey() {
79          return IdRepoConstants.PREF_ACCESS_TOKEN_PAGINATOR_ROWS;
80      }
81  
82      @Override
83      protected List<IColumn<AccessTokenTO, String>> getColumns() {
84          List<IColumn<AccessTokenTO, String>> columns = new ArrayList<>();
85  
86          columns.add(new KeyPropertyColumn<>(
87                  new StringResourceModel(Constants.KEY_FIELD_NAME, this),
88                  Constants.KEY_FIELD_NAME,
89                  Constants.KEY_FIELD_NAME));
90  
91          columns.add(new PropertyColumn<>(new ResourceModel("owner"), "owner", "owner"));
92  
93          columns.add(new AbstractColumn<>(new ResourceModel("issuedAt", "")) {
94  
95              private static final long serialVersionUID = -1822504503325964706L;
96  
97              @Override
98              public void populateItem(
99                      final Item<ICellPopulator<AccessTokenTO>> cellItem,
100                     final String componentId,
101                     final IModel<AccessTokenTO> model) {
102 
103                 try {
104                     SignedJWT jwt = SignedJWT.parse(model.getObject().getBody());
105                     cellItem.add(new Label(
106                             componentId,
107                             SyncopeConsoleSession.get().getDateFormat().format(jwt.getJWTClaimsSet().getIssueTime())));
108                 } catch (ParseException e) {
109                     LOG.error("Could not parse JWT {}", model.getObject().getBody(), e);
110                     cellItem.add(new Label(componentId, StringUtils.EMPTY));
111                 }
112             }
113         });
114 
115         columns.add(new DatePropertyColumn<>(new ResourceModel("expirationTime"), "expirationTime", "expirationTime"));
116 
117         return columns;
118     }
119 
120     @Override
121     public ActionsPanel<AccessTokenTO> getActions(final IModel<AccessTokenTO> model) {
122         ActionsPanel<AccessTokenTO> panel = super.getActions(model);
123 
124         panel.add(new ActionLink<>() {
125 
126             private static final long serialVersionUID = -7978723352517770644L;
127 
128             @Override
129             public void onClick(final AjaxRequestTarget target, final AccessTokenTO ignore) {
130                 try {
131                     restClient.delete(model.getObject().getKey());
132 
133                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
134                     target.add(container);
135                 } catch (SyncopeClientException e) {
136                     LOG.error("While deleting object {}", model.getObject().getKey(), e);
137                     SyncopeConsoleSession.get().onException(e);
138                 }
139                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
140             }
141         }, ActionLink.ActionType.DELETE, IdRepoEntitlement.ACCESS_TOKEN_DELETE, true);
142 
143         return panel;
144     }
145 
146     @Override
147     protected Collection<ActionLink.ActionType> getBatches() {
148         return List.of();
149     }
150 
151     protected class AccessTokenDataProvider extends DirectoryDataProvider<AccessTokenTO> {
152 
153         private static final long serialVersionUID = 6267494272884913376L;
154 
155         public AccessTokenDataProvider(final int paginatorRows) {
156             super(paginatorRows);
157 
158             setSort("owner", SortOrder.ASCENDING);
159         }
160 
161         @Override
162         public Iterator<AccessTokenTO> iterator(final long first, final long count) {
163             int page = ((int) first / paginatorRows);
164             return restClient.list((page < 0 ? 0 : page) + 1, paginatorRows, getSort()).iterator();
165         }
166 
167         @Override
168         public long size() {
169             return restClient.count();
170         }
171 
172         @Override
173         public IModel<AccessTokenTO> model(final AccessTokenTO object) {
174             return new CompoundPropertyModel<>(object);
175         }
176     }
177 
178     public static class Builder
179             extends DirectoryPanel.Builder<AccessTokenTO, AccessTokenTO, AccessTokenRestClient> {
180 
181         private static final long serialVersionUID = 5088962796986706805L;
182 
183         public Builder(final AccessTokenRestClient restClient, final PageReference pageRef) {
184             super(restClient, pageRef);
185         }
186 
187         @Override
188         protected WizardMgtPanel<AccessTokenTO> newInstance(final String id, final boolean wizardInModal) {
189             return new AccessTokenDirectoryPanel(id, this);
190         }
191     }
192 }