You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by ellen <el...@163.com> on 2015/12/08 13:14:34 UTC

Dynamically set policy via message property

Hi Dear all,

I'm using CXF and WS-Policy for my application.

And I want to dynamically set WS-Policy by uisng your way:
http://cxf.apache.org/docs/how-to-define-policies.html

But I have some questions. could you please help me confirm? Appreciate it!
:)

*1, Need I do in both Client and Server side?*
Parse and Store result Policy object into PolicyConstants.POLICY_OVERRIDE
message content property.
Add an interceptor to do that.
I can  easily add an interceptor in client side, but how can I do for server
side?


*2, Can I control which service or operation to use this policy?*

e.g. when using WSDL, we can add the policy into a service/operation, like:
<wsdl:operation name="sayHelloToOther">
        <wsp:PolicyReference URI="#myPolicy"
				wsdl:required="true" />
	<soap:operation soapAction="" style="document" />
......

So can I use this dynamic way and add policy to the fine-grained operation
level?

Or every request will parse and use this policy?


Thanks a lot! :)



--
View this message in context: http://cxf.547215.n5.nabble.com/Dynamically-set-policy-via-message-property-tp5763695.html
Sent from the cxf-user mailing list archive at Nabble.com.

RE: Dynamically set policy via message property

Posted by ellen <el...@163.com>.
Sorry Andrei,

Another question, I can only define one policy in POLICY_OVERRIDE file,
right?

If I want to use two policies such as ws-security and ws-addressing, can I
also define in a policy.xml?

Or I need two policy.xml files?

Thanks! :)



--
View this message in context: http://cxf.547215.n5.nabble.com/Dynamically-set-policy-via-message-property-tp5763695p5764187.html
Sent from the cxf-user mailing list archive at Nabble.com.

RE: Dynamically set policy via message property

Posted by ellen <el...@163.com>.
Hi Andrei,

Thanks a lot for your suggestion. :)

I found a good way to do that, cxf has a
org.apache.cxf.ws.policy.attachment.external.ExternalAttachmentProvider,
which can read external policy attachment file, like:
<attachments xmlns:wsp="http://www.w3.org/ns/ws-policy"
xmlns:test="http://x.y.z/Assertions">
    <wsp:PolicyAttachment>
        <wsp:AppliesTo>
            <wsa:EndpointReferenceType
xmlns:wsa="http://www.w3.org/2005/08/addressing">
                <wsa:Address>http://x.y.z/GreeterPort</wsa:Address>
            </wsa:EndpointReferenceType>
        </wsp:AppliesTo>
        <wsp:Policy>
            <test:A>A</test:A>
        </wsp:Policy>
    </wsp:PolicyAttachment>
</attachments>

This is what I want, I think it can only be used in Spring config:
<externalAttachment/>

I think I can re-write it and register this provider for my cxf bus.

Haha, let me try!! :)

Do you know it?

Thanks a lot! :)



--
View this message in context: http://cxf.547215.n5.nabble.com/Dynamically-set-policy-via-message-property-tp5763695p5764263.html
Sent from the cxf-user mailing list archive at Nabble.com.

RE: Dynamically set policy via message property

Posted by Andrei Shakirin <as...@talend.com>.
Hi Ellen,

> But you know, without WSDL file, I can only define policy by using
> POLICY_OVERRIDE, but I don't know where to define the WS-Policy
> attachments relationship between policy and operation.

This isn't completely correct. In Java first approach you still can define WS-Polices using @Policies and @Policy annotations in your java code.
See system test for details:
 https://github.com/apache/cxf/blob/master/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/policy/JavaFirstPolicyService.java 
https://github.com/apache/cxf/blob/master/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/policy/JavaFirstPolicyServiceTest.java

I would recommend to use this approach in your case.

POLICY_OVERRIDE makes sense only, if polices must be composed or updated dynamically when Service operation is invoked.

Regards,
Andrei.

