// This is copied from PropertyMediator, required to change Content-Type
 private void handleSpecialProperties(
     Object resultValue, org.apache.axis2.context.MessageContext axis2MessageCtx) {
   axis2MessageCtx.setProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE, resultValue);
   Object o =
       axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
   Map headers = (Map) o;
   if (headers != null) {
     headers.remove(HTTP.CONTENT_TYPE);
     headers.put(HTTP.CONTENT_TYPE, resultValue);
   }
 }
示例#2
0
文件: API.java 项目: rcamus/platform
  private String getHostHeader(org.apache.axis2.context.MessageContext msgCtx) {
    Map transportHeaders =
        (Map) msgCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    String hostHeader = null;
    if (transportHeaders != null) {
      hostHeader = (String) transportHeaders.get(HTTP.TARGET_HOST);
    }

    if (hostHeader == null) {
      hostHeader = (String) msgCtx.getProperty(NhttpConstants.SERVICE_PREFIX);
    }
    return hostHeader;
  }
 private boolean mediate(MessageContext synCtx, String format) {
   if (!isDoingXml(synCtx) && !isDoingJson(synCtx)) {
     log.error(
         "#mediate. Could not identify the payload format of the existing payload prior to mediate.");
     return false;
   }
   org.apache.axis2.context.MessageContext axis2MessageContext =
       ((Axis2MessageContext) synCtx).getAxis2MessageContext();
   StringBuffer result = new StringBuffer();
   StringBuffer resultCTX = new StringBuffer();
   regexTransformCTX(resultCTX, synCtx, format);
   replace(resultCTX.toString(), result, synCtx);
   String out = result.toString().trim();
   if (log.isDebugEnabled()) {
     log.debug("#mediate. Transformed payload format>>> " + out);
   }
   if (mediaType.equals(XML_TYPE)) {
     try {
       JsonUtil.removeJsonPayload(axis2MessageContext);
       OMElement omXML = AXIOMUtil.stringToOM(out);
       if (!checkAndReplaceEnvelope(
           omXML,
           synCtx)) { // check if the target of the PF 'format' is the entire SOAP envelop, not
                      // just the body.
         axis2MessageContext.getEnvelope().getBody().addChild(omXML.getFirstElement());
       }
     } catch (XMLStreamException e) {
       handleException("Error creating SOAP Envelope from source " + out, synCtx);
     }
   } else if (mediaType.equals(JSON_TYPE)) {
     JsonUtil.newJsonPayload(axis2MessageContext, out, true, true);
   } else if (mediaType.equals(TEXT_TYPE)) {
     JsonUtil.removeJsonPayload(axis2MessageContext);
     axis2MessageContext.getEnvelope().getBody().addChild(getTextElement(out));
   }
   // need to honour a content-type of the payload media-type as output from the payload
   // {re-merging patch https://wso2.org/jira/browse/ESBJAVA-3014}
   setContentType(synCtx);
   return true;
 }
 /**
  * Sets the content type based on the request content type and payload factory media type. This
  * should be called at the end before returning from the mediate() function.
  *
  * @param synCtx
  */
 private void setContentType(MessageContext synCtx) {
   org.apache.axis2.context.MessageContext a2mc =
       ((Axis2MessageContext) synCtx).getAxis2MessageContext();
   if (mediaType.equals(XML_TYPE)) {
     if (!XML_CONTENT_TYPE.equals(a2mc.getProperty(Constants.Configuration.MESSAGE_TYPE))
         && !SOAP11_CONTENT_TYPE.equals(a2mc.getProperty(Constants.Configuration.MESSAGE_TYPE))
         && !SOAP12_CONTENT_TYPE.equals(a2mc.getProperty(Constants.Configuration.MESSAGE_TYPE))) {
       a2mc.setProperty(Constants.Configuration.MESSAGE_TYPE, XML_CONTENT_TYPE);
       a2mc.setProperty(Constants.Configuration.CONTENT_TYPE, XML_CONTENT_TYPE);
       handleSpecialProperties(XML_CONTENT_TYPE, a2mc);
     }
   } else if (mediaType.equals(JSON_TYPE)) {
     a2mc.setProperty(Constants.Configuration.MESSAGE_TYPE, JSON_CONTENT_TYPE);
     a2mc.setProperty(Constants.Configuration.CONTENT_TYPE, JSON_CONTENT_TYPE);
     handleSpecialProperties(JSON_CONTENT_TYPE, a2mc);
   } else if (mediaType.equals(TEXT_TYPE)) {
     a2mc.setProperty(Constants.Configuration.MESSAGE_TYPE, TEXT_CONTENT_TYPE);
     a2mc.setProperty(Constants.Configuration.CONTENT_TYPE, TEXT_CONTENT_TYPE);
     handleSpecialProperties(TEXT_CONTENT_TYPE, a2mc);
   }
   a2mc.removeProperty("NO_ENTITY_BODY");
 }
