Ejemplo n.º 1
0
  public static String postExecute(
      IRuntimeContext runtime,
      boolean debugMessages,
      boolean doWrapper,
      IOutputHandler outputHandler,
      Map<String, IParameterProvider> parameterProviders,
      HttpServletRequest request,
      HttpServletResponse response,
      List<?> messages,
      boolean deleteGeneratedFiles)
      throws Exception {
    StringBuffer buffer = new StringBuffer();

    boolean hasResponse = outputHandler.isResponseExpected();
    IContentItem responseContentItem =
        outputHandler.getOutputContentItem(
            IOutputHandler.RESPONSE, IOutputHandler.CONTENT, null, null);

    boolean success =
        (runtime != null && runtime.getStatus() == IRuntimeContext.RUNTIME_STATUS_SUCCESS);
    boolean printSuccess = (runtime != null) && success && (!hasResponse || debugMessages);
    boolean printError = (runtime != null) && !success && !response.isCommitted();

    if (printSuccess || printError) {
      final String htmlMimeType = "text/html"; // $NON-NLS-1$
      responseContentItem.setMimeType(htmlMimeType);
      response.setContentType(htmlMimeType);
      IMessageFormatter formatter =
          PentahoSystem.get(IMessageFormatter.class, PentahoSessionHolder.getSession());

      if (printSuccess) {
        formatter.formatSuccessMessage(htmlMimeType, runtime, buffer, debugMessages, doWrapper);
      } else {
        response.resetBuffer();
        formatter.formatFailureMessage(htmlMimeType, runtime, buffer, messages);
      }
    }
    // clear files which was generated during action execution
    // http://jira.pentaho.com/browse/BISERVER-12639
    IUnifiedRepository unifiedRepository = PentahoSystem.get(IUnifiedRepository.class, null);
    if (unifiedRepository != null) {
      for (IContentItem contentItem : runtime.getOutputContentItems()) {
        if (contentItem != null) {
          try {
            contentItem.closeOutputStream();
            if (deleteGeneratedFiles) {
              deleteContentItem(contentItem, unifiedRepository);
            }
          } catch (Exception e) {
            logger.warn(
                Messages.getInstance()
                    .getString("XactionUtil.CANNOT_REMOVE_OUTPUT_FILE", contentItem.getPath()),
                e);
          }
        }
      }
    }
    return buffer.toString();
  }
Ejemplo n.º 2
0
 /**
  * Sets a status and sends an info message.
  *
  * @param code status code
  * @param message info message
  * @throws IOException I/O exception
  */
 public void status(final int code, final String message) throws IOException {
   log(true, code, message);
   if (session != null) session.close();
   res.resetBuffer();
   res.setStatus(code);
   if (code == SC_UNAUTHORIZED) res.setHeader(WWW_AUTHENTICATE, BASIC);
   if (message != null) res.getOutputStream().write(token(message));
 }
  private void overwriteResponseBody(
      HttpServletResponse response, final String formattedOutput, String contentType)
      throws IOException {
    response.resetBuffer();
    response.setContentLength(formattedOutput.length());
    response.setHeader(CommonHttpHeader.CONTENT_TYPE.toString(), contentType);

    // TODO:Enhancement - Update formatter logic for streaming
    // TODO:Enhancement - Update getBytes(...) to use requested content encoding
    response.getOutputStream().write(formattedOutput.getBytes(CharacterSets.UTF_8));
  }
Ejemplo n.º 4
0
  private static void handleException(
      HttpServletResponse response, ObjectMapper objectMapper, Exception exception)
      throws IOException {
    if (!response.isCommitted()) {
      final String errorMessage =
          exception.getMessage() == null ? "null exception" : exception.getMessage();

      response.resetBuffer();
      response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      objectMapper.writeValue(response.getOutputStream(), ImmutableMap.of("error", errorMessage));
    }
    response.flushBuffer();
  }
Ejemplo n.º 5
0
  /**
   * Sets a status and sends an info message.
   *
   * @param code status code
   * @param message info message
   * @param error treat as error (use web server standard output)
   * @throws IOException I/O exception
   */
  public void status(final int code, final String message, final boolean error) throws IOException {
    try {
      log(message, code);
      res.resetBuffer();
      if (code == SC_UNAUTHORIZED) res.setHeader(WWW_AUTHENTICATE, BASIC);

      if (error && code >= SC_BAD_REQUEST) {
        res.sendError(code, message);
      } else {
        res.setStatus(code);
        if (message != null) res.getOutputStream().write(token(message));
      }
    } catch (final IllegalStateException ex) {
      log(Util.message(ex), SC_INTERNAL_SERVER_ERROR);
    }
  }
