public static SOAPBodyElement getChildNode(SOAPBodyElement element, String name) throws SOAPException { @SuppressWarnings("unchecked") Iterator<SOAPBodyElement> elements = element.getChildElements(); while (elements.hasNext()) { SOAPBodyElement childNode = elements.next(); if (childNode.getNodeName().contains(name)) { return childNode; } } throw new SOAPException("Child element: " + name + " not found."); }
private void processRequestNode(final SOAPBodyElement body, final ServiceResponse response, final SOAPMessage soapRequest, final SOAPEnvelope envelope) throws SOAPException { boolean requestFound = false; NodeList list = soapRequest.getSOAPBody().getElementsByTagNameNS("*", response.getProducer().getServiceCode()); if (list.getLength() == 1) { // Copy request from soapRequest requestFound = copyRequestNode((Node) list.item(0), body, response); } // It was not possible to copy the request element, so we must create it if (!requestFound) { SOAPElement temp = body.addChildElement(envelope.createName("request")); if (response.isAddNamespaceToRequest()) { LOGGER.debug("Add provider namespace to request element."); SOAPHelper.addNamespace(temp, response); } } }
private boolean copyRequestNode(final Node node, final SOAPBodyElement body, final ServiceResponse response) { for (int i = 0; i < node.getChildNodes().getLength(); i++) { if (node.getChildNodes().item(i).getNodeType() == Node.ELEMENT_NODE && "request".equals(node.getChildNodes().item(i).getLocalName())) { Node requestNode = (Node) node.getChildNodes().item(i).cloneNode(true); Node importNode = (Node) body.getOwnerDocument().importNode(requestNode, true); body.appendChild(importNode); if (response.isAddNamespaceToRequest()) { LOGGER.debug("Add provider namespace to request element."); SOAPHelper.addNamespace(importNode, response); } return true; } } return false; }
/** * Gets the terminal response. * * @param message * @throws SOAPException */ private void writeTerminalResponse(SOAPMessage message) throws SOAPException { SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); Name terminalResponseName = envelope.createName("EditTerminalResponse", PREFIX, NAMESPACE_URI); SOAPBodyElement terminalResponseElement = (SOAPBodyElement) message.getSOAPBody().getChildElements(terminalResponseName).next(); NodeList list = terminalResponseElement.getChildNodes(); Node n = null; for (int i = 0; i < list.getLength(); i++) { n = list.item(i); if ("status".equalsIgnoreCase(n.getLocalName())) break; } logger.info("status of device = " + n.getNodeValue()); }
/** * This method creates a Terminal Request and sends back the SOAPMessage. * MSISDN value is passed into this method * * @param msisdn * @return * @throws SOAPException */ private SOAPMessage createTerminalRequest(String msisdn) throws SOAPException { SOAPMessage message = messageFactory.createMessage(); message.getMimeHeaders().addHeader("SOAPAction", "http://api.jasperwireless.com/ws/service/terminal/GetTerminalsByMsisdn"); SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); Name terminalRequestName = envelope.createName("GetTerminalsByMsisdnRequest", PREFIX, NAMESPACE_URI); SOAPBodyElement terminalRequestElement = message.getSOAPBody().addBodyElement(terminalRequestName); Name msgId = envelope.createName("messageId", PREFIX, NAMESPACE_URI); SOAPElement msgElement = terminalRequestElement.addChildElement(msgId); msgElement.setValue("TCE-100-ABC-34084"); Name version = envelope.createName("version", PREFIX, NAMESPACE_URI); SOAPElement versionElement = terminalRequestElement.addChildElement(version); versionElement.setValue("1.0"); Name license = envelope.createName("licenseKey", PREFIX, NAMESPACE_URI); SOAPElement licenseElement = terminalRequestElement.addChildElement(license); licenseElement.setValue(apiKey); Name msisdns = envelope.createName("msisdns", PREFIX, NAMESPACE_URI); SOAPElement msisdnsElement = terminalRequestElement.addChildElement(msisdns); Name msisdnName = envelope.createName("msisdn", PREFIX, NAMESPACE_URI); SOAPElement msisdnElement = msisdnsElement.addChildElement(msisdnName); msisdnElement.setValue(msisdn); return message; }
/** * Gets the terminal response. * * @param message * @throws SOAPException */ private String writeTerminalResponse(SOAPMessage message) throws SOAPException { SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); Name terminalResponseName = envelope.createName("GetTerminalsByMsisdnResponse", PREFIX, NAMESPACE_URI); SOAPBodyElement terminalResponseElement = (SOAPBodyElement) message.getSOAPBody().getChildElements(terminalResponseName).next(); Name terminals = envelope.createName("terminals", PREFIX, NAMESPACE_URI); Name terminal = envelope.createName("terminal", PREFIX, NAMESPACE_URI); SOAPBodyElement terminalsElement = (SOAPBodyElement) terminalResponseElement.getChildElements(terminals).next(); SOAPBodyElement terminalElement = (SOAPBodyElement) terminalsElement.getChildElements(terminal).next(); NodeList list = terminalElement.getChildNodes(); Node n = null, node = null; for (int i = 0; i < list.getLength(); i++) { node = list.item(i); logger.info(node.getLocalName() + ":" + node.getFirstChild().getNodeValue()); if ("iccid".equalsIgnoreCase(node.getLocalName())) { n = node; } } return n.getFirstChild().getNodeValue(); }
protected void createBody(SOAPBodyElement body, SOAPFactory spf) throws SOAPException { SOAPElement elm = body.addChildElement(spf.createName("ParameterList")); elm.setAttribute(SOAP_ARRAY_TYPE, "cwmp:SetParameterAttributesStruct[" + String.valueOf(attrs.size()) + "]"); int c = attrs.size(); for (int i = 0; i < c; i++) { SOAPElement param = elm.addChildElement("SetParameterAttributesStruct"); param.addChildElement("Name").setValue(attrs.get(i).Name); param.addChildElement("NotificationChange").setValue(b2s(attrs.get(i).NotificationChange)); param.addChildElement("Notification").setValue(String.valueOf(attrs.get(i).Notification)); param.addChildElement("AccessListChange").setValue(b2s(attrs.get(i).AccessListChange)); SOAPElement al = param.addChildElement(spf.createName("AccessList")); String acl[] = attrs.get(i).AccessList; int ca = acl.length; al.setAttribute(SOAP_ARRAY_TYPE, "xsd:string[" + String.valueOf(ca) + "]"); for (int i2 = 0; i2 < ca; i2++) { SOAPElement acle = al.addChildElement("string"); acle.setValue(acl[i2]); acle.setAttribute(XSI_TYPE, XSD_STRING); } } }
@Override protected void parseBody(SOAPBodyElement body, SOAPFactory spf) throws SOAPException { Iterator pi = getRequestChildElement(spf, body, "ExecResponseList").getChildElements(spf.createName("ExecResponseStruct")); Name nameKey = spf.createName("Command"); Name nameValue = spf.createName("Response"); while (pi.hasNext()) { SOAPElement param = (SOAPElement) pi.next(); String key = getRequestElement(param, nameKey); String value = getRequestElement(param, nameValue); if (value == null) { value = ""; } System.out.append(key + "->" + value); response.put(key, value); } }
public static SOAPMessage createDefaultSoapMessage(String responseMessage, String requestMessage) { try { SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(); SOAPBody body = soapMessage.getSOAPPart().getEnvelope().getBody(); QName payloadName = new QName("http://apache.org/hello_world_soap_http/types", "greetMeResponse", "ns1"); SOAPBodyElement payload = body.addBodyElement(payloadName); SOAPElement message = payload.addChildElement("responseType"); message.addTextNode(responseMessage + " Request was " + requestMessage); return soapMessage; } catch (SOAPException e) { e.printStackTrace(); throw new RuntimeException(e); } }
@Override protected boolean handleInbound(SOAPMessageContext msgContext) { log.info("handleInbound"); try { SOAPMessage soapMessage = msgContext.getMessage(); SOAPBody soapBody = soapMessage.getSOAPBody(); SOAPBodyElement soapBodyElement = (SOAPBodyElement)soapBody.getChildElements().next(); SOAPElement soapElement = (SOAPElement)soapBodyElement.getChildElements().next(); String value = soapElement.getValue(); soapElement.setValue(value + "|UserIn"); } catch (SOAPException e) { throw new WebServiceException(e); } return true; }
@Override protected boolean handleOutbound(SOAPMessageContext msgContext) { log.info("handleOutbound"); try { SOAPMessage soapMessage = msgContext.getMessage(); SOAPBody soapBody = soapMessage.getSOAPBody(); SOAPBodyElement soapBodyElement = (SOAPBodyElement)soapBody.getChildElements().next(); SOAPElement soapElement = (SOAPElement)soapBodyElement.getChildElements().next(); String value = soapElement.getValue(); soapElement.setValue(value + "|UserOut"); } catch (SOAPException e) { throw new WebServiceException(e); } return true; }
@Override protected boolean handleInbound(SOAPMessageContext msgContext) { log.info("handleInbound"); try { SOAPMessage soapMessage = msgContext.getMessage(); SOAPBody soapBody = soapMessage.getSOAPBody(); SOAPBodyElement soapBodyElement = (SOAPBodyElement)soapBody.getChildElements().next(); SOAPElement soapElement = (SOAPElement)soapBodyElement.getChildElements().next(); String value = soapElement.getValue(); soapElement.setValue(value + "|CustomIn"); } catch (SOAPException e) { throw new WebServiceException(e); } return true; }
@Override protected boolean handleOutbound(SOAPMessageContext msgContext) { log.info("handleOutbound"); try { SOAPMessage soapMessage = msgContext.getMessage(); SOAPBody soapBody = soapMessage.getSOAPBody(); SOAPBodyElement soapBodyElement = (SOAPBodyElement)soapBody.getChildElements().next(); SOAPElement soapElement = (SOAPElement)soapBodyElement.getChildElements().next(); String value = soapElement.getValue(); soapElement.setValue(value + "|CustomOut"); } catch (SOAPException e) { throw new WebServiceException(e); } return true; }
@Override protected boolean handleInbound(SOAPMessageContext msgContext) { log.info("handleInbound"); try { SOAPMessage soapMessage = msgContext.getMessage(); SOAPBody soapBody = soapMessage.getSOAPBody(); SOAPBodyElement soapBodyElement = (SOAPBodyElement)soapBody.getChildElements().next(); SOAPElement soapElement = (SOAPElement)soapBodyElement.getChildElements().next(); String value = soapElement.getValue(); soapElement.setValue(value + "|LogIn"); } catch (SOAPException e) { throw new WebServiceException(e); } return true; }
@Override protected boolean handleOutbound(SOAPMessageContext msgContext) { log.info("handleOutbound"); try { SOAPMessage soapMessage = msgContext.getMessage(); SOAPBody soapBody = soapMessage.getSOAPBody(); SOAPBodyElement soapBodyElement = (SOAPBodyElement)soapBody.getChildElements().next(); SOAPElement soapElement = (SOAPElement)soapBodyElement.getChildElements().next(); String value = soapElement.getValue(); soapElement.setValue(value + "|LogOut"); } catch (SOAPException e) { throw new WebServiceException(e); } return true; }
@Override protected boolean handleInbound(SOAPMessageContext msgContext) { log.info("handleInbound"); try { SOAPMessage soapMessage = msgContext.getMessage(); SOAPBody soapBody = soapMessage.getSOAPBody(); SOAPBodyElement soapBodyElement = (SOAPBodyElement)soapBody.getChildElements().next(); SOAPElement soapElement = (SOAPElement)soapBodyElement.getChildElements().next(); String value = soapElement.getValue(); soapElement.setValue(value + "|RoutIn"); } catch (SOAPException e) { throw new WebServiceException(e); } return true; }
@Override protected boolean handleOutbound(SOAPMessageContext msgContext) { log.info("handleOutbound"); try { SOAPMessage soapMessage = msgContext.getMessage(); SOAPBody soapBody = soapMessage.getSOAPBody(); SOAPBodyElement soapBodyElement = (SOAPBodyElement)soapBody.getChildElements().next(); SOAPElement soapElement = (SOAPElement)soapBodyElement.getChildElements().next(); String value = soapElement.getValue(); soapElement.setValue(value + "|RoutOut"); } catch (SOAPException e) { throw new WebServiceException(e); } return true; }
@Override protected boolean handleInbound(SOAPMessageContext msgContext) { log.info("handleInbound"); try { SOAPMessage soapMessage = msgContext.getMessage(); SOAPBody soapBody = soapMessage.getSOAPBody(); SOAPBodyElement soapBodyElement = (SOAPBodyElement)soapBody.getChildElements().next(); SOAPElement soapElement = (SOAPElement)soapBodyElement.getChildElements().next(); String value = soapElement.getValue(); soapElement.setValue(value + "|AuthIn"); } catch (SOAPException e) { throw new WebServiceException(e); } return true; }
@Override protected boolean handleOutbound(SOAPMessageContext msgContext) { log.info("handleOutbound"); try { SOAPMessage soapMessage = msgContext.getMessage(); SOAPBody soapBody = soapMessage.getSOAPBody(); SOAPBodyElement soapBodyElement = (SOAPBodyElement)soapBody.getChildElements().next(); SOAPElement soapElement = (SOAPElement)soapBodyElement.getChildElements().next(); String value = soapElement.getValue(); soapElement.setValue(value + "|AuthOut"); } catch (SOAPException e) { throw new WebServiceException(e); } return true; }
@Override protected boolean handleInbound(SOAPMessageContext msgContext) { log.info("handleInbound"); try { SOAPMessage soapMessage = ((SOAPMessageContext)msgContext).getMessage(); SOAPBody soapBody = soapMessage.getSOAPBody(); SOAPBodyElement soapBodyElement = (SOAPBodyElement)soapBody.getChildElements().next(); SOAPElement soapElement = (SOAPElement)soapBodyElement.getChildElements().next(); String value = soapElement.getValue(); soapElement.setValue(value + "|EpIn"); } catch (SOAPException e) { throw new WebServiceException(e); } return true; }
@Override protected boolean handleOutbound(SOAPMessageContext msgContext) { log.info("handleOutbound"); try { SOAPMessage soapMessage = ((SOAPMessageContext)msgContext).getMessage(); SOAPBody soapBody = soapMessage.getSOAPBody(); SOAPBodyElement soapBodyElement = (SOAPBodyElement)soapBody.getChildElements().next(); SOAPElement soapElement = (SOAPElement)soapBodyElement.getChildElements().next(); String value = soapElement.getValue(); soapElement.setValue(value + "|EpOut"); } catch (SOAPException e) { throw new WebServiceException(e); } return true; }
@Override protected boolean handleInbound(SOAPMessageContext msgContext) { try { SOAPMessage soapMessage = msgContext.getMessage(); SOAPBody soapBody = soapMessage.getSOAPBody(); SOAPBodyElement soapBodyElement = (SOAPBodyElement)soapBody.getChildElements().next(); if(soapBodyElement.getChildElements().hasNext()) { SOAPElement payload = (SOAPElement)soapBodyElement.getChildElements().next(); String value = payload.getValue(); payload.setValue(value + "World"); } } catch (SOAPException e) { throw new WebServiceException(e); } return true; }
@Test @RunAsClient public void testSendMultipartSoapMessage() throws Exception { final MessageFactory msgFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL); final SOAPMessage msg = msgFactory.createMessage(); final SOAPBodyElement bodyElement = msg.getSOAPBody().addBodyElement( new QName("urn:ledegen:soap-attachment:1.0", "echoImage")); bodyElement.addTextNode("cid:" + IN_IMG_NAME); final AttachmentPart ap = msg.createAttachmentPart(); ap.setDataHandler(getResource("saaj/jbws3857/" + IN_IMG_NAME)); ap.setContentId(IN_IMG_NAME); msg.addAttachmentPart(ap); final SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance(); final SOAPConnection connection = conFactory.createConnection(); final SOAPMessage response = connection.call(msg, new URL("http://" + baseURL.getHost()+ ":" + baseURL.getPort() + "/" + PROJECT_NAME + "/testServlet")); final String contentTypeWeHaveSent = getBodyElementTextValue(response); assertContentTypeStarts("multipart/related", contentTypeWeHaveSent); }
private SOAPMessage invokeWithOperationInSOAPAtachment( String operationName, int firstArg, int secondArg) { SOAPMessage request = null; try { request = createRequestMessage(); addOperationSOAPAttachment(operationName, request); SOAPBodyElement operation = getOperation(request); populateOperationArgs(operation, firstArg, secondArg); } catch (SOAPException e) { e.printStackTrace(); } return dispatch.invoke(request); }
private SOAPMessage invokeWithOperationInHTTPHdr(String operationName, int firstArg, int secondArg) { addOperationHTTPHdr(operationName); SOAPMessage request = null; try { request = createRequestMessage(); SOAPBodyElement operation = getOperation(request); populateOperationArgs(operation, firstArg, secondArg); } catch (SOAPException e) { e.printStackTrace(); } return dispatch.invoke(request); }
/** * Very simple operation. * If there are no attachments, an exception is thrown. * Otherwise the message is echoed. */ public SOAPMessage invoke(SOAPMessage soapMessage) { System.out.println(">> SoapMessageCheckMTOMProvider: Request received."); int numAttachments = soapMessage.countAttachments(); if (numAttachments == 0) { System.out.println(">> SoapMessageCheckMTOMProvider: No Attachments."); throw new WebServiceException("No Attachments are detected"); } SOAPMessage response = null; try { MessageFactory factory = MessageFactory.newInstance(); response = factory.createMessage(); SOAPPart soapPart = response.getSOAPPart(); SOAPBody soapBody = soapPart.getEnvelope().getBody(); soapBody.addChildElement((SOAPBodyElement) soapMessage. getSOAPBody().getChildElements().next()); response.addAttachmentPart((AttachmentPart) soapMessage.getAttachments().next()); } catch (SOAPException e) { throw new WebServiceException(e); } System.out.println(">> SoapMessageCheckMTOMProvider: Returning."); return response; // echo back the same message }
private void createSimpleSOAPPart(SOAPMessage message) throws SOAPException { SOAPPart sPart = message.getSOAPPart(); SOAPEnvelope env = sPart.getEnvelope(); SOAPBody body = env.getBody(); SOAPHeader header = env.getHeader(); header.addHeaderElement(env.createName("Header1", "pref", "http://test.apach.org/test")) .addTextNode("This is header1"); Name ns = env.createName("echo", "swa2", "http://fakeNamespace2.org"); final SOAPBodyElement bodyElement = body.addBodyElement(ns); Name ns2 = env.createName("something"); final SOAPElement ele1 = bodyElement.addChildElement(ns2); ele1.addTextNode("This is some text"); Name ns3 = env.createName("ping", "swa3", "http://fakeNamespace3.org"); final SOAPBodyElement bodyElement2 = body.addBodyElement(ns3); Name ns4 = env.createName("another"); final SOAPElement ele2 = bodyElement2.addChildElement(ns4); ele2.addTextNode("This is another text"); }
private SOAPMessage createSOAPMessage(Element elem) throws Exception { String prefix = ""; 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 Name bodyName = factory.createName(elem.getNodeName(), prefix, UDDI_V2_NAMESPACE); SOAPBodyElement bodyElement = soapBody.addBodyElement(bodyName); bodyElement.addNamespaceDeclaration(prefix, UDDI_V2_NAMESPACE); appendAttributes(bodyElement, elem.getAttributes(), factory); appendElements(bodyElement, elem.getChildNodes(), factory); return message; }