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.resources;
20  
21  import java.io.Serializable;
22  import java.net.URI;
23  import java.util.List;
24  import java.util.stream.Collectors;
25  import org.apache.commons.lang3.tuple.Pair;
26  import org.apache.syncope.client.console.rest.ConnectorRestClient;
27  import org.apache.syncope.client.console.topology.TopologyNode;
28  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
29  import org.apache.syncope.common.lib.to.ConnIdBundle;
30  import org.apache.syncope.common.lib.to.ConnInstanceTO;
31  import org.apache.wicket.PageReference;
32  import org.apache.wicket.ajax.AjaxRequestTarget;
33  import org.apache.wicket.extensions.wizard.WizardModel;
34  import org.apache.wicket.markup.ComponentTag;
35  import org.apache.wicket.model.PropertyModel;
36  
37  /**
38   * Modal window with Connector form.
39   */
40  public class ConnectorWizardBuilder extends AbstractResourceWizardBuilder<ConnInstanceTO> {
41  
42      private static final long serialVersionUID = -2025535531121434050L;
43  
44      protected final List<ConnIdBundle> bundles;
45  
46      protected final ConnectorRestClient connectorRestClient;
47  
48      public ConnectorWizardBuilder(
49              final ConnInstanceTO defaultItem,
50              final ConnectorRestClient connectorRestClient,
51              final PageReference pageRef) {
52  
53          super(defaultItem, pageRef);
54  
55          this.bundles = connectorRestClient.getAllBundles().stream().
56                  filter(object -> object.getLocation().equals(defaultItem.getLocation())).collect(Collectors.toList());
57          this.connectorRestClient = connectorRestClient;
58      }
59  
60      @Override
61      protected WizardModel buildModelSteps(final Serializable modelObject, final WizardModel wizardModel) {
62          ConnInstanceTO connInstanceTO = ConnInstanceTO.class.cast(modelObject);
63          wizardModel.add(new ConnectorDetailsPanel(connInstanceTO, bundles));
64          wizardModel.add(new ConnectorConfPanel(connInstanceTO, bundles) {
65  
66              private static final long serialVersionUID = -5886691077681158494L;
67  
68              @Override
69              protected Pair<Boolean, String> check(final AjaxRequestTarget target) {
70                  ConnInstanceTO connInstanceTO = ConnInstanceTO.class.cast(modelObject);
71                  ConnIdBundle bundleTO = ConnectorWizardBuilder.getBundle(connInstanceTO, bundles);
72  
73                  connInstanceTO.setConnectorName(bundleTO.getConnectorName());
74                  connInstanceTO.setBundleName(bundleTO.getBundleName());
75                  connInstanceTO.setVersion(bundleTO.getVersion());
76  
77                  return connectorRestClient.check(connInstanceTO);
78              }
79  
80              @Override
81              protected void onComponentTag(final ComponentTag tag) {
82                  tag.append("class", "scrollable-tab-content", " ");
83              }
84  
85          });
86          wizardModel.add(new ConnCapabilitiesPanel(new PropertyModel<>(connInstanceTO, "capabilities")));
87          return wizardModel;
88      }
89  
90      @Override
91      protected Serializable onApplyInternal(final Serializable modelObject) {
92          ConnInstanceTO connInstanceTO = ConnInstanceTO.class.cast(modelObject);
93          ConnIdBundle bundleTO = ConnectorWizardBuilder.getBundle(connInstanceTO, bundles);
94  
95          connInstanceTO.setConnectorName(bundleTO.getConnectorName());
96          connInstanceTO.setBundleName(bundleTO.getBundleName());
97          connInstanceTO.setVersion(bundleTO.getVersion());
98  
99          // Reset pool configuration if all fields are null
100         if (connInstanceTO.getPoolConf() != null
101                 && connInstanceTO.getPoolConf().getMaxIdle() == null
102                 && connInstanceTO.getPoolConf().getMaxObjects() == null
103                 && connInstanceTO.getPoolConf().getMaxWait() == null
104                 && connInstanceTO.getPoolConf().getMinEvictableIdleTimeMillis() == null
105                 && connInstanceTO.getPoolConf().getMinIdle() == null) {
106 
107             connInstanceTO.setPoolConf(null);
108         }
109 
110         ConnInstanceTO connInstance;
111         if (mode == AjaxWizard.Mode.CREATE) {
112             connInstance = connectorRestClient.create(connInstanceTO);
113         } else {
114             connectorRestClient.update(connInstanceTO);
115             connInstance = connInstanceTO;
116         }
117 
118         return connInstance;
119     }
120 
121     @Override
122     protected Serializable getCreateCustomPayloadEvent(final Serializable afterObject, final AjaxRequestTarget target) {
123         ConnInstanceTO connInstance = ConnInstanceTO.class.cast(afterObject);
124         return new CreateEvent(
125                 connInstance.getKey(),
126                 connInstance.getDisplayName(),
127                 TopologyNode.Kind.CONNECTOR,
128                 URI.create(connInstance.getLocation()).toASCIIString(),
129                 target);
130     }
131 
132     protected static ConnIdBundle getBundle(final ConnInstanceTO connInstanceTO, final List<ConnIdBundle> bundles) {
133         return bundles.stream().
134                 filter(bundle -> bundle.getBundleName().equals(connInstanceTO.getBundleName())
135                 && bundle.getConnectorName().equals(connInstanceTO.getConnectorName())
136                 && bundle.getVersion().equals(connInstanceTO.getVersion())).
137                 findFirst().orElse(null);
138     }
139 }