Search in sources :

Example 1 with RedirectType

use of com.helger.xsds.bdxr.smp2.ac.RedirectType in project peppol-commons by phax.

the class BDXR2Client method saveServiceRedirect.

/**
 * Saves a redirect data object.
 *
 * @param aServiceGroupID
 *        The service group ID to use. May not be <code>null</code>.
 * @param aDocumentTypeID
 *        The document type ID to use. May not be <code>null</code>.
 * @param aRedirect
 *        The redirect to be saved. May not be <code>null</code>.
 * @param aCredentials
 *        The user name and password to use as credentials. May not be
 *        <code>null</code>.
 * @throws SMPClientException
 *         in case something goes wrong
 * @throws SMPClientUnauthorizedException
 *         The user name or password was not correct.
 * @throws SMPClientNotFoundException
 *         A HTTP Not Found was received. This can happen if the service was
 *         not found.
 * @throws SMPClientBadRequestException
 *         The request was not well formed.
 * @see #saveServiceEndpoints(ParticipantIDType, IDType, List,
 *      BasicAuthClientCredentials)
 */
public void saveServiceRedirect(@Nonnull final ParticipantIDType aServiceGroupID, @Nonnull final IDType aDocumentTypeID, @Nonnull final RedirectType aRedirect, @Nonnull final BasicAuthClientCredentials aCredentials) throws SMPClientException {
    ValueEnforcer.notNull(aServiceGroupID, "ServiceGroupID");
    ValueEnforcer.notNull(aDocumentTypeID, "DocumentTypeID");
    ValueEnforcer.notNull(aRedirect, "Redirect");
    ValueEnforcer.notNull(aCredentials, "Credentials");
    final ServiceMetadataType aServiceMetadata = new ServiceMetadataType();
    aServiceMetadata.setSMPVersionID("2.0");
    aServiceMetadata.setID(aDocumentTypeID);
    aServiceMetadata.setParticipantID(aServiceGroupID);
    final ProcessMetadataType aPM = new ProcessMetadataType();
    aPM.setRedirect(aRedirect);
    aServiceMetadata.addProcessMetadata(aPM);
    _saveServiceInformation(aServiceMetadata, aCredentials);
}
Also used : ServiceMetadataType(com.helger.xsds.bdxr.smp2.ServiceMetadataType) ProcessMetadataType(com.helger.xsds.bdxr.smp2.ac.ProcessMetadataType)

Example 2 with RedirectType

use of com.helger.xsds.bdxr.smp2.ac.RedirectType in project peppol-commons by phax.

the class BDXR2ClientReadOnly method getServiceMetadata.

/**
 * Gets a signed service metadata object given by its service group id and its
 * document type. This is a specification compliant method.
 *
 * @param aServiceGroupID
 *        The service group id of the service metadata to get. May not be
 *        <code>null</code>.
 * @param aDocumentTypeID
 *        The document type of the service metadata to get. May not be
 *        <code>null</code>.
 * @return A signed service metadata object. Never <code>null</code>.
 * @throws SMPClientException
 *         in case something goes wrong
 * @throws SMPClientUnauthorizedException
 *         A HTTP Forbidden was received, should not happen.
 * @throws SMPClientNotFoundException
 *         The service group id or document type did not exist.
 * @throws SMPClientBadRequestException
 *         The request was not well formed.
 * @see #getServiceMetadataOrNull(IParticipantIdentifier,
 *      IDocumentTypeIdentifier)
 */
