Ejemplo n.º 1
0
  /**
   * Converts a given {@link Document} to an AXIOM {@link org.apache.axiom.soap.SOAPEnvelope}.
   *
   * @param document the document to be converted
   * @return the converted envelope
   * @throws IllegalArgumentException in case of errors
   * @see org.apache.rampart.util.Axis2Util.getSOAPEnvelopeFromDOMDocument(Document, boolean)
   */
  public static SOAPEnvelope toEnvelope(Document document) {
    try {
      DOMImplementation implementation = document.getImplementation();
      Assert.isInstanceOf(DOMImplementationLS.class, implementation);

      DOMImplementationLS loadSaveImplementation = (DOMImplementationLS) implementation;
      LSOutput output = loadSaveImplementation.createLSOutput();
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      output.setByteStream(bos);

      LSSerializer serializer = loadSaveImplementation.createLSSerializer();
      serializer.write(document, output);

      ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());

      XMLInputFactory inputFactory = StAXUtils.getXMLInputFactory();

      StAXSOAPModelBuilder stAXSOAPModelBuilder =
          new StAXSOAPModelBuilder(inputFactory.createXMLStreamReader(bis), null);
      SOAPEnvelope envelope = stAXSOAPModelBuilder.getSOAPEnvelope();

      // Necessary to build a correct Axiom tree, see SWS-483
      envelope.serialize(new NullOutputStream());

      return envelope;
    } catch (Exception ex) {
      IllegalArgumentException iaex =
          new IllegalArgumentException("Error in converting Document to SOAP Envelope");
      iaex.initCause(ex);
      throw iaex;
    }
  }
Ejemplo 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);
    }
  }
Ejemplo n.º 3
0
  /**
   * Converts a given AXIOM {@link org.apache.axiom.soap.SOAPEnvelope} to a {@link Document}.
   *
   * @param envelope the SOAP envelope to be converted
   * @return the converted document
   * @throws IllegalArgumentException in case of errors
   * @see org.apache.rampart.util.Axis2Util.getDocumentFromSOAPEnvelope(SOAPEnvelope, boolean)
   */
  public static Document toDocument(SOAPEnvelope envelope) {
    try {
      if (envelope instanceof Element) {
        return ((Element) envelope).getOwnerDocument();
      } else {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        envelope.build();
        envelope.serialize(bos);

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        return documentBuilderFactory.newDocumentBuilder().parse(bis);
      }
    } catch (Exception ex) {
      throw new IllegalArgumentException("Error in converting SOAP Envelope to Document", ex);
    }
  }
Ejemplo n.º 4
0
  /**
   * Creates a DOM Document using the SOAP Envelope.
   *
   * @param env An org.apache.axiom.soap.SOAPEnvelope instance
   * @return Returns the DOM Document of the given SOAP Envelope.
   * @throws Exception
   */
  public static Document getDocumentFromSOAPEnvelope(SOAPEnvelope env, boolean useDoom)
      throws WSSecurityException {
    try {
      if (env instanceof Element) {
        return ((Element) env).getOwnerDocument();
      }

      if (useDoom) {
        env.build();

        // Workaround to prevent a bug in AXIOM where
        // there can be an incomplete OMElement as the first child body
        OMElement firstElement = env.getBody().getFirstElement();
        if (firstElement != null) {
          firstElement.build();
        }

        // Get processed headers
        SOAPHeader soapHeader = env.getHeader();
        ArrayList processedHeaderQNames = new ArrayList();
        if (soapHeader != null) {
          Iterator headerBlocs = soapHeader.getChildElements();
          while (headerBlocs.hasNext()) {
            SOAPHeaderBlock element = (SOAPHeaderBlock) headerBlocs.next();
            if (element.isProcessed()) {
              processedHeaderQNames.add(element.getQName());
            }
          }
        }

        // Check the namespace and find SOAP version and factory
        String nsURI = null;
        SOAPFactory factory;
        if (env.getNamespace()
            .getNamespaceURI()
            .equals(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI)) {
          nsURI = SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI;
          factory = DOOMAbstractFactory.getSOAP11Factory();
        } else {
          nsURI = SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI;
          factory = DOOMAbstractFactory.getSOAP12Factory();
        }

        StAXSOAPModelBuilder stAXSOAPModelBuilder =
            new StAXSOAPModelBuilder(env.getXMLStreamReader(), factory, nsURI);
        SOAPEnvelope envelope = (stAXSOAPModelBuilder).getSOAPEnvelope();
        ((OMNode) envelope.getParent()).build();

        // Set the processed flag of the processed headers
        SOAPHeader header = envelope.getHeader();
        for (Iterator iter = processedHeaderQNames.iterator(); iter.hasNext(); ) {
          QName name = (QName) iter.next();
          Iterator omKids = header.getChildrenWithName(name);
          if (omKids.hasNext()) {
            ((SOAPHeaderBlock) omKids.next()).setProcessed();
          }
        }

        Element envElem = (Element) envelope;
        return envElem.getOwnerDocument();
      } else {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        env.build();
        env.serialize(baos);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        return factory.newDocumentBuilder().parse(bais);
      }
    } catch (Exception e) {
      throw new WSSecurityException("Error in converting SOAP Envelope to Document", e);
    }
  }