@Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    if (iwma == null) {
      iwma = IWMainApplication.getIWMainApplication(servletConfig.getServletContext());
    }

    if (request.getParameter(PARAMETER_NAME) != null || request.getParameter("image_id") != null) {
      new MediaOutputWriter().doPost(request, response, iwma);
    } else if (request.getParameter(PRM_SESSION_MEMORY_BUFFER) != null) {
      new MemoryFileBufferWriter().doPost(request, response);
    } else if (request.getParameter(MediaWritable.PRM_WRITABLE_CLASS) != null) {
      IWContext iwc = null;
      try {
        FacesContext facesContext =
            facesContextFactory.getFacesContext(
                servletConfig.getServletContext(), request, response, lifecycle);
        iwc = IWContext.getIWContext(facesContext);
      } catch (Exception e) {
        e.printStackTrace();
      }
      try {
        if (iwc == null) {
          iwc = new IWContext(request, response, servletConfig.getServletContext());
        }

        String mediaWriter = request.getParameter(MediaWritable.PRM_WRITABLE_CLASS);
        MediaWritable mw =
            (MediaWritable)
                RefactorClassRegistry.forName(IWMainApplication.decryptClassName(mediaWriter))
                    .newInstance();
        mw.init(request, iwc);
        response.setContentType(mw.getMimeType());
        ServletOutputStream out = response.getOutputStream();
        mw.writeTo(out);
        out.flush();
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
  }
Esempio n. 2
0
  /**
   * <span class="changed_modified_2_2">Process</span> an incoming request, and create the
   * corresponding response according to the following specification. <div
   * class="changed_modified_2_0">
   *
   * <p>If the <code>request</code> and <code>response</code> arguments to this method are not
   * instances of <code>HttpServletRequest</code> and <code>HttpServletResponse</code>,
   * respectively, the results of invoking this method are undefined.
   *
   * <p>This method must respond to requests that start with the following strings by invoking the
   * <code>sendError</code> method on the response argument (cast to <code>HttpServletResponse
   * </code>), passing the code <code>HttpServletResponse.SC_NOT_FOUND</code> as the argument.
   *
   * <ul>
   *   <pre><code>
   * /WEB-INF/
   * /WEB-INF
   * /META-INF/
   * /META-INF
   * </code></pre>
   * </ul>
   *
   * <p>If none of the cases described above in the specification for this method apply to the
   * servicing of this request, the following action must be taken to service the request.
   *
   * <p>Acquire a {@link FacesContext} instance for this request.
   *
   * <p>Acquire the <code>ResourceHandler</code> for this request by calling {@link
   * javax.faces.application.Application#getResourceHandler}. Call {@link
   * javax.faces.application.ResourceHandler#isResourceRequest}. If this returns <code>true</code>
   * call {@link javax.faces.application.ResourceHandler#handleResourceRequest}. If this returns
   * <code>false</code>, <span class="changed_added_2_2">call {@link
   * javax.faces.lifecycle.Lifecycle#attachWindow} followed by </span> {@link
   * javax.faces.lifecycle.Lifecycle#execute} followed by {@link
   * javax.faces.lifecycle.Lifecycle#render}. If a {@link javax.faces.FacesException} is thrown in
   * either case, extract the cause from the <code>FacesException</code>. If the cause is <code>null
   * </code> extract the message from the <code>FacesException</code>, put it inside of a new <code>
   * ServletException</code> instance, and pass the <code>FacesException</code> instance as the root
   * cause, then rethrow the <code>ServletException</code> instance. If the cause is an instance of
   * <code>ServletException</code>, rethrow the cause. If the cause is an instance of <code>
   * IOException</code>, rethrow the cause. Otherwise, create a new <code>ServletException</code>
   * instance, passing the message from the cause, as the first argument, and the cause itself as
   * the second argument.
   *
   * <p class="changed_modified_2_0_rev_a">The implementation must make it so {@link
   * javax.faces.context.FacesContext#release} is called within a finally block as late as possible
   * in the processing for the JSF related portion of this request. </div>
   *
   * @param req The servlet request we are processing
   * @param resp The servlet response we are creating
   * @throws IOException if an input/output error occurs during processing
   * @throws ServletException if a servlet error occurs during processing
   */
  public void service(ServletRequest req, ServletResponse resp)
      throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) resp;

    requestStart(request.getRequestURI()); // V3 Probe hook

    if (!isHttpMethodValid(request)) {
      response.sendError(HttpServletResponse.SC_BAD_REQUEST);
      return;
    }
    if (Thread.currentThread().isInterrupted()) {
      if (LOGGER.isLoggable(Level.FINER)) {
        LOGGER.log(
            Level.FINE,
            "Thread {0} given to FacesServlet.service() in interrupted state",
            Thread.currentThread().getName());
      }
    }

    // If prefix mapped, then ensure requests for /WEB-INF are
    // not processed.
    String pathInfo = request.getPathInfo();
    if (pathInfo != null) {
      pathInfo = pathInfo.toUpperCase();
      if (pathInfo.startsWith("/WEB-INF/")
          || pathInfo.equals("/WEB-INF")
          || pathInfo.startsWith("/META-INF/")
          || pathInfo.equals("/META-INF")) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
      }
    }

    if (!initFacesContextReleased) {
      FacesContext initFacesContext = FacesContext.getCurrentInstance();
      if (null != initFacesContext) {
        initFacesContext.release();
      }
      initFacesContextReleased = true;
    }

    // Acquire the FacesContext instance for this request
    FacesContext context =
        facesContextFactory.getFacesContext(
            servletConfig.getServletContext(), request, response, lifecycle);

    // Execute the request processing lifecycle for this request
    try {
      ResourceHandler handler = context.getApplication().getResourceHandler();
      if (handler.isResourceRequest(context)) {
        handler.handleResourceRequest(context);
      } else {
        lifecycle.attachWindow(context);
        lifecycle.execute(context);
        lifecycle.render(context);
      }
    } catch (FacesException e) {
      Throwable t = e.getCause();
      if (t == null) {
        throw new ServletException(e.getMessage(), e);
      } else {
        if (t instanceof ServletException) {
          throw ((ServletException) t);
        } else if (t instanceof IOException) {
          throw ((IOException) t);
        } else {
          throw new ServletException(t.getMessage(), t);
        }
      }
    } finally {
      // Release the FacesContext instance for this request
      context.release();
    }

    requestEnd(); // V3 Probe hook
  }