Java tutorial
/** * The MIT License * Copyright (c) 2015 Estonian Information System Authority (RIA), Population Register Centre (VRK) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package ee.ria.xroad.common.certificateprofile.impl; import java.util.Arrays; import java.util.stream.Collectors; import javax.security.auth.x500.X500Principal; import org.apache.commons.lang.StringUtils; import ee.ria.xroad.common.certificateprofile.CertificateProfileInfo; import ee.ria.xroad.common.certificateprofile.DnFieldDescription; import ee.ria.xroad.common.certificateprofile.DnFieldValue; abstract class AbstractCertificateProfileInfo implements CertificateProfileInfo { protected final DnFieldDescription[] fields; AbstractCertificateProfileInfo(DnFieldDescription[] fields) { this.fields = fields; } @Override public DnFieldDescription[] getSubjectFields() { return fields; } @Override public X500Principal createSubjectDn(DnFieldValue[] values) { return new X500Principal( StringUtils.join(Arrays.stream(values).map(this::toString).collect(Collectors.toList()), ", ")); } @Override public void validateSubjectField(DnFieldValue field) throws Exception { DnFieldDescription description = getDescription(field); if (description.isRequired() && StringUtils.isBlank(field.getValue())) { throw new RuntimeException( String.format("Field '%s (%s)' is missing value", description.getLabel(), description.getId())); } } private String toString(DnFieldValue value) { return String.format("%s=%s", value.getId(), value.getValue()); } private DnFieldDescription getDescription(DnFieldValue value) { return Arrays.stream(fields).filter(f -> f.getId().equals(value.getId())).findFirst() .orElseThrow(() -> new RuntimeException("Unknown field: " + value.getId())); } }