public InvocationResponse invoke(MessageContext messageContext) throws AxisFault {

    int tenantID = PublisherUtil.getTenantId(messageContext);

    Map<Integer, EventingConfigData> tenantSpecificEventConfig =
        TenantEventConfigData.getTenantSpecificEventingConfigData();
    EventingConfigData eventingConfigData = tenantSpecificEventConfig.get(tenantID);

    if (eventingConfigData != null && eventingConfigData.isMessageTracingEnable()) {

      if (log.isDebugEnabled()) {
        log.debug("Message tracing enabled.");
      }

      AxisService service = messageContext.getAxisService();

      // Temporary fix for track API manager calls
      if (service == null
          || (SystemFilter.isFilteredOutService(service.getAxisServiceGroup())
              && !AgentUtil.allowedServices(service.getName()))
          || service.isClientSide()) {
        return InvocationResponse.CONTINUE;
      } else {

        String activityUUID = HandlerUtils.getUniqueId();
        Object transportHeaders = messageContext.getProperty(MessageContext.TRANSPORT_HEADERS);

        if (transportHeaders != null) {
          String aid = (String) ((Map) transportHeaders).get(MessageTracerConstants.ACTIVITY_ID);
          if (aid != null) {
            if (aid.equals(MessageTracerConstants.EMPTY_STRING)) {
              ((Map) messageContext.getProperty(MessageContext.TRANSPORT_HEADERS))
                  .put(MessageTracerConstants.ACTIVITY_ID, activityUUID);
              if (log.isDebugEnabled()) {
                log.debug("Propagated AID was empty, IN generating new AID");
              }
            } else {
              activityUUID = aid;
              if (log.isDebugEnabled()) {
                log.debug("IN using propagated AID");
              }
            }
          } else {
            ((Map) messageContext.getProperty(MessageContext.TRANSPORT_HEADERS))
                .put(MessageTracerConstants.ACTIVITY_ID, activityUUID);
            if (log.isDebugEnabled()) {
              log.debug("Propagated AID was null, IN generating new AID");
            }
          }
        } else {
          Map<String, String> headers = new TreeMap<String, String>();
          headers.put(MessageTracerConstants.ACTIVITY_ID, activityUUID);
          messageContext.setProperty(MessageContext.TRANSPORT_HEADERS, headers);
          if (log.isDebugEnabled()) {
            log.debug("Transport headers absent, IN generating new AID");
          }
        }

        messageContext.setProperty(MessageTracerConstants.TENANT_ID, tenantID);

        TracingInfo tracingInfo = new TracingInfo();
        tracingInfo.setActivityId(activityUUID);
        tracingInfo.setServer(AgentUtil.getServerName());
        tracingInfo.setMessageDirection(BAMDataPublisherConstants.IN_DIRECTION);
        tracingInfo.setHost(PublisherUtil.getHostAddress());
        tracingInfo.setServiceName(messageContext.getAxisService().getName());
        tracingInfo.setOperationName(messageContext.getAxisOperation().getName().getLocalPart());
        MessageContext inMessageContext =
            messageContext.getOperationContext().getMessageContext(WSDL2Constants.MESSAGE_LABEL_IN);
        if (inMessageContext != null) {
          Object requestProperty =
              inMessageContext.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
          AgentUtil.extractInfoFromHttpHeaders(tracingInfo, requestProperty);
        }

        try {
          if (eventingConfigData.isDumpBodyEnable()) {
            try {
              Class cls =
                  Class.forName(
                      MessageTracerConstants
                          .ORG_APACHE_SYNAPSE_TRANSPORT_PASSTHRU_UTIL_RELAY_UTILS_CLASS_NAME);
              Class[] paramClasses = new Class[] {MessageContext.class, Boolean.TYPE};
              Method method =
                  cls.getMethod(MessageTracerConstants.BUILD_MESSAGE_METHOD_NAME, paramClasses);
              method.invoke(null, messageContext, false);
            } catch (ClassNotFoundException ignore) {
              // ignore
            } catch (Exception e) {
              throw new AxisFault("Error in building input message: " + e.getMessage(), e);
            }
            SOAPEnvelope soapEnvelope = messageContext.getEnvelope();
            SOAPBody body = soapEnvelope.getBody();
            if (body != null) {
              tracingInfo.setPayload(body.toString());
            }

            SOAPHeader header = soapEnvelope.getHeader();
            if (header != null) {
              tracingInfo.setHeader(header.toString());
            }
          }

          tracingInfo.setTimestamp(System.currentTimeMillis());

          if (MessageContext.IN_FLOW == messageContext.getFLOW()) {
            tracingInfo.setStatus(MessageTracerConstants.STATUS_SUCCESS);
          } else if (MessageContext.IN_FAULT_FLOW == messageContext.getFLOW()) {
            tracingInfo.setStatus(MessageTracerConstants.STATUS_FAULT);
          }

          publisher.publish(tenantID, tracingInfo);
        } catch (OMException e) {
          log.error("Unable to get SOAP details " + e.getMessage(), e);
        }
      }
    }
    return InvocationResponse.CONTINUE;
  }
