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.net.URI;
23  import java.util.List;
24  import java.util.Optional;
25  import org.apache.syncope.client.console.rest.SRARouteRestClient;
26  import org.apache.syncope.client.console.wizards.BaseAjaxWizardBuilder;
27  import org.apache.syncope.client.ui.commons.Constants;
28  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxCheckBoxPanel;
29  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoicePanel;
30  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxSpinnerFieldPanel;
31  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTextFieldPanel;
32  import org.apache.syncope.common.lib.to.SRARouteTO;
33  import org.apache.syncope.common.lib.types.SRARouteType;
34  import org.apache.wicket.PageReference;
35  import org.apache.wicket.extensions.wizard.WizardModel;
36  import org.apache.wicket.extensions.wizard.WizardStep;
37  import org.apache.wicket.model.IModel;
38  import org.apache.wicket.model.Model;
39  import org.apache.wicket.model.PropertyModel;
40  import org.apache.wicket.model.ResourceModel;
41  import org.apache.wicket.model.util.ListModel;
42  import org.apache.wicket.validation.validator.UrlValidator;
43  
44  public class SRARouteWizardBuilder extends BaseAjaxWizardBuilder<SRARouteTO> {
45  
46      private static final long serialVersionUID = 2060352959114706419L;
47  
48      protected final SRARouteRestClient sraRouteRestClient;
49  
50      public SRARouteWizardBuilder(
51              final SRARouteTO route,
52              final SRARouteRestClient sraRouteRestClient,
53              final PageReference pageRef) {
54  
55          super(route, pageRef);
56          this.sraRouteRestClient = sraRouteRestClient;
57      }
58  
59      @Override
60      protected Serializable onApplyInternal(final SRARouteTO modelObject) {
61          if (modelObject.getKey() == null) {
62              sraRouteRestClient.create(modelObject);
63          } else {
64              sraRouteRestClient.update(modelObject);
65          }
66          return modelObject;
67      }
68  
69      @Override
70      protected WizardModel buildModelSteps(final SRARouteTO modelObject, final WizardModel wizardModel) {
71          wizardModel.add(new Profile(modelObject));
72          wizardModel.add(new Predicates(modelObject));
73          wizardModel.add(new Filters(modelObject));
74          return wizardModel;
75      }
76  
77      public static class Profile extends WizardStep {
78  
79          private static final long serialVersionUID = 8610155719550948702L;
80  
81          public Profile(final SRARouteTO route) {
82              add(new AjaxTextFieldPanel(
83                      Constants.NAME_FIELD_NAME, Constants.NAME_FIELD_NAME,
84                      new PropertyModel<>(route, Constants.NAME_FIELD_NAME), false).
85                      addRequiredLabel().setEnabled(true));
86  
87              AjaxTextFieldPanel target = new AjaxTextFieldPanel("target", "target", new IModel<>() {
88  
89                  private static final long serialVersionUID = 1015030402166681242L;
90  
91                  @Override
92                  public String getObject() {
93                      return Optional.ofNullable(route.getTarget()).map(URI::toASCIIString).orElse(null);
94                  }
95  
96                  @Override
97                  public void setObject(final String object) {
98                      route.setTarget(Optional.ofNullable(object).map(URI::create).orElse(null));
99                  }
100             }, false);
101             target.addRequiredLabel().setEnabled(true);
102             target.getField().add(new UrlValidator(new String[] { "http", "https" }));
103             add(target);
104 
105             AjaxTextFieldPanel error = new AjaxTextFieldPanel("error", "error", new IModel<>() {
106 
107                 private static final long serialVersionUID = 1015030402166681242L;
108 
109                 @Override
110                 public String getObject() {
111                     return route.getError() == null ? null : route.getError().toASCIIString();
112                 }
113 
114                 @Override
115                 public void setObject(final String object) {
116                     if (object == null) {
117                         route.setError(null);
118                     } else {
119                         route.setError(URI.create(object));
120                     }
121                 }
122             }, false);
123             error.getField().add(new UrlValidator(new String[] { "http", "https" }));
124             add(error);
125 
126             AjaxDropDownChoicePanel<SRARouteType> type =
127                     new AjaxDropDownChoicePanel<>("type", "type", new PropertyModel<>(route, "type"));
128             type.setChoices(List.of(SRARouteType.values()));
129             type.addRequiredLabel().setEnabled(true);
130             add(type);
131 
132             add(new AjaxCheckBoxPanel("logout", "logout", new PropertyModel<>(route, "logout")));
133 
134             AjaxTextFieldPanel postLogout = new AjaxTextFieldPanel("postLogout", "postLogout", new IModel<>() {
135 
136                 private static final long serialVersionUID = 1015030402166681242L;
137 
138                 @Override
139                 public String getObject() {
140                     return route.getPostLogout() == null ? null : route.getPostLogout().toASCIIString();
141                 }
142 
143                 @Override
144                 public void setObject(final String object) {
145                     if (object == null) {
146                         route.setPostLogout(null);
147                     } else {
148                         route.setPostLogout(URI.create(object));
149                     }
150                 }
151             }, false);
152             postLogout.getField().add(new UrlValidator(new String[] { "http", "https" }));
153             add(postLogout);
154 
155             add(new AjaxCheckBoxPanel("csrf", "csrf", new PropertyModel<>(route, "csrf")));
156 
157             add(new AjaxSpinnerFieldPanel.Builder<Integer>().min(0).build(
158                     "order", "order", Integer.class, new PropertyModel<>(route, "order")).
159                     setRequired(true));
160         }
161     }
162 
163     private static class Predicates extends WizardStep {
164 
165         private static final long serialVersionUID = 5934389493874714599L;
166 
167         Predicates(final SRARouteTO route) {
168             super(new ResourceModel("predicates"), Model.of());
169             add(new SRARoutePredicatePanel("predicates", new ListModel<>(route.getPredicates())));
170         }
171     }
172 
173     private static class Filters extends WizardStep {
174 
175         private static final long serialVersionUID = -6552124285142294023L;
176 
177         Filters(final SRARouteTO route) {
178             super(new ResourceModel("filters"), Model.of());
179             add(new SRARouteFilterPanel("filters", new ListModel<>(route.getFilters())));
180         }
181     }
182 }