@Nonnull
public ServiceMetadataType getServiceMetadata(@Nonnull final IParticipantIdentifier aServiceGroupID, @Nonnull final IDocumentTypeIdentifier aDocumentTypeID) throws SMPClientException {
    ValueEnforcer.notNull(aServiceGroupID, "ServiceGroupID");
    ValueEnforcer.notNull(aDocumentTypeID, "DocumentTypeID");
    final String sURI = getSMPHostURI() + PATH_OASIS_BDXR_SMP_2 + aServiceGroupID.getURIPercentEncoded() + "/" + URL_PART_SERVICES + "/" + aDocumentTypeID.getURIPercentEncoded();
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("BDXR2Client getServiceRegistration@" + sURI);
    final boolean bXSDValidation = isXMLSchemaValidation();
    final boolean bVerifySignature = isVerifySignature();
    final KeyStore aTrustStore = getTrustStore();
    HttpGet aRequest = new HttpGet(sURI);
    BDXR2ServiceMetadataMarshaller aMarshaller = new BDXR2ServiceMetadataMarshaller(bXSDValidation);
    customizeMarshaller(aMarshaller);
    ServiceMetadataType aMetadata = executeGenericRequest(aRequest, new SMPHttpResponseHandlerSigned<>(aMarshaller, aTrustStore).setVerifySignature(bVerifySignature));
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Received response: " + aMetadata);
    if (!SimpleDocumentTypeIdentifier.wrap(aMetadata.getID()).equals(aDocumentTypeID)) {
        // Inconsistency between request and response
        throw new SMPClientException("Requested document type '" + aDocumentTypeID.getURIEncoded() + "' and received '" + CIdentifier.getURIEncoded(aMetadata.getID()) + "' - mismatch. Ignoring request.");
    }
    // If the Redirect element is present, then follow 1 redirect.
    if (isFollowSMPRedirects()) {
        for (final ProcessMetadataType aPM : aMetadata.getProcessMetadata()) {
            final RedirectType aRedirect = aPM.getRedirect();
            if (aRedirect != null) {
                // Follow the redirect
                if (LOGGER.isInfoEnabled())
                    LOGGER.info("Following a redirect from '" + sURI + "' to '" + aRedirect.getPublisherURIValue() + "'");
                aRequest = new HttpGet(aRedirect.getPublisherURIValue());
                // Create a new Marshaller to make sure customization is easy
                aMarshaller = new BDXR2ServiceMetadataMarshaller(bXSDValidation);
                customizeMarshaller(aMarshaller);
                aMetadata = executeGenericRequest(aRequest, new SMPHttpResponseHandlerSigned<>(new BDXR2ServiceMetadataMarshaller(bXSDValidation), aTrustStore).setVerifySignature(bVerifySignature));
                // Check that the certificateUID is correct.
                boolean bCertificateSubjectFound = false;
                if (aMetadata.hasSignatureEntries())
                    outer: for (final Object aObj : aMetadata.getSignatureAtIndex(0).getKeyInfo().getContent()) {
                        final Object aInfoValue = ((JAXBElement<?>) aObj).getValue();
                        if (aInfoValue instanceof X509DataType) {
                            final X509DataType aX509Data = (X509DataType) aInfoValue;
                            for (final Object aX509Obj : aX509Data.getX509IssuerSerialOrX509SKIOrX509SubjectName()) {
                                final JAXBElement<?> aX509element = (JAXBElement<?>) aX509Obj;
                                // Find the first subject (of type string)
                                if (aX509element.getValue() instanceof X509Certificate) {
                                    final X509Certificate aSecondCert = (X509Certificate) aX509element.getValue();
                                    // Check all certs of the source redirect
                                    boolean bFound = false;
                                    final ICommonsList<X509Certificate> aAllRedirectCerts = new CommonsArrayList<>();
                                    for (final CertificateType aCT : aRedirect.getCertificate()) {
                                        try {
                                            final X509Certificate aRedirectCert = CertificateHelper.convertByteArrayToCertficate(aCT.getContentBinaryObjectValue());
                                            if (aRedirectCert != null) {
                                                aAllRedirectCerts.add(aRedirectCert);
                                                // Certificate match?
                                                if (aRedirectCert.equals(aSecondCert)) {
                                                    bFound = true;
                                                    break;
                                                }
                                            }
                                        } catch (final CertificateException ex) {
                                            // Error in certificate in SMP response
                                            LOGGER.error("SMP Redirect contains an invalid certificate", ex);
                                        }
                                    }
                                    if (!bFound)
                                        throw new SMPClientException("No certificate of the redirect matched the provided certificate. Retrieved certificate is '" + aSecondCert + "'. Allowed certificates according to the redirect are: " + aAllRedirectCerts);
                                    bCertificateSubjectFound = true;
                                    break outer;
                                }
                            }
                        }
                    }
                if (!bCertificateSubjectFound)
                    throw new SMPClientException("The X509 certificate did not contain a certificate subject.");
            }
        }
    } else {
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Following SMP redirects is disabled");
    }
    return aMetadata;
}
Also used : SMPHttpResponseHandlerSigned(com.helger.smpclient.httpclient.SMPHttpResponseHandlerSigned) X509DataType(com.helger.xsds.xmldsig.X509DataType) HttpGet(org.apache.http.client.methods.HttpGet) CertificateException(java.security.cert.CertificateException) JAXBElement(javax.xml.bind.JAXBElement) KeyStore(java.security.KeyStore) ProcessMetadataType(com.helger.xsds.bdxr.smp2.ac.ProcessMetadataType) X509Certificate(java.security.cert.X509Certificate) RedirectType(com.helger.xsds.bdxr.smp2.ac.RedirectType) CertificateType(com.helger.xsds.bdxr.smp2.ac.CertificateType) SMPClientException(com.helger.smpclient.exception.SMPClientException) ServiceMetadataType(com.helger.xsds.bdxr.smp2.ServiceMetadataType) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) BDXR2ServiceMetadataMarshaller(com.helger.smpclient.bdxr2.marshal.BDXR2ServiceMetadataMarshaller) Nonnull(javax.annotation.Nonnull)