Example #2
0
  /**
   * Builds a SOAPEnvelope from DOM Document.
   *
   * @param doc - The dom document that contains a SOAP message
   * @param useDoom
   * @return
   * @throws WSSecurityException
   */
  public static SOAPEnvelope getSOAPEnvelopeFromDOMDocument(Document doc, boolean useDoom)
      throws WSSecurityException {

    if (useDoom) {
      try {
        // Get processed headers
        SOAPEnvelope env = (SOAPEnvelope) doc.getDocumentElement();
        ArrayList processedHeaderQNames = new ArrayList();
        SOAPHeader soapHeader = env.getHeader();

        if (soapHeader != null) {
          Iterator headerBlocs = soapHeader.getChildElements();
          while (headerBlocs.hasNext()) {

            OMElement element = (OMElement) headerBlocs.next();
            SOAPHeaderBlock header = null;

            if (element instanceof SOAPHeaderBlock) {
              header = (SOAPHeaderBlock) element;

              // If a header block is not an instance of SOAPHeaderBlock, it means that
              // it is a header we have added in rampart eg. EncryptedHeader and should
              // be converted to SOAPHeaderBlock for processing
            } else {
              header = soapHeader.addHeaderBlock(element.getLocalName(), element.getNamespace());
              Iterator attrIter = element.getAllAttributes();
              while (attrIter.hasNext()) {
                OMAttribute attr = (OMAttribute) attrIter.next();
                header.addAttribute(
                    attr.getLocalName(), attr.getAttributeValue(), attr.getNamespace());
              }
              Iterator nsIter = element.getAllDeclaredNamespaces();
              while (nsIter.hasNext()) {
                OMNamespace ns = (OMNamespace) nsIter.next();
                header.declareNamespace(ns);
              }
              // retrieve all child nodes (including any text nodes)
              // and re-attach to header block
              Iterator children = element.getChildren();
              while (children.hasNext()) {
                OMNode child = (OMNode) children.next();
                child.detach();
                header.addChild(child);
              }

              element.detach();

              soapHeader.build();

              header.setProcessed();
            }

            if (header.isProcessed()) {
              processedHeaderQNames.add(element.getQName());
            }
          }
        }
        XMLStreamReader reader = ((OMElement) doc.getDocumentElement()).getXMLStreamReader();
        StAXSOAPModelBuilder stAXSOAPModelBuilder = new StAXSOAPModelBuilder(reader, null);
        SOAPEnvelope envelope = stAXSOAPModelBuilder.getSOAPEnvelope();

        // 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();
          }
        }

        envelope.build();

        return envelope;

      } catch (FactoryConfigurationError e) {
        throw new WSSecurityException(e.getMessage());
      }
    } else {
      try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        XMLUtils.outputDOM(doc.getDocumentElement(), os, true);
        ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray());

        StAXSOAPModelBuilder stAXSOAPModelBuilder =
            new StAXSOAPModelBuilder(
                XMLInputFactory.newInstance().createXMLStreamReader(bais), null);
        return stAXSOAPModelBuilder.getSOAPEnvelope();
      } catch (Exception e) {
        throw new WSSecurityException(e.getMessage());
      }
    }
  }
