eu.europa.esig.dss.tsl.sync.TrustServiceProviderBuilder Maven / Gradle / Ivy
/**
* DSS - Digital Signature Services
* Copyright (C) 2015 European Commission, provided under the CEF programme
*
* This file is part of the "DSS - Digital Signature Services" project.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package eu.europa.esig.dss.tsl.sync;
import eu.europa.esig.dss.model.timedependent.TimeDependentValues;
import eu.europa.esig.dss.model.tsl.TrustService;
import eu.europa.esig.dss.model.tsl.TrustServiceProvider;
import eu.europa.esig.dss.model.tsl.TrustServiceStatusAndInformationExtensions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Builds {@code TrustServiceProvider}
*/
public class TrustServiceProviderBuilder {
/** Map of names (the key is the language) */
private Map> names;
/** Map of trade names */
private Map> tradeNames;
/** List of registration identifiers */
private List registrationIdentifiers;
/** Map of postal addresses */
private Map postalAddresses;
/** Map of electronic addresses */
private Map> electronicAddresses;
/** Map of information */
private Map information;
/** List of trust services */
private List services;
/** The territory (country) */
private String territory;
/**
* Default constructor
*/
public TrustServiceProviderBuilder() {
// empty
}
/**
* Copy the original object
*
* @param original the original trust service provider
*/
public TrustServiceProviderBuilder(TrustServiceProvider original) {
Objects.requireNonNull(original, "TrustServiceProvider cannot be null!");
this.names = original.getNames();
this.tradeNames = original.getTradeNames();
this.registrationIdentifiers = original.getRegistrationIdentifiers();
this.postalAddresses = original.getPostalAddresses();
this.electronicAddresses = original.getElectronicAddresses();
this.information = original.getInformation();
this.services = original.getServices();
this.territory = original.getTerritory();
}
/**
* Builds {@code TrustServiceProvider}
*
* @return {@link TrustServiceProvider}
*/
public TrustServiceProvider build() {
final TrustServiceProvider trustServiceProvider = new TrustServiceProvider();
trustServiceProvider.setNames(getNames());
trustServiceProvider.setTradeNames(getTradeNames());
trustServiceProvider.setRegistrationIdentifiers(getRegistrationIdentifiers());
trustServiceProvider.setPostalAddresses(getPostalAddresses());
trustServiceProvider.setElectronicAddresses(getElectronicAddresses());
trustServiceProvider.setInformation(getInformation());
trustServiceProvider.setServices(getServices());
trustServiceProvider.setTerritory(getTerritory());
return trustServiceProvider;
}
/**
* Gets a map of names (first key is the language)
*
* @return a map of names
*/
public Map> getNames() {
return getUnmodifiableMapWithLists(names);
}
/**
* Sets a map of names
*
* @param names a map of names (first key is the language)
* @return this {@link TrustServiceProviderBuilder}
*/
public TrustServiceProviderBuilder setNames(Map> names) {
this.names = names;
return this;
}
/**
* Gets a map of trade names
*
* @return a map of trade names
*/
public Map> getTradeNames() {
return getUnmodifiableMapWithLists(tradeNames);
}
/**
* Sets a map of trade names
*
* @param tradeNames a map of trade names (first key is the language)
* @return this {@link TrustServiceProviderBuilder}
*/
public TrustServiceProviderBuilder setTradeNames(Map> tradeNames) {
this.tradeNames = tradeNames;
return this;
}
/**
* Gets registration identifiers
*
* @return a list of {@link String}s
*/
public List getRegistrationIdentifiers() {
return getUnmodifiableList(registrationIdentifiers);
}
/**
* Sets registration identifiers
*
* @param registrationIdentifiers a list of {@link String}s
* @return this {@link TrustServiceProviderBuilder}
*/
public TrustServiceProviderBuilder setRegistrationIdentifiers(List registrationIdentifiers) {
this.registrationIdentifiers = registrationIdentifiers;
return this;
}
/**
* Gets a map of postal addresses
*
* @return a map of postal addresses
*/
public Map getPostalAddresses() {
return getUnmodifiableMap(postalAddresses);
}
/**
* Sets a map of postal addresses
*
* @param postalAddresses a map of postal addresses
* @return this {@link TrustServiceProviderBuilder}
*/
public TrustServiceProviderBuilder setPostalAddresses(Map postalAddresses) {
this.postalAddresses = postalAddresses;
return this;
}
/**
* Gets a map of electronic addresses
*
* @return a map of electronic addresses
*/
public Map> getElectronicAddresses() {
return getUnmodifiableMapWithLists(electronicAddresses);
}
/**
* Sets a map of electronic addresses
*
* @param electronicAddresses a map of electronic addresses
* @return this {@link TrustServiceProviderBuilder}
*/
public TrustServiceProviderBuilder setElectronicAddresses(Map> electronicAddresses) {
this.electronicAddresses = electronicAddresses;
return this;
}
/**
* Gets a map of information
*
* @return a map of information
*/
public Map getInformation() {
return getUnmodifiableMap(information);
}
/**
* Sets a map of information
*
* @param information a map of information
* @return this {@link TrustServiceProviderBuilder}
*/
public TrustServiceProviderBuilder setInformation(Map information) {
this.information = information;
return this;
}
/**
* Gets a list of trust services
*
* @return a list of {@link TrustService}s
*/
public List getServices() {
return getUnmodifiableTrustServices(services);
}
/**
* Sets a list of trust services
*
* @param services a list of {@link TrustService}s
* @return this {@link TrustServiceProviderBuilder}
*/
public TrustServiceProviderBuilder setServices(List services) {
this.services = services;
return this;
}
/**
* Gets territory (country)
*
* @return {@link String}
*/
public String getTerritory() {
return territory;
}
/**
* Sets territory (country)
*
* @param territory {@link String}
* @return this {@link TrustServiceProviderBuilder}
*/
public TrustServiceProviderBuilder setTerritory(String territory) {
this.territory = territory;
return this;
}
private List getUnmodifiableList(List originalList) {
List newList = new ArrayList<>();
if (originalList != null && !originalList.isEmpty()) {
newList.addAll(originalList);
}
return Collections.unmodifiableList(newList);
}
private Map getUnmodifiableMap(Map originalMap) {
Map newMap = new HashMap<>();
if (originalMap != null && !originalMap.isEmpty()) {
newMap.putAll(originalMap);
}
return Collections.unmodifiableMap(newMap);
}
private Map> getUnmodifiableMapWithLists(Map> originalMap) {
Map> copyMap = new HashMap<>();
if (originalMap != null && !originalMap.isEmpty()) {
for (Map.Entry> mapEntry : originalMap.entrySet()) {
copyMap.put(mapEntry.getKey(), Collections.unmodifiableList(mapEntry.getValue()));
}
}
return Collections.unmodifiableMap(copyMap);
}
private List getUnmodifiableTrustServices(List originalTrustServices) {
List copyTrustServices = new ArrayList<>();
if (originalTrustServices != null && !originalTrustServices.isEmpty()) {
for (TrustService trustService : originalTrustServices) {
TrustService.TrustServiceBuilder trustServiceBuilder = new TrustService.TrustServiceBuilder();
TrustService copyTrustService = trustServiceBuilder.setCertificates(getUnmodifiableList(trustService.getCertificates()))
.setStatusAndInformationExtensions(getUnmodifiableTimeDependentValues(trustService.getStatusAndInformationExtensions()))
.build();
copyTrustServices.add(copyTrustService);
}
}
return Collections.unmodifiableList(copyTrustServices);
}
private TimeDependentValues getUnmodifiableTimeDependentValues(
TimeDependentValues timeDependentValues) {
List copyTSSAndIEs = new ArrayList<>();
for (TrustServiceStatusAndInformationExtensions status : timeDependentValues) {
TrustServiceStatusAndInformationExtensions.TrustServiceStatusAndInformationExtensionsBuilder builder =
new TrustServiceStatusAndInformationExtensions.TrustServiceStatusAndInformationExtensionsBuilder();
TrustServiceStatusAndInformationExtensions copyStatus = builder.setNames(getUnmodifiableMapWithLists(status.getNames()))
.setType(status.getType())
.setStatus(status.getStatus())
.setConditionsForQualifiers(getUnmodifiableList(status.getConditionsForQualifiers()))
.setAdditionalServiceInfoUris(getUnmodifiableList(status.getAdditionalServiceInfoUris()))
.setServiceSupplyPoints(getUnmodifiableList(status.getServiceSupplyPoints()))
.setExpiredCertsRevocationInfo(status.getExpiredCertsRevocationInfo())
.setStartDate(status.getStartDate())
.setEndDate(status.getEndDate())
.build();
copyTSSAndIEs.add(copyStatus);
}
return new TimeDependentValues<>(copyTSSAndIEs);
}
}