You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by alos <al...@gmail.com> on 2013/03/12 01:09:00 UTC

Retrive WSDL using basic authentication

Hey guys!

So I've been working with the JaxWsDynamicClientFactory. I´m always
able to get the WSDL like this:

JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://example.com/EchoService?wsdl");

However a client's new WSDL requires me to pass along an
authentication header in the HTTP message.

Is there a way to do this using the JaxWsDynamicClientFactory??

So far we´ve tried to get a hold of an Interceptor (so I can get the
HTTPRequest object and do my thing) but it looks like these are
created AFTER the client is instanced.

Any help will be appreciated!

Re: Retrive WSDL using basic authentication

Posted by "zynick." <zi...@gmail.com>.
after reading the source, i found the solution:

bus.setExtension(conf, *HTTPConduitConfigurer.class*); 

took me some time to found this.. should have smell the code earlier :)



--
View this message in context: http://cxf.547215.n5.nabble.com/Retrive-WSDL-using-basic-authentication-tp5724416p5727406.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Retrive WSDL using basic authentication

Posted by hackmgnt <zi...@gmail.com>.
Hello,

I have tried what you explained on above but still didn't work. Here's my
code:

import java.util.Arrays;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.CXFBusFactory;
import org.apache.cxf.configuration.security.AuthorizationPolicy;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transport.http.HTTPConduitConfigurer;

public class Authorization {

    public static void main(String[] args) throws Exception {
        String wsdl = "http://localhost/WebService/Service?wsdl";
        String username = "user";
        String password = "password";
        String method = "foo";
        
        Bus bus = CXFBusFactory.getThreadDefaultBus();
        MyHTTPConduitConfigurer conf = new MyHTTPConduitConfigurer(username,
password);
        bus.setExtension(conf, MyHTTPConduitConfigurer.class);
        JaxWsDynamicClientFactory dcf =
JaxWsDynamicClientFactory.newInstance(bus);
        
        Client client = dcf.createClient(wsdl);

        Object[] res = client.invoke(method);
        
        System.out.println(Arrays.deepToString(res));
    }
}

class MyHTTPConduitConfigurer implements HTTPConduitConfigurer {
    
    private final String username;
    private final String password;
    
    public MyHTTPConduitConfigurer(String username, String password) {
        this.username = username;
        this.password = password;
    }
    
    @Override
    public void configure(String name, String address, HTTPConduit c) {
       
System.out.println("AuthorizationHTTPConduitConfigurer.configure()");
        AuthorizationPolicy ap = new AuthorizationPolicy();
        ap.setUserName(username);
        ap.setPassword(password);
        c.setAuthorization(ap);
    }
}


and the result:
Exception in thread "main"
org.apache.cxf.service.factory.ServiceConstructionException: Failed to
create service.
	at
org.apache.cxf.wsdl11.WSDLServiceFactory.<init>(WSDLServiceFactory.java:86)
	at
org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:292)
	at
org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:235)
	at
org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:228)
	at
org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:183)
	at com.ssms.raps.atom.Authorization.main(Authorization.java:27)
Caused by: javax.wsdl.WSDLException: WSDLException: faultCode=PARSER_ERROR:
Problem parsing 'http://192.168.200.221/WebService/Service1.asmx?wsdl'.:
java.io.IOException: Server returned HTTP response code: 401 for URL:
http://192.168.200.221/WebService/Service1.asmx?wsdl
	at com.ibm.wsdl.xml.WSDLReaderImpl.getDocument(WSDLReaderImpl.java:2198)
	at com.ibm.wsdl.xml.WSDLReaderImpl.readWSDL(WSDLReaderImpl.java:2390)
	at com.ibm.wsdl.xml.WSDLReaderImpl.readWSDL(WSDLReaderImpl.java:2422)
	at
org.apache.cxf.wsdl11.WSDLManagerImpl.loadDefinition(WSDLManagerImpl.java:262)
	at
org.apache.cxf.wsdl11.WSDLManagerImpl.getDefinition(WSDLManagerImpl.java:205)
	at
org.apache.cxf.wsdl11.WSDLServiceFactory.<init>(WSDLServiceFactory.java:84)
	... 5 more
