@RequestMapping(value = "/project/{projectName}/appender/copy.html", method = RequestMethod.POST)
  public String copy(
      @Valid @ModelAttribute("spec") final CopySpec spec, final BindingResult result) {
    if (result.hasErrors()) {
      return VIEW_NAME;
    }

    final AppenderConfig source = configManager.find(AppenderConfig.class, spec.getSourceId());
    final ProjectConfig project = source.getProject();
    final AppenderConfig newAppender = ControllerHelper.cloneAppender(source);
    newAppender.setName(spec.getName());
    newAppender.setProject(project);
    project.addAppender(newAppender);
    configManager.save(project);
    return String.format("redirect:/secure/project/%s/edit.html#appender", project.getName());
  }
Пример #2
0
  @Override
  protected void service(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    String requestMethod = req.getMethod().toLowerCase();
    String requestPath = req.getPathInfo();
    Handler handler = ControllerHelper.getHandler(requestMethod, requestPath);
    if (handler != null) {

      Class<?> controllerClass = handler.getControllerClass();
      Object controllerBean = BeanHelper.getBean(controllerClass);
      Map<String, Object> paramMap = new HashMap<String, Object>();
      Enumeration<String> paramNames = req.getParameterNames();
      while (paramNames.hasMoreElements()) {
        String paramName = paramNames.nextElement();
        String paramValue = req.getParameter(paramName);
        paramMap.put(paramName, paramValue);
      }
      String body = CodecUtil.decodeURL(StreamUtil.getString(req.getInputStream()));
      if (StringUtil.isNotEmpty(body)) {
        String[] params = StringUtil.splitString(body, "&");
        if (ArrayUtil.isNotEmpty(params)) {
          for (String param : params) {
            String[] array = StringUtil.splitString(param, "=");
            if (ArrayUtil.isNotEmpty(array) && array.length == 2) {
              String paramName = array[0];
              String paramValue = array[1];
              paramMap.put(paramName, paramValue);
            }
          }
        }
      }

      Param param = new Param(paramMap);
      Method actionMethod = handler.getActionMethod();
      Object result = ReflectionUtil.invokeMethod(controllerBean, actionMethod, param);
      if (result instanceof View) {
        View view = (View) result;
        String path = view.getPath();
        if (StringUtil.isNotEmpty(path)) {
          if (path.startsWith("/")) {
            resp.sendRedirect(req.getContextPath() + path);
          } else {
            Map<String, Object> model = view.getModel();
            for (Map.Entry<String, Object> entry : model.entrySet()) {
              req.setAttribute(entry.getKey(), entry.getValue());
            }
            req.getRequestDispatcher(ConfigHelper.getAppJspPath() + path).forward(req, resp);
          }
        }

      } else if (result instanceof Data) {
        Data data = (Data) result;
        Object model = data.getModel();
        if (model != null) {
          resp.setContentType("application/json");
          resp.setCharacterEncoding("UTF-8");
          PrintWriter writer = resp.getWriter();
          String json = JsonUtil.toJSON(model);
          writer.write(json);
          writer.flush();
          writer.close();
        }
      }
    }
  }