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.core.provisioning.java.data;
20  
21  import java.util.Base64;
22  import org.apache.syncope.common.lib.to.SAML2IdPEntityTO;
23  import org.apache.syncope.core.persistence.api.entity.EntityFactory;
24  import org.apache.syncope.core.persistence.api.entity.am.SAML2IdPEntity;
25  import org.apache.syncope.core.provisioning.api.data.SAML2IdPEntityDataBinder;
26  
27  public class SAML2IdPEntityDataBinderImpl implements SAML2IdPEntityDataBinder {
28  
29      protected final EntityFactory entityFactory;
30  
31      public SAML2IdPEntityDataBinderImpl(final EntityFactory entityFactory) {
32          this.entityFactory = entityFactory;
33      }
34  
35      @Override
36      public SAML2IdPEntity create(final SAML2IdPEntityTO entityTO) {
37          SAML2IdPEntity entity = entityFactory.newEntity(SAML2IdPEntity.class);
38          entity.setKey(entityTO.getKey());
39          return update(entity, entityTO);
40      }
41  
42      @Override
43      public SAML2IdPEntity update(final SAML2IdPEntity entity, final SAML2IdPEntityTO entityTO) {
44          entity.setEncryptionCertificate(entityTO.getEncryptionCertificate() == null
45                  ? null
46                  : Base64.getDecoder().decode(entityTO.getEncryptionCertificate()));
47          entity.setEncryptionKey(entityTO.getEncryptionKey() == null
48                  ? null
49                  : Base64.getDecoder().decode(entityTO.getEncryptionKey()));
50          entity.setMetadata(entityTO.getMetadata() == null
51                  ? null
52                  : Base64.getDecoder().decode(entityTO.getMetadata()));
53          entity.setSigningCertificate(entityTO.getSigningCertificate() == null
54                  ? null
55                  : Base64.getDecoder().decode(entityTO.getSigningCertificate()));
56          entity.setSigningKey(entityTO.getSigningKey() == null
57                  ? null
58                  : Base64.getDecoder().decode(entityTO.getSigningKey()));
59          return entity;
60      }
61  
62      @Override
63      public SAML2IdPEntityTO getSAML2IdPEntityTO(final SAML2IdPEntity entity) {
64          SAML2IdPEntityTO entityTO = new SAML2IdPEntityTO();
65          entityTO.setKey(entity.getKey());
66          entityTO.setMetadata(Base64.getEncoder().encodeToString(entity.getMetadata()));
67          if (entity.getEncryptionCertificate() != null) {
68              entityTO.setEncryptionCertificate(
69                      Base64.getEncoder().encodeToString(entity.getEncryptionCertificate()));
70          }
71          if (entity.getEncryptionKey() != null) {
72              entityTO.setEncryptionKey(
73                      Base64.getEncoder().encodeToString(entity.getEncryptionKey()));
74          }
75          if (entity.getSigningCertificate() != null) {
76              entityTO.setSigningCertificate(
77                      Base64.getEncoder().encodeToString(entity.getSigningCertificate()));
78          }
79          if (entity.getSigningKey() != null) {
80              entityTO.setSigningKey(Base64.getEncoder().encodeToString(entity.getSigningKey()));
81          }
82          return entityTO;
83      }
84  }