Example 3 with RedirectType

use of com.helger.xsds.bdxr.smp2.ac.RedirectType in project peppol-commons by phax.

the class BDXR2Client method saveServiceEndpoints.

/**
 * Saves a service information data object.
 *
 * @param aServiceGroupID
 *        The service group ID to use. May not be <code>null</code>.
 * @param aDocumentTypeID
 *        The document type ID to use. May not be <code>null</code>.
 * @param aEndpoints
 *        The endpoints to the created or updated. May not be
 *        <code>null</code>.
 * @param aCredentials
 *        The user name and password to use as credentials. May not be
 *        <code>null</code>.
 * @throws SMPClientException
 *         in case something goes wrong
 * @throws SMPClientUnauthorizedException
 *         The user name or password was not correct.
 * @throws SMPClientNotFoundException
 *         A HTTP Not Found was received. This can happen if the service was
 *         not found.
 * @throws SMPClientBadRequestException
 *         The request was not well formed.
 * @see #saveServiceRedirect(ParticipantIDType, IDType, RedirectType,
 *      BasicAuthClientCredentials)
 */
public void saveServiceEndpoints(@Nonnull final ParticipantIDType aServiceGroupID, @Nonnull final IDType aDocumentTypeID, @Nonnull final List<EndpointType> aEndpoints, @Nonnull final BasicAuthClientCredentials aCredentials) throws SMPClientException {
    ValueEnforcer.notNull(aServiceGroupID, "ServiceGroupID");
    ValueEnforcer.notNull(aDocumentTypeID, "DocumentTypeID");
    ValueEnforcer.notNull(aEndpoints, "Endpoints");
    ValueEnforcer.notNull(aCredentials, "Credentials");
    final ServiceMetadataType aServiceMetadata = new ServiceMetadataType();
    aServiceMetadata.setSMPVersionID("2.0");
    aServiceMetadata.setID(aDocumentTypeID);
    aServiceMetadata.setParticipantID(aServiceGroupID);
    final ProcessMetadataType aPM = new ProcessMetadataType();
    aPM.getEndpoint().addAll(aEndpoints);
    aServiceMetadata.addProcessMetadata(aPM);
    _saveServiceInformation(aServiceMetadata, aCredentials);
}
Also used : ServiceMetadataType(com.helger.xsds.bdxr.smp2.ServiceMetadataType) ProcessMetadataType(com.helger.xsds.bdxr.smp2.ac.ProcessMetadataType)

Aggregations

ServiceMetadataType (com.helger.xsds.bdxr.smp2.ServiceMetadataType)3 ProcessMetadataType (com.helger.xsds.bdxr.smp2.ac.ProcessMetadataType)3 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 BDXR2ServiceMetadataMarshaller (com.helger.smpclient.bdxr2.marshal.BDXR2ServiceMetadataMarshaller)1 SMPClientException (com.helger.smpclient.exception.SMPClientException)1 SMPHttpResponseHandlerSigned (com.helger.smpclient.httpclient.SMPHttpResponseHandlerSigned)1 CertificateType (com.helger.xsds.bdxr.smp2.ac.CertificateType)1 RedirectType (com.helger.xsds.bdxr.smp2.ac.RedirectType)1 X509DataType (com.helger.xsds.xmldsig.X509DataType)1 KeyStore (java.security.KeyStore)1 CertificateException (java.security.cert.CertificateException)1 X509Certificate (java.security.cert.X509Certificate)1 Nonnull (javax.annotation.Nonnull)1 JAXBElement (javax.xml.bind.JAXBElement)1 HttpGet (org.apache.http.client.methods.HttpGet)1