Ejemplo n.º 6
0
  /**
   * Handles a CORS request that violates specification.
   *
   * @param request The {@link HttpServletRequest} object.
   * @param response The {@link HttpServletResponse} object.
   * @param filterChain The {@link FilterChain} object.
   * @throws IOException
   * @throws ServletException
   */
  public void handleInvalidCORS(
      final HttpServletRequest request,
      final HttpServletResponse response,
      final FilterChain filterChain) {
    String origin = request.getHeader(CORSFilter.REQUEST_HEADER_ORIGIN);
    String method = request.getMethod();
    String accessControlRequestHeaders =
        request.getHeader(REQUEST_HEADER_ACCESS_CONTROL_REQUEST_HEADERS);

    String message = "Invalid CORS request; Origin=" + origin + ";Method=" + method;
    if (accessControlRequestHeaders != null) {
      message = message + ";Access-Control-Request-Headers=" + accessControlRequestHeaders;
    }
    response.setContentType("text/plain");
    response.setStatus(HttpServletResponse.SC_FORBIDDEN);
    response.resetBuffer();

    log(message);
  }
Ejemplo n.º 7
0
  public static String postExecute(
      IRuntimeContext runtime,
      boolean debugMessages,
      boolean doWrapper,
      IOutputHandler outputHandler,
      Map<String, IParameterProvider> parameterProviders,
      HttpServletRequest request,
      HttpServletResponse response,
      List<?> messages)
      throws Exception {
    StringBuffer buffer = new StringBuffer();

    boolean hasResponse = outputHandler.isResponseExpected();
    IContentItem responseContentItem =
        outputHandler.getOutputContentItem(
            IOutputHandler.RESPONSE, IOutputHandler.CONTENT, null, null);

    boolean success =
        (runtime != null && runtime.getStatus() == IRuntimeContext.RUNTIME_STATUS_SUCCESS);
    boolean printSuccess = (runtime != null) && success && (!hasResponse || debugMessages);
    boolean printError = (runtime != null) && !success && !response.isCommitted();

    if (printSuccess || printError) {
      final String htmlMimeType = "text/html"; // $NON-NLS-1$
      responseContentItem.setMimeType(htmlMimeType);
      response.setContentType(htmlMimeType);
      IMessageFormatter formatter =
          PentahoSystem.get(IMessageFormatter.class, PentahoSessionHolder.getSession());

      if (printSuccess) {
        formatter.formatSuccessMessage(htmlMimeType, runtime, buffer, debugMessages, doWrapper);
      } else {
        response.resetBuffer();
        formatter.formatFailureMessage(htmlMimeType, runtime, buffer, messages);
      }
    }
    return buffer.toString();
  }
Ejemplo n.º 8
0
  public void handleRequest(
      Site site, HttpServletRequest req, HttpServletResponse res, String pageSlug)
      throws IOException, ServletException {

    if (site.getCanViewGroup().isMember(Authenticate.getUser())) {
      if (site.getPublished()) {
        try {
          String baseUrl = "/" + site.getBaseUrl();
          if (pageSlug.startsWith(baseUrl)) {
            pageSlug = pageSlug.substring(baseUrl.length());
          }
          if (pageSlug.endsWith("/") && !req.getRequestURI().equals(req.getContextPath() + "/")) {
            handleLeadingSlash(req, res, site);
          } else if (pageSlug.startsWith("/static/")) {
            handleStaticResource(req, res, site, pageSlug);
          } else if (pageSlug.startsWith("/rss")) {
            handleRSS(req, res, site, pageSlug);
          } else {
            renderCMSPage(req, res, site, pageSlug);
          }
        } catch (Exception e) {
          logger.error("Exception while rendering CMS page " + req.getRequestURI(), e);
          if (res.isCommitted()) {
            return;
          }
          res.reset();
          res.resetBuffer();
          errorPage(req, res, site, 500);
        }
      } else {
        res.sendError(404);
      }
    } else {
      res.sendError(404);
      return;
    }
  }
Ejemplo n.º 9
0
 @Override
 public void resetBuffer() {
   response.resetBuffer();
 }
 public synchronized void resetBuffer() {
   actualResponse.resetBuffer();
 }
Ejemplo n.º 11
0
 public void resetBuffer() {
   rawResponse.resetBuffer();
 }