Example #3
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);
    }
  }
  /**
   * Actual transformation of the current message into a fault message
   *
   * @param synCtx the current message context
   * @param soapVersion SOAP version of the resulting fault desired
   * @param synLog the Synapse log to use
   */
  private void makeSOAPFault(MessageContext synCtx, int soapVersion, SynapseLog synLog) {

    if (synLog.isTraceOrDebugEnabled()) {
      synLog.traceOrDebug("Creating a SOAP " + (soapVersion == SOAP11 ? "1.1" : "1.2") + " fault");
    }

    // get the correct SOAP factory to be used
    SOAPFactory factory =
        (soapVersion == SOAP11
            ? OMAbstractFactory.getSOAP11Factory()
            : OMAbstractFactory.getSOAP12Factory());

    // create the SOAP fault document and envelope
    OMDocument soapFaultDocument = factory.createOMDocument();
    SOAPEnvelope faultEnvelope = factory.getDefaultFaultEnvelope();
    soapFaultDocument.addChild(faultEnvelope);

    // create the fault element  if it is need
    SOAPFault fault = faultEnvelope.getBody().getFault();
    if (fault == null) {
      fault = factory.createSOAPFault();
    }

    // populate it
    setFaultCode(synCtx, factory, fault, soapVersion);
    setFaultReason(synCtx, factory, fault, soapVersion);
    setFaultNode(factory, fault);
    setFaultRole(factory, fault);
    setFaultDetail(synCtx, factory, fault);

    // set the all headers of original SOAP Envelope to the Fault Envelope
    if (synCtx.getEnvelope() != null) {
      SOAPHeader soapHeader = synCtx.getEnvelope().getHeader();
      if (soapHeader != null) {
        for (Iterator iter = soapHeader.examineAllHeaderBlocks(); iter.hasNext(); ) {
          Object o = iter.next();
          if (o instanceof SOAPHeaderBlock) {
            SOAPHeaderBlock header = (SOAPHeaderBlock) o;
            faultEnvelope.getHeader().addChild(header);
          } else if (o instanceof OMElement) {
            faultEnvelope.getHeader().addChild((OMElement) o);
          }
        }
      }
    }

    if (synLog.isTraceOrDebugEnabled()) {
      String msg =
          "Original SOAP Message : "
              + synCtx.getEnvelope().toString()
              + "Fault Message created : "
              + faultEnvelope.toString();
      if (synLog.isTraceTraceEnabled()) {
        synLog.traceTrace(msg);
      }
      if (log.isTraceEnabled()) {
        log.trace(msg);
      }
    }

    // overwrite current message envelope with new fault envelope
    try {
      synCtx.setEnvelope(faultEnvelope);
    } catch (AxisFault af) {
      handleException(
          "Error replacing current SOAP envelope " + "with the fault envelope", af, synCtx);
    }

    if (synCtx.getFaultTo() != null) {
      synCtx.setTo(synCtx.getFaultTo());
    } else if (synCtx.getReplyTo() != null) {
      synCtx.setTo(synCtx.getReplyTo());
    } else {
      synCtx.setTo(null);
    }

    // set original messageID as relatesTo
    if (synCtx.getMessageID() != null) {
      RelatesTo relatesTo = new RelatesTo(synCtx.getMessageID());
      synCtx.setRelatesTo(new RelatesTo[] {relatesTo});
    }

    synLog.traceOrDebug("End : Fault mediator");
  }