Search in sources :

Example 1 with HttpClientSettings

use of com.helger.httpclient.HttpClientSettings in project phoss-directory by phax.

the class SMPBusinessCardProvider method getBusinessCard.

@Nullable
public PDExtendedBusinessCard getBusinessCard(@Nonnull final IParticipantIdentifier aParticipantID) {
    final HttpClientSettings aHCS = new HttpClientSettings().setProxyHost(_getHttpProxy()).setProxyCredentials(_getHttpProxyCredentials());
    PDExtendedBusinessCard aBC;
    if (m_aSMPURI != null) {
        // Use a preselected SMP URI
        switch(m_eSMPMode) {
            case PEPPOL:
                {
                    final SMPClientReadOnly aSMPClient = new SMPClientReadOnly(m_aSMPURI);
                    _configureSMPClient(aSMPClient);
                    aBC = getBusinessCardPeppolSMP(aParticipantID, aSMPClient, aHCS);
                    break;
                }
            case OASIS_BDXR_V1:
                {
                    final BDXRClientReadOnly aSMPClient = new BDXRClientReadOnly(m_aSMPURI);
                    _configureSMPClient(aSMPClient);
                    aBC = getBusinessCardBDXR1(aParticipantID, aSMPClient, aHCS);
                    break;
                }
            case OASIS_BDXR_V2:
                {
                    final BDXR2ClientReadOnly aSMPClient = new BDXR2ClientReadOnly(m_aSMPURI);
                    _configureSMPClient(aSMPClient);
                    aBC = getBusinessCardBDXR2(aParticipantID, aSMPClient, aHCS);
                    break;
                }
            default:
                throw new IllegalStateException("Unsupported SMP mode " + m_eSMPMode);
        }
    } else {
        // SML auto detect
        aBC = null;
        for (final ISMLInfo aSML : m_aSMLInfoProvider.get()) {
            // Create SMP client and query SMP
            switch(m_eSMPMode) {
                case PEPPOL:
                    {
                        try {
                            final SMPClientReadOnly aSMPClient = new SMPClientReadOnly(m_aURLProvider, aParticipantID, aSML);
                            _configureSMPClient(aSMPClient);
                            aBC = getBusinessCardPeppolSMP(aParticipantID, aSMPClient, aHCS);
                        } catch (final SMPDNSResolutionException ex) {
                        // Happens if a non-existing URL is queried
                        }
                        break;
                    }
                case OASIS_BDXR_V1:
                    {
                        try {
                            final BDXRClientReadOnly aSMPClient = new BDXRClientReadOnly(m_aURLProvider, aParticipantID, aSML);
                            _configureSMPClient(aSMPClient);
                            aBC = getBusinessCardBDXR1(aParticipantID, aSMPClient, aHCS);
                        } catch (final SMPDNSResolutionException ex) {
                        // Happens if a non-existing URL is queried
                        }
                        break;
                    }
                case OASIS_BDXR_V2:
                    {
                        try {
                            final BDXR2ClientReadOnly aSMPClient = new BDXR2ClientReadOnly(m_aURLProvider, aParticipantID, aSML);
                            _configureSMPClient(aSMPClient);
                            aBC = getBusinessCardBDXR2(aParticipantID, aSMPClient, aHCS);
                        } catch (final SMPDNSResolutionException ex) {
                        // Happens if a non-existing URL is queried
                        }
                        break;
                    }
                default:
                    throw new IllegalStateException("Unsupported SMP mode " + m_eSMPMode);
            }
            // Found one?
            if (aBC != null)
                break;
        }
    }
    if (aBC != null)
        LOGGER.info("Found BusinessCard for '" + aParticipantID.getURIEncoded() + "' with " + aBC.getBusinessCard().businessEntities().size() + " entities and " + aBC.getDocumentTypeCount() + " document types");
    return aBC;
}
Also used : BDXR2ClientReadOnly(com.helger.smpclient.bdxr2.BDXR2ClientReadOnly) SMPClientReadOnly(com.helger.smpclient.peppol.SMPClientReadOnly) ISMLInfo(com.helger.peppol.sml.ISMLInfo) HttpClientSettings(com.helger.httpclient.HttpClientSettings) SMPDNSResolutionException(com.helger.smpclient.url.SMPDNSResolutionException) BDXRClientReadOnly(com.helger.smpclient.bdxr1.BDXRClientReadOnly) Nullable(javax.annotation.Nullable)

Example 2 with HttpClientSettings

use of com.helger.httpclient.HttpClientSettings in project phase4 by phax.

the class MainOldAS4Client method main.

/**
 * Starting point for the SAAJ - SOAP Client Testing
 *
 * @param args
 *        ignored
 */
