/**
   * Whether the original request received by the synapse is REST
   *
   * @param originalInMsgCtx request message
   * @return <code>true</code> if the request was a REST request
   */
  private static boolean isRequestRest(MessageContext originalInMsgCtx) {

    boolean isRestRequest =
        originalInMsgCtx.getProperty(NhttpConstants.REST_REQUEST_CONTENT_TYPE) != null;

    if (!isRestRequest) {

      String httpMethod =
          (String) originalInMsgCtx.getProperty(Constants.Configuration.HTTP_METHOD);

      isRestRequest =
          Constants.Configuration.HTTP_METHOD_GET.equals(httpMethod)
              || Constants.Configuration.HTTP_METHOD_DELETE.equals(httpMethod)
              || Constants.Configuration.HTTP_METHOD_PUT.equals(httpMethod)
              || RESTConstants.METHOD_OPTIONS.equals(httpMethod)
              || Constants.Configuration.HTTP_METHOD_HEAD.equals(httpMethod);

      if (!isRestRequest) {

        isRestRequest =
            Constants.Configuration.HTTP_METHOD_POST.equals(httpMethod)
                && HTTPTransportUtils.isRESTRequest(
                    String.valueOf(
                        originalInMsgCtx.getProperty(Constants.Configuration.MESSAGE_TYPE)));

        if (!isRestRequest) {
          isRestRequest =
              (String.valueOf(originalInMsgCtx.getProperty(Constants.Configuration.MESSAGE_TYPE))
                      .equals(HTTPConstants.MEDIA_TYPE_TEXT_XML)
                  && originalInMsgCtx.getSoapAction() == null);
        }
      }
    }
    return isRestRequest;
  }
  public VerbInfoDTO findMatchingVerb(MessageContext synCtx)
      throws ResourceNotFoundException, APISecurityException {

    VerbInfoDTO verb = null;

    // This function is used by more than one handler. If on one execution of this function, it has
    // found and placed
    // the matching verb in the cache, the same can be re-used from all handlers since all handlers
    // share the same
    // MessageContext. The API_RESOURCE_CACHE_KEY property will be set in the MessageContext to
    // indicate that the
    // verb has been put into the cache.
    String resourceCacheKey = (String) synCtx.getProperty(APIConstants.API_RESOURCE_CACHE_KEY);
    if (resourceCacheKey != null) {
      verb = (VerbInfoDTO) getResourceCache().get(resourceCacheKey);
      // Cache hit
      if (verb != null) {
        if (log.isDebugEnabled()) {
          log.debug("Found resource in Cache for key: ".concat(resourceCacheKey));
        }
        return verb;
      }
      if (log.isDebugEnabled()) {
        log.debug("Resource not found in cache for key: ".concat(resourceCacheKey));
      }
    }
    String resourceString = (String) synCtx.getProperty(APIConstants.API_ELECTED_RESOURCE);
    String apiContext = (String) synCtx.getProperty(RESTConstants.REST_API_CONTEXT);
    String apiVersion = (String) synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION);
    String fullRequestPath = (String) synCtx.getProperty(RESTConstants.REST_FULL_REQUEST_PATH);
    String apiName = (String) synCtx.getProperty(RESTConstants.SYNAPSE_REST_API);

    String requestPath = Utils.getRequestPath(synCtx, fullRequestPath, apiContext, apiVersion);
    if ("".equals(requestPath)) {
      requestPath = "/";
    }

    if (log.isDebugEnabled()) {
      log.debug("Setting REST_SUB_REQUEST_PATH in msg context: ".concat(requestPath));
    }
    synCtx.setProperty(RESTConstants.REST_SUB_REQUEST_PATH, requestPath);

    String httpMethod =
        (String)
            ((Axis2MessageContext) synCtx)
                .getAxis2MessageContext()
                .getProperty(Constants.Configuration.HTTP_METHOD);
    if (resourceString == null) {
      API selectedApi = synCtx.getConfiguration().getAPI(apiName);
      Resource selectedResource = null;

      if (selectedApi != null) {
        Resource[] selectedAPIResources = selectedApi.getResources();

        Set<Resource> acceptableResources = new HashSet<Resource>();

        for (Resource resource : selectedAPIResources) {
          // If the requesting method is OPTIONS or if the Resource contains the requesting method
          if (RESTConstants.METHOD_OPTIONS.equals(httpMethod)
              || (resource.getMethods() != null
                  && Arrays.asList(resource.getMethods()).contains(httpMethod))) {
            acceptableResources.add(resource);
          }
        }

        if (acceptableResources.size() > 0) {
          for (RESTDispatcher dispatcher : RESTUtils.getDispatchers()) {
            Resource resource = dispatcher.findResource(synCtx, acceptableResources);
            if (resource != null && Arrays.asList(resource.getMethods()).contains(httpMethod)) {
              selectedResource = resource;
              break;
            }
          }
        }
      }

      if (selectedResource == null) {
        // No matching resource found.
        String msg = "Could not find matching resource for " + requestPath;
        log.error(msg);
        throw new ResourceNotFoundException(msg);
      }

      resourceString = selectedResource.getDispatcherHelper().getString();
      resourceCacheKey =
          APIUtil.getResourceInfoDTOCacheKey(apiContext, apiVersion, resourceString, httpMethod);

      if (log.isDebugEnabled()) {
        log.debug("Selected Resource: ".concat(resourceString));
      }
      // Set the elected resource
      synCtx.setProperty(APIConstants.API_ELECTED_RESOURCE, resourceString);
    }
    verb = (VerbInfoDTO) getResourceCache().get(resourceCacheKey);

    // Cache hit
    if (verb != null) {
      if (log.isDebugEnabled()) {
        log.debug("Got Resource from cache for key: ".concat(resourceCacheKey));
      }
      // Set cache key in the message context so that it can be used by the subsequent handlers.
      synCtx.setProperty(APIConstants.API_RESOURCE_CACHE_KEY, resourceCacheKey);
      return verb;
    }

    if (log.isDebugEnabled()) {
      log.debug("Cache miss for Resource for key: ".concat(resourceCacheKey));
    }

    String apiCacheKey = APIUtil.getAPIInfoDTOCacheKey(apiContext, apiVersion);
    APIInfoDTO apiInfoDTO = null;
    apiInfoDTO = (APIInfoDTO) getResourceCache().get(apiCacheKey);

    // Cache miss
    if (apiInfoDTO == null) {
      if (log.isDebugEnabled()) {
        log.debug("Could not find API object in cache for key: ".concat(apiCacheKey));
      }
      apiInfoDTO = doGetAPIInfo(apiContext, apiVersion);
      getResourceCache().put(apiCacheKey, apiInfoDTO);
    }
    if (apiInfoDTO.getResources() != null) {
      for (ResourceInfoDTO resourceInfoDTO : apiInfoDTO.getResources()) {
        if ((resourceString.trim()).equalsIgnoreCase(resourceInfoDTO.getUrlPattern().trim())) {
          for (VerbInfoDTO verbDTO : resourceInfoDTO.getHttpVerbs()) {
            if (verbDTO.getHttpVerb().equals(httpMethod)) {
              if (log.isDebugEnabled()) {
                log.debug("Putting resource object in cache with key: ".concat(resourceCacheKey));
              }
              verbDTO.setRequestKey(resourceCacheKey);
              // Store verb in cache
              getResourceCache().put(resourceCacheKey, verbDTO);
              // Set cache key in the message context so that it can be used by the subsequent
              // handlers.
              synCtx.setProperty(APIConstants.API_RESOURCE_CACHE_KEY, resourceCacheKey);
              return verbDTO;
            }
          }
        }
      }
    }
    return null;
  }