private SOAPEnvelope buildSoapEnvelope(String clientID, String value) {

    String targetEPR = "http://localhost:9000/soap/Service1";
    String opration = "sampleOperation";

    SOAPFactory soapFactory = OMAbstractFactory.getSOAP12Factory();

    OMNamespace wsaNamespace =
        soapFactory.createOMNamespace("http://www.w3.org/2005/08/addressing", "wsa");

    SOAPEnvelope envelope = soapFactory.createSOAPEnvelope();

    SOAPHeader header = soapFactory.createSOAPHeader();
    envelope.addChild(header);

    OMNamespace synNamespace =
        soapFactory.createOMNamespace("http://ws.apache.org/ns/synapse", "syn");
    OMElement clientIDElement = soapFactory.createOMElement("ClientID", synNamespace);
    clientIDElement.setText(clientID);
    header.addChild(clientIDElement);

    SOAPBody body = soapFactory.createSOAPBody();
    envelope.addChild(body);

    OMElement valueElement = soapFactory.createOMElement("Value", null);
    valueElement.setText(value);
    body.addChild(valueElement);

    return envelope;
  }
Esempio n. 2
0
  private void createAndSendSOAPResponse(
      Map<String, Object> serviceResults, String serviceName, HttpServletResponse response)
      throws EventHandlerException {
    try {
      // setup the response
      Debug.logVerbose("[EventHandler] : Setting up response message", module);
      String xmlResults = SoapSerializer.serialize(serviceResults);
      // Debug.logInfo("xmlResults ==================" + xmlResults, module);
      XMLStreamReader reader =
          XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xmlResults));
      StAXOMBuilder resultsBuilder = new StAXOMBuilder(reader);
      OMElement resultSer = resultsBuilder.getDocumentElement();

      // create the response soap
      SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
      SOAPEnvelope resEnv = factory.createSOAPEnvelope();
      SOAPBody resBody = factory.createSOAPBody();
      OMElement resService = factory.createOMElement(new QName(serviceName + "Response"));
      resService.addChild(resultSer.getFirstElement());
      resBody.addChild(resService);
      resEnv.addChild(resBody);

      // The declareDefaultNamespace method doesn't work see
      // (https://issues.apache.org/jira/browse/AXIS2-3156)
      // so the following doesn't work:
      // resService.declareDefaultNamespace(ModelService.TNS);
      // instead, create the xmlns attribute directly:
      OMAttribute defaultNS = factory.createOMAttribute("xmlns", null, ModelService.TNS);
      resService.addAttribute(defaultNS);

      // log the response message
      if (Debug.verboseOn()) {
        try {
          Debug.logInfo("Response Message:\n" + resEnv + "\n", module);
        } catch (Throwable t) {
        }
      }

      resEnv.serialize(response.getOutputStream());
      response.getOutputStream().flush();
    } catch (Exception e) {
      Debug.logError(e, module);
      throw new EventHandlerException(e.getMessage(), e);
    }
  }
  public static org.apache.axis2.context.MessageContext attachMessage(
      String jsonMessage, org.apache.axis2.context.MessageContext axis2Ctx) {
    if (jsonMessage == null) {
      log.error("Cannot attach null JSON string.");
      return null;
    }

    AxisConfiguration axisConfig = axis2Ctx.getConfigurationContext().getAxisConfiguration();
    if (axisConfig == null) {
      log.warn("Cannot create AxisConfiguration. AxisConfiguration is null.");
      return null;
    }

    try {
      SOAPFactory soapFactory = new SOAP12Factory();
      SOAPEnvelope envelope = soapFactory.createSOAPEnvelope();
      envelope.addChild(JsonUtil.newJsonPayload(axis2Ctx, jsonMessage, true, true));
      axis2Ctx.setEnvelope(envelope);
      return axis2Ctx;
    } catch (Exception e) {
      log.error("Cannot attach message to Message Context. Error: " + e.getMessage(), e);
      return null;
    }
  }