Search in sources :

Example 1 with HttpClient

use of org.mitre.taxii.client.HttpClient in project metron by apache.

the class TaxiiHandler method buildClient.

private static HttpClient buildClient(URL proxy, String username, String password) throws Exception {
    // Start with a default TAXII HTTP client.
    HttpClient client = new HttpClient();
    // Create an Apache HttpClientBuilder to be customized by the command line arguments.
    HttpClientBuilder builder = HttpClientBuilder.create().useSystemProperties();
    // Proxy
    if (proxy != null) {
        HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort(), proxy.getProtocol());
        builder.setProxy(proxyHost);
    }
    // Basic authentication. User & Password
    if (username != null ^ password != null) {
        throw new Exception("'username' and 'password' arguments are required to appear together.");
    }
    // from:  http://stackoverflow.com/questions/19517538/ignoring-ssl-certificate-in-apache-httpclient-4-3
    SSLContextBuilder ssbldr = new SSLContextBuilder();
    ssbldr.loadTrustMaterial(null, new TrustSelfSignedStrategy());
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(ssbldr.build(), SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", new PlainConnectionSocketFactory()).register("https", sslsf).build();
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
    // max connection
    cm.setMaxTotal(20);
    // ""
    System.setProperty("jsse.enableSNIExtension", "false");
    CloseableHttpClient httpClient = builder.setSSLSocketFactory(sslsf).setConnectionManager(cm).build();
    client.setHttpclient(httpClient);
    return client;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) HttpHost(org.apache.http.HttpHost) HttpClient(org.mitre.taxii.client.HttpClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLContextBuilder(org.apache.http.conn.ssl.SSLContextBuilder) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) JAXBException(javax.xml.bind.JAXBException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 2 with HttpClient

use of org.mitre.taxii.client.HttpClient in project metron by apache.

the class TaxiiHandler method discoverPollingClient.

private static DiscoveryResults discoverPollingClient(URL proxy, URL endpoint, String username, String password, HttpClientContext context, String defaultCollection) throws Exception {
    DiscoveryResults results = new DiscoveryResults();
    {
        HttpClient discoverClient = buildClient(proxy, username, password);
        String sessionID = MessageHelper.generateMessageId();
        // Prepare the message to send.
        DiscoveryRequest request = messageFactory.get().createDiscoveryRequest().withMessageId(sessionID);
        DiscoveryResponse response = call(discoverClient, endpoint.toURI(), request, context, DiscoveryResponse.class);
        for (ServiceInstanceType serviceInstance : response.getServiceInstances()) {
            if (serviceInstance.isAvailable() && serviceInstance.getServiceType() == ServiceTypeEnum.POLL) {
                results.pollEndpoint = new URL(serviceInstance.getAddress());
            } else if (serviceInstance.isAvailable() && serviceInstance.getServiceType() == ServiceTypeEnum.COLLECTION_MANAGEMENT) {
                results.collectionManagementEndpoint = new URL(serviceInstance.getAddress());
            }
        }
        if (results.pollEndpoint == null) {
            throw new RuntimeException("Unable to discover a poll TAXII feed");
        }
    }
    if (defaultCollection == null) // get collections
    {
        HttpClient discoverClient = buildClient(proxy, username, password);
        String sessionID = MessageHelper.generateMessageId();
        CollectionInformationRequest request = messageFactory.get().createCollectionInformationRequest().withMessageId(sessionID);
        CollectionInformationResponse response = call(discoverClient, results.collectionManagementEndpoint.toURI(), request, context, CollectionInformationResponse.class);
        LOG.info("Unable to find the default collection; available collections are:");
        for (CollectionRecordType c : response.getCollections()) {
            LOG.info(c.getCollectionName());
            results.collections.add(c.getCollectionName());
        }
        System.exit(0);
    }
    return results;
}
Also used : CollectionInformationResponse(org.mitre.taxii.messages.xml11.CollectionInformationResponse) ServiceInstanceType(org.mitre.taxii.messages.xml11.ServiceInstanceType) DiscoveryResponse(org.mitre.taxii.messages.xml11.DiscoveryResponse) CollectionRecordType(org.mitre.taxii.messages.xml11.CollectionRecordType) HttpClient(org.mitre.taxii.client.HttpClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) CollectionInformationRequest(org.mitre.taxii.messages.xml11.CollectionInformationRequest) DiscoveryRequest(org.mitre.taxii.messages.xml11.DiscoveryRequest) URL(java.net.URL)

Aggregations

CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)2 HttpClient (org.mitre.taxii.client.HttpClient)2 IOException (java.io.IOException)1 URL (java.net.URL)1 JAXBException (javax.xml.bind.JAXBException)1 DatatypeConfigurationException (javax.xml.datatype.DatatypeConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 HttpHost (org.apache.http.HttpHost)1 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)1 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)1 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)1 SSLContextBuilder (org.apache.http.conn.ssl.SSLContextBuilder)1 TrustSelfSignedStrategy (org.apache.http.conn.ssl.TrustSelfSignedStrategy)1 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)1 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)1 CollectionInformationRequest (org.mitre.taxii.messages.xml11.CollectionInformationRequest)1 CollectionInformationResponse (org.mitre.taxii.messages.xml11.CollectionInformationResponse)1 CollectionRecordType (org.mitre.taxii.messages.xml11.CollectionRecordType)1 DiscoveryRequest (org.mitre.taxii.messages.xml11.DiscoveryRequest)1 DiscoveryResponse (org.mitre.taxii.messages.xml11.DiscoveryResponse)1