Search in sources :

Example 1 with CustomerService

use of httpsdemo.common.CustomerService in project cxf by apache.

the class Client method main.

public static void main(String[] args) throws Exception {
    String keyStoreLoc = "src/main/config/clientKeystore.jks";
    KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(new FileInputStream(keyStoreLoc), "cspass".toCharArray());
    SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(keyStore, null).loadKeyMaterial(keyStore, "ckpass".toCharArray()).build();
    /*
         * Send HTTP GET request to query customer info using portable HttpClient
         * object from Apache HttpComponents
         */
    SSLConnectionSocketFactory sf = new SSLConnectionSocketFactory(sslcontext);
    System.out.println("Sending HTTPS GET request to query customer info");
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sf).build();
    HttpGet httpget = new HttpGet(BASE_SERVICE_URL + "/123");
    BasicHeader bh = new BasicHeader("Accept", "text/xml");
    httpget.addHeader(bh);
    CloseableHttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    entity.writeTo(System.out);
    response.close();
    httpclient.close();
    /*
         *  Send HTTP PUT request to update customer info, using CXF WebClient method
         *  Note: if need to use basic authentication, use the WebClient.create(baseAddress,
         *  username,password,configFile) variant, where configFile can be null if you're
         *  not using certificates.
         */
    System.out.println("\n\nSending HTTPS PUT to update customer name");
    WebClient wc = WebClient.create(BASE_SERVICE_URL, CLIENT_CONFIG_FILE);
    Customer customer = new Customer();
    customer.setId(123);
    customer.setName("Mary");
    Response resp = wc.put(customer);
    /*
         *  Send HTTP POST request to add customer, using JAXRSClientProxy
         *  Note: if need to use basic authentication, use the JAXRSClientFactory.create(baseAddress,
         *  username,password,configFile) variant, where configFile can be null if you're
         *  not using certificates.
         */
    System.out.println("\n\nSending HTTPS POST request to add customer");
    CustomerService proxy = JAXRSClientFactory.create(BASE_SERVICE_URL, CustomerService.class, CLIENT_CONFIG_FILE);
    customer = new Customer();
    customer.setName("Jack");
    resp = wc.post(customer);
    System.out.println("\n");
    System.exit(0);
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) CustomerService(httpsdemo.common.CustomerService) HttpEntity(org.apache.http.HttpEntity) Customer(httpsdemo.common.Customer) HttpGet(org.apache.http.client.methods.HttpGet) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) WebClient(org.apache.cxf.jaxrs.client.WebClient) FileInputStream(java.io.FileInputStream) Response(javax.ws.rs.core.Response) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicHeader(org.apache.http.message.BasicHeader)

Aggregations

Customer (httpsdemo.common.Customer)1 CustomerService (httpsdemo.common.CustomerService)1 FileInputStream (java.io.FileInputStream)1 KeyStore (java.security.KeyStore)1 SSLContext (javax.net.ssl.SSLContext)1 Response (javax.ws.rs.core.Response)1 WebClient (org.apache.cxf.jaxrs.client.WebClient)1 HttpEntity (org.apache.http.HttpEntity)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 BasicHeader (org.apache.http.message.BasicHeader)1