Author: gmazza
Date: Fri Apr 8 00:00:43 2011
New Revision: 1090066
URL: http://svn.apache.org/viewvc?rev=1090066&view=rev
Log:
Updated HTTPS client to use WebClient and JAXRSClientFactory
Added:
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/client/
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/client/Client.java
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/common/
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/common/Customer.java
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/common/CustomerService.java
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/server/
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/server/CustomerServiceImpl.java
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/server/Server.java
Removed:
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/jaxrs/
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/resources/add_customer.xml
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/resources/update_customer.xml
Modified:
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/README.txt
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/pom.xml
Modified:
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/README.txt
URL:
http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/README.txt?rev=1090066&r1=1090065&r2=1090066&view=diff
==============================================================================
---
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/README.txt
(original)
+++
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/README.txt
Fri Apr 8 00:00:43 2011
@@ -7,12 +7,12 @@ by doing the communication using HTTPS.
The JAX-RS server is configured with a HTTPS listener. The listener
requires client authentication so the client must provide suitable
credentials. The listener configuration is taken from the
-"CherryServer.xml" file located under demo directory.
+"ServerConfig.xml" file located under demo directory.
The client is configured to provide its certificate "CN=Wibble" and
-chain stored in the Java KeyStore "certs/wibble.jks" to the server. The
-server authenticates the client's certificate using its trust store
-"certs/truststore.jks", which holds the Certificate Authorities'
+chain stored in the Java KeyStore "certs/clientKeystore.jks" to the server.
+The server authenticates the client's certificate using the truststore
+"certs/commonstore.jks", which holds the Certificate Authorities'
certificates.
Likewise the client authenticates the server's certificate "CN=Cherry"
@@ -26,7 +26,7 @@ argument to the JVM.
But please note that it is not adviseable to store sensitive data such
as passwords stored in a clear text configuration file, unless the
file is sufficiently protected by OS level permissions. The KeyStores
-may be configured programatically so using user interaction may be
+may be configured programmatically so using user interaction may be
employed to keep passwords from being stored in configuration files.
The approach taken here is for demonstration reasons only.
Modified:
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/pom.xml
URL:
http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/pom.xml?rev=1090066&r1=1090065&r2=1090066&view=diff
==============================================================================
--- cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/pom.xml
(original)
+++ cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/pom.xml
Fri Apr 8 00:00:43 2011
@@ -62,7 +62,7 @@
<goal>java</goal>
</goals>
<configuration>
- <mainClass>jaxrs.server.Server</mainClass>
+
<mainClass>httpsdemo.server.Server</mainClass>
</configuration>
</execution>
</executions>
@@ -85,7 +85,7 @@
<goal>java</goal>
</goals>
<configuration>
- <mainClass>jaxrs.client.Client</mainClass>
+
<mainClass>httpsdemo.client.Client</mainClass>
</configuration>
</execution>
</executions>
Added:
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/client/Client.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/client/Client.java?rev=1090066&view=auto
==============================================================================
---
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/client/Client.java
(added)
+++
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/client/Client.java
Fri Apr 8 00:00:43 2011
@@ -0,0 +1,102 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package httpsdemo.client;
+
+import java.io.File;
+import javax.ws.rs.core.Response;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.contrib.ssl.AuthSSLProtocolSocketFactory;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.protocol.Protocol;
+import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
+import org.apache.cxf.jaxrs.client.WebClient;
+import httpsdemo.common.Customer;
+import httpsdemo.common.CustomerService;
+
+public final class Client {
+
+ private static final String CLIENT_CONFIG_FILE = "ClientConfig.xml";
+ private static final String BASE_SERVICE_URL =
+ "https://localhost:9000/customerservice/customers";
+
+ private Client() {
+ }
+
+ public static void main(String args[]) throws Exception {
+
+ File clientKeystore = new File("certs/clientKeystore.jks");
+ File truststore = new File("certs/commonTruststore.jks");
+
+ // Send HTTP GET request to query customer info - using portable
HttpClient method
+ Protocol authhttps = new Protocol("https",
+ new
AuthSSLProtocolSocketFactory(clientKeystore.toURI().toURL(), "password",
+ truststore.toURI().toURL(), "password"),
+ 9000);
+ Protocol.registerProtocol("https", authhttps);
+
+ System.out.println("Sending HTTPS GET request to query customer info");
+ HttpClient httpclient = new HttpClient();
+ GetMethod httpget = new GetMethod(BASE_SERVICE_URL + "/123");
+ httpget.addRequestHeader("Accept" , "text/xml");
+
+ // If Basic Authentication required could use:
+ /*
+ String authorizationHeader = "Basic "
+ +
org.apache.cxf.common.util.Base64Utility.encode("username:password".getBytes());
+ httpget.addRequestHeader("Authorization", authorizationHeader);
+ */
+ try {
+ httpclient.executeMethod(httpget);
+ System.out.println(httpget.getResponseBodyAsString());
+ } finally {
+ httpget.releaseConnection();
+ }
+
+ /*
+ * 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("Sending 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");
+ System.out.println("Sending 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);
+ }
+}
Added:
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/common/Customer.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/common/Customer.java?rev=1090066&view=auto
==============================================================================
---
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/common/Customer.java
(added)
+++
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/common/Customer.java
Fri Apr 8 00:00:43 2011
@@ -0,0 +1,43 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package httpsdemo.common;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement(name = "Customer")
+public class Customer {
+ private long id;
+ private String name;
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Added:
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/common/CustomerService.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/common/CustomerService.java?rev=1090066&view=auto
==============================================================================
---
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/common/CustomerService.java
(added)
+++
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/common/CustomerService.java
Fri Apr 8 00:00:43 2011
@@ -0,0 +1,52 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package httpsdemo.common;
+
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.core.Response;
+
+/**
+ * This interface describes a JAX-RS root resource. All the JAXRS annotations
(except those overridden) will
+ * be inherited by classes implementing it.
+ */
+@Path("/customerservice/")
+public interface CustomerService {
+
+ @GET
+ @Path("/customers/{id}/")
+ Customer getCustomer(@PathParam("id") String id);
+
+ @PUT
+ @Path("/customers/")
+ Response updateCustomer(Customer customer);
+
+ @POST
+ @Path("/customers/")
+ Response addCustomer(Customer customer);
+
+ @DELETE
+ @Path("/customers/{id}/")
+ Response deleteCustomer(@PathParam("id") String id);
+
+}
Added:
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/server/CustomerServiceImpl.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/server/CustomerServiceImpl.java?rev=1090066&view=auto
==============================================================================
---
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/server/CustomerServiceImpl.java
(added)
+++
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/server/CustomerServiceImpl.java
Fri Apr 8 00:00:43 2011
@@ -0,0 +1,89 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package httpsdemo.server;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ws.rs.core.Response;
+import httpsdemo.common.Customer;
+import httpsdemo.common.CustomerService;
+
+public class CustomerServiceImpl implements CustomerService {
+ long currentId = 123;
+ Map<Long, Customer> customers = new HashMap<Long, Customer>();
+
+ public CustomerServiceImpl() {
+ init();
+ }
+
+ public Customer getCustomer(String id) {
+ System.out.println("----invoking getCustomer, Customer id is: " + id);
+ long idNumber = Long.parseLong(id);
+ Customer c = customers.get(idNumber);
+ return c;
+ }
+
+ public Response updateCustomer(Customer customer) {
+ System.out.println("----invoking updateCustomer, Customer name is: " +
customer.getName());
+ Customer c = customers.get(customer.getId());
+ Response r;
+ if (c != null) {
+ customers.put(customer.getId(), customer);
+ r = Response.ok().build();
+ } else {
+ r = Response.notModified().build();
+ }
+
+ return r;
+ }
+
+ public Response addCustomer(Customer customer) {
+ System.out.println("----invoking addCustomer, Customer name is: " +
customer.getName());
+ customer.setId(++currentId);
+
+ customers.put(customer.getId(), customer);
+
+ return Response.ok(customer).build();
+ }
+
+ public Response deleteCustomer(String id) {
+ System.out.println("----invoking deleteCustomer, Customer id is: " +
id);
+ long idNumber = Long.parseLong(id);
+ Customer c = customers.get(idNumber);
+
+ Response r;
+ if (c != null) {
+ r = Response.ok().build();
+ customers.remove(idNumber);
+ } else {
+ r = Response.notModified().build();
+ }
+
+ return r;
+ }
+
+ final void init() {
+ Customer c = new Customer();
+ c.setName("John");
+ c.setId(123);
+ customers.put(c.getId(), c);
+ }
+
+}
Added:
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/server/Server.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/server/Server.java?rev=1090066&view=auto
==============================================================================
---
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/server/Server.java
(added)
+++
cxf/trunk/distribution/src/main/release/samples/jax_rs/basic_https/src/main/java/httpsdemo/server/Server.java
Fri Apr 8 00:00:43 2011
@@ -0,0 +1,55 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package httpsdemo.server;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
+import org.apache.cxf.bus.spring.SpringBusFactory;
+import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
+
+public class Server {
+
+ static {
+ // set the configuration file
+ SpringBusFactory factory = new SpringBusFactory();
+ Bus bus = factory.createBus("ServerConfig.xml");
+ BusFactory.setDefaultBus(bus);
+ }
+
+ protected Server() throws Exception {
+ JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
+ sf.setResourceClasses(CustomerServiceImpl.class);
+ sf.setResourceProvider(CustomerServiceImpl.class,
+ new SingletonResourceProvider(new CustomerServiceImpl()));
+ sf.setAddress("https://localhost:9000/");
+
+ sf.create();
+ }
+
+ public static void main(String args[]) throws Exception {
+ new Server();
+ System.out.println("Server ready...");
+
+ Thread.sleep(5 * 60 * 1000);
+ System.out.println("Server exiting");
+ System.exit(0);
+ }
+}