> -----Original Message-----
> From: ellen [mailto:ellenxiao0220@163.com]
> Sent: Montag, 21. Dezember 2015 14:51
> To: users@cxf.apache.org
> Subject: RE: Dynamically set policy via message property
> 
> Thanks a lot Andrei! :)
> 
> This is about a requirement from our end user.
> 
> User defines their web service provider without using WSDL (also doesn't
> use Spring), they just use @WebService annotation to define service, so they
> cannot get WSDL file and cannot add any policy in the WSDL file.
> 
> But they also want to use Policy such as WS-Security for their application.
> 
> And define some policy on operation level.
> 
> I have know know to define and add a policy by using POLICY_OVERRIDE, like
> this:
> https://github.com/ashakirin/cxf.howtos/blob/master/ws-
> policy.dynamic/src/main/java/com/example/customerservice/interceptors/
> DynamicPolicyInInterceptor.java
> 
> But you know, without WSDL file, I can only define policy by using
> POLICY_OVERRIDE, but I don't know where to define the WS-Policy
> attachments relationship between policy and operation.
> 
> So you know I want to decide at runtime. :)
> 
> I have a solution: create a new file(such as named attachment.xml) and
> define the policy and operation/service relationship, like this:
> wspolicy1|service1:operation1
> wspolicy2|service2:operation1
> 
> Then I read this relationship and check in my DynamicPolicyInInterceptor if
> this operation need to bind the policy or not.
> 
> 
> So I would like to know if this is my right way to do that? or CXF has a better
> way?
> 
> Thanks a lot! :)
> 
> 
> 
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Dynamically-
> set-policy-via-message-property-tp5763695p5764182.html
> Sent from the cxf-user mailing list archive at Nabble.com.

RE: Dynamically set policy via message property

Posted by ellen <el...@163.com>.
Thanks a lot Andrei! :)

This is about a requirement from our end user.

User defines their web service provider without using WSDL (also doesn't use
Spring), they just use @WebService annotation to define service, so they
cannot get WSDL file and cannot add any policy in the WSDL file.

But they also want to use Policy such as WS-Security for their application.

And define some policy on operation level.

I have know know to define and add a policy by using POLICY_OVERRIDE, like
this:
https://github.com/ashakirin/cxf.howtos/blob/master/ws-policy.dynamic/src/main/java/com/example/customerservice/interceptors/DynamicPolicyInInterceptor.java

But you know, without WSDL file, I can only define policy by using
POLICY_OVERRIDE, but I don't know where to define the WS-Policy attachments
relationship between policy and operation.

So you know I want to decide at runtime. :)

I have a solution: create a new file(such as named attachment.xml) and
define the policy and operation/service relationship, like this:
wspolicy1|service1:operation1
wspolicy2|service2:operation1

Then I read this relationship and check in my DynamicPolicyInInterceptor if
this operation need to bind the policy or not.


So I would like to know if this is my right way to do that? or CXF has a
better way?

Thanks a lot! :)



--
View this message in context: http://cxf.547215.n5.nabble.com/Dynamically-set-policy-via-message-property-tp5763695p5764182.html
Sent from the cxf-user mailing list archive at Nabble.com.

RE: Dynamically set policy via message property

Posted by Andrei Shakirin <as...@talend.com>.
Hi Ellen,

> But how to decide which operation should apply this policy?

It is your design decision which service operations have to be assumed with the WS-Policies. 
For example, write operations may be authenticated, this can be unnecessary for read operations of your service.
 
> Can I define it in somewhere? So that I can know the operation:policy
> relationship then decide in my interceptor? Such as:
> Service1:Operation1:policy1
> Service1:Operation2:policy2

Well, normally this kind of relationship is defined in WSDL directly or using WS-Policy attachments http://www.w3.org/TR/ws-policy-attach/ .
If you know at design time which policy are applied to service operations - this is the best way to proceed, because you don't need any own interceptors: CXF will get this definition and apply the policies automatically.
But as far as I understood, you would like to decide at runtime which policy have to be applied. In this case you really need to add own interceptor and use PolicyConstants.POLICY_OVERRIDE message property.
Could explain your use case a bit more detailed, that I can help you more effectively?

Regards,
Andrei.


