@Override
  public NSArray getRequestHandlerPathForRequest(WORequest request) {
    NSMutableArray<Object> requestHandlerPath = new NSMutableArray<Object>();

    try {
      String path = request._uriDecomposed().requestHandlerPath();

      ERXRoute matchingRoute =
          setupRequestWithRouteForMethodAndPath(request, request.method(), path);
      if (matchingRoute != null) {
        @SuppressWarnings("unchecked")
        NSDictionary<ERXRoute.Key, String> keys =
            (NSDictionary<ERXRoute.Key, String>)
                request.userInfo().objectForKey(ERXRouteRequestHandler.KeysKey);
        String controller = keys.objectForKey(ERXRoute.ControllerKey);
        String actionName = keys.objectForKey(ERXRoute.ActionKey);
        requestHandlerPath.addObject(controller);
        requestHandlerPath.addObject(actionName);
      } else {
        requestHandlerPath.addObject(
            ERXProperties.stringForKeyWithDefault(
                "ERXRest.missingControllerName", "ERXMissingRouteController"));
        requestHandlerPath.addObject("missing");
        // throw new FileNotFoundException("There is no controller for the route '" + path + "'.");
      }

    } catch (Throwable t) {
      throw new RuntimeException("Failed to process the requested route.", t);
    }

    return requestHandlerPath;
  }
 @Override
 @SuppressWarnings("unchecked")
 public WOAction getActionInstance(Class class1, Class[] aclass, Object[] aobj) {
   ERXRouteController controller =
       (ERXRouteController) super.getActionInstance(class1, aclass, aobj);
   WORequest request = (WORequest) aobj[0];
   setupRouteControllerFromUserInfo(controller, request.userInfo());
   return controller;
 }
示例#3
0
  public void takeValuesFromRequest(WORequest request, WOContext context) {
    if (context._wasFormSubmitted()) {
      super.takeValuesFromRequest(request, context);

      String apiKey = ERXProperties.stringForKey("er.captcha.akismet.apiKey");
      String url =
          ERXProperties.stringForKeyWithDefault(
              "er.captcha.akismet.url", "http://" + request._serverName());
      Akismet api = new Akismet(apiKey, url);
      if (ERXApplication.isDevelopmentModeSafe()) {
        if (!api.verifyAPIKey()) {
          throw new RuntimeException(
              "The API key you provided is invalid. Please set a valid api key in the property 'er.captcha.akismet.apiKey'.");
        }
      }

      String ipAddress = stringValueForBinding("remoteAddress", request._remoteAddress());
      String userAgent = stringValueForBinding("userAgent", request.headerForKey("user-agent"));
      String referrer = stringValueForBinding("referrer", request.headerForKey("referer"));
      String permalink = stringValueForBinding("permalink");
      String commentType = stringValueForBinding("commentType");
      String author = stringValueForBinding("author");
      String authorEmail = stringValueForBinding("authorEmail");
      String authorURL = stringValueForBinding("authorURL");
      String content = stringValueForBinding("content");
      Map other = null;

      boolean isSpam =
          api.commentCheck(
              ipAddress,
              userAgent,
              referrer,
              permalink,
              commentType,
              author,
              authorEmail,
              authorURL,
              content,
              other);
      if (isSpam) {
        validationFailedWithException(
            new NSValidation.ValidationException("Spam check failed."),
            this,
            ERXSimpleSpamCheck.SPAM_CHECK_KEY);
        setValueForBinding(Boolean.FALSE, "valid");
      } else {
        setValueForBinding(Boolean.TRUE, "valid");
      }
    }
  }
