List of usage examples for javax.xml.soap SOAPMessage saveChanges
public abstract void saveChanges() throws SOAPException;
From source file:org.wso2.carbon.mdm.mobileservices.windowspc.services.wstep.util.MessageHandler.java
/** * This method adds Timestamp for SOAP header, and adds Content-length for HTTP header for * avoiding HTTP chunking.//from w w w .j a v a 2 s . c o m * * @param context */ @Override public boolean handleMessage(SOAPMessageContext context) { Boolean outBoundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); if (outBoundProperty) { SOAPMessage message = context.getMessage(); SOAPHeader header = null; SOAPEnvelope envelope = null; try { header = message.getSOAPHeader(); envelope = message.getSOAPPart().getEnvelope(); } catch (SOAPException e) { Response.serverError().build(); } if (header == null) { try { header = envelope.addHeader(); } catch (SOAPException e) { Response.serverError().build(); } } SOAPFactory soapFactory = null; try { soapFactory = SOAPFactory.newInstance(); } catch (SOAPException e) { Response.serverError().build(); } QName qNamesSecurity = new QName(Constants.CertificateEnrollment.WS_SECURITY_TARGET_NAMESPACE, Constants.CertificateEnrollment.SECURITY); SOAPHeaderElement Security = null; try { Security = header.addHeaderElement(qNamesSecurity); } catch (SOAPException e) { Response.serverError().build(); } Name attributeName = null; try { attributeName = soapFactory.createName(Constants.CertificateEnrollment.TIMESTAMP_ID, Constants.CertificateEnrollment.TIMESTAMP_U, Constants.CertificateEnrollment.WSS_SECURITY_UTILITY); } catch (SOAPException e) { Response.serverError().build(); } QName qNameTimestamp = new QName(Constants.CertificateEnrollment.WSS_SECURITY_UTILITY, Constants.CertificateEnrollment.TIMESTAMP); SOAPHeaderElement timestamp = null; try { timestamp = header.addHeaderElement(qNameTimestamp); timestamp.addAttribute(attributeName, Constants.CertificateEnrollment.TIMESTAMP_0); } catch (SOAPException e) { Response.serverError().build(); } DateTime dateTime = new DateTime(); DateTime expiredDateTime = dateTime.plusMinutes(5); String createdISOTime = dateTime.toString(ISODateTimeFormat.dateTime()); String expiredISOTime = expiredDateTime.toString(ISODateTimeFormat.dateTime()); createdISOTime = createdISOTime.substring(0, createdISOTime.length() - 6); createdISOTime = createdISOTime + "Z"; expiredISOTime = expiredISOTime.substring(0, expiredISOTime.length() - 6); expiredISOTime = expiredISOTime + "Z"; QName qNameCreated = new QName(Constants.CertificateEnrollment.WSS_SECURITY_UTILITY, Constants.CertificateEnrollment.CREATED); SOAPHeaderElement SOAPHeaderCreated = null; try { SOAPHeaderCreated = header.addHeaderElement(qNameCreated); SOAPHeaderCreated.addTextNode(createdISOTime); } catch (SOAPException e) { Response.serverError().build(); } QName qNameExpires = new QName(Constants.CertificateEnrollment.WSS_SECURITY_UTILITY, Constants.CertificateEnrollment.EXPIRES); SOAPHeaderElement SOAPHeaderExpires = null; try { SOAPHeaderExpires = header.addHeaderElement(qNameExpires); SOAPHeaderExpires.addTextNode(expiredISOTime); } catch (SOAPException e) { Response.serverError().build(); } try { timestamp.addChildElement(SOAPHeaderCreated); timestamp.addChildElement(SOAPHeaderExpires); Security.addChildElement(timestamp); } catch (SOAPException e) { Response.serverError().build(); } try { message.saveChanges(); } catch (SOAPException e) { Response.serverError().build(); } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { message.writeTo(outputStream); } catch (IOException e) { Response.serverError().build(); } catch (SOAPException e) { Response.serverError().build(); } String messageString = null; try { messageString = new String(outputStream.toByteArray(), Constants.CertificateEnrollment.UTF_8); } catch (UnsupportedEncodingException e) { Response.serverError().build(); } Map<String, List<String>> headers = (Map<String, List<String>>) context .get(MessageContext.HTTP_REQUEST_HEADERS); headers = new HashMap<String, List<String>>(); headers.put(Constants.CertificateEnrollment.CONTENT_LENGTH, Arrays.asList(String.valueOf(messageString.length()))); context.put(MessageContext.HTTP_REQUEST_HEADERS, headers); } return true; }
From source file:test.saaj.TestAttachmentSerialization.java
public int saveMsgWithAttachments(OutputStream os) throws Exception { MessageFactory mf = MessageFactory.newInstance(); SOAPMessage msg = mf.createMessage(); SOAPPart sp = msg.getSOAPPart(); SOAPEnvelope envelope = sp.getEnvelope(); SOAPHeader header = envelope.getHeader(); SOAPBody body = envelope.getBody(); SOAPElement el = header.addHeaderElement(envelope.createName("field4", NS_PREFIX, NS_URI)); SOAPElement el2 = el.addChildElement("field4b", NS_PREFIX); SOAPElement el3 = el2.addTextNode("field4value"); el = body.addBodyElement(envelope.createName("bodyfield3", NS_PREFIX, NS_URI)); el2 = el.addChildElement("bodyfield3a", NS_PREFIX); el2.addTextNode("bodyvalue3a"); el2 = el.addChildElement("bodyfield3b", NS_PREFIX); el2.addTextNode("bodyvalue3b"); el2 = el.addChildElement("datefield", NS_PREFIX); AttachmentPart ap = msg.createAttachmentPart(); ap.setContent("some attachment text...", "text/plain"); msg.addAttachmentPart(ap);// w w w. j a v a 2 s. co m String jpgfilename = "docs/images/axis.jpg"; File myfile = new File(jpgfilename); FileDataSource fds = new FileDataSource(myfile); DataHandler dh = new DataHandler(fds); AttachmentPart ap2 = msg.createAttachmentPart(dh); ap2.setContentType("image/jpg"); msg.addAttachmentPart(ap2); // Test for Bug #17664 if (msg.saveRequired()) { msg.saveChanges(); } MimeHeaders headers = msg.getMimeHeaders(); assertTrue(headers != null); String[] contentType = headers.getHeader("Content-Type"); assertTrue(contentType != null); msg.writeTo(os); os.flush(); return msg.countAttachments(); }