I am new to Axis2 and Webservices. Would someone know how to add key-valye in http header using axis 2 generated stubs?
I was looking for a method like httpheader in stub._getServiceClient() but cant find one.
Any help is appreciated,
Thanks
shivendra tripathi
Ranch Hand
Joined: Aug 26, 2008
Posts: 263
posted
0
Try adding it in message context. I tried code mentioned below and was able to see http headers in TCP monitor.
Sorry for my ignorance, but what actually
is "res"?
shivendra tripathi
Ranch Hand
Joined: Aug 26, 2008
Posts: 263
posted
0
res is referring to OperationContext, though it's not a good naming convention. I would have referred it with oc
ethan james
Greenhorn
Joined: Mar 28, 2010
Posts: 6
posted
0
Hi Shivendra,
I did something like:
OperationContext operationContext = stub._getServiceClient().getLastOperationContext();
MessageContext mc = new MessageContext();
mc.setEnvelope(mc.getEnvelope()); // where to retrieve the getEnvelope()?
operationContext.addMessageContext(mc);
List headers = new ArrayList();
headers.add(new Header("http_header1", "value1"));
headers.add(new Header("http_header2", "value2"));
mc.setProperty(HTTPConstants.HTTP_HEADERS, headers);
operationContext.addMessageContext(mc);
This is from the client side, but its throwing an error, am i doing something wrong?
Thanks
shivendra tripathi
Ranch Hand
Joined: Aug 26, 2008
Posts: 263
posted
0
I can help you only if you can post the stack trace of the error.
ethan james
Greenhorn
Joined: Mar 28, 2010
Posts: 6
posted
0
Hi Shivendra,
OperationContext operationContext = stub._getServiceClient().getLastOperationContext();
MessageContext mc = new MessageContext();
mc.setEnvelope(mc.getEnvelope());
operationContext.addMessageContext(mc); // this line throws java.lang.NullPointerException
INFO [org.apache.axis2.deployment.ModuleDeployer] Deploying module: metadataExchange-1.5 - file:/home/developer1/Desktop/lib/mex-1.5.jar
java.lang.NullPointerException
Hope you can help.
Thanks
shivendra tripathi
Ranch Hand
Joined: Aug 26, 2008
Posts: 263
posted
0
Can you provide your stub code and complete stack trace of the error? And some details about what are you trying to do? If you are doing wsdl2java please provide your wsdl.
Thanks
-SHIV
ethan james
Greenhorn
Joined: Mar 28, 2010
Posts: 6
posted
0
Hi Shivendra,
I am trying to put username/password into Http, though it might not be the correct implementation, just want to know that it can be done.
public static void main(String[] args) throws Exception {
try {
HelloWorldAuthenticateImplServiceStub stub =
new HelloWorldAuthenticateImplServiceStub();
getGreeting(stub);
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void getGreeting(HelloWorldAuthenticateImplServiceStub stub)
throws AxisFault {
HelloWorldAuthenticateImplServiceStub.GreetingE req =
new HelloWorldAuthenticateImplServiceStub.GreetingE();
HelloWorldAuthenticateImplServiceStub.Greeting param =
new HelloWorldAuthenticateImplServiceStub.Greeting();
param.setArg0("ETHAN");
req.setGreeting(param);
HelloWorldAuthenticateImplServiceStub.GreetingResponseE res = null;
OperationContext operationContext = stub._getServiceClient().getLastOperationContext();
MessageContext mc = new MessageContext();
mc.setEnvelope(mc.getEnvelope());
List headers = new ArrayList();
headers.add(new Header("username", "myusername"));
headers.add(new Header("password", "mypassword"));
mc.setProperty(HTTPConstants.HTTP_HEADERS, headers);
operationContext.addMessageContext(mc);
I asked for your stub code because following line is creating problem.
And since getLastOperationContext() is not part of axis API my assumption is, we need to make some changes in this. As name suggest it return you the last OperationContext but if this is your fist operation then what? I guess it will return null and that is what happening here. It will be good if you post your stub and code related to creation of operation context.
ethan james
Greenhorn
Joined: Mar 28, 2010
Posts: 6
posted
0
Hi,
This is the stub generated, sorry for it is very long,
t
/**
* HelloWorldAuthenticateImplServiceStub.java
*
* This file was auto-generated from WSDL
* by the Apache Axis2 version: 1.5.1 Built on : Oct 19, 2009 (10:59:00 EDT)
*/
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
public class HelloWorldAuthenticateImplServiceStub extends org.apache.axis2.client.Stub
{
protected org.apache.axis2.description.AxisOperation[] _operations;
//creating the Service with a unique name
_service = new org.apache.axis2.description.AxisService("HelloWorldAuthenticateImplService" + getUniqueSuffix());
addAnonymousOperations();
//creating the operations
org.apache.axis2.description.AxisOperation __operation;
_operations = new org.apache.axis2.description.AxisOperation[1];
__operation = new org.apache.axis2.description.OutInAxisOperation();
//adding SOAP soap_headers
_serviceClient.addHeadersToEnvelope(env);
// set the message context with that soap envelope
_messageContext.setEnvelope(env);
// add the message contxt to the operation client
_operationClient.addMessageContext(_messageContext);
//execute the operation client
_operationClient.execute(true);
org.apache.axiom.om.OMElement faultElt = f.getDetail();
if (faultElt!=null){
if (faultExceptionNameMap.containsKey(faultElt.getQName())){
//make the fault by reflection
try{
java.lang.String exceptionClassName = (java.lang.String)faultExceptionClassNameMap.get(faultElt.getQName());
java.lang.Class exceptionClass = java.lang.Class.forName(exceptionClassName);
java.lang.Exception ex=
(java.lang.Exception) exceptionClass.newInstance();
//message class
java.lang.String messageClassName = (java.lang.String)faultMessageMap.get(faultElt.getQName());
java.lang.Class messageClass = java.lang.Class.forName(messageClassName);
java.lang.Object messageObject = fromOM(faultElt,messageClass,null);
java.lang.reflect.Method m = exceptionClass.getMethod("setFaultMessage",
new java.lang.Class[]{messageClass});
m.invoke(ex,new java.lang.Object[]{messageObject});
throw new java.rmi.RemoteException(ex.getMessage(), ex);
}catch(java.lang.ClassCastException e){
// we cannot intantiate the class - throw the original Axis fault
throw f;
} catch (java.lang.ClassNotFoundException e) {
// we cannot intantiate the class - throw the original Axis fault
throw f;
}catch (java.lang.NoSuchMethodException e) {
// we cannot intantiate the class - throw the original Axis fault
throw f;
} catch (java.lang.reflect.InvocationTargetException e) {
// we cannot intantiate the class - throw the original Axis fault
throw f;
} catch (java.lang.IllegalAccessException e) {
// we cannot intantiate the class - throw the original Axis fault
throw f;
} catch (java.lang.InstantiationException e) {
// we cannot intantiate the class - throw the original Axis fault
throw f;
}
}else{
throw f;
}
}else{
throw f;
}
} finally {
_messageContext.getTransportOut().getSender().cleanup(_messageContext);
}
}
/**
* Auto generated method signature for Asynchronous Invocations
*
* @see sample.Client.HelloWorldAuthenticateImplService#startgreeting
* @param greeting0
// create SOAP envelope with that payload
org.apache.axiom.soap.SOAPEnvelope env=null;
final org.apache.axis2.context.MessageContext _messageContext = new org.apache.axis2.context.MessageContext();
public void onError(java.lang.Exception error) {
if (error instanceof org.apache.axis2.AxisFault) {
org.apache.axis2.AxisFault f = (org.apache.axis2.AxisFault) error;
org.apache.axiom.om.OMElement faultElt = f.getDetail();
if (faultElt!=null){
if (faultExceptionNameMap.containsKey(faultElt.getQName())){
//make the fault by reflection
try{
java.lang.String exceptionClassName = (java.lang.String)faultExceptionClassNameMap.get(faultElt.getQName());
java.lang.Class exceptionClass = java.lang.Class.forName(exceptionClassName);
java.lang.Exception ex=
(java.lang.Exception) exceptionClass.newInstance();
//message class
java.lang.String messageClassName = (java.lang.String)faultMessageMap.get(faultElt.getQName());
java.lang.Class messageClass = java.lang.Class.forName(messageClassName);
java.lang.Object messageObject = fromOM(faultElt,messageClass,null);
java.lang.reflect.Method m = exceptionClass.getMethod("setFaultMessage",
new java.lang.Class[]{messageClass});
m.invoke(ex,new java.lang.Object[]{messageObject});
callback.receiveErrorgreeting(new java.rmi.RemoteException(ex.getMessage(), ex));
} catch(java.lang.ClassCastException e){
// we cannot intantiate the class - throw the original Axis fault
callback.receiveErrorgreeting(f);
} catch (java.lang.ClassNotFoundException e) {
// we cannot intantiate the class - throw the original Axis fault
callback.receiveErrorgreeting(f);
} catch (java.lang.NoSuchMethodException e) {
// we cannot intantiate the class - throw the original Axis fault
callback.receiveErrorgreeting(f);
} catch (java.lang.reflect.InvocationTargetException e) {
// we cannot intantiate the class - throw the original Axis fault
callback.receiveErrorgreeting(f);
} catch (java.lang.IllegalAccessException e) {
// we cannot intantiate the class - throw the original Axis fault
callback.receiveErrorgreeting(f);
} catch (java.lang.InstantiationException e) {
// we cannot intantiate the class - throw the original Axis fault
callback.receiveErrorgreeting(f);
} catch (org.apache.axis2.AxisFault e) {
// we cannot intantiate the class - throw the original Axis fault
callback.receiveErrorgreeting(f);
}
} else {
callback.receiveErrorgreeting(f);
}
} else {
callback.receiveErrorgreeting(f);
}
} else {
callback.receiveErrorgreeting(error);
}
}
if (opNameArray == null) {
return false;
}
for (int i = 0; i < opNameArray.length; i++) {
if (opName.equals(opNameArray[i])) {
return true;
}
}
return false;
}
//http://127.0.0.1:8080/sample5-sample5/HelloWorldAuthenticateImpl
public static class ExtensionMapper{
if (
"http://ws.hello.sample.net/".equals(namespaceURI) &&
"greetingResponse".equals(typeName)){
return GreetingResponse.Factory.parse(reader);
}
if (
"http://ws.hello.sample.net/".equals(namespaceURI) &&
"greeting".equals(typeName)){
return Greeting.Factory.parse(reader);
}
throw new org.apache.axis2.databinding.ADBException("Unsupported type " + namespaceURI + " " + typeName);
}
}
public static class GreetingResponse
implements org.apache.axis2.databinding.ADBBean{
/* This type was generated from the piece of schema that had
name = greetingResponse
Namespace URI = http://ws.hello.sample.net/ Namespace Prefix = ns1
*/
/* This tracker boolean wil be used to detect whether the user called the set method
* for this attribute. It will be used to determine whether to include this field
* in the serialized XML
*/
protected boolean local_returnTracker = false ;
if (local_return != null){
elementList.add(org.apache.axis2.databinding.utils.ConverterUtil.convertToString(local_return));
} else {
throw new org.apache.axis2.databinding.ADBException("return cannot be null!!");
}
}
return new org.apache.axis2.databinding.utils.reader.ADBXMLStreamReaderImpl(qName, elementList.toArray(), attribList.toArray());
}
/**
* Factory class that keeps the parse method
*/
public static class Factory{
/**
* static method to create the object
* Precondition: If this object is an element, the current or next start element starts this object and any intervening reader events are ignorable
* If this object is not an element, it is a complex type and the reader is at the event just after the outer start element
* Postcondition: If this object is an element, the reader is positioned at its end element
* If this object is a complex type, the reader is positioned at the end element of its outer element
*/
public static GreetingResponse parse(javax.xml.stream.XMLStreamReader reader) throws java.lang.Exception{
GreetingResponse object =
new GreetingResponse();
while (!reader.isStartElement() && !reader.isEndElement())
reader.next();
if (reader.getAttributeValue("http://www.w3.org/2001/XMLSchema-instance","type")!=null){
java.lang.String fullTypeName = reader.getAttributeValue("http://www.w3.org/2001/XMLSchema-instance",
"type");
if (fullTypeName!=null){
java.lang.String nsPrefix = null;
if (fullTypeName.indexOf(":") > -1){
nsPrefix = fullTypeName.substring(0,fullTypeName.indexOf(":"));
}
nsPrefix = nsPrefix==null?"":nsPrefix;
java.lang.String type = fullTypeName.substring(fullTypeName.indexOf(":")+1);
if (!"greetingResponse".equals(type)){
//find namespace for the prefix
java.lang.String nsUri = reader.getNamespaceContext().getNamespaceURI(nsPrefix);
return (GreetingResponse)ExtensionMapper.getTypeObject(
nsUri,type,reader);
}
}
}
// Note all attributes that were handled. Used to differ normal attributes
// from anyAttributes.
java.util.Vector handledAttributes = new java.util.Vector();
reader.next();
while (!reader.isStartElement() && !reader.isEndElement()) reader.next();
if (reader.isStartElement() && new javax.xml.namespace.QName("","return").equals(reader.getName())){
} // End of if for expected property start element
else {
}
while (!reader.isStartElement() && !reader.isEndElement())
reader.next();
if (reader.isStartElement())
// A start element we are not expecting indicates a trailing invalid property
throw new org.apache.axis2.databinding.ADBException("Unexpected subelement " + reader.getLocalName());
/**
*
* @param parentQName
* @param factory
* @return org.apache.axiom.om.OMElement
*/
public org.apache.axiom.om.OMElement getOMElement (
final javax.xml.namespace.QName parentQName,
final org.apache.axiom.om.OMFactory factory) throws org.apache.axis2.databinding.ADBException{
org.apache.axiom.om.OMDataSource dataSource =
new org.apache.axis2.databinding.ADBDataSource(this,MY_QNAME){
public void serialize(org.apache.axis2.databinding.utils.writer.MTOMAwareXMLStreamWriter xmlWriter) throws javax.xml.stream.XMLStreamException {
GreetingE.this.serialize(MY_QNAME,factory,xmlWriter);
}
};
return new org.apache.axiom.om.impl.llom.OMSourcedElementImpl(
MY_QNAME,factory,dataSource);
}
public void serialize(final javax.xml.namespace.QName parentQName,
final org.apache.axiom.om.OMFactory factory,
org.apache.axis2.databinding.utils.writer.MTOMAwareXMLStreamWriter xmlWriter)
throws javax.xml.stream.XMLStreamException, org.apache.axis2.databinding.ADBException{
serialize(parentQName,factory,xmlWriter,false);
}
public void serialize(final javax.xml.namespace.QName parentQName,
final org.apache.axiom.om.OMFactory factory,
org.apache.axis2.databinding.utils.writer.MTOMAwareXMLStreamWriter xmlWriter,
boolean serializeType)
throws javax.xml.stream.XMLStreamException, org.apache.axis2.databinding.ADBException{
//We can safely assume an element has only one type associated with it
if (localGreeting==null){
throw new org.apache.axis2.databinding.ADBException("Property cannot be null!");
}
localGreeting.serialize(MY_QNAME,factory,xmlWriter);
}
/**
* Util method to write an attribute with the ns prefix
*/
private void writeAttribute(java.lang.String prefix,java.lang.String namespace,java.lang.String attName,
java.lang.String attValue,javax.xml.stream.XMLStreamWriter xmlWriter) throws javax.xml.stream.XMLStreamException{
if (xmlWriter.getPrefix(namespace) == null) {
xmlWriter.writeNamespace(prefix, namespace);
xmlWriter.setPrefix(prefix, namespace);
//We can safely assume an element has only one type associated with it
return localGreeting.getPullParser(MY_QNAME);
}
/**
* Factory class that keeps the parse method
*/
public static class Factory{
/**
* static method to create the object
* Precondition: If this object is an element, the current or next start element starts this object and any intervening reader events are ignorable
* If this object is not an element, it is a complex type and the reader is at the event just after the outer start element
* Postcondition: If this object is an element, the reader is positioned at its end element
* If this object is a complex type, the reader is positioned at the end element of its outer element
*/
public static GreetingE parse(javax.xml.stream.XMLStreamReader reader) throws java.lang.Exception{
GreetingE object =
new GreetingE();
while (!reader.isStartElement() && !reader.isEndElement())
reader.next();
// Note all attributes that were handled. Used to differ normal attributes
// from anyAttributes.
java.util.Vector handledAttributes = new java.util.Vector();
while(!reader.isEndElement()) {
if (reader.isStartElement() ){
if (reader.isStartElement() && new javax.xml.namespace.QName("http://ws.hello.sample.net/","greeting").equals(reader.getName())){
} // End of if for expected property start element
else{
// A start element we are not expecting indicates an invalid parameter was passed
throw new org.apache.axis2.databinding.ADBException("Unexpected subelement " + reader.getLocalName());
}
public static class Greeting
implements org.apache.axis2.databinding.ADBBean{
/* This type was generated from the piece of schema that had
name = greeting
Namespace URI = http://ws.hello.sample.net/ Namespace Prefix = ns1
*/
/* This tracker boolean wil be used to detect whether the user called the set method
* for this attribute. It will be used to determine whether to include this field
* in the serialized XML
*/
protected boolean localArg0Tracker = false ;
if (localArg0 != null){
elementList.add(org.apache.axis2.databinding.utils.ConverterUtil.convertToString(localArg0));
} else {
throw new org.apache.axis2.databinding.ADBException("arg0 cannot be null!!");
}
}
return new org.apache.axis2.databinding.utils.reader.ADBXMLStreamReaderImpl(qName, elementList.toArray(), attribList.toArray());
}
/**
* Factory class that keeps the parse method
*/
public static class Factory{
/**
* static method to create the object
* Precondition: If this object is an element, the current or next start element starts this object and any intervening reader events are ignorable
* If this object is not an element, it is a complex type and the reader is at the event just after the outer start element
* Postcondition: If this object is an element, the reader is positioned at its end element
* If this object is a complex type, the reader is positioned at the end element of its outer element
*/
public static Greeting parse(javax.xml.stream.XMLStreamReader reader) throws java.lang.Exception{
Greeting object =
new Greeting();
while (!reader.isStartElement() && !reader.isEndElement())
reader.next();
if (reader.getAttributeValue("http://www.w3.org/2001/XMLSchema-instance","type")!=null){
java.lang.String fullTypeName = reader.getAttributeValue("http://www.w3.org/2001/XMLSchema-instance",
"type");
if (fullTypeName!=null){
java.lang.String nsPrefix = null;
if (fullTypeName.indexOf(":") > -1){
nsPrefix = fullTypeName.substring(0,fullTypeName.indexOf(":"));
}
nsPrefix = nsPrefix==null?"":nsPrefix;
java.lang.String type = fullTypeName.substring(fullTypeName.indexOf(":")+1);
if (!"greeting".equals(type)){
//find namespace for the prefix
java.lang.String nsUri = reader.getNamespaceContext().getNamespaceURI(nsPrefix);
return (Greeting)ExtensionMapper.getTypeObject(
nsUri,type,reader);
}
}
}
// Note all attributes that were handled. Used to differ normal attributes
// from anyAttributes.
java.util.Vector handledAttributes = new java.util.Vector();
reader.next();
while (!reader.isStartElement() && !reader.isEndElement()) reader.next();
} // End of if for expected property start element
else {
}
while (!reader.isStartElement() && !reader.isEndElement())
reader.next();
if (reader.isStartElement())
// A start element we are not expecting indicates a trailing invalid property
throw new org.apache.axis2.databinding.ADBException("Unexpected subelement " + reader.getLocalName());
/**
*
* @param parentQName
* @param factory
* @return org.apache.axiom.om.OMElement
*/
public org.apache.axiom.om.OMElement getOMElement (
final javax.xml.namespace.QName parentQName,
final org.apache.axiom.om.OMFactory factory) throws org.apache.axis2.databinding.ADBException{
org.apache.axiom.om.OMDataSource dataSource =
new org.apache.axis2.databinding.ADBDataSource(this,MY_QNAME){
public void serialize(org.apache.axis2.databinding.utils.writer.MTOMAwareXMLStreamWriter xmlWriter) throws javax.xml.stream.XMLStreamException {
GreetingResponseE.this.serialize(MY_QNAME,factory,xmlWriter);
}
};
return new org.apache.axiom.om.impl.llom.OMSourcedElementImpl(
MY_QNAME,factory,dataSource);
}
public void serialize(final javax.xml.namespace.QName parentQName,
final org.apache.axiom.om.OMFactory factory,
org.apache.axis2.databinding.utils.writer.MTOMAwareXMLStreamWriter xmlWriter)
throws javax.xml.stream.XMLStreamException, org.apache.axis2.databinding.ADBException{
serialize(parentQName,factory,xmlWriter,false);
}
public void serialize(final javax.xml.namespace.QName parentQName,
final org.apache.axiom.om.OMFactory factory,
org.apache.axis2.databinding.utils.writer.MTOMAwareXMLStreamWriter xmlWriter,
boolean serializeType)
throws javax.xml.stream.XMLStreamException, org.apache.axis2.databinding.ADBException{
//We can safely assume an element has only one type associated with it
if (localGreetingResponse==null){
throw new org.apache.axis2.databinding.ADBException("Property cannot be null!");
}
localGreetingResponse.serialize(MY_QNAME,factory,xmlWriter);
}
/**
* Util method to write an attribute with the ns prefix
*/
private void writeAttribute(java.lang.String prefix,java.lang.String namespace,java.lang.String attName,
java.lang.String attValue,javax.xml.stream.XMLStreamWriter xmlWriter) throws javax.xml.stream.XMLStreamException{
if (xmlWriter.getPrefix(namespace) == null) {
xmlWriter.writeNamespace(prefix, namespace);
xmlWriter.setPrefix(prefix, namespace);
//We can safely assume an element has only one type associated with it
return localGreetingResponse.getPullParser(MY_QNAME);
}
/**
* Factory class that keeps the parse method
*/
public static class Factory{
/**
* static method to create the object
* Precondition: If this object is an element, the current or next start element starts this object and any intervening reader events are ignorable
* If this object is not an element, it is a complex type and the reader is at the event just after the outer start element
* Postcondition: If this object is an element, the reader is positioned at its end element
* If this object is a complex type, the reader is positioned at the end element of its outer element
*/
public static GreetingResponseE parse(javax.xml.stream.XMLStreamReader reader) throws java.lang.Exception{
GreetingResponseE object =
new GreetingResponseE();
while (!reader.isStartElement() && !reader.isEndElement())
reader.next();
// Note all attributes that were handled. Used to differ normal attributes
// from anyAttributes.
java.util.Vector handledAttributes = new java.util.Vector();
while(!reader.isEndElement()) {
if (reader.isStartElement() ){
if (reader.isStartElement() && new javax.xml.namespace.QName("http://ws.hello.sample.net/","greetingResponse").equals(reader.getName())){
} // End of if for expected property start element
else{
// A start element we are not expecting indicates an invalid parameter was passed
throw new org.apache.axis2.databinding.ADBException("Unexpected subelement " + reader.getLocalName());
}
I didn't find implementation of _getServiceClient() and getLastOperationContext() in the code you posted. Which I was interested to see.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35258
7
posted
0
Ethan,
nobody is going to read through all that code. Firstly, it's much too much - you probably need to remove at least 90% of it to make it more palatable to people. Secondly, being unformatted, it's essentially unreadable - please UseCodeTags. If you edit your post to address these two issues you have a much better chance that someone will try to understand it.