1
7
22package com.borneo.net.soap;
30
31import java.io.BufferedInputStream;
32import java.io.File;
33import java.io.IOException;
34import java.io.InputStream;
35import java.net.MalformedURLException;
36import java.net.URL;
37import java.rmi.RemoteException;
38import java.util.ArrayList;
39import java.util.Collection;
40import java.util.Iterator;
41import java.util.LinkedList;
42import java.util.Vector;
43
44import javax.activation.DataHandler;
45import javax.activation.FileDataSource;
46
47import javax.xml.namespace.QName;
48import javax.xml.rpc.ParameterMode;
49import javax.xml.rpc.ServiceException;
50import javax.xml.soap.AttachmentPart;
51import javax.xml.soap.Name;
52import javax.xml.soap.SOAPBody;
53import javax.xml.soap.SOAPBodyElement;
54import javax.xml.soap.SOAPConnection;
55import javax.xml.soap.SOAPElement;
56import javax.xml.soap.SOAPException;
57import javax.xml.soap.SOAPMessage;
58
59import org.apache.axis.AxisFault;
60import org.apache.axis.Constants;
61import org.apache.axis.Message;
62import org.apache.axis.MessageContext;
63import org.apache.axis.attachments.OctetStream;
64import org.apache.axis.client.Call;
65import org.apache.axis.client.Service;
66import org.apache.axis.description.ParameterDesc;
67import org.apache.axis.encoding.TypeMapping;
68import org.apache.axis.encoding.TypeMappingRegistry;
69import org.apache.axis.encoding.TypeMappingRegistryImpl;
70import org.apache.axis.encoding.XMLType;
71import org.apache.axis.message.RPCElement;
72import org.apache.axis.message.RPCParam;
73import org.apache.axis.message.SOAPEnvelope;
74import org.apache.axis.soap.MessageFactoryImpl;
75import org.apache.axis.soap.SOAPConnectionFactoryImpl;
76import org.apache.log4j.Logger;
77import org.apache.log4j.PropertyConfigurator;
78
79import com.borneo.util.FileSelector;
80import com.borneo.util.POProperty;
81
82
33
34public class SAAJCallMgr {
35
36 SOAPConnection soapConnection;
37 Message reqMessage;
39 Message resMessage;
40
41 SOAPEnvelope requestEnvelope;
42 SOAPElement elementRpcOp;
43 protected static Logger log;
44 private MessageContext msgContext = null ;
45
46
47 public void setConnection(SOAPConnection conn){
50 log.debug("beg. getconn ");
51 soapConnection = conn;
52 }
53 public SOAPConnection getConnection(){
54 return soapConnection;
55 }
56
59 public void putFile(File afile, String wsdlURL){
62 AttachmentPart attachment =
63 getReqMessage().createAttachmentPart(
64 new DataHandler(
65 new FileDataSource(
66 afile
67 )
68 )
69 );
70 getReqMessage().addAttachmentPart(attachment);
71 try{
72 elementRpcOp.addAttribute(
73 requestEnvelope.createName("href")
74 , "cid:" + attachment.getContentId()
75 );
76 log.debug("making SOAP call to " +wsdlURL);
77 javax.xml.soap.SOAPMessage returnedSOAPMessage =
78 getConnection().call(getReqMessage(), wsdlURL);
79 } catch (SOAPException e) {
80 e.printStackTrace();
82 }
83 }
84 public void putFile(Collection fileset){
85 for (
86 Iterator i = fileset.iterator();i.hasNext();
87 ){
88 putFile(
89 (File)i.next()
90 ,
91 POProperty.get(
92 "wsdl.service.addr.host"
93 )
94 + "/"
95 + POProperty.get(
96 "wsdl.service.addr.axis.webapp"
97 )
98 + "/"
99 + POProperty.get(
00 "wsdl.service.addr.port.name"
01 )
02 );
03 }
04 }
05 public void putFile(String name, String wsdlURL){
06
19 System.out.println("beg. put fnm , endptURL : "
20 +name
21 +" "
22 +wsdlURL
23 );
24 try {
25
26
27
29 for ( Iterator i =
30 FileSelector.getFiles(name).iterator() ;
31 i.hasNext();
32 ){
33 AttachmentPart attachment =
34 getReqMessage().createAttachmentPart(
35 new DataHandler(
36 new FileDataSource(
37 (File)i.next()
38 )
39 )
40 );
41 getReqMessage().addAttachmentPart(attachment);
42 elementRpcOp.addAttribute(
43 requestEnvelope.createName("href")
44 , "cid:" + attachment.getContentId()
45 );
46 } log.debug("making SOAP call to " +wsdlURL);
48 javax.xml.soap.SOAPMessage returnedSOAPMessage =
49 getConnection().call(getReqMessage(), wsdlURL);
50 } catch (SOAPException e) {
51 e.printStackTrace();
53 }
54 }
55 public MessageContext getMessageContext(){
56 return msgContext;
57 }
58
59 public void setMessageContext(MessageContext msgC){
60 msgContext = msgC;
61 }
62
63 public Message getReqMessage(){
64 return reqMessage;
65 }
66
67
73 public void setReqMessage(Message msg){
74 log.debug("beg. getmsg ");
75
76 reqMessage = msg;
77
78 }
79
80
84 public SOAPEnvelope getRequestEnvelope(){
85 return requestEnvelope;
86 }
87
88 public SOAPEnvelope getRequestEnvelope(Message msg){
89 try {
90 return msg.getSOAPEnvelope();
91
92
95 } catch (AxisFault e) {
96 e.printStackTrace();
98 return null;
99 }
00 }
01
02 public void setRequestEnvelope(SOAPEnvelope env){
03
04 requestEnvelope = env;
05 }
06
07
11 public SOAPBody getBody(){
12 try {
13 return getRequestEnvelope(this.getReqMessage()).getBody();
14 } catch (SOAPException e) {
15 e.printStackTrace();
17 return null;
18 }
19 }
20
25 public SOAPElement getElementRpcOp(){
26 return elementRpcOp;
27 }
28
29
36 public void setElementRpcOp(SOAPElement element){
37 elementRpcOp = element;
38 }
39
40
48 public Name setQName(String name){
49 try {
50 return getRequestEnvelope(this.getReqMessage())
51 .createName(name
52 );
56 } catch (SOAPException e) {
57 e.printStackTrace();
59 return null;
60 }
61 }
62 public Name setQName(String name, String prefix, String uri){
63 try {
64 return getRequestEnvelope(this.getReqMessage())
65 .createName(
66 name, prefix, uri
67 );
68 } catch (SOAPException e) {
69 e.printStackTrace();
71 return null;
72 }
73 }
74
80 public SOAPElement addMethod(String name){
81 try {
82 return
83 getBody()
84 .addBodyElement(
85 setQName(name)
86 );
87 } catch (SOAPException e) {
88 e.printStackTrace();
90 return null;
91 }
92 }
93
94
95
06 public AttachmentPart createFileAttachment(File afile){
07 return
08 getReqMessage().createAttachmentPart(
09 new DataHandler(
10 new FileDataSource(
11 afile
12 )
13 )
14 );
15 }
16
17
31 public void addAttachment(File fil){
36 AttachmentPart myAttachment = createFileAttachment(fil);
37 getReqMessage().addAttachmentPart(myAttachment);
38 try {
39 getElementRpcOp()
40 .addChildElement(
41 getRequestEnvelope(this.getReqMessage()).createName("dh")
42 )
43 .addAttribute(
44 getRequestEnvelope(this.getReqMessage()).createName("href")
45 , "cid:" + myAttachment.getContentId()
46 );
47 } catch (SOAPException e) {
48 e.printStackTrace();
50 }
51 }
52 public void addAttachment(File fil, String parm){
53 AttachmentPart myAttachment = createFileAttachment(fil);
54 getReqMessage().addAttachmentPart(myAttachment);
55 try {
56 getElementRpcOp()
57 .addChildElement(
58 getRequestEnvelope(this.getReqMessage()).createName(parm)
59 )
60 .addAttribute(
61 getRequestEnvelope(this.getReqMessage()).createName("href")
62 , "cid:" + myAttachment.getContentId()
63 );
64 } catch (SOAPException e) {
65 e.printStackTrace();
67 }
68 }
69
70
73 public void addAttachment(File fil, SOAPBodyElement element){
74 addAttachment(
76 new DataHandler(
77 new FileDataSource(
78 fil
79 )
80 )
81 ,
82 element
83 );
84
85 }
86
98 public void addRPCParm ( String name, String nameVal){
99 try {
01 getElementRpcOp()
02 .addChildElement(
03 getRequestEnvelope(this.getReqMessage())
04 .createName(name)
05 )
06 .addTextNode(nameVal);
07 } catch (SOAPException e) {
08 e.printStackTrace();
10 }
11 }
12
13
14
23 public SOAPMessage call ( SOAPMessage message ){
24 try {
25 return getConnection().call(
26 message
27 ,
28 POProperty.get(
29 "wsdl.service.addr.host"
30 )
31 + "/"
32 + POProperty.get(
33 "wsdl.service.addr.axis.webapp"
34 )
35 + "/"
36 + POProperty.get(
37 "wsdl.service.addr.port.name"
38 )
39 );
40 } catch (SOAPException e) {
41 e.printStackTrace();
43 return null;
44 }
45 }
46 public Object[] setParam(DataHandler[] dh){
49 RPCParam rpcParam = new RPCParam(
50 ""
51 , "attachments"
52 ,dh
53 );
54 rpcParam.setParamDesc(
55 new ParameterDesc(new QName("attachments"),
56 ParameterDesc.IN,
57 XMLType.SOAP_ARRAY)
58 );
59 Vector result = new Vector();
60 result.add(rpcParam);
61 return ( result.toArray() );
62 }
63
64 public Object[] setParams(String topicname,DataHandler[] dh){
67 RPCParam rpcParam = new RPCParam(
68 ""
69 , "topicname"
70 ,topicname
71 );
72 rpcParam.setParamDesc(
73 new ParameterDesc(new QName("topicname"),
74 ParameterDesc.IN,
75 XMLType.XSD_STRING)
76 );
77 Vector result = new Vector();
78 result.add(rpcParam);
79
80 RPCParam rpcParam2 = new RPCParam(
81 ""
82 , "attachments"
83 ,dh
84 );
85 rpcParam.setParamDesc(
86 new ParameterDesc(new QName("attachments"),
87 ParameterDesc.IN,
88 XMLType.SOAP_ARRAY)
89 );
90 result.add(rpcParam2);
91 return ( result.toArray() );
92 }
93 public Object[] setParams(String topicname, InputStream[] is){
06 LinkedList osList = new LinkedList();
07 for (int i = 0; i < is.length; i++) {
08 osList.add(this.setOctet(is[i]));
09 }
10 OctetStream[] osarray = new OctetStream[0];
11 return this.setParams(topicname
12 , (OctetStream[])new ArrayList(osList).toArray(osarray));
13 }
14
15 public Object[] setParams(String topicname, OctetStream[] is){
20 RPCParam rpcParam = new RPCParam(
21 ""
22 , "topicnameo"
23 ,topicname
24 );
25 rpcParam.setParamDesc(
26 new ParameterDesc(new QName("topicnameo"),
27 ParameterDesc.IN,
28 XMLType.XSD_STRING)
29 );
30 Vector result = new Vector();
31 result.add(rpcParam);
32
33 RPCParam rpcParam2 = new RPCParam(
34 ""
35 , "octetstream"
36 ,is
37 );
38 rpcParam.setParamDesc(
39 new ParameterDesc(new QName("octetstream"),
40 ParameterDesc.IN,
41 XMLType.SOAP_ARRAY)
42 );
43 result.add(rpcParam2);
44 return ( result.toArray() );
45 }
46
47
48 public Object[] setParams(String topicname){
51 RPCParam rpcParam = new RPCParam(
52 ""
53 , "topicname"
54 ,topicname
55 );
56 rpcParam.setParamDesc(
57 new ParameterDesc(new QName("topicname"),
58 ParameterDesc.IN,
59 XMLType.XSD_STRING)
60 );
61 Vector result = new Vector();
62 result.add(rpcParam);
63 return ( result.toArray() );
64 }
65
66 public DataHandler[] setDH(Collection fileset){
67 LinkedList dhList = new LinkedList();
68 for (
69 Iterator i = fileset.iterator();i.hasNext();
70 ){
71 dhList.add(
72 new DataHandler(
73 new FileDataSource(
74 (File)i.next()
75 )
76 )
77 );
78 }
79 DataHandler[] dharray = new DataHandler[0];
80 return
81 (DataHandler[])new ArrayList(dhList).toArray(dharray);
82 }
83
91 public void addAttachment(DataHandler dh, SOAPBodyElement element){
92
95 org.apache.axis.attachments.AttachmentPart
96 myAttachment = new org.apache.axis.attachments.AttachmentPart(
97 new DataHandler(
98 dh
99 ,"text/plain"
00 )
01 );
02 getReqMessage().addAttachmentPart(myAttachment);
03 try {
04 element
05 .addChildElement(
06 getRequestEnvelope(this.getReqMessage()).createName("item")
07 )
08 .addAttribute(
09 getRequestEnvelope(this.getReqMessage()).createName("href")
10 , "cid:" + myAttachment.getContentId()
11 );
12 } catch (SOAPException e) {
13 e.printStackTrace();
15 }
16 }
17
25 public Call getCall(){
26 try {
27 Call call = (Call)(new Service().createCall());
28
29
38
39
40
42
46
48 call.setTargetEndpointAddress(
50 new URL(
51 POProperty.get(
52 "wsdl.service.addr.host"
53 )
54 + "/"
55 + POProperty.get(
56 "wsdl.service.addr.axis.webapp"
57 )
58 + "/"
59 + POProperty.get(
60 "wsdl.service.addr.port.name"
61 )
62 )
63 );
64 call.setOperationName(
68 new QName(
69 "urn:EchoAttachmentsService"
70 ,
71 POProperty.get(
72 "soap.rpc.methodname.attachment"
73 )
74 )
75 );
76
77
91 call.addParameter(
93 "attachments"
94 , XMLType.SOAP_ARRAY ,ParameterMode.IN
96 ); call.setReturnType(XMLType.SOAP_ARRAY);
03 return call;
04
05 } catch (ServiceException e) {
06 e.printStackTrace();
08 }
09 catch (MalformedURLException e) {
10 e.printStackTrace();
12 }
13 return null;
14 }
15
29 public Object diiCall(Collection fileset, Call argcal){
30
31 LinkedList dhList = new java.util.LinkedList();
32 for (
33 Iterator i = fileset.iterator();i.hasNext();
34 ){
35 dhList.add(
36 new DataHandler(
37 new FileDataSource(
38 (File)i.next()
39 )
40 )
41 );
42 }
43 try {
44 DataHandler[] dharray = new DataHandler[0];
45 return argcal.invoke(
46 new Object[]{
47 new ArrayList(dhList).toArray(dharray)
48 }
49 );
50 } catch (RemoteException e) {
51 e.printStackTrace();
53 }
54 return new Object();
55 }
56
57 public void close(){
58 try {
59 getConnection().close();
60 } catch (SOAPException e) {
61 e.printStackTrace();
63 }
64 }
65
66 public void setTypeMappingRegistry() {
67 TypeMappingRegistry tmr = null;
68 TypeMapping tm = null;
69
70
71 tmr = new TypeMappingRegistryImpl();
72
73 String encodingStyle = Constants.URI_DEFAULT_SOAP_ENC;
75 tm = (TypeMapping) tmr.getTypeMapping(encodingStyle);
76 TypeMapping df = (TypeMapping) tmr.getDefaultTypeMapping();
77
78 Class[] myclass = df.getAllClasses();
79
80 for ( int j = 0
81 ;j < myclass.length ;
82 j++
83 ){
84 log.debug(
85 "mapd : "
86 + myclass[j].getName()
87 + " "
88 +df.getTypeQName(myclass[j]).toString()
89 + " "
90 +df.getSerializer(myclass[j])
91 )
92 ;
93 }
94
95
01
02 }
03 private OctetStream setOctet(InputStream is){
04 BufferedInputStream bis = new BufferedInputStream(is);
05 OctetStream out = new OctetStream();
06
07 byte[] buf = new byte[20 * 1024]; int bytesRead;
09 try {
10 while ((bytesRead = bis.read(buf)) != -1) {
11 out.setBytes(buf);
12 }
13 } catch (IOException e) {
14 e.printStackTrace();
16 }
17 return out;
18 }
19
20
21 public static void main(String[] args) {
33 SAAJCallMgr myMgr= new SAAJCallMgr();
34 myMgr.setMessageContext(
38 new MessageContext(
39 new Service().getEngine()
40 )
41 );
42
43 if (
44 POProperty.get(
45 "ws.invocation.typ"
46 ).equalsIgnoreCase("soapconnection")
47 ) {
48 try {
49 myMgr.setConnection(
50 SOAPConnectionFactoryImpl.newInstance()
51 .createConnection()
52 );
53 myMgr.setReqMessage(
54
55 (Message)MessageFactoryImpl.newInstance().createMessage()
56 );
57 } catch (UnsupportedOperationException e) {
58 e.printStackTrace();
60 } catch (SOAPException e) {
61 e.printStackTrace();
63 }
64
65 myMgr.setElementRpcOp(myMgr.addMethod("echo"));
66 log.debug("RpcOp as element " + myMgr.getElementRpcOp().toString());
67 for ( Iterator i =
70 FileSelector.getFiles(args[0]).iterator() ;
71 i.hasNext();
72 ){
73 myMgr.addAttachment(
74 (File)i.next()
75 , "dh"
76 );
77 }
78 for ( Iterator i =
80 FileSelector.getFiles(args[1]).iterator() ;
81 i.hasNext();
82 ){
83 myMgr.addAttachment(
84 (File)i.next()
85 , "dh2"
86 );
87 }
88 myMgr.call(myMgr.getReqMessage());
89 myMgr.close();
90 }else if (
91 POProperty.get(
92 "ws.invocation.typ"
93 ).equalsIgnoreCase("diiservice")
94 )
95 {
96 Object ret = myMgr.diiCall(
97 FileSelector.getFiles(args[0])
98 ,
99 myMgr.getCall()
00 );
01 }
03 else if (
06 POProperty.get(
07 "ws.invocation.typ"
08 ).equalsIgnoreCase("soaparrayofdhattach")
09 )
10 {
11 myMgr.setTypeMappingRegistry();
13 try {
14 myMgr.setConnection(
15 SOAPConnectionFactoryImpl.newInstance()
16 .createConnection()
17 );
18 myMgr.setReqMessage(
19 (Message)MessageFactoryImpl.newInstance().createMessage()
20 );
21
22
23 RPCElement myRpcEl =
25 new RPCElement(
26 "urn:EchoAttachmentsService"
27 ,"echoDir"
28 ,myMgr.setDH(
29 FileSelector.getFiles(
30 args[0]
31 )
32 )
33 );
34 log.debug("using encd styl : "+
35 myMgr.getMessageContext(
36 ).getSOAPConstants().getEncodingURI());
37 myRpcEl.setEncodingStyle(
38 myMgr.getMessageContext(
39 ).getSOAPConstants().getEncodingURI()
40 );
41 myRpcEl.setParentElement(myMgr.getBody());
42
44 } catch (SOAPException e) {
45 e.printStackTrace();
47 }
48 myMgr.call(myMgr.getReqMessage());
49 myMgr.close();
50 }
51
52
62 else if (
63 POProperty.get(
64 "ws.invocation.typ"
65 ).equalsIgnoreCase("soapconnparms")
66 )
67 myMgr.setRequestEnvelope(
69 new SOAPEnvelope(
70 myMgr.getMessageContext().getSOAPConstants()
71 ,myMgr.getMessageContext().getSchemaVersion()
72 )
73 );
74
75 RPCElement myRpcEl =
77 new RPCElement(
78 "urn:EchoAttachmentsService"
79 ,"echoDir"
80 ,myMgr.setParam(
81 myMgr.setDH(
82 FileSelector.getFiles(
83 args[0]
84 )
85 )
86 )
87 );
88 log.debug("using encd styl : "+
89 myMgr.getMessageContext(
90 ).getSOAPConstants().getEncodingURI());
91 try {
92 myRpcEl.setEncodingStyle(
93 myMgr.getMessageContext(
94 ).getSOAPConstants().getEncodingURI()
95 );
96 } catch (SOAPException e) {
97 e.printStackTrace();
99 }
00 myMgr.getRequestEnvelope().addBodyElement(myRpcEl);
02 myMgr.getRequestEnvelope().setMessageType(Message.REQUEST);
03 try {
04 myMgr.getMessageContext().setTargetService(
05 myMgr.getRequestEnvelope()
06 .getFirstBody()
07 .getNamespaceURI()
08 );
09 myMgr.setConnection(
10 SOAPConnectionFactoryImpl.newInstance()
11 .createConnection()
12 );
13 } catch (AxisFault e1) {
14 e1.printStackTrace();
16 } catch (UnsupportedOperationException e) {
17 e.printStackTrace();
19 } catch (SOAPException e) {
20 e.printStackTrace();
22 }
23 myMgr.setReqMessage(
25 new Message(
26 myMgr.getRequestEnvelope()
27 )
28 );
29 myMgr.getReqMessage().setMessageContext(
30 myMgr.getMessageContext()
31 );
32 myMgr.call(myMgr.getReqMessage());
33 myMgr.close();
34 }
35
36 static
37 {
38 log = Logger.getLogger((SAAJCallMgr.class).getName());
39 ClassLoader cl = SAAJCallMgr.class.getClassLoader();
40 PropertyConfigurator.configure(cl.getResource(
41 "config/log4j.properties"));
42 }
43 }
44