示例#5
0
文件: API.java 项目: rcamus/platform
  void process(MessageContext synCtx) {
    if (log.isDebugEnabled()) {
      log.debug(
          "Processing message with ID: "
              + synCtx.getMessageID()
              + " through the "
              + "API: "
              + name);
    }

    synCtx.setProperty(RESTConstants.SYNAPSE_REST_API, getName());
    synCtx.setProperty(RESTConstants.SYNAPSE_REST_API_VERSION, versionStrategy.getVersion());
    synCtx.setProperty(RESTConstants.REST_API_CONTEXT, context);

    // Remove the API context part from the REST_URL_POSTFIX
    String restURLPostfix =
        (String)
            ((Axis2MessageContext) synCtx)
                .getAxis2MessageContext()
                .getProperty(NhttpConstants.REST_URL_POSTFIX);
    if (restURLPostfix != null) {
      if (!restURLPostfix.startsWith("/")) {
        restURLPostfix = "/" + restURLPostfix;
      }
      if (restURLPostfix.startsWith(context)) {
        restURLPostfix = restURLPostfix.substring(context.length());
        if (versionStrategy instanceof URLBasedVersionStrategy) {
          String version = versionStrategy.getVersion();
          if (restURLPostfix.startsWith(version)) {
            restURLPostfix = restURLPostfix.substring(version.length());
          } else if (restURLPostfix.startsWith("/" + version)) {
            restURLPostfix = restURLPostfix.substring(version.length() + 1);
          }
        }
        ((Axis2MessageContext) synCtx)
            .getAxis2MessageContext()
            .setProperty(NhttpConstants.REST_URL_POSTFIX, restURLPostfix);
      }
    }

    for (Handler handler : handlers) {
      if (log.isDebugEnabled()) {
        log.debug(
            "Processing message with ID: "
                + synCtx.getMessageID()
                + " through "
                + "handler: "
                + handler.getClass().getName());
      }

      boolean proceed;
      if (synCtx.isResponse()) {
        proceed = handler.handleResponse(synCtx);
      } else {
        proceed = handler.handleRequest(synCtx);
      }

      if (!proceed) {
        return;
      }
    }

    if (synCtx.isResponse()) {
      String resourceName = (String) synCtx.getProperty(RESTConstants.SYNAPSE_RESOURCE);
      if (resourceName != null) {
        Resource resource = resources.get(resourceName);
        if (resource != null) {
          resource.process(synCtx);
        }
      } else if (log.isDebugEnabled()) {
        log.debug("No resource information on the response: " + synCtx.getMessageID());
      }
      return;
    }

    String path = RESTUtils.getFullRequestPath(synCtx);
    String subPath;
    if (versionStrategy.getVersionType().equals(VersionStrategyFactory.TYPE_URL)) {
      // for URL based
      // request --> http://{host:port}/context/version/path/to/resource
      subPath = path.substring(context.length() + versionStrategy.getVersion().length() + 1);
    } else {
      subPath = path.substring(context.length());
    }
    if ("".equals(subPath)) {
      subPath = "/";
    }
    synCtx.setProperty(RESTConstants.REST_SUB_REQUEST_PATH, subPath);

    org.apache.axis2.context.MessageContext msgCtx =
        ((Axis2MessageContext) synCtx).getAxis2MessageContext();
    String hostHeader = getHostHeader(msgCtx);
    if (hostHeader != null) {
      synCtx.setProperty(
          RESTConstants.REST_URL_PREFIX, msgCtx.getIncomingTransportName() + "://" + hostHeader);
    }

    Set<Resource> acceptableResources = new HashSet<Resource>();
    for (Resource r : resources.values()) {
      if (r.canProcess(synCtx)) {
        acceptableResources.add(r);
      }
    }

    boolean processed = false;
    if (!acceptableResources.isEmpty()) {
      for (RESTDispatcher dispatcher : RESTUtils.getDispatchers()) {
        Resource resource = dispatcher.findResource(synCtx, acceptableResources);
        if (resource != null) {
          resource.process(synCtx);
          processed = true;
          break;
        }
      }
    }

    if (!processed) {
      if (log.isDebugEnabled()) {
        log.debug("No matching resource was found for the request: " + synCtx.getMessageID());
      }

      Mediator sequence = synCtx.getSequence(RESTConstants.NO_MATCHING_RESOURCE_HANDLER);
      if (sequence != null) {
        sequence.mediate(synCtx);
      }
    }
  }
示例#6
0
文件: API.java 项目: rcamus/platform
  boolean canProcess(MessageContext synCtx) {
    if (synCtx.isResponse()) {
      String apiName = (String) synCtx.getProperty(RESTConstants.SYNAPSE_REST_API);
      String version =
          synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION) == null
              ? ""
              : (String) synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION);
      // if api name is not matching OR versions are different
      if (!getName().equals(apiName) || !versionStrategy.getVersion().equals(version)) {
        return false;
      }
    } else {
      String path = RESTUtils.getFullRequestPath(synCtx);
      if (!path.startsWith(context + "/")
          && !path.startsWith(context + "?")
          && !context.equals(path)
          && !"/".equals(context)) {
        if (log.isDebugEnabled()) {
          log.debug("API context: " + context + " does not match request URI: " + path);
        }
        return false;
      }

      if (!versionStrategy.isMatchingVersion(synCtx)) {
        return false;
      }

      if (host != null || port != -1) {
        org.apache.axis2.context.MessageContext msgCtx =
            ((Axis2MessageContext) synCtx).getAxis2MessageContext();
        String hostHeader = getHostHeader(msgCtx);
        if (hostHeader != null) {
          if (host != null && !host.equals(extractHostName(hostHeader))) {
            if (log.isDebugEnabled()) {
              log.debug(
                  "API host: "
                      + host
                      + " does not match host information "
                      + "in the request: "
                      + hostHeader);
            }
            return false;
          }

          if (port != -1
              && port != extractPortNumber(hostHeader, msgCtx.getIncomingTransportName())) {
            if (log.isDebugEnabled()) {
              log.debug(
                  "API port: "
                      + port
                      + " does not match port information "
                      + "in the request: "
                      + hostHeader);
            }
            return false;
          }
        } else {
          if (log.isDebugEnabled()) {
            log.debug("Host information not available on the message");
          }
          return false;
        }
      }
    }

    return true;
  }