> -----Original Message-----
> From: ellen [mailto:ellenxiao0220@163.com]
> Sent: Freitag, 18. Dezember 2015 13:13
> To: users@cxf.apache.org
> Subject: RE: Dynamically set policy via message property
> 
> Thanks a lot Andrei! :)
> 
> BTW, about the question 2:
> 
> You said:
> Yes, basically you can determine service operation in your interceptor and
> decide (on the base of operation) which parsed policy should be applied.
> 
> But how to decide which operation should apply this policy?
> 
> Can I define it in somewhere? So that I can know the operation:policy
> relationship then decide in my interceptor? Such as:
> 
> Service1:Operation1:policy1
> Service1:Operation2:policy2
> 
> 
> Thanks a lot! :)
> 
> 
> 
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Dynamically-
> set-policy-via-message-property-tp5763695p5764163.html
> Sent from the cxf-user mailing list archive at Nabble.com.

RE: Dynamically set policy via message property

Posted by ellen <el...@163.com>.
Thanks a lot Andrei! :)

BTW, about the question 2:

You said:
Yes, basically you can determine service operation in your interceptor and
decide (on the base of operation) which parsed policy should be applied. 

But how to decide which operation should apply this policy?

Can I define it in somewhere? So that I can know the operation:policy
relationship then decide in my interceptor? Such as:

Service1:Operation1:policy1
Service1:Operation2:policy2


Thanks a lot! :)



--
View this message in context: http://cxf.547215.n5.nabble.com/Dynamically-set-policy-via-message-property-tp5763695p5764163.html
Sent from the cxf-user mailing list archive at Nabble.com.

RE: Dynamically set policy via message property

Posted by Andrei Shakirin <as...@talend.com>.
Hi Ellen,

See the answers below:

> -----Original Message-----
> From: ellen [mailto:ellenxiao0220@163.com]
> Sent: Dienstag, 8. Dezember 2015 13:15
> To: users@cxf.apache.org
> Subject: Dynamically set policy via message property
> 
> Hi Dear all,
> 
> I'm using CXF and WS-Policy for my application.
> 
> And I want to dynamically set WS-Policy by uisng your way:
> http://cxf.apache.org/docs/how-to-define-policies.html
> 
> But I have some questions. could you please help me confirm? Appreciate it!
> :)
> 
> *1, Need I do in both Client and Server side?* Parse and Store result Policy
> object into PolicyConstants.POLICY_OVERRIDE message content property.
> Add an interceptor to do that.
> I can  easily add an interceptor in client side, but how can I do for server side?

You can add interceptors on client as well as on the server side. There are some ways to do that:
a) Spring/blueprint configuration
b) programmatically through serverFactoryBean
c) using WS-Policy

Some ways are described in http://cxf.apache.org/docs/interceptors.html

> 
> 
> *2, Can I control which service or operation to use this policy?*
> 
> e.g. when using WSDL, we can add the policy into a service/operation, like:
> <wsdl:operation name="sayHelloToOther">
>         <wsp:PolicyReference URI="#myPolicy"
> 				wsdl:required="true" />
> 	<soap:operation soapAction="" style="document" /> ......
> 
> So can I use this dynamic way and add policy to the fine-grained operation
> level?

Yes, basically you can determine service operation in your interceptor and decide (on the base of operation) which parsed policy should be applied.
For example this code extract operation name from the service model:

   	           if ((message.getExchange() != null) &&
    			(message.getExchange().getBindingOperationInfo() != null) &&
    			(message.getExchange().getBindingOperationInfo().getOperationInfo() != null)) {
    		       QName operationName = message.getExchange().getBindingOperationInfo().getOperationInfo().getName();
                        }


> 
> Or every request will parse and use this policy?

The policy set using PolicyConstants.POLICY_OVERRIDE are valid only for current message.

> 
> 
> Thanks a lot! :)
> 
You welcome.

Regards,
Andrei.

> 
> 
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Dynamically-
> set-policy-via-message-property-tp5763695.html
> Sent from the cxf-user mailing list archive at Nabble.com.