Search in sources :

Example 6 with SMPClientException

use of com.helger.smpclient.exception.SMPClientException in project peppol-commons by phax.

the class AbstractGenericSMPClient method getConvertedException.

/**
 * Convert the passed generic HTTP exception into a more specific exception.
 *
 * @param ex
 *        The generic exception. May not be <code>null</code>.
 * @return A new SMP specific exception, using the passed exception as the
 *         cause.
 */
@Nonnull
public static SMPClientException getConvertedException(@Nonnull final Exception ex) {
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Converting exception of class '" + ex.getClass().getName() + "' to an SMP expception");
    if (ex instanceof SMPClientException)
        return (SMPClientException) ex;
    if (ex instanceof HttpResponseException) {
        final HttpResponseException hex = (HttpResponseException) ex;
        final int nHttpStatus = hex.getStatusCode();
        switch(nHttpStatus) {
            case HttpStatus.SC_BAD_REQUEST:
                return new SMPClientBadRequestException(hex);
            case HttpStatus.SC_FORBIDDEN:
                return new SMPClientUnauthorizedException(hex);
            case HttpStatus.SC_NOT_FOUND:
                return new SMPClientNotFoundException(hex);
            default:
                return new SMPClientException("Error thrown with HTTP status code " + nHttpStatus, hex);
        }
    }
    // Special case
    if (ex instanceof UnknownHostException)
        return new SMPClientNotFoundException((UnknownHostException) ex);
    if (ex instanceof ConnectException)
        return new SMPClientNotFoundException((ConnectException) ex);
    // For new SMPClientBadResponseException
    if (ex instanceof ClientProtocolException && ex.getCause() instanceof SMPClientException)
        return (SMPClientException) ex.getCause();
    // Generic version
    return new SMPClientException("Unknown error thrown by SMP server (" + ex.getMessage() + ")", ex);
}
Also used : SMPClientNotFoundException(com.helger.smpclient.exception.SMPClientNotFoundException) UnknownHostException(java.net.UnknownHostException) SMPClientException(com.helger.smpclient.exception.SMPClientException) SMPClientBadRequestException(com.helger.smpclient.exception.SMPClientBadRequestException) HttpResponseException(org.apache.http.client.HttpResponseException) SMPClientUnauthorizedException(com.helger.smpclient.exception.SMPClientUnauthorizedException) ConnectException(java.net.ConnectException) ClientProtocolException(org.apache.http.client.ClientProtocolException) Nonnull(javax.annotation.Nonnull)

Example 7 with SMPClientException

use of com.helger.smpclient.exception.SMPClientException in project peppol-commons by phax.

the class BDXRClientReadOnly 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)
 * @since v8.0.0
 */
