You are viewing a plain text version of this content. The canonical link for it is here.
Posted to soap-user@ws.apache.org by Rina Rivera <rr...@momentumsoftware.com> on 2001/03/08 23:25:37 UTC

How to Prefix/Qualify Parameters in a Request?

The schema of the webservice I am trying to invoke specifies
elementFormDefault='qualified'. What is the correct way to specify the
prefix for the parameters I send in the request?

I could hard-code the prefix in the name of the parameter, because I already
know that SOAP will call it "ns1". For example:

Parameter param = new Parameter("ns1:MyParam", Integer.class, new
Integer(1), null);


But there has to be a better way. I should be able to get the prefix
corresponding to a given namespace URI. I have been looking at the SOAP
source code and I can tell that, if only I had access to the namespace stack
that gets passed to the marshall methods, I could find the correct prefix.

Am I missing something here? Is there a way I can get the information I need
from the Call object I create to invoke the webservice?

Thanks in advance,
Rina


RE: How to Prefix/Qualify Parameters in a Request?

Posted by Rina Rivera <rr...@momentumsoftware.com>.
Hi,

I was wondering if my message got thru the first time around since I have
not seen a response to my question. Again, I would like to know if there is
a correct way, using Apache-SOAP, to specify the prefix of a qualified
parameter. In other words, how do I create a request with

	<ns1:myparameter ...>...</ns1:myparameter>

instead of just

	<myparameter ...>...</myparameter>

I have been thinking about this problem for a while and I think that
Apache-SOAP should supply a Parameter constructor that takes a QName,
instead of a String, as the name of the parameter. In addition,
org.apache.soap.encoding.soapenc.ParameterSerializer should prefix the
parameter according to the URI specified by the Parameter, if one exists. Of
course, this is a suggestion based upon the premise that there is not a
"right" way to prefix parameters currently. If anybody knows of one, PLEASE
RESPOND TO THIS MESSAGE. Otherwise I will send my request to soap-dev.

In the meantime, I have found a workaround for my problem which I would like
to share with the rest of you. It is a bit more elegant than hardcoding
"ns1:" in the name of my parameters ;-)

In order to qualify my parameters I did the following:
	1. Created a subclass of ParameterSerializer. This class keeps track of the
namespace URI of the parameters and takes care of adding the correct prefix
to the parameter name.
	2. Registered an instance of my serializer in the SOAPMappingRegistry of my
Call object.

Here is the Serializer implementation:

public class PrefixedParameterSerializer extends ParameterSerializer {

  private String NamespaceURI = null;

  public PrefixedParameterSerializer() {
  }

  public PrefixedParameterSerializer(String namespaceURI) {
    this.NamespaceURI = namespaceURI;
  }

  public void marshall(String inScopeEncStyle, Class javaType, Object src,
                       Object context, Writer sink, NSStack nsStack,
                       XMLJavaMappingRegistry xjmr, SOAPContext ctx)
    throws IllegalArgumentException, IOException
  {
    Parameter param = (Parameter)src;
    String paramName = param.getName();

    if(this.NamespaceURI != null) {
      String prefix = nsStack.getPrefixFromURI(this.NamespaceURI);
      if(prefix != null && !prefix.trim().equals("")) {
        param.setName(prefix + ":" + paramName);
      }
    }

    super.marshall(inScopeEncStyle, javaType, src, context, sink,
                   nsStack, xjmr, ctx);
  }
}

In my client, I do the following:

    SOAPMappingRegistry smr = new SOAPMappingRegistry();

    // Define the serializers for the prefixed parameters
    PrefixedParameterSerializer serializer =
        new PrefixedParameterSerializer(myServiceDefinitionURI);
    smr.mapTypes(Constants.NS_URI_SOAP_ENC, RPCConstants.Q_ELEM_PARAMETER,
                 Parameter.class, serializer, serializer);
    ...
    // Build the call.
    Call call = new Call();
	...
    call.setSOAPMappingRegistry(smr);


-----Original Message-----
From: Rina Rivera [mailto:rrivera@momentumsoftware.com]
Sent: Thursday, March 08, 2001 4:26 PM
To: soap-user@xml.apache.org
Subject: How to Prefix/Qualify Parameters in a Request?


The schema of the webservice I am trying to invoke specifies
elementFormDefault='qualified'. What is the correct way to specify the
prefix for the parameters I send in the request?

I could hard-code the prefix in the name of the parameter, because I already
know that SOAP will call it "ns1". For example:

