public JSONWebServiceAction getJSONWebServiceAction(HttpServletRequest request) {

    String path = GetterUtil.getString(request.getPathInfo());

    String method = GetterUtil.getString(request.getMethod());

    String pathParameters = null;

    JSONRPCRequest jsonRpcRequest = null;

    int pathParametersIndex = _getPathParametersIndex(path);

    if (pathParametersIndex != -1) {
      pathParameters = path.substring(pathParametersIndex);

      path = path.substring(0, pathParametersIndex);
    } else {
      if (method.equals(HttpMethods.POST) && !PortalUtil.isMultipartRequest(request)) {

        jsonRpcRequest = JSONRPCRequest.detectJSONRPCRequest(request);

        if (jsonRpcRequest != null) {
          path += StringPool.SLASH + jsonRpcRequest.getMethod();

          method = null;
        }
      }
    }

    JSONWebServiceActionParameters jsonWebServiceActionParameters =
        new JSONWebServiceActionParameters();

    jsonWebServiceActionParameters.collectAll(request, pathParameters, jsonRpcRequest);

    String[] parameterNames = jsonWebServiceActionParameters.getParameterNames();

    int jsonWebServiceActionConfigIndex =
        _getJSONWebServiceActionConfigIndex(path, method, parameterNames);

    if (jsonWebServiceActionConfigIndex == -1) {
      throw new RuntimeException(
          "No JSON web service action associated with path " + path + " and method " + method);
    }

    JSONWebServiceActionConfig jsonWebServiceActionConfig =
        _jsonWebServiceActionConfigs.get(jsonWebServiceActionConfigIndex);

    return new JSONWebServiceActionImpl(jsonWebServiceActionConfig, jsonWebServiceActionParameters);
  }
  @Override
  public void service(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {

    if (PortalUtil.isMultipartRequest(request)) {
      UploadServletRequest uploadServletRequest = new UploadServletRequestImpl(request);

      request = uploadServletRequest;
    }

    String path = GetterUtil.getString(request.getPathInfo());

    if ((!path.equals(StringPool.BLANK) && !path.equals(StringPool.SLASH))
        || (request.getParameter("discover") != null)) {

      Locale locale = PortalUtil.getLocale(request, response, true);

      LocaleThreadLocal.setThemeDisplayLocale(locale);

      super.service(request, response);

      return;
    }

    if (_log.isDebugEnabled()) {
      _log.debug("Servlet context " + request.getContextPath());
    }

    String apiPath = PortalUtil.getPathMain() + "/portal/api/jsonws";

    HttpSession session = request.getSession();

    ServletContext servletContext = session.getServletContext();

    boolean remoteAccess = AccessControlThreadLocal.isRemoteAccess();

    try {
      AccessControlThreadLocal.setRemoteAccess(true);

      String contextPath = PortalContextLoaderListener.getPortalServletContextPath();

      if (servletContext.getContext(contextPath) != null) {
        if (!contextPath.equals(StringPool.SLASH) && apiPath.startsWith(contextPath)) {

          apiPath = apiPath.substring(contextPath.length());
        }

        RequestDispatcher requestDispatcher = request.getRequestDispatcher(apiPath);

        requestDispatcher.forward(request, response);
      } else {
        String servletContextPath = ContextPathUtil.getContextPath(servletContext);

        String redirectPath = "/api/jsonws?contextPath=" + HttpUtil.encodeURL(servletContextPath);

        response.sendRedirect(redirectPath);
      }
    } finally {
      AccessControlThreadLocal.setRemoteAccess(remoteAccess);
    }
  }