@Nonnull
public SignedServiceMetadataType getServiceMetadata(@Nonnull final IParticipantIdentifier aServiceGroupID, @Nonnull final IDocumentTypeIdentifier aDocumentTypeID) throws SMPClientException {
    ValueEnforcer.notNull(aServiceGroupID, "ServiceGroupID");
    ValueEnforcer.notNull(aDocumentTypeID, "DocumentTypeID");
    final String sURI = getSMPHostURI() + aServiceGroupID.getURIPercentEncoded() + "/" + URL_PART_SERVICES + "/" + aDocumentTypeID.getURIPercentEncoded();
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("BDXRClient getServiceRegistration@" + sURI);
    final boolean bXSDValidation = isXMLSchemaValidation();
    final boolean bVerifySignature = isVerifySignature();
    final KeyStore aTrustStore = getTrustStore();
    HttpGet aRequest = new HttpGet(sURI);
    BDXR1MarshallerSignedServiceMetadataType aMarshaller = new BDXR1MarshallerSignedServiceMetadataType(bXSDValidation);
    customizeMarshaller(aMarshaller);
    SignedServiceMetadataType aMetadata = executeGenericRequest(aRequest, new SMPHttpResponseHandlerSigned<>(aMarshaller, aTrustStore).setVerifySignature(bVerifySignature));
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Received response: " + aMetadata);
    // If the Redirect element is present, then follow 1 redirect.
    if (isFollowSMPRedirects()) {
        if (aMetadata.getServiceMetadata() != null && aMetadata.getServiceMetadata().getRedirect() != null) {
            final RedirectType aRedirect = aMetadata.getServiceMetadata().getRedirect();
            // Follow the redirect
            if (LOGGER.isInfoEnabled())
                LOGGER.info("Following a redirect from '" + sURI + "' to '" + aRedirect.getHref() + "'");
            aRequest = new HttpGet(aRedirect.getHref());
            // Create a new Marshaller to make sure customization is easy
            aMarshaller = new BDXR1MarshallerSignedServiceMetadataType(bXSDValidation);
            customizeMarshaller(aMarshaller);
            aMetadata = executeGenericRequest(aRequest, new SMPHttpResponseHandlerSigned<>(aMarshaller, aTrustStore).setVerifySignature(bVerifySignature));
            // Check that the certificateUID is correct
            boolean bCertificateSubjectFound = false;
            for (final Object aObj : aMetadata.getSignature().getKeyInfo().getContent()) {
                final Object aInfoValue = ((JAXBElement<?>) aObj).getValue();
                if (aInfoValue instanceof X509DataType) {
                    final X509DataType aX509Data = (X509DataType) aInfoValue;
                    if (containsRedirectSubject(aX509Data, aRedirect.getCertificateUID())) {
                        bCertificateSubjectFound = true;
                        break;
                    }
                }
            }
            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) SignedServiceMetadataType(com.helger.xsds.bdxr.smp1.SignedServiceMetadataType) BDXR1MarshallerSignedServiceMetadataType(com.helger.smpclient.bdxr1.marshal.BDXR1MarshallerSignedServiceMetadataType) JAXBElement(javax.xml.bind.JAXBElement) KeyStore(java.security.KeyStore) RedirectType(com.helger.xsds.bdxr.smp1.RedirectType) BDXR1MarshallerSignedServiceMetadataType(com.helger.smpclient.bdxr1.marshal.BDXR1MarshallerSignedServiceMetadataType) SMPClientException(com.helger.smpclient.exception.SMPClientException) Nonnull(javax.annotation.Nonnull)

Aggregations

SMPClientException (com.helger.smpclient.exception.SMPClientException)7 HttpGet (org.apache.http.client.methods.HttpGet)6 UnknownHostException (java.net.UnknownHostException)4 Nonnull (javax.annotation.Nonnull)4 HttpResponseException (org.apache.http.client.HttpResponseException)4 VisibleForTesting (com.helger.commons.annotation.VisibleForTesting)3 HttpClientManager (com.helger.httpclient.HttpClientManager)3 PDBusinessCard (com.helger.pd.businesscard.generic.PDBusinessCard)3 IDocumentTypeIdentifier (com.helger.peppolid.IDocumentTypeIdentifier)3 IIdentifierFactory (com.helger.peppolid.factory.IIdentifierFactory)3 SMPHttpResponseHandlerSigned (com.helger.smpclient.httpclient.SMPHttpResponseHandlerSigned)3 X509DataType (com.helger.xsds.xmldsig.X509DataType)3 IOException (java.io.IOException)3 KeyStore (java.security.KeyStore)3 Nullable (javax.annotation.Nullable)3 JAXBElement (javax.xml.bind.JAXBElement)3 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 BDXR1MarshallerSignedServiceMetadataType (com.helger.smpclient.bdxr1.marshal.BDXR1MarshallerSignedServiceMetadataType)1 BDXR2ServiceMetadataMarshaller (com.helger.smpclient.bdxr2.marshal.BDXR2ServiceMetadataMarshaller)1 SMPClientBadRequestException (com.helger.smpclient.exception.SMPClientBadRequestException)1