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.SAML2SPEntityTO;
23  import org.apache.syncope.core.persistence.api.entity.EntityFactory;
24  import org.apache.syncope.core.persistence.api.entity.am.SAML2SPEntity;
25  import org.apache.syncope.core.provisioning.api.data.SAML2SPEntityDataBinder;
26  
27  public class SAML2SPEntityDataBinderImpl implements SAML2SPEntityDataBinder {
28  
29      protected final EntityFactory entityFactory;
30  
31      public SAML2SPEntityDataBinderImpl(final EntityFactory entityFactory) {
32          this.entityFactory = entityFactory;
33      }
34  
35      @Override
36      public SAML2SPEntity create(final SAML2SPEntityTO entityTO) {
37          SAML2SPEntity entity = entityFactory.newEntity(SAML2SPEntity.class);
38          entity.setKey(entityTO.getKey());
39          return update(entity, entityTO);
40      }
41  
42      @Override
43      public SAML2SPEntity update(final SAML2SPEntity entity, final SAML2SPEntityTO entityTO) {
44          entity.setKeystore(entityTO.getKeystore() == null
45                  ? null
46                  : Base64.getDecoder().decode(entityTO.getKeystore()));
47          entity.setMetadata(entityTO.getMetadata() == null
48                  ? null
49                  : Base64.getDecoder().decode(entityTO.getMetadata()));
50          return entity;
51      }
52  
53      @Override
54      public SAML2SPEntityTO getSAML2SPEntityTO(final SAML2SPEntity entity) {
55          SAML2SPEntityTO entityTO = new SAML2SPEntityTO();
56          entityTO.setKey(entity.getKey());
57          if (entity.getKeystore() != null) {
58              entityTO.setKeystore(Base64.getEncoder().encodeToString(entity.getKeystore()));
59          }
60          if (entity.getMetadata() != null) {
61              entityTO.setMetadata(Base64.getEncoder().encodeToString(entity.getMetadata()));
62          }
63          return entityTO;
64      }
65  }