List of usage examples for javax.xml.soap SOAPMessage getSOAPHeader
public SOAPHeader getSOAPHeader() throws SOAPException
From source file:org.codice.ddf.security.interceptor.GuestInterceptor.java
@Override public void handleMessage(SoapMessage message) throws Fault { if (guestAccessDenied) { LOGGER.debug("Guest Access not enabled - no message checking performed."); return;/*from w w w . j av a 2s . co m*/ } if (message == null) { LOGGER.error("Incoming SOAP message is null - guest interceptor makes no sense."); return; } SOAPMessage soapMessage = getSOAPMessage(message); internalHandleMessage(message, soapMessage); if (LOGGER.isTraceEnabled()) { try { LOGGER.trace("SOAP request after guest interceptor: {}", XMLUtils.prettyFormat(soapMessage.getSOAPHeader().getParentNode())); } catch (SOAPException e) { //ignore } } }
From source file:org.codice.ddf.security.interceptor.GuestInterceptor.java
private void createAddressing(SoapMessage message, SOAPMessage soapMessage) { SOAPFactory soapFactory;/*from w ww .ja va 2s . co m*/ try { soapFactory = SOAPFactory.newInstance(); } catch (SOAPException e) { LOGGER.error("Could not create a SOAPFactory.", e); return; // can't add anything if we can't create it } String addressingProperty = org.apache.cxf.ws.addressing.JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES_INBOUND; AddressingProperties addressingProperties = new AddressingProperties(); try { SOAPElement action = soapFactory.createElement(org.apache.cxf.ws.addressing.Names.WSA_ACTION_NAME, org.apache.cxf.ws.addressing.JAXWSAConstants.WSA_PREFIX, org.apache.cxf.ws.security.wss4j.DefaultCryptoCoverageChecker.WSA_NS); action.addTextNode((String) message.get(org.apache.cxf.message.Message.REQUEST_URL)); AttributedURIType attributedString = new AttributedURIType(); String actionValue = StringUtils.defaultIfEmpty((String) message.get(SoapBindingConstants.SOAP_ACTION), ""); attributedString.setValue(actionValue); addressingProperties.setAction(attributedString); soapMessage.getSOAPHeader().addChildElement(action); } catch (SOAPException e) { LOGGER.error("Unable to add addressing action.", e); } try { SOAPElement messageId = soapFactory.createElement(org.apache.cxf.ws.addressing.Names.WSA_MESSAGEID_NAME, org.apache.cxf.ws.addressing.JAXWSAConstants.WSA_PREFIX, org.apache.cxf.ws.security.wss4j.DefaultCryptoCoverageChecker.WSA_NS); String uuid = "urn:uuid:" + UUID.randomUUID().toString(); messageId.addTextNode(uuid); AttributedURIType attributedString = new AttributedURIType(); attributedString.setValue(uuid); addressingProperties.setMessageID(attributedString); soapMessage.getSOAPHeader().addChildElement(messageId); } catch (SOAPException e) { LOGGER.error("Unable to add addressing messageId.", e); } try { SOAPElement to = soapFactory.createElement(org.apache.cxf.ws.addressing.Names.WSA_TO_NAME, org.apache.cxf.ws.addressing.JAXWSAConstants.WSA_PREFIX, org.apache.cxf.ws.security.wss4j.DefaultCryptoCoverageChecker.WSA_NS); to.addTextNode((String) message.get(org.apache.cxf.message.Message.REQUEST_URL)); EndpointReferenceType endpointReferenceType = new EndpointReferenceType(); AttributedURIType attributedString = new AttributedURIType(); attributedString.setValue((String) message.get(org.apache.cxf.message.Message.REQUEST_URL)); endpointReferenceType.setAddress(attributedString); addressingProperties.setTo(endpointReferenceType); soapMessage.getSOAPHeader().addChildElement(to); } catch (SOAPException e) { LOGGER.error("Unable to add addressing to.", e); } try { SOAPElement replyTo = soapFactory.createElement(org.apache.cxf.ws.addressing.Names.WSA_REPLYTO_NAME, org.apache.cxf.ws.addressing.JAXWSAConstants.WSA_PREFIX, org.apache.cxf.ws.security.wss4j.DefaultCryptoCoverageChecker.WSA_NS); SOAPElement address = soapFactory.createElement(org.apache.cxf.ws.addressing.Names.WSA_ADDRESS_NAME, org.apache.cxf.ws.addressing.JAXWSAConstants.WSA_PREFIX, org.apache.cxf.ws.security.wss4j.DefaultCryptoCoverageChecker.WSA_NS); address.addTextNode(org.apache.cxf.ws.addressing.Names.WSA_ANONYMOUS_ADDRESS); replyTo.addChildElement(address); soapMessage.getSOAPHeader().addChildElement(replyTo); } catch (SOAPException e) { LOGGER.error("Unable to add addressing replyTo.", e); } message.put(addressingProperty, addressingProperties); }
From source file:org.jboss.jaxr.juddi.transport.SaajTransport.java
private SOAPMessage createSOAPMessage(Element elem) throws Exception { String prefix = "uddi"; MessageFactory msgFactory = MessageFactory.newInstance(); SOAPFactory factory = SOAPFactory.newInstance(); SOAPMessage message = msgFactory.createMessage(); message.getSOAPHeader().detachNode(); SOAPPart soapPart = message.getSOAPPart(); SOAPBody soapBody = soapPart.getEnvelope().getBody(); //Create the outer body element String uddins = IRegistry.UDDI_V2_NAMESPACE; Name bodyName = factory.createName(elem.getNodeName(), prefix, uddins); SOAPBodyElement bodyElement = soapBody.addBodyElement(bodyName); bodyElement.addNamespaceDeclaration(prefix, uddins); appendAttributes(bodyElement, elem.getAttributes(), factory); appendElements(bodyElement, elem.getChildNodes(), factory); return message; }
From source file:org.jboss.jaxr.juddi.transport.WS4EESaajTransport.java
public Element send(Element request, URL endpointURL) throws RegistryException { log.debug("Request message:" + XMLUtils.toString(request)); if ("true".equalsIgnoreCase(debugProp)) System.out.println("Request Element:" + XMLUtils.toString(request)); Element response = null;//w w w. j a va 2 s. co m try { MessageFactory msgFactory = MessageFactory.newInstance(); SOAPMessage message = msgFactory.createMessage(); message.getSOAPHeader().detachNode(); SOAPPart soapPart = message.getSOAPPart(); SOAPBody soapBody = soapPart.getEnvelope().getBody(); soapBody.addChildElement(getSOAPElement(soapBody, request)); //There seems to be a bug in the Saaj/Axis implementation that requires //message to be written to an output stream ByteArrayOutputStream by = new ByteArrayOutputStream(); message.writeTo(by); //Does not do anything by.close(); if ("true".equalsIgnoreCase(debugProp)) message.writeTo(System.out); //Make the SAAJ Call now SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection connection = soapConnectionFactory.createConnection(); SOAPMessage soapResponse = connection.call(message, endpointURL); if ("true".equalsIgnoreCase(debugProp)) { System.out.println("Response is:"); soapResponse.writeTo(System.out); } soapBody = soapResponse.getSOAPBody(); boolean hasFault = soapBody.hasFault(); if (hasFault) { SOAPFault soapFault = soapBody.getFault(); String faultStr = soapFault.getFaultCode() + "::" + soapFault.getFaultString(); throw new RegistryException(faultStr); } response = getFirstChildElement(soapBody); } catch (Exception ex) { ex.printStackTrace(); log.error(ex); throw new RegistryException(ex); } log.debug("Response message:" + XMLUtils.getText(response)); return response; }
From source file:org.openhie.test.xds.util.SoapMessageSender.java
public static void main(String args[]) throws Exception { SoapMessageSender test = new SoapMessageSender(); // Create SOAP Connection SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection soapConnection = soapConnectionFactory.createConnection(); // Send SOAP Message to SOAP Server String url = "http://iol.test.ohie.org:5001/xdsrepository"; SOAPMessage so = test.getSoapMessageFromString(); SOAPHeader header = so.getSOAPHeader(); log.info(soapConnection.call(test.getSoapMessageFromString(), url)); // print SOAP Response System.out.print("Response SOAP Message:"); //soapResponse.writeTo(System.out); soapConnection.close();//from w w w . j a v a2 s. c o m }
From source file:org.openhie.test.xds.util.SoapMessageSender.java
private SOAPMessage getSoapMessageFromString() throws SOAPException, IOException { MessageFactory factory = MessageFactory.newInstance(); //MimeHeaders header=new MimeHeaders(); //header.addHeader("Content-Type","application/xop+xml; application/soap+xml" + "action=urn:ihe:iti:2007:ProvideAndRegisterDocumentSet-b" + "boundary=MIMEBoundaryurn_uuid_E3F7CE4554928DA89B1231365678616;"+ "Content-Transfer-Encoding=binary" + "Content-ID=\"<0.urn:uuid:E3F7CE4554928DA89B1231365678617@apache.org>\""); //header.addHeader("action", "urn:ihe:iti:2007:ProvideAndRegisterDocumentSet-b"); SOAPMessage message = MessageFactory.newInstance().createMessage(); SOAPPart soapPart = message.getSOAPPart(); MimeHeaders header = message.getMimeHeaders(); //header.addHeader("Content-Type","application/xop+xml; application/soap+xml" + "action=urn:ihe:iti:2007:ProvideAndRegisterDocumentSet-b" + "boundary=MIMEBoundaryurn_uuid_E3F7CE4554928DA89B1231365678616;"+ "Content-Transfer-Encoding=binary" + "Content-ID=\"<0.urn:uuid:E3F7CE4554928DA89B1231365678617@apache.org>\""); //header.addHeader("Content-Type", "multipart/related;start=<rootpart*17f1ec84-85ec-4f1f-9b29-423a6a2e354f@example.jaxws.sun.com>;type=application/xop+xml;boundary=uuid:17f1ec84-85ec-4f1f-9b29-423a6a2e354f\";start-info=\"application/soap+xml\";action=\"urn:ihe:iti:2007:ProvideAndRegisterDocumentSet-b"); header.setHeader("Content-Type", "application/soap+xml;charset=UTF-8"); SOAPHeader soap = message.getSOAPHeader(); soapPart.setContent(new StreamSource(new FileInputStream( "/Users/snkasthu/SourceCode/cds/openhie-integration-tests/src/test/resources/xds/OHIE-XDS-01-10.xml"))); System.out.print("Request SOAP Message :"); message.writeTo(System.out);/* w ww .j a v a 2 s . c o m*/ System.out.println(); return message; }
From source file:org.wso2.carbon.device.mgt.mobile.windows.api.services.enrollment.util.MessageHandler.java
/** * This method adds Timestamp for SOAP header, and adds Content-length for HTTP header for * avoiding HTTP chunking.// w w w. ja v a 2 s . co m * * @param context - Context of the SOAP Message */ @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().entity("SOAP message content cannot be read.").build(); } try { if ((header == null) && (envelope != null)) { header = envelope.addHeader(); } } catch (SOAPException e) { Response.serverError().entity("SOAP header cannot be added.").build(); } SOAPFactory soapFactory = null; try { soapFactory = SOAPFactory.newInstance(); } catch (SOAPException e) { Response.serverError().entity("Cannot get an instance of SOAP factory.").build(); } QName qNamesSecurity = new QName(PluginConstants.WS_SECURITY_TARGET_NAMESPACE, PluginConstants.CertificateEnrolment.SECURITY); SOAPHeaderElement Security = null; Name attributeName = null; try { if (header != null) { Security = header.addHeaderElement(qNamesSecurity); } if (soapFactory != null) { attributeName = soapFactory.createName(PluginConstants.CertificateEnrolment.TIMESTAMP_ID, PluginConstants.CertificateEnrolment.TIMESTAMP_U, PluginConstants.CertificateEnrolment.WSS_SECURITY_UTILITY); } } catch (SOAPException e) { Response.serverError().entity("Security header cannot be added.").build(); } QName qNameTimestamp = new QName(PluginConstants.CertificateEnrolment.WSS_SECURITY_UTILITY, PluginConstants.CertificateEnrolment.TIMESTAMP); SOAPHeaderElement timestamp = null; try { if (header != null) { timestamp = header.addHeaderElement(qNameTimestamp); timestamp.addAttribute(attributeName, PluginConstants.CertificateEnrolment.TIMESTAMP_0); } } catch (SOAPException e) { Response.serverError().entity("Exception while adding timestamp header.").build(); } DateTime dateTime = new DateTime(); DateTime expiredDateTime = dateTime.plusMinutes(VALIDITY_TIME); String createdISOTime = dateTime.toString(ISODateTimeFormat.dateTime()); String expiredISOTime = expiredDateTime.toString(ISODateTimeFormat.dateTime()); createdISOTime = createdISOTime.substring(TIMESTAMP_BEGIN_INDEX, createdISOTime.length() - TIMESTAMP_END_INDEX); createdISOTime = createdISOTime + TIME_ZONE; expiredISOTime = expiredISOTime.substring(TIMESTAMP_BEGIN_INDEX, expiredISOTime.length() - TIMESTAMP_END_INDEX); expiredISOTime = expiredISOTime + TIME_ZONE; QName qNameCreated = new QName(PluginConstants.CertificateEnrolment.WSS_SECURITY_UTILITY, PluginConstants.CertificateEnrolment.CREATED); SOAPHeaderElement SOAPHeaderCreated = null; try { if (header != null) { SOAPHeaderCreated = header.addHeaderElement(qNameCreated); SOAPHeaderCreated.addTextNode(createdISOTime); } } catch (SOAPException e) { Response.serverError().entity("Exception while creating SOAP header.").build(); } QName qNameExpires = new QName(PluginConstants.CertificateEnrolment.WSS_SECURITY_UTILITY, PluginConstants.CertificateEnrolment.EXPIRES); SOAPHeaderElement SOAPHeaderExpires = null; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); String messageString = null; try { if (header != null) { SOAPHeaderExpires = header.addHeaderElement(qNameExpires); SOAPHeaderExpires.addTextNode(expiredISOTime); } if ((timestamp != null) && (Security != null)) { timestamp.addChildElement(SOAPHeaderCreated); timestamp.addChildElement(SOAPHeaderExpires); Security.addChildElement(timestamp); } message.saveChanges(); message.writeTo(outputStream); messageString = new String(outputStream.toByteArray(), PluginConstants.CertificateEnrolment.UTF_8); } catch (SOAPException e) { Response.serverError().entity("Exception while creating timestamp SOAP header.").build(); } catch (IOException e) { Response.serverError().entity("Exception while writing message to output stream.").build(); } Map<String, List<String>> headers = (Map<String, List<String>>) context .get(MessageContext.HTTP_REQUEST_HEADERS); headers = new HashMap<String, List<String>>(); if (messageString != null) { headers.put(PluginConstants.CONTENT_LENGTH, Arrays.asList(String.valueOf(messageString.length()))); } context.put(MessageContext.HTTP_REQUEST_HEADERS, headers); } return true; }
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 ww .jav a 2 s . co 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:ru.codeinside.gws.crypto.cryptopro.CryptoProvider.java
private static VerifyResult verifyMessage(final SOAPMessage message) throws Exception { final long startMs = System.currentTimeMillis(); try {/* w w w .j a va2 s . com*/ final SOAPPart doc = message.getSOAPPart(); final SOAPEnvelope envelope = doc.getEnvelope(); final SOAPHeader soapHeader = message.getSOAPHeader(); final Element recipient = findSecurityToken(soapHeader, ACTOR_RECIPIENT); final X509Certificate recipientCert; if (recipient != null) { final ValidateResult recipientResult = validate(recipient); if (recipientResult.error != null) { return new VerifyResult(" : " + recipientResult.error, null, recipientResult.cert); } recipientCert = recipientResult.cert; } else { recipientCert = null; } final Element smev = findSecurityToken(soapHeader, ACTOR_SMEV); if (smev == null) { return new VerifyResult(" : ??", null, recipientCert); } final ValidateResult smevResult = validate(smev); return new VerifyResult(smevResult.error, smevResult.cert, recipientCert); } finally { if (log.isDebugEnabled()) { log.debug("VERIFY: " + (System.currentTimeMillis() - startMs) + "ms"); } } }
From source file:ru.codeinside.gws.crypto.cryptopro.CryptoProvider.java
public void sign(final SOAPMessage message) { try {/*from w w w . j ava 2s .co m*/ loadCertificate(); final long startMs = System.currentTimeMillis(); final SOAPPart doc = message.getSOAPPart(); final QName wsuId = doc.getEnvelope().createQName("Id", "wsu"); final SOAPHeader header = message.getSOAPHeader(); final QName actor = header.createQName("actor", header.getPrefix()); final SOAPElement security = header.addChildElement("Security", "wsse", WSSE); security.addAttribute(actor, ACTOR_SMEV); SOAPElement binarySecurityToken = security.addChildElement("BinarySecurityToken", "wsse"); binarySecurityToken.setAttribute("EncodingType", WSS_BASE64_BINARY); binarySecurityToken.setAttribute("ValueType", WSS_X509V3); binarySecurityToken.setValue(DatatypeConverter.printBase64Binary(cert.getEncoded())); binarySecurityToken.addAttribute(wsuId, "CertId"); XMLSignature signature = new XMLSignature(doc, "", XMLSignature.ALGO_ID_SIGNATURE_GOST_GOST3410_3411, Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS); { Element element = signature.getElement(); Element keyInfo = doc.createElementNS(Constants.SignatureSpecNS, "KeyInfo"); Element securityTokenReference = doc.createElementNS(WSSE, "SecurityTokenReference"); Element reference = doc.createElementNS(WSSE, "Reference"); reference.setAttribute("URI", "#CertId"); reference.setAttribute("ValueType", WSS_X509V3); securityTokenReference.appendChild(reference); keyInfo.appendChild(securityTokenReference); element.appendChild(keyInfo); security.appendChild(element); } Transforms transforms = new Transforms(doc); transforms.addTransform(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS); signature.addDocument("#body", transforms, MessageDigestAlgorithm.ALGO_ID_DIGEST_GOST3411); signature.sign(privateKey); if (log.isDebugEnabled()) { log.debug("SIGN: " + (System.currentTimeMillis() - startMs) + "ms"); } } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } }