@Test public void testSWA() throws Exception { SOAPFactory soapFac = SOAPFactory.newInstance(); MessageFactory msgFac = MessageFactory.newInstance(); SOAPConnectionFactory conFac = SOAPConnectionFactory.newInstance(); SOAPMessage msg = msgFac.createMessage(); QName sayHi = new QName("http://apache.org/hello_world_rpclit", "sayHiWAttach"); msg.getSOAPBody().addChildElement(soapFac.createElement(sayHi)); AttachmentPart ap1 = msg.createAttachmentPart(); ap1.setContent("Attachment content", "text/plain"); msg.addAttachmentPart(ap1); AttachmentPart ap2 = msg.createAttachmentPart(); ap2.setContent("Attachment content - Part 2", "text/plain"); msg.addAttachmentPart(ap2); msg.saveChanges(); SOAPConnection con = conFac.createConnection(); URL endpoint = new URL("http://localhost:9008/SOAPServiceProviderRPCLit/SoapPortProviderRPCLit1"); SOAPMessage response = con.call(msg, endpoint); QName sayHiResp = new QName("http://apache.org/hello_world_rpclit", "sayHiResponse"); assertNotNull(response.getSOAPBody().getChildElements(sayHiResp)); assertEquals(2, response.countAttachments()); }
private void doTestSoapConnection(boolean disableChunking) throws Exception { SOAPFactory soapFac = SOAPFactory.newInstance(); MessageFactory msgFac = MessageFactory.newInstance(); SOAPConnectionFactory conFac = SOAPConnectionFactory.newInstance(); SOAPMessage msg = msgFac.createMessage(); if (disableChunking) { // this is the custom header checked by ServiceImpl msg.getMimeHeaders().addHeader("Transfer-Encoding-Disabled", "true"); // this is a hint to SOAPConnection that the chunked encoding is not needed msg.getMimeHeaders().addHeader("Transfer-Encoding", "disabled"); } QName sayHi = new QName("http://www.jboss.org/jbossws/saaj", "sayHello"); msg.getSOAPBody().addChildElement(soapFac.createElement(sayHi)); AttachmentPart ap1 = msg.createAttachmentPart(); char[] content = new char[16 * 1024]; Arrays.fill(content, 'A'); ap1.setContent(new String(content), "text/plain"); msg.addAttachmentPart(ap1); AttachmentPart ap2 = msg.createAttachmentPart(); ap2.setContent("Attachment content - Part 2", "text/plain"); msg.addAttachmentPart(ap2); msg.saveChanges(); SOAPConnection con = conFac.createConnection(); final String serviceURL = baseURL.toString(); URL endpoint = new URL(serviceURL); SOAPMessage response = con.call(msg, endpoint); QName sayHiResp = new QName("http://www.jboss.org/jbossws/saaj", "sayHelloResponse"); Iterator<?> sayHiRespIterator = response.getSOAPBody().getChildElements(sayHiResp); SOAPElement soapElement = (SOAPElement) sayHiRespIterator.next(); assertNotNull(soapElement); assertEquals(2, response.countAttachments()); String[] values = response.getMimeHeaders().getHeader("Transfer-Encoding-Disabled"); if (disableChunking) { // this means that the ServiceImpl executed the code branch verifying // that chunking was disabled assertNotNull(values); assertTrue(values.length == 1); } else { assertNull(values); } }
private void addAttachment(SOAPMessageContext messageContext) { if (logger.isInfoEnabled()) { logger.info("Inside handler addAttachment Method"); } byte[] byt = null; SOAPMessage message; AttachmentPart attach; byt = (byte[]) messageContext.get("attachment"); WrappedMessageContext wmc = (WrappedMessageContext) messageContext; Message cxfmsg = wmc.getWrappedMessage(); SOAPMessageContextImpl smci = new SOAPMessageContextImpl(cxfmsg); message = smci.getMessage(); if (byt != null) { logger.info("Attachment content :::" + new String(byt)); ByteArrayDataSource source = new ByteArrayDataSource(byt, "Multipart/Related"); DataHandler dataHandler = new DataHandler(source); // attach = message.createAttachmentPart(byt, "Multipart/Related"); attach = message.createAttachmentPart(dataHandler); // attach.setMimeHeader("attc", "abcd"); // attach.setContentId("1234567"); attach.setMimeHeader("KeyA", "ValueA"); attach.setContentId("1234567"); logger.info("Content Id :: " + attach.getContentId()); message.addAttachmentPart(attach); } messageContext.setMessage(message); }
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException { String retval = "<html> <H4>"; try { // Create a message factory. MessageFactory mf = MessageFactory.newInstance(); // Create a message from the message factory. SOAPMessage msg = mf.createMessage(); // Message creation takes care of creating the SOAPPart - a // required part of the message as per the SOAP 1.1 // specification. SOAPPart sp = msg.getSOAPPart(); // Retrieve the envelope from the soap part to start building // the soap message. SOAPEnvelope envelope = sp.getEnvelope(); // Create a soap header from the envelope. SOAPHeader hdr = envelope.getHeader(); // Create a soap body from the envelope. SOAPBody bdy = envelope.getBody(); // Add a soap body element to the soap body SOAPBodyElement gltp = bdy.addBodyElement( envelope.createName("GetLastTradePrice", "ztrade", "http://wombat.ztrade.com")); gltp.addChildElement(envelope.createName("symbol", "ztrade", "http://wombat.ztrade.com")) .addTextNode("SUNW"); StringBuffer urlSB = new StringBuffer(); urlSB.append(req.getScheme()).append("://").append(req.getServerName()); urlSB.append(":").append(req.getServerPort()).append(req.getContextPath()); String reqBase = urlSB.toString(); if (data == null) { data = reqBase + "/index.html"; } // Want to set an attachment from the following url. // Get context URL url = new URL(data); AttachmentPart ap = msg.createAttachmentPart(new DataHandler(url)); ap.setContentType("text/html"); // Add the attachment part to the message. msg.addAttachmentPart(ap); // Create an endpoint for the recipient of the message. if (to == null) { to = reqBase + "/receiver"; } URL urlEndpoint = new URL(to); System.err.println("Sending message to URL: " + urlEndpoint); System.err.println("Sent message is logged in \"sent.msg\""); retval += " Sent message (check \"sent.msg\") and "; FileOutputStream sentFile = new FileOutputStream("sent.msg"); msg.writeTo(sentFile); sentFile.close(); // Send the message to the provider using the connection. SOAPMessage reply = con.call(msg, urlEndpoint); if (reply != null) { FileOutputStream replyFile = new FileOutputStream("reply.msg"); reply.writeTo(replyFile); replyFile.close(); System.err.println("Reply logged in \"reply.msg\""); retval += " received reply (check \"reply.msg\").</H4> </html>"; } else { System.err.println("No reply"); retval += " no reply was received. </H4> </html>"; } } catch (Throwable e) { e.printStackTrace(); logger.severe("Error in constructing or sending message " + e.getMessage()); retval += " There was an error " + "in constructing or sending message. </H4> </html>"; } try { OutputStream os = resp.getOutputStream(); os.write(retval.getBytes()); os.flush(); os.close(); } catch (IOException e) { e.printStackTrace(); logger.severe("Error in outputting servlet response " + e.getMessage()); } }
public void writeTo(SOAPMessage saaj) throws SOAPException { AttachmentPart part = saaj.createAttachmentPart(); part.setDataHandler(asDataHandler()); part.setContentId(contentId); saaj.addAttachmentPart(part); }