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;
20  
21  import java.io.Serializable;
22  import java.nio.charset.StandardCharsets;
23  import java.util.Base64;
24  import javax.ws.rs.core.MediaType;
25  import org.apache.syncope.client.console.rest.SAML2SPEntityRestClient;
26  import org.apache.syncope.client.console.wicket.markup.html.form.BinaryFieldPanel;
27  import org.apache.syncope.client.ui.commons.Constants;
28  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTextFieldPanel;
29  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
30  import org.apache.syncope.common.lib.to.SAML2SPEntityTO;
31  import org.apache.wicket.PageReference;
32  import org.apache.wicket.extensions.wizard.WizardModel;
33  import org.apache.wicket.extensions.wizard.WizardStep;
34  import org.apache.wicket.model.PropertyModel;
35  
36  public class SAML2SPEntityWizardBuilder extends SAML2EntityWizardBuilder<SAML2SPEntityTO> {
37  
38      private static final long serialVersionUID = 2400472385439304277L;
39  
40      protected final SAML2SPEntityRestClient saml2SPEntityRestClient;
41  
42      public SAML2SPEntityWizardBuilder(
43              final SAML2SPEntityTO defaultItem,
44              final SAML2SPEntityRestClient saml2SPEntityRestClient,
45              final PageReference pageRef) {
46  
47          super(defaultItem, pageRef);
48          this.saml2SPEntityRestClient = saml2SPEntityRestClient;
49      }
50  
51      @Override
52      protected Serializable onApplyInternal(final SAML2SPEntityTO modelObject) {
53          if (modelObject.getMetadata() != null) {
54              modelObject.setMetadata(Base64.getEncoder().encodeToString(
55                      modelObject.getMetadata().getBytes(StandardCharsets.UTF_8)));
56          }
57          saml2SPEntityRestClient.set(modelObject);
58          return modelObject;
59      }
60  
61      @Override
62      protected WizardModel buildModelSteps(final SAML2SPEntityTO modelObject, final WizardModel wizardModel) {
63          if (modelObject.getMetadata() != null) {
64              modelObject.setMetadata(new String(Base64.getDecoder().decode(
65                      modelObject.getMetadata()), StandardCharsets.UTF_8));
66          }
67  
68          wizardModel.add(new Profile(modelObject, mode == AjaxWizard.Mode.CREATE));
69          wizardModel.add(new Metadata(modelObject, pageRef));
70          wizardModel.add(new BinaryPem(modelObject, "keystore", pageRef));
71          return wizardModel;
72      }
73  
74      protected static class Profile extends WizardStep {
75  
76          private static final long serialVersionUID = -3043839139187792810L;
77  
78          Profile(final SAML2SPEntityTO entity, final boolean isNew) {
79              AjaxTextFieldPanel key = new AjaxTextFieldPanel(
80                      Constants.KEY_FIELD_NAME, Constants.KEY_FIELD_NAME,
81                      new PropertyModel<>(entity, Constants.KEY_FIELD_NAME));
82              key.addRequiredLabel();
83              key.setEnabled(isNew);
84              add(key);
85          }
86      }
87  
88      protected class BinaryPem extends Pem {
89  
90          private static final long serialVersionUID = 1L;
91  
92          public BinaryPem(final SAML2SPEntityTO entity, final String property, final PageReference pageRef) {
93              super(property);
94  
95              add(new BinaryFieldPanel(
96                      "content",
97                      "",
98                      new PropertyModel<>(entity, property),
99                      MediaType.APPLICATION_OCTET_STREAM,
100                     entity.getKey()) {
101 
102                 private static final long serialVersionUID = -3268213909514986831L;
103 
104                 @Override
105                 protected PageReference getPageReference() {
106                     return pageRef;
107                 }
108             });
109         }
110     }
111 }