Samples JDK
Server.java
1 package com.freemindcafe.apache.cxf.wsdl.sample1;
2 
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.net.URL;
8 import java.security.GeneralSecurityException;
9 import java.security.KeyStore;
10 import java.security.KeyStoreException;
11 import java.security.NoSuchAlgorithmException;
12 import java.security.UnrecoverableKeyException;
13 import java.security.cert.CertificateException;
14 import java.util.HashMap;
15 import java.util.Map;
16 
17 import javax.net.ssl.KeyManager;
18 import javax.net.ssl.KeyManagerFactory;
19 import javax.net.ssl.TrustManager;
20 import javax.net.ssl.TrustManagerFactory;
21 import javax.xml.namespace.QName;
22 
23 import org.apache.cxf.Bus;
24 import org.apache.cxf.BusFactory;
25 import org.apache.cxf.configuration.jsse.TLSServerParameters;
26 import org.apache.cxf.configuration.security.ClientAuthentication;
27 import org.apache.cxf.configuration.security.FiltersType;
28 import org.apache.cxf.interceptor.LoggingInInterceptor;
29 import org.apache.cxf.interceptor.LoggingOutInterceptor;
30 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
31 import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
32 import org.apache.cxf.transport.http_jetty.JettyHTTPServerEngineFactory;
37 import org.junit.Test;
38 
39 public class Server {
40 
41  /**
42  * It has basic auth, wsse security token as in interceptor.
43  * It also has out and fault interceptors.
44  * Interceptors prints messages on server console.
45  */
46  @Test
47  public void start_server_without_ssl() throws Exception{
48 
49  GreeterImpl implementor = new GreeterImpl();
50  JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
51  svrFactory.setServiceClass(Greeter.class);
52  svrFactory.setAddress("http://localhost:9001/SoapContext/SoapPort");
53  svrFactory.setServiceBean(implementor);
54  //in interceptors
55  svrFactory.getInInterceptors().add(new LoggingInInterceptor());
56  svrFactory.getInInterceptors().add(new BasicAuthInterceptor());
57  Map<String, Object> properties = new HashMap<>();
58  properties.put("action", "UsernameToken");
59  properties.put("passwordType", "PasswordText");
60  properties.put("passwordCallbackRef", new SecurityContextCallback());
61  svrFactory.getInInterceptors().add(new WSSecurityInterceptor(properties));
62  // out normal response interceptor
63  svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
64  svrFactory.getOutInterceptors().add(new SoapOutSecurityInterceptor());
65  //out fault interceptor
66  svrFactory.getOutFaultInterceptors().add(new SoapOutSecurityFaultInterceptor());
67 
68  org.apache.cxf.endpoint.Server server = svrFactory.create();
69  String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
70  System.out.println("Server started at " + endpoint);
71 
72  synchronized(server){
73  server.wait();
74  }
75  }
76 
77  /**
78  * @throws Exception
79  */
80  @Test
82 
83  GreeterImpl implementor = new GreeterImpl();
84  JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
85  svrFactory.setServiceClass(Greeter.class);
86  svrFactory.setAddress("http://localhost:9001/SoapContext/SoapPort");
87  svrFactory.setServiceBean(implementor);
88  //in interceptors
89  svrFactory.getInInterceptors().add(new LoggingInInterceptor());
90  svrFactory.getInInterceptors().add(new BasicAuthInterceptor());
91  Map<String, Object> properties = new HashMap<>();
92  properties.put("action", "UsernameToken");
93  properties.put("passwordType", "PasswordText");
94  properties.put("passwordCallbackRef", new SecurityContextCallback());
95  svrFactory.getInInterceptors().add(new WSSecurityInterceptor(properties));
96  // out normal response interceptor
97  svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
98  svrFactory.getOutInterceptors().add(new SoapOutSecurityInterceptor());
99  //out fault interceptor
100  svrFactory.getOutFaultInterceptors().add(new SoapOutSecurityFaultInterceptor());
101 
102  org.apache.cxf.endpoint.Server server = svrFactory.create();
103  String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
104  System.out.println("Server started at " + endpoint);
105 
106  //client
107  JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
108  factory.getInInterceptors().add(new LoggingInInterceptor());
109  factory.getOutInterceptors().add(new LoggingOutInterceptor());
110  factory.setServiceClass(Greeter.class);
111  factory.setAddress("http://localhost:9000/SoapContext/SoapPort");
112  Greeter client = (Greeter) factory.create();
113 
114  client.greetMeOneWay("abc");
115 
116  synchronized(server){
117  server.wait();
118  }
119 
120  }
121 
122  /**
123  * @throws Exception
124  */
125  @Test
127 
128  Bus bus = BusFactory.getDefaultBus();
129 
130  GreeterImpl implementor = new GreeterImpl();
131  JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
132  svrFactory.setServiceClass(Greeter.class);
133  svrFactory.setAddress("http://localhost:9001/SoapContext/SoapPort");
134  svrFactory.setServiceBean(implementor);
135  //in interceptors
136  bus.getInInterceptors().add(new LoggingInInterceptor());
137  bus.getInInterceptors().add(new BasicAuthInterceptor());
138  Map<String, Object> properties = new HashMap<>();
139  properties.put("action", "UsernameToken");
140  properties.put("passwordType", "PasswordText");
141  properties.put("passwordCallbackRef", new SecurityContextCallback());
142  bus.getInInterceptors().add(new WSSecurityInterceptor(properties));
143  // out normal response interceptor
144  bus.getOutInterceptors().add(new LoggingOutInterceptor());
145  bus.getOutInterceptors().add(new SoapOutSecurityInterceptor());
146  //out fault interceptor
147  bus.getOutFaultInterceptors().add(new SoapOutSecurityFaultInterceptor());
148 
149  org.apache.cxf.endpoint.Server server = svrFactory.create();
150  String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
151  System.out.println("Server started at " + endpoint);
152 
153  //client
154  JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
155  factory.getInInterceptors().add(new LoggingInInterceptor());
156  factory.getOutInterceptors().add(new LoggingOutInterceptor());
157  factory.setServiceClass(Greeter.class);
158  factory.setAddress("http://localhost:9000/SoapContext/SoapPort");
159  Greeter client = (Greeter) factory.create();
160 
161  client.greetMeOneWay("abc");
162 
163  synchronized(server){
164  server.wait();
165  }
166 
167  }
168 
169  /**
170  * @throws Exception
171  * Invocation order of interceptors depends on the phase.
172  * Please refer to http://cxf.apache.org/docs/interceptors.html for the Phase ordering
173  * Since Phase.PRE_PROTOCOL comes before Phase.UNMARSHAL, SSL interceptor and WSSecurityInterceptor will be invoked first.
174  * If two interceptors share the same Phase, then they will be invoked in the order they
175  * are attched to the bus. Whichsoever is atatched first is invoked first.
176  */
177  @Test
178  public void start_server_with_2_way_ssl() throws Exception{
179  GreeterImpl implementor = new GreeterImpl();
180  JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
181  svrFactory.setServiceClass(Greeter.class);
182  svrFactory.setAddress("https://localhost:9001/SoapContext/SoapPort");
183  svrFactory.setServiceBean(implementor);
184 
185  //in interceptors
186  //Order of interceptors is decided by phases
187  //RECEIVE -> (PRE/USER/POST)_PROTOCOL (This could have muliple interceptors) -> UNMARSHAL
188  svrFactory.getInInterceptors().add(new LoggingInInterceptor());
189  svrFactory.getInInterceptors().add(new BasicAuthInterceptor());
190  Map<String, Object> properties = new HashMap<>();
191  properties.put("action", "UsernameToken");
192  properties.put("passwordType", "PasswordText");
193  properties.put("passwordCallbackRef", new SecurityContextCallback());
194  svrFactory.getInInterceptors().add(new WSSecurityInterceptor(properties));
195  svrFactory.getInInterceptors().add(new SSLInterceptor());
196  // out normal response interceptor
197  svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
198  svrFactory.getOutInterceptors().add(new SoapOutSecurityInterceptor());
199  //out fault interceptor
200  svrFactory.getOutFaultInterceptors().add(new SoapOutSecurityFaultInterceptor());
201 
202  svrFactory = configureSSLOnTheServer(svrFactory, 9001);
203  org.apache.cxf.endpoint.Server server = svrFactory.create();
204  String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
205  System.out.println("Server started at " + endpoint);
206  synchronized(server){
207  server.wait();
208  }
209  }
210 
211 
212  private JaxWsServerFactoryBean configureSSLOnTheServer(JaxWsServerFactoryBean sf, int port) {
213  try {
214  System.setProperty("javax.net.debug", "ssl:handshake");
215  TLSServerParameters tlsParams = new TLSServerParameters();
216  KeyStore keyStore = KeyStore.getInstance("JKS");
217  String password = "password";
218  File keystoreFile = new File("src\\com\\freemindcafe\\apache\\cxf\\wsdl\\sample1\\serverkeystore.jks");
219  keyStore.load(new FileInputStream(keystoreFile), password.toCharArray());
220  KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
221  keyFactory.init(keyStore, password.toCharArray());
222  KeyManager[] km = keyFactory.getKeyManagers();
223  tlsParams.setKeyManagers(km);
224 
225  File truststoreFile = new File("src\\com\\freemindcafe\\apache\\cxf\\wsdl\\sample1\\serverkeystore.jks");
226  keyStore.load(new FileInputStream(truststoreFile), password.toCharArray());
227  TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
228  trustFactory.init(keyStore);
229  TrustManager[] tm = trustFactory.getTrustManagers();
230  tlsParams.setTrustManagers(tm);
231 // FiltersType filter = new FiltersType();
232 // filter.getInclude().add(".*_EXPORT_.*");
233 // filter.getInclude().add(".*_EXPORT1024_.*");
234 // filter.getInclude().add(".*_WITH_DES_.*");
235 // filter.getInclude().add(".*_WITH_NULL_.*");
236 // filter.getExclude().add(".*_DH_anon_.*");
237 // tlsParams.setCipherSuitesFilter(filter);
238  ClientAuthentication ca = new ClientAuthentication();
239  ca.setRequired(true);
240  ca.setWant(true);
241  tlsParams.setClientAuthentication(ca);
242  JettyHTTPServerEngineFactory factory = new JettyHTTPServerEngineFactory();
243  factory.setTLSServerParametersForPort(port, tlsParams);
244  } catch (KeyStoreException kse) {
245  System.out.println("Security configuration failed with the following: " + kse.getCause());
246  } catch (NoSuchAlgorithmException nsa) {
247  System.out.println("Security configuration failed with the following: " + nsa.getCause());
248  } catch (FileNotFoundException fnfe) {
249  System.out.println("Security configuration failed with the following: " + fnfe.getCause());
250  } catch (UnrecoverableKeyException uke) {
251  System.out.println("Security configuration failed with the following: " + uke.getCause());
252  } catch (CertificateException ce) {
253  System.out.println("Security configuration failed with the following: " + ce.getCause());
254  } catch (GeneralSecurityException gse) {
255  System.out.println("Security configuration failed with the following: " + gse.getCause());
256  } catch (IOException ioe) {
257  System.out.println("Security configuration failed with the following: " + ioe.getCause());
258  }
259 
260  return sf;
261  }
262 
263 }
void start_server_without_ssl_and_use_client_in_same_jvm_with_interceptors_on_endpoint()
Definition: Server.java:81
void start_server_without_ssl_and_use_client_in_same_jvm_with_interceptors_on_bus()
Definition: Server.java:126