kimhorn wrote:
>
> How do you access the Message Context with the POJOCommand. The mediator
> in discussion
> writes properties into the TRANSPORT header for the message sent to
> targetWeb Services.
>
> Kim
>
> How would you implement this :
>
>
> package net.icsglobal.thelma.synapse;
>
> import java.util.Map;
>
> import org.apache.axiom.om.util.Base64;
> import org.apache.commons.logging.Log;
> import org.apache.commons.logging.LogFactory;
> import org.apache.synapse.Mediator;
> import org.apache.synapse.MessageContext;
> import org.apache.synapse.SynapseException;
> import org.apache.synapse.core.axis2.Axis2MessageContext;
>
> /**
> * This is a simple HTTP basic authentication mediator. It must be used in
> a try
> * tag. The properties reqUsername must be provided in the synapse
> * configuration. Property password is optional and will not be checked if
> it is
> * not provided. If the authorization fails a faultmessage may be
> generated in
> * the onerror child tag of the try tag.
> */
> public class BasicAuthenticationMediator implements Mediator {
> private static final Log log = LogFactory
> .getLog(BasicAuthenticationMediator.class);
>
> public int getTraceState() {
> return 0;
> }
>
> public void setTraceState(int traceState) {
> traceState = 0;
> }
>
> /**
> * The username to check for. Should be provided in <class> tag as
> property
> */
> private String reqUsername = null;
>
> /**
> * The password to check for (cleartext). Should be provided in <class>
> tag
> * as property. If not set the username will be checked only!
> */
> private String reqPassword = null;
>
> public void setReqPassword(String reqPassword) {
> this.reqPassword = reqPassword;
> }
>
> public void setReqUsername(String reqUsername) {
> this.reqUsername = reqUsername;
> }
>
> public String getType() {
> return this.getClass().getName();
> }
>
> public boolean mediate(MessageContext synCtx) {
> String headerUsername = null;
> String headerPassword = null;
>
> log.debug("BasicAuthenticationMediator: Authentication
> started...");
> if (reqUsername == null) {
> throw new SynapseException("reqUsername property MUST
> be provided!");
> }
>
> // 1) extract authentication headers
> org.apache.axis2.context.MessageContext mc =
> ((Axis2MessageContext)
> synCtx)
> .getAxis2MessageContext();
> Map map = (Map) mc
>
> .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
> String sAuth = (String) map.get("Authorization");
> if (sAuth == null) {
> throw new SynapseException(
> "No authorization headers found! Please
> check if the client sends
> basic authentication headers!");
> }
> sAuth = sAuth.trim();
>
> // 2) extract username & password (if provided)
> if (sAuth.startsWith("Basic ")) {
> sAuth = new String(Base64.decode(sAuth.substring(6)));
> int i = sAuth.indexOf(':');
>
> if (i == -1) {
> headerUsername = sAuth;
> } else {
> headerUsername = sAuth.substring(0, i);
> headerPassword = sAuth.substring(i + 1);
> if (headerPassword != null &&
> headerPassword.equals("")) {
> headerPassword = null;
> }
> }
> }
>
> // 3) check the authentication...
> if (headerUsername == null || reqUsername == null) {
> throw new SynapseException("Authorization failed!");
> }
>
> if (!reqUsername.equals(headerUsername)) {
> throw new SynapseException("Authorization failed!");
> }
>
> if (reqPassword != null) {
> if (!reqPassword.equals(headerPassword)) {
> throw new SynapseException("Authorization
> failed!");
> }
> }
>
> log.debug("BasicAuthenticationMediator: Authentication
> succeeded!");
>
> // Authentication succeeded
> return true;
> }
>
> }
>
>
>
>
>
>
> Ruwan Linton wrote:
>>
>> Charith,
>>
>> I think the question is on setting the properties to the class (meaning
>> invoking the setters)
>>
>> So if you have them in the message as properties, then you could use a
>> pojoCommand mediator [1] and use the expression attrbiute to specify an
>> xpath to extract the values stored in the message. If that is stored as a
>> property in the message context you could user the get-property function
>> by
>> passing the property name as the expression.
>>
>> [1] -
>> http://synapse.apache.org/Synapse_Configuration_Language.html#pojoCommand
>>
>> Thanks,
>> Ruwan
>>
>> On Mon, Mar 9, 2009 at 8:35 AM, Charith Wickramarachchi <
>> [email protected]> wrote:
>>
>>> Hi Kimhorn,
>>>
>>> AFAIK the class mediator use setter injection to set the properties in
>>> the
>>> class.
>>>
>>> this implies your class must have a setter for every property that you
>>> are
>>> going to populate from out side.
>>>
>>> ex property int abc---> setter : setAbc(int abc)
>>>
>>> In your case if the properties reqUsername and reqPassword have been set
>>> earlier or (set from the out side )you can ommit them in the
>>> confguration
>>>
>>> ex:
>>> <class name="net.icsglobal.thelma.synapse.CreateBasicAuthMediator">
>>> </class>
>>> this will work fine if your properties are set from outside;
>>>
>>> thank you,
>>>
>>> Charith
>>>
>>>
>>>
>>>
>>> On Mon, Mar 9, 2009 at 8:08 AM, kimhorn <[email protected]> wrote:
>>>
>>> >
>>> > I have a problem calling a Java class when the property values are set
>>> > elsewhere
>>> > in script. For example Username and Password are set earlier. How do I
>>> pass
>>> > them to my class, without specifying then as a Value ?
>>> >
>>> > Obviously this works:
>>> >
>>> > <class name="net.icsglobal.thelma.synapse.CreateBasicAuthMediator">
>>> > <property name="reqUsername" value="username"/>
>>> > <property name="reqPassword" value="password0"/>
>>> > </class>
>>> >
>>> > What do I do if reqUsername and reqUsername have been set earlier ?
>>> >
>>> > <class name="net.icsglobal.thelma.synapse.CreateBasicAuthMediator">
>>> > <property name="reqUsername" />
>>> > <property name="reqPassword" />
>>> > </class>
>>> >
>>> > Doesn't work ?
>>> >
>>> > I would like these to be set by java code.
>>> > Setting these values, in text, each time is not secure.
>>> >
>>> >
>>> >
>>> >
>>> >
>>> >
>>> >
>>> >
>>> > --
>>> > View this message in context:
>>> >
>>> http://www.nabble.com/Java-Class-Properties-Setting-tp22406236p22406236.html
>>> > Sent from the Synapse - User mailing list archive at Nabble.com.
>>> >
>>> >
>>>
>>>
>>> --
>>> Charith Dhanushka Wickramarachchi
>>> http://charithwiki.blogspot.com/
>>>
>>
>>
>>
>> --
>> Ruwan Linton
>> http://wso2.org - "Oxygenating the Web Services Platform"
>> http://ruwansblog.blogspot.com/
>>
>>
>
>
--
View this message in context:
http://www.nabble.com/Java-Class-Properties-Setting-tp22406236p22409512.html
Sent from the Synapse - User mailing list archive at Nabble.com.