public static void main(final String[] args) {
    try (final AS4ResourceHelper aResHelper = new AS4ResourceHelper()) {
        String sURL = "http://127.0.0.1:8080/as4";
        if (false)
            sURL = "http://msh.holodeck-b2b.org:8080/msh";
        // Deactivate if not sending to local holodeck
        if (false)
            sURL = "http://localhost:8080/msh/";
        final HttpClientSettings aHCS = new HttpClientSettings();
        if (sURL.startsWith("https"))
            aHCS.setSSLContextTrustAll();
        if (false) {
            aHCS.setProxyHost(new HttpHost("172.30.9.6", 8080));
            aHCS.addNonProxyHostsFromPipeString("localhost|127.0.0.1");
        }
        final CloseableHttpClient aClient = new HttpClientFactory(aHCS).createHttpClient();
        LOGGER.info("Sending to " + sURL);
        final HttpPost aPost = new HttpPost(sURL);
        final ICommonsList<WSS4JAttachment> aAttachments = new CommonsArrayList<>();
        final Node aPayload = DOMReader.readXMLDOM(new ClassPathResource("SOAPBodyPayload.xml"));
        final ESoapVersion eSoapVersion = ESoapVersion.SOAP_12;
        final IAS4CryptoFactory aCryptoFactory = AS4CryptoFactoryProperties.getDefaultInstance();
        if (true) {
            // No Mime Message Not signed or encrypted, just SOAP + Payload in SOAP
            // -
            // Body
            // final Document aDoc = TestMessages.testSignedUserMessage
            // (ESOAPVersion.SOAP_11, aPayload, aAttachments);
            final AS4UserMessage aMsg = MockClientMessages.createUserMessageNotSigned(eSoapVersion, aPayload, aAttachments);
            final Document aDoc = aMsg.getAsSoapDocument(aPayload);
            aPost.setEntity(new HttpXMLEntity(aDoc, eSoapVersion.getMimeType()));
        } else if (false) {
            // BodyPayload SIGNED
            final Document aDoc = MockClientMessages.createUserMessageSigned(eSoapVersion, aPayload, aAttachments, aResHelper);
            aPost.setEntity(new HttpXMLEntity(aDoc, eSoapVersion.getMimeType()));
        } else if (false) {
            // BodyPayload ENCRYPTED
            final AS4UserMessage aMsg = MockClientMessages.createUserMessageNotSigned(eSoapVersion, aPayload, aAttachments);
            Document aDoc = aMsg.getAsSoapDocument(aPayload);
            aDoc = AS4Encryptor.encryptSoapBodyPayload(aCryptoFactory, eSoapVersion, aDoc, false, AS4CryptParams.createDefault().setAlias("dummy"));
            aPost.setEntity(new HttpXMLEntity(aDoc, eSoapVersion.getMimeType()));
        } else if (true) {
            aAttachments.add(WSS4JAttachment.createOutgoingFileAttachment(Phase4OutgoingAttachment.builder().data(ClassPathResource.getAsFile("attachment/test.xml.gz")).mimeType(CMimeType.APPLICATION_GZIP).build(), aResHelper));
            final AS4UserMessage aMsg = MockClientMessages.createUserMessageNotSigned(eSoapVersion, null, aAttachments);
            final AS4MimeMessage aMimeMsg = MimeMessageCreator.generateMimeMessage(eSoapVersion, AS4Signer.createSignedMessage(aCryptoFactory, aMsg.getAsSoapDocument(null), eSoapVersion, aMsg.getMessagingID(), aAttachments, aResHelper, false, AS4SigningParams.createDefault()), aAttachments);
            // Move all global mime headers to the POST request
            MessageHelperMethods.forEachHeaderAndRemoveAfterwards(aMimeMsg, aPost::addHeader, true);
            aPost.setEntity(new HttpMimeMessageEntity(aMimeMsg));
        } else if (false) {
            Document aDoc = MockClientMessages.createUserMessageSigned(eSoapVersion, aPayload, aAttachments, aResHelper);
            aDoc = AS4Encryptor.encryptSoapBodyPayload(aCryptoFactory, eSoapVersion, aDoc, false, AS4CryptParams.createDefault().setAlias("dummy"));
            aPost.setEntity(new HttpXMLEntity(aDoc, eSoapVersion.getMimeType()));
        } else
            throw new IllegalStateException("Some test message should be selected :)");
        // re-instantiate if you want to see the request that is getting sent
        LOGGER.info(EntityUtils.toString(aPost.getEntity()));
        final CloseableHttpResponse aHttpResponse = aClient.execute(aPost);
        LOGGER.info("GET Response Status:: " + aHttpResponse.getStatusLine().getStatusCode());
        // print result
        LOGGER.info(EntityUtils.toString(aHttpResponse.getEntity()));
    } catch (final Exception e) {
        LOGGER.error("Error occurred while sending SOAP Request to Server", e);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) IAS4CryptoFactory(com.helger.phase4.crypto.IAS4CryptoFactory) Node(org.w3c.dom.Node) HttpXMLEntity(com.helger.phase4.http.HttpXMLEntity) Document(org.w3c.dom.Document) AS4UserMessage(com.helger.phase4.messaging.domain.AS4UserMessage) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) HttpHost(org.apache.http.HttpHost) ESoapVersion(com.helger.phase4.soap.ESoapVersion) AS4MimeMessage(com.helger.phase4.messaging.mime.AS4MimeMessage) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpClientSettings(com.helger.httpclient.HttpClientSettings) HttpMimeMessageEntity(com.helger.phase4.http.HttpMimeMessageEntity) AS4ResourceHelper(com.helger.phase4.util.AS4ResourceHelper) HttpClientFactory(com.helger.httpclient.HttpClientFactory) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) WSS4JAttachment(com.helger.phase4.attachment.WSS4JAttachment)