Parameter param = new Parameter("ns1:MyParam", Integer.class, new
Integer(1), null);


But there has to be a better way. I should be able to get the prefix
corresponding to a given namespace URI. I have been looking at the SOAP
source code and I can tell that, if only I had access to the namespace stack
that gets passed to the marshall methods, I could find the correct prefix.

Am I missing something here? Is there a way I can get the information I need
from the Call object I create to invoke the webservice?

Thanks in advance,
Rina



RE: How to Prefix/Qualify Parameters in a Request?

Posted by Rina Rivera <rr...@momentumsoftware.com>.
Hi,

I was wondering if my message got thru the first time around since I have
not seen a response to my question. Again, I would like to know if there is
a correct way, using Apache-SOAP, to specify the prefix of a qualified
parameter. In other words, how do I create a request with

	<ns1:myparameter ...>...</ns1:myparameter>

instead of just

	<myparameter ...>...</myparameter>

I have been thinking about this problem for a while and I think that
Apache-SOAP should supply a Parameter constructor that takes a QName,
instead of a String, as the name of the parameter. In addition,
org.apache.soap.encoding.soapenc.ParameterSerializer should prefix the
parameter according to the URI specified by the Parameter, if one exists. Of
course, this is a suggestion based upon the premise that there is not a
"right" way to prefix parameters currently. If anybody knows of one, PLEASE
RESPOND TO THIS MESSAGE. Otherwise I will send my request to soap-dev.

In the meantime, I have found a workaround for my problem which I would like
to share with the rest of you. It is a bit more elegant than hardcoding
"ns1:" in the name of my parameters ;-)

In order to qualify my parameters I did the following:
	1. Created a subclass of ParameterSerializer. This class keeps track of the
namespace URI of the parameters and takes care of adding the correct prefix
to the parameter name.
	2. Registered an instance of my serializer in the SOAPMappingRegistry of my
Call object.

Here is the Serializer implementation:

public class PrefixedParameterSerializer extends ParameterSerializer {

  private String NamespaceURI = null;

  public PrefixedParameterSerializer() {
  }

  public PrefixedParameterSerializer(String namespaceURI) {
    this.NamespaceURI = namespaceURI;
  }

  public void marshall(String inScopeEncStyle, Class javaType, Object src,
                       Object context, Writer sink, NSStack nsStack,
                       XMLJavaMappingRegistry xjmr, SOAPContext ctx)
    throws IllegalArgumentException, IOException
  {
    Parameter param = (Parameter)src;
    String paramName = param.getName();

    if(this.NamespaceURI != null) {
      String prefix = nsStack.getPrefixFromURI(this.NamespaceURI);
      if(prefix != null && !prefix.trim().equals("")) {
        param.setName(prefix + ":" + paramName);
      }
    }

    super.marshall(inScopeEncStyle, javaType, src, context, sink,
                   nsStack, xjmr, ctx);
  }
}

In my client, I do the following:

    SOAPMappingRegistry smr = new SOAPMappingRegistry();

    // Define the serializers for the prefixed parameters
    PrefixedParameterSerializer serializer =
        new PrefixedParameterSerializer(myServiceDefinitionURI);
    smr.mapTypes(Constants.NS_URI_SOAP_ENC, RPCConstants.Q_ELEM_PARAMETER,
                 Parameter.class, serializer, serializer);
    ...
    // Build the call.
    Call call = new Call();
	...
    call.setSOAPMappingRegistry(smr);


-----Original Message-----
From: Rina Rivera [mailto:rrivera@momentumsoftware.com]
Sent: Thursday, March 08, 2001 4:26 PM
To: soap-user@xml.apache.org
Subject: How to Prefix/Qualify Parameters in a Request?


The schema of the webservice I am trying to invoke specifies
elementFormDefault='qualified'. What is the correct way to specify the
prefix for the parameters I send in the request?

I could hard-code the prefix in the name of the parameter, because I already
know that SOAP will call it "ns1". For example:

Parameter param = new Parameter("ns1:MyParam", Integer.class, new
Integer(1), null);


But there has to be a better way. I should be able to get the prefix
corresponding to a given namespace URI. I have been looking at the SOAP
source code and I can tell that, if only I had access to the namespace stack
that gets passed to the marshall methods, I could find the correct prefix.

Am I missing something here? Is there a way I can get the information I need
from the Call object I create to invoke the webservice?

Thanks in advance,
Rina