Caused by: java.io.IOException: Server returned HTTP response code: 401 for
URL: http://192.168.200.221/WebService/Service1.asmx?wsdl
	at
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1403)
	at
com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:654)
	at
com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:189)
	at
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:783)
	at
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:748)
	at
com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
	at
com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:239)
	at
com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:288)
	at com.ibm.wsdl.xml.WSDLReaderImpl.getDocument(WSDLReaderImpl.java:2188)
	... 10 more


note that
System.out.println("AuthorizationHTTPConduitConfigurer.configure()") in
MyHTTPConduitConfigurer didn't print.

Am I missing something here?



--
View this message in context: http://cxf.547215.n5.nabble.com/Retrive-WSDL-using-basic-authentication-tp5724416p5727057.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Retrive WSDL using basic authentication

Posted by Freeman Fang <fr...@gmail.com>.
Hi,

Ensure the
bus.setExtension(myHttpConduitConfig, MyHTTPConduitConfigurer.class);
before
Client client = dcf.createClient("http://example.com/EchoService?wsdl");

-------------
Freeman(Yue) Fang

Red Hat, Inc. 
FuseSource is now part of Red Hat
Web: http://fusesource.com | http://www.redhat.com/
Twitter: freemanfang
Blog: http://freemanfang.blogspot.com
http://blog.sina.com.cn/u/1473905042
weibo: @Freeman小屋

On 2013-3-14, at 下午11:08, Alos wrote:

> Thanks for all the help! 
> 
> I was unable to register the MyHTTPConduitConfigurer  as there is no way to get a reference to the JaxWsDynamicClientFactory's bus. What I ended up doing was subclassing the JaxWsDynamicClientFactory and overwriting the method where the bus is created! 
> 
> However when I use my new subclass I never see my System.outs that I added to my MyHTTPConduitConfigurer. 
> 
> Any ideas on what might be happening?
> 
> Sent from my iPhone
> 
> On Mar 12, 2013, at 11:57 PM, Freeman Fang <fr...@gmail.com> wrote:
> 
>> Hi,
>> 
>> Yes, you can write your own HTTPConduitConfigurer and register to the bus which you get fromJaxWsDynamicClientFactory name, String address, HTTPConduit c) {
>>         AuthorizationPolicy authorizationPolicy = new AuthorizationPolicy();
>>         authorizationPolicy.setUserName("...");
>>         authorizationPolicy.setPassword("...");
>>         c.setAuthorization(authorizationPolicy);
>>    }
>> }
>> 
>> then register MyHTTPConduitConfigurer to the bus which you get from JaxWsDynamicClientFactory, something like
>> MyHTTPConduitConfigurer myHttpConduitConfig = new MyHTTPConduitConfigurer();
>> bus.setExtension(myHttpConduitConfig, MyHTTPConduitConfigurer.class);
>> 
>> Hope this helps
>> -------------
>> Freeman(Yue) Fang
>> 
>> Red Hat, Inc. 
>> FuseSource is now part of Red Hat
>> Web: http://fusesource.com | http://www.redhat.com/
>> Twitter: freemanfang
>> Blog: http://freemanfang.blogspot.com
>> http://blog.sina.com.cn/u/1473905042
>> weibo: @Freeman小屋
>> 
>> On 2013-3-13, at 上午2:04, Alos wrote:
>> 
>>> This would force me to have a "hard coded" XML with http:conduits for each of the services using this authentication and thus defeating the purpose of my DynamicClient. Is there a way of creating these http:conduits in code? 
>>> 
>>> BTW thanks so much for responding so quickly :-) 
>>> 
>>> Sent from my iPhone
>>> 
>>> On Mar 11, 2013, at 8:40 PM, Freeman Fang <fr...@gmail.com> wrote:
>>> 
>>>> Hi,
>>>> 
>>>> You need add a spring configuration, add something like
>>>> 
>>>> <http:conduit name="http://example.com/.*"> 
>>>> <authorization>
>>>>   <sec:UserName>myuser</sec:UserName>
>>>>   <sec:Password>mypasswd</sec:Password>
>>>>   <sec:AuthorizationType>Basic</sec:AuthorizationType>
>>>> </authorization>
>>>> </http:conduit>
>>>> 
>>>> take a look at [1] to get more details
>>>> 
>>>> [1]http://cxf.apache.org/docs/client-http-transport-including-ssl-support.html
>>>> 
>>>> -------------
>>>> Freeman(Yue) Fang
>>>> 
>>>> Red Hat, Inc. 
>>>> FuseSource is now part of Red Hat
>>>> Web: http://fusesource.com | http://www.redhat.com/
>>>> Twitter: freemanfang
>>>> Blog: http://freemanfang.blogspot.com
>>>> http://blog.sina.com.cn/u/1473905042
>>>> weibo: @Freeman小屋
>>>> 
>>>> On 2013-3-12, at 上午8:09, alos wrote:
>>>> 
>>>>> Hey guys!
>>>>> 
>>>>> So I've been working with the JaxWsDynamicClientFactory. I´m always
>>>>> able to get the WSDL like this:
>>>>> 
>>>>> JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
>>>>> Client client = dcf.createClient("http://example.com/EchoService?wsdl");
>>>>> 
>>>>> However a client's new WSDL requires me to pass along an
>>>>> authentication header in the HTTP message.
>>>>> 
>>>>> Is there a way to do this using the JaxWsDynamicClientFactory??
>>>>> 
>>>>> So far we´ve tried to get a hold of an Interceptor (so I can get the
>>>>> HTTPRequest object and do my thing) but it looks like these are
>>>>> created AFTER the client is instanced.
>>>>> 
>>>>> Any help will be appreciated!
>> 