示例#4
0
  /**
   * Gets a shared browser object for given request. Parses <code>"user-agent"</code> string in the
   * request and gets the appropiate browser object.
   *
   * <p>This is the primary method to call from application logics, and once you get a browser
   * object, you are responsible to call {@link #retainBrowser retainBrowser} to keep the browser
   * object in the browser pool.
   *
   * <p>You are also required to call {@link #releaseBrowser releaseBrowser} to release the browser
   * from the pool when it is no longer needed.
   *
   * @param request WORequest
   * @return a shared browser object
   */
  public ERXBrowser browserMatchingRequest(WORequest request) {
    if (request == null) {
      throw new IllegalArgumentException("Request can't be null.");
    }

    String ua = request.headerForKey("user-agent");
    if (ua == null) {
      return getBrowserInstance(
          ERXBrowser.UNKNOWN_BROWSER,
          ERXBrowser.UNKNOWN_VERSION,
          ERXBrowser.UNKNOWN_VERSION,
          ERXBrowser.UNKNOWN_PLATFORM,
          null);
    }

    ERXBrowser result = (ERXBrowser) _cache.objectForKey(ua);
    if (result == null) {
      String browserName = parseBrowserName(ua);
      String version = parseVersion(ua);
      String mozillaVersion = parseMozillaVersion(ua);
      String platform = parsePlatform(ua);
      NSDictionary userInfo =
          new NSDictionary(
              new Object[] {parseCPU(ua), parseGeckoVersion(ua)},
              new Object[] {"cpu", "geckoRevision"});

      result = getBrowserInstance(browserName, version, mozillaVersion, platform, userInfo);
      _cache.setObjectForKey(result, ua);
    }
    return result;
  }
示例#5
0
 public IERXRequestDescription descriptionObjectForContext(WOContext aContext, String string) {
   if (aContext != null) {
     try {
       WOComponent component = aContext.page();
       String componentName = component != null ? component.name() : "NoNameComponent";
       String additionalInfo = "(no additional Info)";
       WORequest request = aContext.request();
       String requestHandler =
           request != null ? request.requestHandlerKey() : "NoRequestHandler";
       if (!requestHandler.equals("wo")) {
         additionalInfo = additionalInfo + aContext.request().uri();
       }
       return new ERXNormalRequestDescription(componentName, requestHandler, additionalInfo);
     } catch (RuntimeException e) {
       log.error("Cannot get context description since received exception.", e);
     }
   }
   return new ERXEmptyRequestDescription(string);
 }
 /*
  * (non-Javadoc)
  *
  * @see
  * com.webobjects.appserver.WORequestHandler#handleRequest(com.webobjects
  * .appserver.WORequest)
  */
 @Override
 public WOResponse handleRequest(WORequest request) {
   // TODO Auto-generated method stub
   WOResponse aResponse = null;
   WOApplication anApplication = Application.app();
   if (anApplication.isRefusingNewSessions()
       && !request.isSessionIDInRequest()
       && request.isUsingWebServer()) {
     aResponse = generateRequestRefusal(request);
   } else {
     Object lock = anApplication.requestHandlingLock();
     if (lock != null)
       synchronized (lock) {
         aResponse = _handleRequest(request);
       }
     else aResponse = _handleRequest(request);
   }
   if (aResponse == null) aResponse = nullResponse();
   return aResponse;
 }
  /**
   * Sets up the request userInfo for the given request for a request of the given method and path.
   *
   * @param request the request to configure the userInfo on
   * @param method the request method
   * @param path the request path
   * @return the matching route for this method and path
   */
  public ERXRoute setupRequestWithRouteForMethodAndPath(
      WORequest request, String method, String path) {
    @SuppressWarnings("unchecked")
    NSDictionary<String, Object> userInfo = request.userInfo();
    NSMutableDictionary<String, Object> mutableUserInfo;
    if (userInfo instanceof NSMutableDictionary) {
      mutableUserInfo = (NSMutableDictionary<String, Object>) userInfo;
    } else if (userInfo != null) {
      mutableUserInfo = userInfo.mutableClone();
    } else {
      mutableUserInfo = new NSMutableDictionary<String, Object>();
    }

    ERXRoute matchingRoute = routeForMethodAndPath(method, path, mutableUserInfo);

    if (
    /*matchingRoute != null && */ mutableUserInfo != userInfo) {
      request.setUserInfo(mutableUserInfo);
    }

    return matchingRoute;
  }
 public WOResponse generateRequestRefusal(WORequest aRequest) {
   WODynamicURL aURIString = aRequest._uriDecomposed();
   String contentString =
       (new StringBuilder())
           .append(
               "D�sol�, votre demande n'a pas pu �tre imm�diatement trait�es. S'il vous pla�t essayer cette URL: <a href=\"")
           .append(aURIString)
           .append("\">")
           .append(aURIString)
           .append("</a>")
           .toString();
   aURIString.setApplicationNumber("-1");
   WOResponse aResponse = WOApplication.application().createResponseInContext(null);
   WOResponse._redirectResponse(aResponse, aURIString.toString(), contentString);
   return aResponse;
 }
  public WOResponse _handleRequest(WORequest request) {
    // Retrieve the application object. We need to inform it of awake/sleep
    // and use some of its helper methods.
    WOApplication application = Application.app();

    WOResponse response;
    WOContext context;

    application.awake();
    try {
      // Instantiate the action object for this request.
      // The WOAction sets up the context and restores the session and so
      // on.
      WOAction action = new ContentAction(request);

      // Retrieve the context object from the action.
      context = action.context();

      // Retrieve the content path. e.g. blog or blog/2009/10/10/foobar or
      // whatever.
      String contentPath = request.requestHandlerPath();

      // TODO: We probably could use some exception handling here.
      // 1. performActionNamed throws generating the WOActionResults
      // 2. performActionNamed returns null
      // 3. generateResponse throws
      // 4. generateResponse returns null (although we do kind of handle
      // this already).

      // Ask the action object to handle the request. Unlike normal action
      // objects the
      // ContentAction object takes a path instead of the first part of a
      // method name.
      WOActionResults actionResults = action.performActionNamed(contentPath);

      // Generate the response object.
      if (actionResults != null) response = actionResults.generateResponse();
      else response = null;

      // FIXME: When we do add error handling, do we or don't we save the
      // session in the
      // event of an error?
      if (context != null) {
        // Check the session in to the session store. Particularly
        // important if the
        // session store is out of process.
        application.saveSessionForContext(context);
      }
    } finally {
      // End of request.
      application.sleep();
    }

    // Ah, the joys of calling private APIs. For some reason both
    // WOActionRequestHandler
    // and WOComponentRequestHandler know about and call this method as
    // virtually the
    // last thing before returning the response. I am somewhat unclear as to
    // why this
    // method is private and why it isn't called by our caller instead of
    // within the
    // request handler.
    // It is imperative that this method be called because it generates HTTP
    // Set-Cookie
    // headers from the NSArray<WOCookie>. Without this no cookies will ever
    // function.
    if (response != null) response._finalizeInContext(context);

    return response;
  }
