Example #1
0
  @Override
  public void getResource(final HttpServletRequest request, final HttpServletResponse response)
      throws ServletException, IOException {
    ServletLifecycle.beginRequest(request);
    try {
      ServletContexts.instance().setRequest(request);
      ConversationPropagation.instance().restoreConversationId(request.getParameterMap());
      Manager.instance().restoreConversation();
      ServletLifecycle.resumeConversation(request);

      final ReportResult reportResult =
          (ReportResult) Contexts.getConversationContext().get("user.reportResult");

      if (reportResult != null) {
        response.setContentType("text/csv");
        response.setCharacterEncoding("UTF-8");

        final PrintWriter out = response.getWriter();

        boolean first = true;

        for (final ColumnDefinition column : reportResult.getGroupByColumns()) {
          if (!first) {
            out.print(DELIM);
          }
          out.print(toCSVString(column.getHeader()));
          first = false;
        }
        for (final ColumnDefinition column : reportResult.getColumns()) {
          if (!first) {
            out.print(DELIM);
          }
          out.print(toCSVString(column.getHeader()));
          first = false;
        }
        out.println();

        for (final ReportResultGroup group : reportResult.getGroups()) {
          writeReportGroup(0, group, out);
        }
        for (final ReportRow row : reportResult.getRows()) {
          writeReportRow(row, reportResult.getColumns(), out);
        }
        out.close();
      }
    } finally {
      ServletLifecycle.endRequest(request);
    }
  }
  @Before
  public void setUp() throws Exception {
    ServletContext servletContext = initServletContext();
    ServletLifecycle.beginApplication(servletContext);
    new Initialization(servletContext).create().init();

    servletContext.setAttribute(Gravity.class.getName(), mockGravity);

    MockHttpSession session =
        new MockHttpSession(servletContext) {
          @Override
          public String getId() {
            return "TEST$SESSION";
          }
        };
    MockHttpServletRequest request = new MockHttpServletRequest(session);
    MockHttpServletResponse response = new MockHttpServletResponse();

    Configuration cfg = new ConfigurationImpl();
    cfg.setGraniteConfig("/WEB-INF/granite/granite-config-seam.xml");
    servletContext.setAttribute(ServletGraniteConfig.GRANITE_CONFIG_CONFIGURATION_KEY, cfg);
    GraniteConfig graniteConfig = ServletGraniteConfig.loadConfig(servletContext);
    ServicesConfig servicesConfig = ServletServicesConfig.loadConfig(servletContext);
    HttpGraniteContext.createThreadIntance(
        graniteConfig, servicesConfig, servletContext, request, response);

    interceptor.before(requestMessage);

    SeamServiceFactory seamFactory = new SeamServiceFactory();
    seamFactory.configure(new XMap("properties"));
    @SuppressWarnings("unchecked")
    Destination destination =
        new Destination("seam", Collections.EMPTY_LIST, XMap.EMPTY_XMAP, null, null, null);
    invoker = new SeamServiceInvoker(destination, seamFactory);
  }