Re: Retrive WSDL using basic authentication

Posted by Alos <al...@gmail.com>.
Thanks for all the help! 

I was unable to register the MyHTTPConduitConfigurer  as there is no way to get a reference to the JaxWsDynamicClientFactory's bus. What I ended up doing was subclassing the JaxWsDynamicClientFactory and overwriting the method where the bus is created! 

However when I use my new subclass I never see my System.outs that I added to my MyHTTPConduitConfigurer. 

Any ideas on what might be happening?

Sent from my iPhone

On Mar 12, 2013, at 11:57 PM, Freeman Fang <fr...@gmail.com> wrote:

> Hi,
> 
> Yes, you can write your own HTTPConduitConfigurer and register to the bus which you get fromJaxWsDynamicClientFactory name, String address, HTTPConduit c) {
>          AuthorizationPolicy authorizationPolicy = new AuthorizationPolicy();
>          authorizationPolicy.setUserName("...");
>          authorizationPolicy.setPassword("...");
>          c.setAuthorization(authorizationPolicy);
>     }
> }
> 
> then register MyHTTPConduitConfigurer to the bus which you get from JaxWsDynamicClientFactory, something like
> MyHTTPConduitConfigurer myHttpConduitConfig = new MyHTTPConduitConfigurer();
> bus.setExtension(myHttpConduitConfig, MyHTTPConduitConfigurer.class);
> 
> Hope this helps
> -------------
> Freeman(Yue) Fang
> 
> Red Hat, Inc. 
> FuseSource is now part of Red Hat
> Web: http://fusesource.com | http://www.redhat.com/
> Twitter: freemanfang
> Blog: http://freemanfang.blogspot.com
> http://blog.sina.com.cn/u/1473905042
> weibo: @Freeman小屋
> 
> On 2013-3-13, at 上午2:04, Alos wrote:
> 
>> This would force me to have a "hard coded" XML with http:conduits for each of the services using this authentication and thus defeating the purpose of my DynamicClient. Is there a way of creating these http:conduits in code? 
>> 
>> BTW thanks so much for responding so quickly :-) 
>> 
>> Sent from my iPhone
>> 
>> On Mar 11, 2013, at 8:40 PM, Freeman Fang <fr...@gmail.com> wrote:
>> 
>>> Hi,
>>> 
>>> You need add a spring configuration, add something like
>>> 
>>> <http:conduit name="http://example.com/.*"> 
>>> <authorization>
>>>    <sec:UserName>myuser</sec:UserName>
>>>    <sec:Password>mypasswd</sec:Password>
>>>    <sec:AuthorizationType>Basic</sec:AuthorizationType>
>>> </authorization>
>>> </http:conduit>
>>> 
>>> take a look at [1] to get more details
>>> 
>>> [1]http://cxf.apache.org/docs/client-http-transport-including-ssl-support.html
>>> 
>>> -------------
>>> Freeman(Yue) Fang
>>> 
>>> Red Hat, Inc. 
>>> FuseSource is now part of Red Hat
>>> Web: http://fusesource.com | http://www.redhat.com/
>>> Twitter: freemanfang
>>> Blog: http://freemanfang.blogspot.com
>>> http://blog.sina.com.cn/u/1473905042
>>> weibo: @Freeman小屋
>>> 
>>> On 2013-3-12, at 上午8:09, alos wrote:
>>> 
>>>> Hey guys!
>>>> 
>>>> So I've been working with the JaxWsDynamicClientFactory. I´m always
>>>> able to get the WSDL like this:
>>>> 
>>>> JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
>>>> Client client = dcf.createClient("http://example.com/EchoService?wsdl");
>>>> 
>>>> However a client's new WSDL requires me to pass along an
>>>> authentication header in the HTTP message.
>>>> 
>>>> Is there a way to do this using the JaxWsDynamicClientFactory??
>>>> 
>>>> So far we´ve tried to get a hold of an Interceptor (so I can get the
>>>> HTTPRequest object and do my thing) but it looks like these are
>>>> created AFTER the client is instanced.
>>>> 
>>>> Any help will be appreciated!
> 

