HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setAllowChunking(false);
conduit.setClient(httpClientPolicy);
Could you take a quick look at the code below and let me know if
there is anything I'm doing that I shouldn't be doing or should be
doing differently?
Also, please let me know if there is anything I should look at to
determine why it isn't serializing the response back correctly. I
have full debug logging on in log4j.properties.
Thanks in advance,
Gary
Here are the classes:
TestClient.java
===========
...
import org.apache.cxf.configuration.jsse.TLSClientParameters;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor;
import org.apache.cxf.binding.soap.saaj.SAAJInInterceptor;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;
import javax.xml.ws.BindingProvider;
import java.util.HashMap;
import java.util.Map;
...
public class TestClient {
private static String URL = "https://some.host:1234/path/to/the/service
";
private TestClient() {
}
public static MyServiceResponseTypeShape sendRequest(String
someRequestParam) throws Exception {
MyService service = new MyService();
MyServicePortType portType = service.getMyServicePort();
Client client = ClientProxy.getClient(portType);
Endpoint endpoint = client.getEndpoint();
// Enable Logging
// from http://cwiki.apache.org/CXF20DOC/debugging.html
client.getInInterceptors().add(new LoggingInInterceptor());
client.getOutInterceptors().add(new LoggingOutInterceptor());
HTTPConduit conduit = (HTTPConduit) client.getConduit();
// for more info see:
http://cwiki.apache.org/confluence/display/CXF20DOC/Client+HTTP+Transport+%28including+SSL+support%29
// set timeout
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
//httpClientPolicy.setConnectionTimeout(36000);
httpClientPolicy.setAllowChunking(false);
//httpClientPolicy.setReceiveTimeout(32000);
conduit.setClient(httpClientPolicy);
// set endpoint, since the default one in the wsdl has wrong
protocol and port
TLSClientParameters params = conduit.getTlsClientParameters();
if (params == null) {
params = new TLSClientParameters();
conduit.setTlsClientParameters(params);
}
// NOTE! ONLY DO THIS FOR TESTING, NOT PRODUCTION!
// this is to get around the error:
// The https URL hostname does not match the Common Name (CN)
on the server certificate. To disable this check (NOT recommended
for production) set the CXF client TLS configuration property
"disableCNCheck" to true.
params.setDisableCNCheck(true);
// override the endpoint that is defined in WSDL as http with
old port to be https with new port ((BindingProvider)
portType
).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
URL);
// set username and password
// see: http://cwiki.apache.org/CXF20DOC/ws-security.html
// No security on client-side
Map<String, Object> inProps = new HashMap<String, Object>();
inProps.put(WSHandlerConstants.ACTION,
WSHandlerConstants.NO_SECURITY);
WSS4JInInterceptor wssIn = new WSS4JInInterceptor(inProps);
endpoint.getInInterceptors().add(wssIn);
endpoint.getInInterceptors().add(new SAAJInInterceptor()); //
2.0.x only; not needed in 2.1+
// Server-side authN
Map<String, Object> outProps = new HashMap<String, Object>();
outProps.put(WSHandlerConstants.ACTION,
WSHandlerConstants.USERNAME_TOKEN);
outProps.put(WSHandlerConstants.USER, "my_username");
outProps.put(WSHandlerConstants.PASSWORD_TYPE,
WSConstants.PW_TEXT);
//outProps.put(WSHandlerConstants.PASSWORD_TYPE,
WSConstants.PW_DIGEST);
outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS,
MyCallbackHandler.class.getName());
WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
endpoint.getOutInterceptors().add(wssOut);
endpoint.getOutInterceptors().add(new
SAAJOutInterceptor()); // 2.0.x only; not needed in 2.1+
// note: createMyServiceRequestTypeShape() is a method on this
class that creates the request object
MyServiceRequestTypeShape req =
createMyServiceRequestTypeShape(someRequestParam);
return portType.doSomething(req);
}
public MyServiceRequestTypeShape
createMyServiceRequestTypeShape(String someRequestParam) {
...
}
}
MyCallbackHandler.java
==================
...
import org.apache.ws.security.WSPasswordCallback;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.callback.CallbackHandler;
import java.io.IOException;
public class MyCallbackHandler implements CallbackHandler {
public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException {
WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
// set the password for our message.
pc.setPassword("my_password");
}
}
pom.xml
======
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd
">
<modelVersion>4.0.0</modelVersion>
<groupId>cxftestclient</groupId>
<artifactId>cxftestclient</artifactId>
<packaging>jar</packaging>
<name>CXF Integration Test</name>
<version>1.0-SNAPSHOT</version>
<properties>
<cxf.version>2.1.1-SNAPSHOT</cxf.version>
</properties>
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>logkit</groupId>
<artifactId>logkit</artifactId>
</exclusion>
<exclusion>
<groupId>avalon-framework</groupId>
<artifactId>avalon-framework</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-security</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- Jetty is needed if you're are not using the CXFServlet -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${basedir}/target/generated/
src/main/java</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/wsdl/
myService.wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<reportFormat>brief</reportFormat>
<useFile>false</useFile>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>central-m2-repository</id>
<name>Central Maven 2 Repository</name>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>apache.org</id>
<name>Maven Snapshots</name>
<url>http://people.apache.org/repo/m2-snapshot-repository</
url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central-m2-repository</id>
<name>Central Maven 2 Repository</name>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>apache.org</id>
<name>Maven Plugin Snapshots</name>
<url>http://people.apache.org/repo/m2-snapshot-repository</
url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>