public void testRestURLPostfix3() throws Exception {
    API api = new API(TEST_API, "/services/Foo");
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(TEST_API, api);

    RESTRequestHandler handler = new RESTRequestHandler();

    MessageContext synCtx = getMessageContext(synapseConfig, false, "/services/Foo/test", "GET");
    // When the service path is in the URL, NHTTP transport removes that portion
    // from the postfix
    ((Axis2MessageContext) synCtx)
        .getAxis2MessageContext()
        .setProperty(NhttpConstants.REST_URL_POSTFIX, "/test");
    handler.process(synCtx);
    checkRestURLPostfix(synCtx, "/test");
  }
Beispiel #2
0
  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);
      }
    }
  }