Re: Retrive WSDL using basic authentication

Posted by Freeman Fang <fr...@gmail.com>.
Hi,

Yes, you can write your own HTTPConduitConfigurer and register to the bus which you get from JaxWsDynamicClientFactory, some code should be like

public class MyHTTPConduitConfigurer implements HTTPConduitConfigurer {
     public void configure(String name, String address, HTTPConduit c) {
          AuthorizationPolicy authorizationPolicy = new AuthorizationPolicy();
          authorizationPolicy.setUserName("...");
          authorizationPolicy.setPassword("...");
          c.setAuthorization(authorizationPolicy);
     }
}

then register MyHTTPConduitConfigurer to the bus which you get from JaxWsDynamicClientFactory, something like
MyHTTPConduitConfigurer myHttpConduitConfig = new MyHTTPConduitConfigurer();
bus.setExtension(myHttpConduitConfig, MyHTTPConduitConfigurer.class);

Hope this helps
-------------
Freeman(Yue) Fang

Red Hat, Inc. 
FuseSource is now part of Red Hat
Web: http://fusesource.com | http://www.redhat.com/
Twitter: freemanfang
Blog: http://freemanfang.blogspot.com
http://blog.sina.com.cn/u/1473905042
weibo: @Freeman小屋

On 2013-3-13, at 上午2:04, Alos wrote:

> This would force me to have a "hard coded" XML with http:conduits for each of the services using this authentication and thus defeating the purpose of my DynamicClient. Is there a way of creating these http:conduits in code? 
> 
> BTW thanks so much for responding so quickly :-) 
> 
> Sent from my iPhone
> 
> On Mar 11, 2013, at 8:40 PM, Freeman Fang <fr...@gmail.com> wrote:
> 
>> Hi,
>> 
>> You need add a spring configuration, add something like
>> 
>> <http:conduit name="http://example.com/.*"> 
>> <authorization>
>>     <sec:UserName>myuser</sec:UserName>
>>     <sec:Password>mypasswd</sec:Password>
>>     <sec:AuthorizationType>Basic</sec:AuthorizationType>
>>  </authorization>
>> </http:conduit>
>> 
>> take a look at [1] to get more details
>> 
>> [1]http://cxf.apache.org/docs/client-http-transport-including-ssl-support.html
>> 
>> -------------
>> Freeman(Yue) Fang
>> 
>> Red Hat, Inc. 
>> FuseSource is now part of Red Hat
>> Web: http://fusesource.com | http://www.redhat.com/
>> Twitter: freemanfang
>> Blog: http://freemanfang.blogspot.com
>> http://blog.sina.com.cn/u/1473905042
>> weibo: @Freeman小屋
>> 
>> On 2013-3-12, at 上午8:09, alos wrote:
>> 
>>> Hey guys!
>>> 
>>> So I've been working with the JaxWsDynamicClientFactory. I´m always
>>> able to get the WSDL like this:
>>> 
>>> JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
>>> Client client = dcf.createClient("http://example.com/EchoService?wsdl");
>>> 
>>> However a client's new WSDL requires me to pass along an
>>> authentication header in the HTTP message.
>>> 
>>> Is there a way to do this using the JaxWsDynamicClientFactory??
>>> 
>>> So far we´ve tried to get a hold of an Interceptor (so I can get the
>>> HTTPRequest object and do my thing) but it looks like these are
>>> created AFTER the client is instanced.
>>> 
>>> Any help will be appreciated!
>> 


