1 16 17 package samples.attachments; 18 import org.w3c.dom.Document ; 19 import org.w3c.dom.Element ; 20 import org.w3c.dom.Node ; 21 22 import javax.activation.DataHandler ; 23 24 25 28 29 36 public class EchoAttachmentsService { 37 38 42 public DataHandler echo( DataHandler dh) { 43 System.err.println("In echo"); 44 45 49 52 55 if (dh == null ) System.err.println("dh is null"); 56 else System.err.println("Received \""+dh.getClass().getName()+"\"."); 57 return dh; 58 } 59 60 64 public DataHandler [] echoDir( DataHandler [] attachments) { 65 System.err.println("In echoDir"); 66 67 71 74 77 if (attachments == null ) System.err.println("attachments is null!"); 78 else System.err.println("Got " + attachments.length + " attachments!"); 79 return attachments; 80 } 81 82 public Document attachments( Document xml) 83 throws org.apache.axis.AxisFault,java.io.IOException , org.xml.sax.SAXException , 84 java.awt.datatransfer.UnsupportedFlavorException ,javax.xml.parsers.ParserConfigurationException , 85 java.lang.ClassNotFoundException ,javax.xml.soap.SOAPException { 86 System.err.println("In message handling attachments directly."); 87 org.apache.axis.MessageContext msgContext= org.apache.axis.MessageContext.getCurrentContext(); 88 89 org.apache.axis.Message reqMsg= msgContext.getRequestMessage(); 90 91 org.apache.axis.attachments.Attachments attachments=reqMsg.getAttachmentsImpl(); 92 93 if(null == attachments){ 94 throw new org.apache.axis.AxisFault("No support for attachments" ); 95 } 96 97 Element rootEl= xml.getDocumentElement(); 98 99 Element caEl= getNextFirstChildElement(rootEl); 100 StringBuffer fullmsg= new StringBuffer (); 101 java.util.Vector reply= new java.util.Vector (); 102 103 104 for(int count=1 ;caEl != null; caEl= getNextSiblingElement(caEl), ++count){ 105 String HREF= caEl.getAttribute("href"); 106 org.apache.axis.Part p= attachments.getAttachmentByReference(href); 107 if(null == p) 108 throw new org.apache.axis.AxisFault("Attachment for ref='"+href+"' not found." ); 109 String ordinalStr =getOrdinalHeaders(p); 110 if( null == ordinalStr || ordinalStr.trim().length()==0) 111 throw new org.apache.axis.AxisFault("Ordinal for attachment ref='"+href+"' not found." ); 112 int ordinal= Integer.parseInt(ordinalStr); 113 if(count != ordinal) 114 throw new org.apache.axis.AxisFault("Ordinal for attachment ref='"+href+"' excpected" + count + " got " + ordinal +"." ); 115 116 if(!"text/plain".equals(p.getContentType())) 118 throw new org.apache.axis.AxisFault("Attachment ref='"+href+"' bad content-type:'"+p.getContentType()+"'." ); 119 120 DataHandler dh= ((org.apache.axis.attachments.AttachmentPart)p).getDataHandler(); 122 String pmsg=(String )dh.getContent(); 123 fullmsg.append(pmsg); 124 reply.add(pmsg); 125 } 126 if(!(samples.attachments.TestRef .TheKey.equals(fullmsg.toString()))) 127 throw new org.apache.axis.AxisFault("Fullmsg not correct'"+fullmsg +"'." ); 128 System.out.println(fullmsg.toString()); 129 130 java.io.ByteArrayOutputStream byteStream = new java.io.ByteArrayOutputStream (); 132 java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream (byteStream); 133 oos.writeObject(reply); 134 oos.close(); 135 byte[] replyJavaSerialized= byteStream.toByteArray(); 136 byteStream=null; oos= null; 137 138 org.apache.axis.attachments.AttachmentPart replyPart= new 139 org.apache.axis.attachments.AttachmentPart( 140 new DataHandler ( new MemoryOnlyDataSource(replyJavaSerialized, 141 java.awt.datatransfer.DataFlavor.javaSerializedObjectMimeType+"; class=\"" 142 + reply.getClass().getName()+"\""))); 143 144 org.apache.axis.Message rspMsg= msgContext.getResponseMessage(); 146 rspMsg.addAttachmentPart(replyPart); 147 148 String ordinalPattern=""; 150 for(java.util.Iterator ai=reqMsg.getAttachments(); ai.hasNext();){ 151 org.apache.axis.Part p= (org.apache.axis.Part) ai.next(); 152 ordinalPattern += getOrdinalHeaders(p); 153 } 154 155 StringBuffer msgBody = new StringBuffer ("\n<attachments xmlns=\""); 157 msgBody.append(rootEl.getNamespaceURI()) 158 .append("\">\n") 159 .append("\t<attachment HREF=\"") 160 .append(replyPart.getContentIdRef()) 161 .append("\" ordinalPattern=\"") 162 .append(ordinalPattern) 163 .append("\"/>\n") 164 .append("</attachments>\n"); 165 166 return 168 org.apache.axis.utils.XMLUtils.newDocument( 169 new org.xml.sax.InputSource (new java.io.ByteArrayInputStream ( 170 msgBody.toString().getBytes()))); 171 } 172 Element getNextFirstChildElement(Node n) { 173 if(n== null) return null; 174 n= n.getFirstChild(); 175 for(; n!= null && !(n instanceof Element ); n= n.getNextSibling()); 176 return (Element )n; 177 } 178 179 Element getNextSiblingElement(Node n) { 180 if(n== null) return null; 181 n= n.getNextSibling(); 182 for(; n!= null && !(n instanceof Element ); n= n.getNextSibling()); 183 return (Element )n; 184 } 185 String getOrdinalHeaders( org.apache.axis.Part p){ 186 StringBuffer ret= new StringBuffer (); 187 for(java.util.Iterator i= p.getMatchingMimeHeaders( new String []{samples.attachments.TestRef.positionHTTPHeader}); 188 i.hasNext();){ 189 javax.xml.soap.MimeHeader mh= (javax.xml.soap.MimeHeader ) i.next(); 190 String v= mh.getValue(); 191 if(v != null) ret.append(v.trim()); 192 } 193 return ret.toString(); 194 } 195 196 197 static class MemoryOnlyDataSource extends org.apache.axis.attachments.ManagedMemoryDataSource{ 198 199 MemoryOnlyDataSource( byte [] in, String contentType) throws java.io.IOException { 200 super( new java.io.ByteArrayInputStream ( in) , Integer.MAX_VALUE -2, contentType, true); 201 } 202 MemoryOnlyDataSource( String in, String contentType)throws java.io.IOException { 203 this( in.getBytes() , contentType); 204 } 205 } 206 207 } 208 209 | Popular Tags |