Author: ningjiang
Date: Wed Jun 18 23:13:55 2008
New Revision: 669396
URL: http://svn.apache.org/viewvc?rev=669396&view=rev
Log:
CXF-1617 support set basic authentication from ProxyFactoryBean
Modified:
cxf/trunk/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/ClientFactoryBean.java
cxf/trunk/rt/frontend/simple/src/test/java/org/apache/cxf/frontend/spring/SpringBeansTest.java
cxf/trunk/rt/frontend/simple/src/test/java/org/apache/cxf/frontend/spring/clients.xml
cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitTest.java
Modified:
cxf/trunk/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/ClientFactoryBean.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/ClientFactoryBean.java?rev=669396&r1=669395&r2=669396&view=diff
==============================================================================
---
cxf/trunk/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/ClientFactoryBean.java
(original)
+++
cxf/trunk/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/ClientFactoryBean.java
Wed Jun 18 23:13:55 2008
@@ -21,6 +21,7 @@
import java.util.Map;
import org.apache.cxf.BusException;
+import org.apache.cxf.configuration.security.AuthorizationPolicy;
import org.apache.cxf.databinding.DataBinding;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.ClientImpl;
@@ -33,23 +34,23 @@
public class ClientFactoryBean extends AbstractWSDLBasedEndpointFactory {
private Client client;
-
+
public ClientFactoryBean() {
this(new ReflectionServiceFactoryBean());
}
- public ClientFactoryBean(ReflectionServiceFactoryBean factory) {
+ public ClientFactoryBean(ReflectionServiceFactoryBean factory) {
super(factory);
}
public Client create() {
-
+
if (client != null) {
return client;
}
applyExtraClass();
try {
Endpoint ep = createEndpoint();
-
+ applyProperties(ep);
createClient(ep);
initializeAnnotationInterceptors(ep, getServiceClass());
} catch (EndpointException e) {
@@ -72,7 +73,7 @@
}
}
}
-
+
protected void applyExtraClass() {
DataBinding dataBinding = getServiceFactory().getDataBinding();
if (dataBinding instanceof JAXBDataBinding) {
@@ -83,12 +84,22 @@
}
}
}
-
+
+
+ protected void applyProperties(Endpoint ep) {
+ //Apply the AuthorizationPolicy to the endpointInfo
+ Map props = this.getProperties();
+ if (props != null && props.get(AuthorizationPolicy.class.getName()) !=
null) {
+ AuthorizationPolicy ap =
(AuthorizationPolicy)props.get(AuthorizationPolicy.class.getName());
+ ep.getEndpointInfo().addExtensor(ap);
+ }
+ }
+
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
- }
+ }
}
Modified:
cxf/trunk/rt/frontend/simple/src/test/java/org/apache/cxf/frontend/spring/SpringBeansTest.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/simple/src/test/java/org/apache/cxf/frontend/spring/SpringBeansTest.java?rev=669396&r1=669395&r2=669396&view=diff
==============================================================================
---
cxf/trunk/rt/frontend/simple/src/test/java/org/apache/cxf/frontend/spring/SpringBeansTest.java
(original)
+++
cxf/trunk/rt/frontend/simple/src/test/java/org/apache/cxf/frontend/spring/SpringBeansTest.java
Wed Jun 18 23:13:55 2008
@@ -27,6 +27,7 @@
import org.apache.cxf.binding.soap.SoapBindingConfiguration;
import org.apache.cxf.binding.soap.saaj.SAAJInInterceptor;
import org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor;
+import org.apache.cxf.configuration.security.AuthorizationPolicy;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.NullConduitSelector;
import org.apache.cxf.frontend.ClientProxy;
@@ -37,66 +38,67 @@
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.service.factory.HelloService;
import org.apache.cxf.service.factory.HelloServiceImpl;
+import org.apache.cxf.service.model.EndpointInfo;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringBeansTest extends Assert {
-
+
@Test
public void testServers() throws Exception {
- ClassPathXmlApplicationContext ctx =
+ ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext(new String[]
{"/org/apache/cxf/frontend/spring/servers.xml"});
ServerFactoryBean bean = (ServerFactoryBean) ctx.getBean("simple");
assertNotNull(bean);
-
+
if (!(bean.getServiceBean() instanceof HelloServiceImpl)) {
fail("can't get the right serviceBean");
}
bean = (ServerFactoryBean) ctx.getBean("inlineImplementor");
if (!(bean.getServiceBean() instanceof HelloServiceImpl)) {
fail("can't get the right serviceBean");
- }
+ }
bean = (ServerFactoryBean) ctx.getBean("inlineSoapBinding");
- assertNotNull(bean);
-
-
-
+ assertNotNull(bean);
+
+
+
BindingConfiguration bc = bean.getBindingConfig();
assertTrue(bc instanceof SoapBindingConfiguration);
SoapBindingConfiguration sbc = (SoapBindingConfiguration) bc;
assertTrue(sbc.getVersion() instanceof Soap12);
-
+
bean = (ServerFactoryBean) ctx.getBean("simpleWithBindingId");
- assertEquals("get the wrong BindingId",
+ assertEquals("get the wrong BindingId",
bean.getBindingId(),
"http://cxf.apache.org/bindings/xformat");
-
+
}
-
+
@Test
public void testClients() throws Exception {
- ClassPathXmlApplicationContext ctx =
+ ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext(new String[]
{"/org/apache/cxf/frontend/spring/clients.xml"});
Object bean = ctx.getBean("client1.proxyFactory");
assertNotNull(bean);
-
- ClientProxyFactoryBean cpfbean = (ClientProxyFactoryBean)bean;
+
+ ClientProxyFactoryBean cpfbean = (ClientProxyFactoryBean)bean;
BindingConfiguration bc = cpfbean.getBindingConfig();
assertTrue(bc instanceof SoapBindingConfiguration);
SoapBindingConfiguration sbc = (SoapBindingConfiguration) bc;
assertTrue(sbc.getVersion() instanceof Soap12);
-
+
HelloService greeter = (HelloService) ctx.getBean("client1");
assertNotNull(greeter);
-
+
Client client = ClientProxy.getClient(greeter);
assertNotNull("expected ConduitSelector", client.getConduitSelector());
assertTrue("unexpected ConduitSelector",
client.getConduitSelector() instanceof NullConduitSelector);
-
+
List<Interceptor> inInterceptors = client.getInInterceptors();
boolean saaj = false;
boolean logging = false;
@@ -109,7 +111,7 @@
}
assertTrue(saaj);
assertTrue(logging);
-
+
saaj = false;
logging = false;
for (Interceptor<?> i : client.getOutInterceptors()) {
@@ -121,16 +123,26 @@
}
assertTrue(saaj);
assertTrue(logging);
-
- ClientProxyFactoryBean clientProxyFactoryBean =
+
+ ClientProxyFactoryBean clientProxyFactoryBean =
(ClientProxyFactoryBean) ctx.getBean("client2.proxyFactory");
assertNotNull(clientProxyFactoryBean);
-
- assertEquals("get the wrong bindingId",
+
+ assertEquals("get the wrong bindingId",
clientProxyFactoryBean.getBindingId(),
"http://cxf.apache.org/bindings/xformat");
-
+
greeter = (HelloService) ctx.getBean("client2");
assertNotNull(greeter);
+
+ greeter = (HelloService) ctx.getBean("client3");
+ assertNotNull(greeter);
+
+ client = ClientProxy.getClient(greeter);
+ EndpointInfo epi = client.getEndpoint().getEndpointInfo();
+ AuthorizationPolicy ap = epi.getExtensor(AuthorizationPolicy.class);
+ assertNotNull("The AuthorizationPolicy instance should not be null",
ap);
+ assertEquals("Get the wrong username", ap.getUserName(), "testUser");
+ assertEquals("Get the wrong password", ap.getPassword(), "password");
}
}
Modified:
cxf/trunk/rt/frontend/simple/src/test/java/org/apache/cxf/frontend/spring/clients.xml
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/simple/src/test/java/org/apache/cxf/frontend/spring/clients.xml?rev=669396&r1=669395&r2=669396&view=diff
==============================================================================
---
cxf/trunk/rt/frontend/simple/src/test/java/org/apache/cxf/frontend/spring/clients.xml
(original)
+++
cxf/trunk/rt/frontend/simple/src/test/java/org/apache/cxf/frontend/spring/clients.xml
Wed Jun 18 23:13:55 2008
@@ -29,7 +29,7 @@
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-xml.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
-
+
<bean class="org.apache.cxf.transport.local.LocalTransportFactory"
lazy-init="false">
<property name="transportIds">
<list>
@@ -40,11 +40,11 @@
</list>
</property>
</bean>
-
+
<bean id="saajIn"
class="org.apache.cxf.binding.soap.saaj.SAAJInInterceptor"/>
<bean id="saajOut"
class="org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor"/>
-
- <simple:client id="client1"
+
+ <simple:client id="client1"
serviceClass="org.apache.cxf.service.factory.HelloService"
address="http://localhost:9000/foo"
serviceName="s:SOAPService"
@@ -64,13 +64,20 @@
<bean class="org.apache.cxf.endpoint.NullConduitSelector"/>
</simple:conduitSelector>
</simple:client>
-
- <simple:client id="client2"
+
+ <simple:client id="client2"
serviceClass="org.apache.cxf.service.factory.HelloService"
address="http://localhost:9000/foo2"
serviceName="s:XMLService"
xmlns:s="http://apache.org/hello_world_soap_http"
- endpointName="s:XMLPort"
- bindingId="http://cxf.apache.org/bindings/xformat">
+ endpointName="s:XMLPort"
+ bindingId="http://cxf.apache.org/bindings/xformat">
</simple:client>
+
+ <simple:client id="client3"
+ serviceClass="org.apache.cxf.service.factory.HelloService"
+ address="http://localhost:9000/foo2"
+ username="testUser"
+ password="password">
+ </simple:client>
</beans>
Modified:
cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitTest.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitTest.java?rev=669396&r1=669395&r2=669396&view=diff
==============================================================================
---
cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitTest.java
(original)
+++
cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitTest.java
Wed Jun 18 23:13:55 2008
@@ -55,11 +55,11 @@
public class HTTPConduitTest extends Assert {
private Message inMessage;
private IMocksControl control;
-
+
@Before
public void setUp() throws Exception {
}
-
+
@After
public void tearDown() {
}
@@ -77,9 +77,9 @@
message.put(Message.PROTOCOL_HEADERS, headers);
return message;
}
-
+
/**
- * This test class is a Basic Auth Supplier with a
+ * This test class is a Basic Auth Supplier with a
* preemptive UserPass.
*/
class BasicAuthSupplier extends HttpBasicAuthSupplier {
@@ -104,14 +104,14 @@
ei.setAddress("http://nowhere.com/bar/foo");
HTTPConduit conduit = new HTTPConduit(bus, ei, null);
conduit.finalizeConfig();
-
- EndpointReferenceType target =
+
+ EndpointReferenceType target =
EndpointReferenceUtils.getEndpointReference(
"http://nowhere.com/bar/foo");
-
+
// Test call
EndpointReferenceType ref = conduit.getTarget();
-
+
assertNotNull("unexpected null target", ref);
assertEquals("unexpected target",
EndpointReferenceUtils.getAddress(ref),
@@ -126,7 +126,7 @@
conduit.getURL().getPath(),
"/bar/foo");
}
-
+
/**
* Verfies one of the tenents of our interface -- the Conduit sets up
* an OutputStream on the message after a "prepare".
@@ -140,18 +140,45 @@
conduit.finalizeConfig();
Message message = getNewMessage();
-
+
// Test call
conduit.prepare(message);
-
- assertNotNull("Conduit should always set output stream.",
+
+ assertNotNull("Conduit should always set output stream.",
message.getContent(OutputStream.class));
}
+ @Test
+ public void testAuthPolicyFromEndpointInfo() throws Exception {
+ Bus bus = new CXFBusImpl();
+ EndpointInfo ei = new EndpointInfo();
+ AuthorizationPolicy ap = new AuthorizationPolicy();
+ ap.setPassword("password");
+ ap.setUserName("testUser");
+ ei.addExtensor(ap);
+ ei.setAddress("http://nowhere.com/bar/foo");
+ HTTPConduit conduit = new HTTPConduit(bus, ei, null);
+ conduit.finalizeConfig();
+ Message message = getNewMessage();
+
+ // Test call
+ conduit.prepare(message);
+
+ Map<String, List<String>> headers =
+ CastUtils.cast((Map<?, ?>)message.get(Message.PROTOCOL_HEADERS));
+
+ assertNotNull("Authorization Header should exist",
+ headers.get("Authorization"));
+
+ assertEquals("Unexpected Authorization Token",
+ "Basic " +
Base64Utility.encode("testUser:password".getBytes()),
+ headers.get("Authorization").get(0));
+ }
+
/**
* This test verifies the precidence of Authorization Information.
* Setting authorization information on the Message takes precidence
- * over a Basic Auth Supplier with preemptive UserPass, and that
+ * over a Basic Auth Supplier with preemptive UserPass, and that
* followed by setting it directly on the Conduit.
*/
@Test
@@ -161,58 +188,58 @@
ei.setAddress("http://nowhere.com/bar/foo");
HTTPConduit conduit = new HTTPConduit(bus, ei, null);
conduit.finalizeConfig();
-
+
conduit.getAuthorization().setUserName("Satan");
conduit.getAuthorization().setPassword("hell");
Message message = getNewMessage();
-
+
// Test call
conduit.prepare(message);
-
+
Map<String, List<String>> headers =
CastUtils.cast((Map<?, ?>)message.get(Message.PROTOCOL_HEADERS));
-
+
assertNotNull("Authorization Header should exist",
headers.get("Authorization"));
-
- assertEquals("Unexpected Authorization Token",
+
+ assertEquals("Unexpected Authorization Token",
"Basic " + Base64Utility.encode("Satan:hell".getBytes()),
headers.get("Authorization").get(0));
-
+
// Setting a Basic Auth User Pass should override
conduit.setBasicAuthSupplier(new BasicAuthSupplier());
message = getNewMessage();
-
+
// Test Call
conduit.prepare(message);
-
+
headers =
CastUtils.cast((Map<?, ?>)message.get(Message.PROTOCOL_HEADERS));
- assertEquals("Unexpected Authorization Token",
+ assertEquals("Unexpected Authorization Token",
"Basic " + Base64Utility.encode("Gandalf:staff".getBytes()),
headers.get("Authorization").get(0));
-
+
// Setting authorization policy on the message should override all.
AuthorizationPolicy authPolicy = new AuthorizationPolicy();
authPolicy.setUserName("Hello");
authPolicy.setPassword("world");
message = getNewMessage();
message.put(AuthorizationPolicy.class, authPolicy);
-
+
conduit.prepare(message);
-
+
headers =
CastUtils.cast((Map<?, ?>)message.get(Message.PROTOCOL_HEADERS));
-
- assertEquals("Unexpected Authorization Token",
+
+ assertEquals("Unexpected Authorization Token",
"Basic " + Base64Utility.encode("Hello:world".getBytes()),
headers.get("Authorization").get(0));
}
public void testDecoupledEndpoint() throws Exception {
control = EasyMock.createNiceControl();
-
+
Bus bus = new CXFBusImpl();
URL decoupledURL = new URL("http://nowhere.com/response");
@@ -230,31 +257,31 @@
EasyMock.expectLastCall().andReturn(destination);
destination.setMessageObserver(
EasyMock.isA(HTTPConduit.InterposedMessageObserver.class));
-
+
control.replay();
-
+
EndpointInfo ei = new EndpointInfo();
ei.setAddress("http://nowhere.com/bar/foo");
HTTPConduit conduit = new HTTPConduit(bus, ei, null);
conduit.finalizeConfig();
-
+
// Test call
conduit.getClient().setDecoupledEndpoint(decoupledURL.toString());
-
+
assertNotNull("expected back channel", conduit.getBackChannel());
-
+
MessageObserver observer = new MessageObserver() {
public void onMessage(Message m) {
inMessage = m;
}
};
-
+
// Test call
conduit.setMessageObserver(observer);
Message incoming = new MessageImpl();
conduit.getDecoupledObserver().onMessage(incoming);
-
+
assertSame("expected pass thru onMessage() notification",
inMessage,
incoming);
@@ -287,5 +314,5 @@
control = null;
}
}
-
+
}