Re: Retrive WSDL using basic authentication

Posted by Alos <al...@gmail.com>.
This would force me to have a "hard coded" XML with http:conduits for each of the services using this authentication and thus defeating the purpose of my DynamicClient. Is there a way of creating these http:conduits in code? 

BTW thanks so much for responding so quickly :-) 

Sent from my iPhone

On Mar 11, 2013, at 8:40 PM, Freeman Fang <fr...@gmail.com> wrote:

> Hi,
> 
> You need add a spring configuration, add something like
> 
> <http:conduit name="http://example.com/.*"> 
> <authorization>
>      <sec:UserName>myuser</sec:UserName>
>      <sec:Password>mypasswd</sec:Password>
>      <sec:AuthorizationType>Basic</sec:AuthorizationType>
>   </authorization>
> </http:conduit>
> 
> take a look at [1] to get more details
> 
> [1]http://cxf.apache.org/docs/client-http-transport-including-ssl-support.html
> 
> -------------
> Freeman(Yue) Fang
> 
> Red Hat, Inc. 
> FuseSource is now part of Red Hat
> Web: http://fusesource.com | http://www.redhat.com/
> Twitter: freemanfang
> Blog: http://freemanfang.blogspot.com
> http://blog.sina.com.cn/u/1473905042
> weibo: @Freeman小屋
> 
> On 2013-3-12, at 上午8:09, alos wrote:
> 
>> Hey guys!
>> 
>> So I've been working with the JaxWsDynamicClientFactory. I´m always
>> able to get the WSDL like this:
>> 
>> JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
>> Client client = dcf.createClient("http://example.com/EchoService?wsdl");
>> 
>> However a client's new WSDL requires me to pass along an
>> authentication header in the HTTP message.
>> 
>> Is there a way to do this using the JaxWsDynamicClientFactory??
>> 
>> So far we´ve tried to get a hold of an Interceptor (so I can get the
>> HTTPRequest object and do my thing) but it looks like these are
>> created AFTER the client is instanced.
>> 
>> Any help will be appreciated!
> 

Re: Retrive WSDL using basic authentication

Posted by Freeman Fang <fr...@gmail.com>.
Hi,

You need add a spring configuration, add something like

<http:conduit name="http://example.com/.*"> 
<authorization>
      <sec:UserName>myuser</sec:UserName>
      <sec:Password>mypasswd</sec:Password>
      <sec:AuthorizationType>Basic</sec:AuthorizationType>
   </authorization>
</http:conduit>

take a look at [1] to get more details

[1]http://cxf.apache.org/docs/client-http-transport-including-ssl-support.html

-------------
Freeman(Yue) Fang

Red Hat, Inc. 
FuseSource is now part of Red Hat
Web: http://fusesource.com | http://www.redhat.com/
Twitter: freemanfang
Blog: http://freemanfang.blogspot.com
http://blog.sina.com.cn/u/1473905042
weibo: @Freeman小屋

On 2013-3-12, at 上午8:09, alos wrote:

> Hey guys!
> 
> So I've been working with the JaxWsDynamicClientFactory. I´m always
> able to get the WSDL like this:
> 
> JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
> Client client = dcf.createClient("http://example.com/EchoService?wsdl");
> 
> However a client's new WSDL requires me to pass along an
> authentication header in the HTTP message.
> 
> Is there a way to do this using the JaxWsDynamicClientFactory??
> 
> So far we´ve tried to get a hold of an Interceptor (so I can get the
> HTTPRequest object and do my thing) but it looks like these are
> created AFTER the client is instanced.
> 
> Any help will be appreciated!