Пример #1
0
  /**
   * Add custom binders assigned to the method
   *
   * <p>Custom binders assigned to the method will be added to the binding manager instance.
   *
   * @param manager the manager to add binders to
   * @param method the method to evaluate for additional binders
   */
  protected static void addMethodBinders(final BindingManager manager, final Method method) {
    final ControllerBinder[] binders = method.getAnnotationsByType(ControllerBinder.class);

    if (binders == null) {
      return;
    }

    for (final ControllerBinder binder : binders) {
      try {
        final Binder binderImpl = binder.value().newInstance();
        if (binderImpl instanceof ControllerBinderParametersAware) {
          ((ControllerBinderParametersAware) binderImpl).setParameters(binder.parameters());
        }
        manager.addBinder(binderImpl);
      } catch (InstantiationException | IllegalAccessException e) {
        manager.addBinder(new ErrorBinder(e));
      }
    }
  }
Пример #2
0
  protected RequestHandler processCall(
      final Match match, final HttpServletRequest request, final HttpServletResponse response)
      throws Exception {
    final Map<String, Object> data = new HashMap<String, Object>();

    data.put("request", request);
    data.put("response", response);
    data.put("session", request.getSession());
    data.put("principal", request.getUserPrincipal());

    // create new binding manager

    final BindingManager manager = BindingManager.create(data);

    // add controller binders

    manager.addBinder(new RequestParameterBinder(request));
    manager.addBinder(new PathVariableBinder(match));
    manager.addBinder(new FormDataBinder(request, this.controller));

    addMethodBinders(manager, this.m);

    final de.dentrassi.osgi.web.controller.binding.BindingManager.Call call =
        manager.bind(this.m, this.controller);
    final Object result = call.invoke();

    if (result instanceof ModelAndView) {
      return new ModelAndViewRequestHandler(
          (ModelAndView) result, this.controller.getClass(), this.m);
    } else if (result instanceof String) {
      return new ModelAndViewRequestHandler(
          new ModelAndView((String) result), this.controller.getClass(), this.m);
    } else if (result == null) {
      return new NoOpRequestHandler();
    } else {
      throw new IllegalStateException(
          String.format("Response type %s is unsupported", result.getClass()));
    }
  }