示例#10
0
  @SuppressWarnings("unchecked")
  @Override
  public WOResponse handleRequest(WORequest request) {
    WOApplication application = WOApplication.application();
    application.awake();
    try {
      WOContext context = application.createContextForRequest(request);
      WOResponse response = application.createResponseInContext(context);

      Object output;
      try {
        String inputString = request.contentString();
        JSONObject input = new JSONObject(inputString);
        String wosid = request.cookieValueForKey("wosid");
        if (wosid == null) {
          ERXMutableURL url = new ERXMutableURL();
          url.setQueryParameters(request.queryString());
          wosid = url.queryParameter("wosid");
          if (wosid == null && input.has("wosid")) {
            wosid = input.getString("wosid");
          }
        }
        context._setRequestSessionID(wosid);
        WOSession session = null;
        if (context._requestSessionID() != null) {
          session = WOApplication.application().restoreSessionWithID(wosid, context);
        }
        if (session != null) {
          session.awake();
        }
        try {
          JSONComponentCallback componentCallback = null;

          ERXDynamicURL url = new ERXDynamicURL(request._uriDecomposed());
          String requestHandlerPath = url.requestHandlerPath();
          JSONRPCBridge jsonBridge;
          if (requestHandlerPath != null && requestHandlerPath.length() > 0) {
            String componentNameAndInstance = requestHandlerPath;
            String componentInstance;
            String componentName;
            int slashIndex = componentNameAndInstance.indexOf('/');
            if (slashIndex == -1) {
              componentName = componentNameAndInstance;
              componentInstance = null;
            } else {
              componentName = componentNameAndInstance.substring(0, slashIndex);
              componentInstance = componentNameAndInstance.substring(slashIndex + 1);
            }

            if (session == null) {
              session = context.session();
            }

            String bridgesKey =
                (componentInstance == null) ? "_JSONGlobalBridges" : "_JSONInstanceBridges";
            Map<String, JSONRPCBridge> componentBridges =
                (Map<String, JSONRPCBridge>) session.objectForKey(bridgesKey);
            if (componentBridges == null) {
              int limit =
                  ERXProperties.intForKeyWithDefault(
                      (componentInstance == null)
                          ? "er.ajax.json.globalBacktrackCacheSize"
                          : "er.ajax.json.backtrackCacheSize",
                      WOApplication.application().pageCacheSize());
              componentBridges = new LRUMap<String, JSONRPCBridge>(limit);
              session.setObjectForKey(componentBridges, bridgesKey);
            }
            jsonBridge = componentBridges.get(componentNameAndInstance);
            if (jsonBridge == null) {
              Class componentClass = _NSUtilities.classWithName(componentName);
              JSONComponent component;
              if (JSONComponent.class.isAssignableFrom(componentClass)) {
                component =
                    (JSONComponent)
                        _NSUtilities.instantiateObject(
                            componentClass,
                            new Class[] {WOContext.class},
                            new Object[] {context},
                            true,
                            false);
              } else {
                throw new SecurityException(
                    "There is no JSON component named '" + componentName + "'.");
              }
              jsonBridge =
                  createBridgeForComponent(
                      component, componentName, componentInstance, componentBridges);
            }

            componentCallback = new JSONComponentCallback(context);
            jsonBridge.registerCallback(componentCallback, WOContext.class);
          } else {
            jsonBridge = _sharedBridge;
          }

          try {
            output = jsonBridge.call(new Object[] {request, response, context}, input);
          } finally {
            if (componentCallback != null) {
              jsonBridge.unregisterCallback(componentCallback, WOContext.class);
            }
          }

          if (context._session() != null) {
            WOSession contextSession = context._session();
            // If this is a new session, then we have to force it to be a cookie session
            if (wosid == null) {
              boolean storesIDsInCookies = contextSession.storesIDsInCookies();
              try {
                contextSession.setStoresIDsInCookies(true);
                contextSession._appendCookieToResponse(response);
              } finally {
                contextSession.setStoresIDsInCookies(storesIDsInCookies);
              }
            } else {
              contextSession._appendCookieToResponse(response);
            }
          }
          if (output != null) {
            response.appendContentString(output.toString());
          }

          if (response != null) {
            response._finalizeInContext(context);
            response.disableClientCaching();
          }
        } finally {
          try {
            if (session != null) {
              session.sleep();
            }
          } finally {
            if (context._session() != null) {
              WOApplication.application().saveSessionForContext(context);
            }
          }
        }
      } catch (NoSuchElementException e) {
        e.printStackTrace();
        output =
            new JSONRPCResult(
                JSONRPCResult.CODE_ERR_NOMETHOD, null, JSONRPCResult.MSG_ERR_NOMETHOD);
      } catch (JSONException e) {
        e.printStackTrace();
        output = new JSONRPCResult(JSONRPCResult.CODE_ERR_PARSE, null, JSONRPCResult.MSG_ERR_PARSE);
      } catch (Throwable t) {
        t.printStackTrace();
        output = new JSONRPCResult(JSONRPCResult.CODE_ERR_PARSE, null, t.getMessage());
      }

      return response;
    } finally {
      application.sleep();
    }
  }
  // @Override
  public WOResponse handleRequest(WORequest request) {
    if (!ERSelenium.testsEnabled()) {
      return new WOResponse();
    }

    NSArray pathElements = request.requestHandlerPathArray();

    StringBuilder builder = new StringBuilder();
    Iterator iter = pathElements.iterator();
    while (iter.hasNext()) {
      builder.append(iter.next());
      if (iter.hasNext()) builder.append("/");
    }

    String filePath = builder.toString();
    log.debug("Processing file '" + filePath + "'");

    /*
     * Syncrhonization mistakes are possible here, but not fatal at all.
     * At the worst case the file will be read 2-or-more times instead of 1 (if process 1
     * checks that the file is not cached and process 2 does the same check before
     * process 1 has updated the cache).
     */

    CachedFile cachedFile;
    synchronized (_cache) {
      cachedFile = (CachedFile) _cache.objectForKey(filePath);
    }

    if (cachedFile == null) {
      cachedFile = new CachedFile();

      URL fileUrl =
          WOApplication.application()
              .resourceManager()
              .pathURLForResourceNamed(filePath, "ERSelenium", null);
      if (fileUrl == null) {
        throw new RuntimeException("Can't find specified resource ('" + filePath + "')");
      }
      cachedFile.mimeType =
          WOApplication.application().resourceManager().contentTypeForResourceNamed(filePath);
      if (cachedFile.mimeType == null) {
        throw new RuntimeException("Can't determine resource mime type ('" + filePath + "')");
      }

      try {
        cachedFile.data = new NSData(ERXFileUtilities.bytesFromInputStream(fileUrl.openStream()));
      } catch (Exception e) {
        throw new RuntimeException("Error reading file '" + fileUrl.getPath() + "'", e);
      }

      synchronized (_cache) {
        _cache.setObjectForKey(cachedFile, filePath);
      }
    }

    WOResponse response = new WOResponse();
    response.setHeader(cachedFile.mimeType, "content-type");
    response.setContent(cachedFile.data);

    NSNotificationCenter.defaultCenter()
        .postNotification(WORequestHandler.DidHandleRequestNotification, response);
    return response;
  }
  @Override
  public WOResponse handleRequest(WORequest request) {
    int bufferSize = 16384;

    WOApplication application = WOApplication.application();
    application.awake();
    try {
      WOContext context = application.createContextForRequest(request);
      WOResponse response = application.createResponseInContext(context);

      String sessionIdKey = application.sessionIdKey();
      String sessionId = (String) request.formValueForKey(sessionIdKey);
      if (sessionId == null) {
        sessionId = request.cookieValueForKey(sessionIdKey);
      }
      context._setRequestSessionID(sessionId);
      if (context._requestSessionID() != null) {
        application.restoreSessionWithID(sessionId, context);
      }

      try {
        final WODynamicURL url = request._uriDecomposed();
        final String requestPath = url.requestHandlerPath();
        final Matcher idMatcher = Pattern.compile("^id/(\\d+)/").matcher(requestPath);

        final Integer requestedAttachmentID;
        String requestedWebPath;

        final boolean requestedPathContainsAnAttachmentID = idMatcher.find();
        if (requestedPathContainsAnAttachmentID) {
          requestedAttachmentID = Integer.valueOf(idMatcher.group(1));
          requestedWebPath = idMatcher.replaceFirst("/");
        } else {
          // MS: This is kind of goofy because we lookup by path, your web path needs to
          // have a leading slash on it.
          requestedWebPath = "/" + requestPath;
          requestedAttachmentID = null;
        }

        try {
          InputStream attachmentInputStream;
          String mimeType;
          String fileName;
          long length;
          String queryString = url.queryString();
          boolean proxyAsAttachment =
              (queryString != null && queryString.contains("attachment=true"));

          EOEditingContext editingContext = ERXEC.newEditingContext();
          editingContext.lock();

          try {
            ERAttachment attachment =
                fetchAttachmentFor(editingContext, requestedAttachmentID, requestedWebPath);

            if (_delegate != null && !_delegate.attachmentVisible(attachment, request, context)) {
              throw new SecurityException("You are not allowed to view the requested attachment.");
            }
            mimeType = attachment.mimeType();
            length = attachment.size().longValue();
            fileName = attachment.originalFileName();
            ERAttachmentProcessor<ERAttachment> attachmentProcessor =
                ERAttachmentProcessor.processorForType(attachment);
            if (!proxyAsAttachment) {
              proxyAsAttachment = attachmentProcessor.proxyAsAttachment(attachment);
            }
            InputStream rawAttachmentInputStream =
                attachmentProcessor.attachmentInputStream(attachment);
            attachmentInputStream = new BufferedInputStream(rawAttachmentInputStream, bufferSize);
          } finally {
            editingContext.unlock();
          }

          response.setHeader(mimeType, "Content-Type");
          response.setHeader(String.valueOf(length), "Content-Length");

          if (proxyAsAttachment) {
            response.setHeader("attachment; filename=\"" + fileName + "\"", "Content-Disposition");
          }

          response.setStatus(200);
          response.setContentStream(attachmentInputStream, bufferSize, length);
        } catch (SecurityException e) {
          NSLog.out.appendln(e);
          response.setContent(e.getMessage());
          response.setStatus(403);
        } catch (NoSuchElementException e) {
          NSLog.out.appendln(e);
          response.setContent(e.getMessage());
          response.setStatus(404);
        } catch (FileNotFoundException e) {
          NSLog.out.appendln(e);
          response.setContent(e.getMessage());
          response.setStatus(404);
        } catch (IOException e) {
          NSLog.out.appendln(e);
          response.setContent(e.getMessage());
          response.setStatus(500);
        }

        return response;
      } finally {
        if (context._requestSessionID() != null) {
          WOApplication.application().saveSessionForContext(context);
        }
      }
    } finally {
      application.sleep();
    }
  }
  public WOResponse handleRequest(WORequest request) {
    WOResponse response = null;
    FileInputStream is = null;
    long length = 0;
    String contentType = null;
    String uri = request.uri();
    if (uri.charAt(0) == '/') {
      WOResourceManager rm = application.resourceManager();
      String documentRoot = documentRoot();
      File file = null;
      StringBuffer sb = new StringBuffer(documentRoot.length() + uri.length());
      String wodataKey = request.stringFormValueForKey("wodata");
      if (uri.startsWith("/cgi-bin") && wodataKey != null) {
        uri = wodataKey;
        if (uri.startsWith("file:")) {
          // remove file:/
          uri = uri.substring(5);
        } else {

        }
      } else {
        int index = uri.indexOf("/wodata=");

        if (index >= 0) {
          uri = uri.substring(index + "/wodata=".length());
        } else {
          sb.append(documentRoot);
        }
      }

      if (_useRequestHandlerPath) {
        try {
          WODynamicURL dynamicURL = new WODynamicURL(uri);
          String requestHandlerPath = dynamicURL.requestHandlerPath();
          if (requestHandlerPath == null || requestHandlerPath.length() == 0) {
            sb.append(uri);
          } else {
            sb.append("/");
            sb.append(requestHandlerPath);
          }
        } catch (Exception e) {
          throw new RuntimeException("Failed to parse URL '" + uri + "'.", e);
        }
      } else {
        sb.append(uri);
      }

      String path = sb.toString();
      try {
        path = path.replaceAll("\\?.*", "");
        if (request.userInfo() != null && !request.userInfo().containsKey("HttpServletRequest")) {
          /* PATH_INFO is already decoded by the servlet container */
          path = path.replace('+', ' ');
          path = URLDecoder.decode(path, CharEncoding.UTF_8);
        }
        file = new File(path);
        length = file.length();
        is = new FileInputStream(file);

        contentType = rm.contentTypeForResourceNamed(path);
        log.debug("Reading file '" + file + "' for uri: " + uri);
      } catch (IOException ex) {
        if (!uri.toLowerCase().endsWith("/favicon.ico")) {
          log.info("Unable to get contents of file '" + file + "' for uri: " + uri);
        }
      }
    } else {
      log.error("Can't fetch relative path: " + uri);
    }
    response = _generateResponseForInputStream(is, length, contentType);
    NSNotificationCenter.defaultCenter()
        .postNotification(WORequestHandler.DidHandleRequestNotification, response);
    response._finalizeInContext(null);
    return response;
  }