Example 3 with HttpClientSettings

use of com.helger.httpclient.HttpClientSettings in project phoss-directory by phax.

the class SMPBusinessCardProviderTest method testBabelway.

@Test
@Ignore("Only for on demand testing :)")
public void testBabelway() {
    final SMPBusinessCardProvider aBI = SMPBusinessCardProvider.createWithSMLAutoDetect(PDServerConfiguration.getSMPMode(), PDServerConfiguration.getURLProvider(), SML_SUPPLIER);
    final PDExtendedBusinessCard aExtBI = aBI.getBusinessCardPeppolSMP(PeppolIdentifierFactory.INSTANCE.createParticipantIdentifierWithDefaultScheme("9956:0471349823"), new SMPClientReadOnly(URLHelper.getAsURI("https://int.babelway.net/smp/")), new HttpClientSettings());
    assertNotNull(aExtBI);
    LOGGER.info(aExtBI.toString());
}
Also used : SMPClientReadOnly(com.helger.smpclient.peppol.SMPClientReadOnly) HttpClientSettings(com.helger.httpclient.HttpClientSettings) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with HttpClientSettings

use of com.helger.httpclient.HttpClientSettings in project phoss-directory by phax.

the class SMPBusinessCardProviderTest method testFetchLocal.

@Test
@Ignore("Only for on demand testing :)")
public void testFetchLocal() {
    final SMPBusinessCardProvider aBI = SMPBusinessCardProvider.createWithSMLAutoDetect(PDServerConfiguration.getSMPMode(), PDServerConfiguration.getURLProvider(), SML_SUPPLIER);
    final PDExtendedBusinessCard aExtBI = aBI.getBusinessCardPeppolSMP(PeppolIdentifierFactory.INSTANCE.createParticipantIdentifierWithDefaultScheme("9999:ghx"), new SMPClientReadOnly(URLHelper.getAsURI("http://localhost:90")), new HttpClientSettings());
    assertNotNull(aExtBI);
    LOGGER.info(aExtBI.toString());
}
Also used : SMPClientReadOnly(com.helger.smpclient.peppol.SMPClientReadOnly) HttpClientSettings(com.helger.httpclient.HttpClientSettings) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with HttpClientSettings

use of com.helger.httpclient.HttpClientSettings in project phase4 by phax.

the class AbstractUserMessageTestSetUp method setUpHttpClient.

@Before
public void setUpHttpClient() throws GeneralSecurityException {
    final HttpClientSettings aHCS = new HttpClientSettings();
    aHCS.setSSLContextTrustAll();
    aHCS.setSocketTimeoutMS(500_000);
    aHCS.setRetryCount(m_nRetries);
    aHCS.setRetryMode(ERetryMode.RETRY_ALWAYS);
    m_aHttpClient = new HttpClientFactory(aHCS).createHttpClient();
}
Also used : HttpClientSettings(com.helger.httpclient.HttpClientSettings) HttpClientFactory(com.helger.httpclient.HttpClientFactory) Before(org.junit.Before)

Aggregations

HttpClientSettings (com.helger.httpclient.HttpClientSettings)5 SMPClientReadOnly (com.helger.smpclient.peppol.SMPClientReadOnly)3 HttpClientFactory (com.helger.httpclient.HttpClientFactory)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)1 ISMLInfo (com.helger.peppol.sml.ISMLInfo)1 WSS4JAttachment (com.helger.phase4.attachment.WSS4JAttachment)1 IAS4CryptoFactory (com.helger.phase4.crypto.IAS4CryptoFactory)1 HttpMimeMessageEntity (com.helger.phase4.http.HttpMimeMessageEntity)1 HttpXMLEntity (com.helger.phase4.http.HttpXMLEntity)1 AS4UserMessage (com.helger.phase4.messaging.domain.AS4UserMessage)1 AS4MimeMessage (com.helger.phase4.messaging.mime.AS4MimeMessage)1 ESoapVersion (com.helger.phase4.soap.ESoapVersion)1 AS4ResourceHelper (com.helger.phase4.util.AS4ResourceHelper)1 BDXRClientReadOnly (com.helger.smpclient.bdxr1.BDXRClientReadOnly)1 BDXR2ClientReadOnly (com.helger.smpclient.bdxr2.BDXR2ClientReadOnly)1 SMPDNSResolutionException (com.helger.smpclient.url.SMPDNSResolutionException)1 IOException (java.io.IOException)1