コード例 #1
0
ファイル: CoyoteAdapter.java プロジェクト: xiaobao2110/tomcat
  @Override
  public void checkRecycled(org.apache.coyote.Request req, org.apache.coyote.Response res) {
    Request request = (Request) req.getNote(ADAPTER_NOTES);
    Response response = (Response) res.getNote(ADAPTER_NOTES);
    String messageKey = null;
    if (request != null && request.getHost() != null) {
      messageKey = "coyoteAdapter.checkRecycled.request";
    } else if (response != null && response.getContentWritten() != 0) {
      messageKey = "coyoteAdapter.checkRecycled.response";
    }
    if (messageKey != null) {
      // Log this request, as it has probably skipped the access log.
      // The log() method will take care of recycling.
      log(req, res, 0L);

      if (connector.getState().isAvailable()) {
        if (log.isInfoEnabled()) {
          log.info(sm.getString(messageKey), new RecycleRequiredException());
        }
      } else {
        // There may be some aborted requests.
        // When connector shuts down, the request and response will not
        // be reused, so there is no issue to warn about here.
        if (log.isDebugEnabled()) {
          log.debug(sm.getString(messageKey), new RecycleRequiredException());
        }
      }
    }
  }
コード例 #2
0
  @Override
  public void flushBuffer() throws IOException {

    if (isFinished())
      //            throw new IllegalStateException
      //                (/*sm.getString("responseFacade.finished")*/);
      return;

    if (SecurityUtil.isPackageProtectionEnabled()) {
      try {
        AccessController.doPrivileged(
            new PrivilegedExceptionAction<Void>() {

              @Override
              public Void run() throws IOException {
                response.setAppCommitted(true);

                response.flushBuffer();
                return null;
              }
            });
      } catch (PrivilegedActionException e) {
        Exception ex = e.getException();
        if (ex instanceof IOException) {
          throw (IOException) ex;
        }
      }
    } else {
      response.setAppCommitted(true);

      response.flushBuffer();
    }
  }
コード例 #3
0
  @Override
  public void write(Response response, int statusCode, Object body) throws IOException {

    response.setStatus(statusCode);
    response.setContentType(APPLICATION_JSON);
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.writeValue(response.getWriter(), body);
  }
コード例 #4
0
  @Override
  public void sendError(int sc) throws IOException {

    if (isCommitted())
      throw new IllegalStateException(sm.getString("coyoteResponse.sendError.ise"));

    response.setAppCommitted(true);

    response.sendError(sc);
  }
コード例 #5
0
  @Override
  public void sendRedirect(String location) throws IOException {

    if (isCommitted())
      throw new IllegalStateException(sm.getString("coyoteResponse.sendRedirect.ise"));

    response.setAppCommitted(true);

    response.sendRedirect(location);
  }
コード例 #6
0
  @Override
  public PrintWriter getWriter() throws IOException {

    //        if (isFinished())
    //            throw new IllegalStateException
    //                (/*sm.getString("responseFacade.finished")*/);

    PrintWriter writer = response.getWriter();
    if (isFinished()) response.setSuspended(true);
    return (writer);
  }
コード例 #7
0
  @Override
  public ServletOutputStream getOutputStream() throws IOException {

    //        if (isFinished())
    //            throw new IllegalStateException
    //                (/*sm.getString("responseFacade.finished")*/);

    ServletOutputStream sos = response.getOutputStream();
    if (isFinished()) response.setSuspended(true);
    return (sos);
  }
コード例 #8
0
ファイル: CoyoteAdapter.java プロジェクト: xiaobao2110/tomcat
  @Override
  public void log(org.apache.coyote.Request req, org.apache.coyote.Response res, long time) {

    Request request = (Request) req.getNote(ADAPTER_NOTES);
    Response response = (Response) res.getNote(ADAPTER_NOTES);

    if (request == null) {
      // Create objects
      request = connector.createRequest();
      request.setCoyoteRequest(req);
      response = connector.createResponse();
      response.setCoyoteResponse(res);

      // Link objects
      request.setResponse(response);
      response.setRequest(request);

      // Set as notes
      req.setNote(ADAPTER_NOTES, request);
      res.setNote(ADAPTER_NOTES, response);

      // Set query string encoding
      req.getParameters().setQueryStringEncoding(connector.getURIEncoding());
    }

    try {
      // Log at the lowest level available. logAccess() will be
      // automatically called on parent containers.
      boolean logged = false;
      if (request.mappingData != null) {
        if (request.mappingData.context != null) {
          logged = true;
          ((Context) request.mappingData.context).logAccess(request, response, time, true);
        } else if (request.mappingData.host != null) {
          logged = true;
          ((Host) request.mappingData.host).logAccess(request, response, time, true);
        }
      }
      if (!logged) {
        connector.getService().getContainer().logAccess(request, response, time, true);
      }
    } catch (Throwable t) {
      ExceptionUtils.handleThrowable(t);
      log.warn(sm.getString("coyoteAdapter.accesslogFail"), t);
    } finally {
      request.recycle();
      response.recycle();
    }
  }
コード例 #9
0
  private void bz51881(CometEvent event) throws IOException {
    String[] tomcatVersion = config.getServletContext().getServerInfo().substring(14).split("\\.");
    String minorVersion = tomcatVersion[2];
    if (minorVersion.indexOf("-") != -1) {
      minorVersion = minorVersion.substring(0, minorVersion.indexOf("-"));
      if (Integer.valueOf(minorVersion) == 22) {
        minorVersion = "23";
      }
    }

    if (Integer.valueOf(tomcatVersion[0]) == 7 && Integer.valueOf(minorVersion) < 23) {
      logger.info(
          "Patching Tomcat 7.0.22 and lower bz51881. Expect NPE inside CoyoteAdapter, just ignore them. Upgrade to 7.0.23");
      try {
        RequestFacade request = RequestFacade.class.cast(event.getHttpServletRequest());
        Field coyoteRequest = RequestFacade.class.getDeclaredField("request");
        coyoteRequest.setAccessible(true);
        Request r = (Request) coyoteRequest.get(request);
        r.recycle();

        Field mappingData = Request.class.getDeclaredField("mappingData");
        mappingData.setAccessible(true);
        MappingData m = new MappingData();
        m.context = null;
        mappingData.set(r, m);
      } catch (Throwable t) {
        logger.trace("Was unable to recycle internal Tomcat object");
      } finally {
        try {
          event.close();
        } catch (IllegalStateException e) {
          logger.trace("", e);
        }
      }

      try {
        ResponseFacade response = ResponseFacade.class.cast(event.getHttpServletResponse());
        Field coyoteResponse = ResponseFacade.class.getDeclaredField("response");
        coyoteResponse.setAccessible(true);
        Response r = (Response) coyoteResponse.get(response);
        r.recycle();
      } catch (Throwable t) {
        logger.trace("Was unable to recycle internal Tomcat object");
      }
    } else {
      event.close();
    }
  }
コード例 #10
0
  @Override
  public void setLocale(Locale loc) {

    if (isCommitted()) return;

    response.setLocale(loc);
  }
コード例 #11
0
  @Override
  public void reset() {

    if (isCommitted()) throw new IllegalStateException(sm.getString("coyoteResponse.reset.ise"));

    response.reset();
  }
コード例 #12
0
  @Override
  public void addIntHeader(String name, int value) {

    if (isCommitted()) return;

    response.addIntHeader(name, value);
  }
コード例 #13
0
  @Override
  public void setContentLength(int len) {

    if (isCommitted()) return;

    response.setContentLength(len);
  }
コード例 #14
0
  @Override
  public void addCookie(Cookie cookie) {

    if (isCommitted()) return;

    response.addCookie(cookie);
  }
コード例 #15
0
  @Override
  public void setHeader(String name, String value) {

    if (isCommitted()) return;

    response.setHeader(name, value);
  }
コード例 #16
0
  /**
   * Register requests for tracking, whenever needed.
   *
   * @param request The servlet request to be processed
   * @param response The servlet response to be created
   * @exception IOException if an input/output error occurs
   * @exception ServletException if a servlet error occurs
   */
  @Override
  public void invoke(Request request, Response response) throws IOException, ServletException {
    // Perform the request
    getNext().invoke(request, response);

    if (request.isComet() && !response.isClosed()) {
      // Start tracking this connection, since this is a
      // begin event, and Comet mode is on
      HttpSession session = request.getSession(true);

      // Track the connection for webapp reload
      cometRequests.add(request);

      // Track the connection for session expiration
      synchronized (session) {
        Request[] requests = (Request[]) session.getAttribute(cometRequestsAttribute);
        if (requests == null) {
          requests = new Request[1];
          requests[0] = request;
          session.setAttribute(cometRequestsAttribute, requests);
        } else {
          Request[] newRequests = new Request[requests.length + 1];
          for (int i = 0; i < requests.length; i++) {
            newRequests[i] = requests[i];
          }
          newRequests[requests.length] = request;
          session.setAttribute(cometRequestsAttribute, newRequests);
        }
      }
    }
  }
コード例 #17
0
  @Override
  public void setStatus(int sc, String sm) {

    if (isCommitted()) return;

    response.setStatus(sc, sm);
  }
コード例 #18
0
  public void finish() {

    if (response == null) {
      throw new IllegalStateException(sm.getString("responseFacade.nullResponse"));
    }

    response.setSuspended(true);
  }
コード例 #19
0
  public boolean isFinished() {

    if (response == null) {
      throw new IllegalStateException(sm.getString("responseFacade.nullResponse"));
    }

    return response.isSuspended();
  }
コード例 #20
0
  public long getContentWritten() {

    if (response == null) {
      throw new IllegalStateException(sm.getString("responseFacade.nullResponse"));
    }

    return response.getContentWritten();
  }
コード例 #21
0
  @Override
  public void setBufferSize(int size) {

    if (isCommitted())
      throw new IllegalStateException(sm.getString("coyoteResponse.setBufferSize.ise"));

    response.setBufferSize(size);
  }
コード例 #22
0
 public static Request setupRequest(Manager manager, String sessionId) {
   MockRequest request = new MockRequest(manager);
   request.setRequestedSessionId(sessionId);
   Response response = new Response();
   try {
     response.setConnector(new Connector("http"));
     response.setCoyoteResponse(new org.apache.coyote.Response());
     //            org.apache.coyote.Response coyoteResponse = new org.apache.coyote.Response();
     //            coyoteResponse.setOutputBuffer(new InternalOutputBuffer(coyoteResponse));
     //            response.setCoyoteResponse(coyoteResponse);
     //            response.setConnector(new Connector("http"));
   } catch (Exception e) {
     throw new IllegalStateException(e);
   }
   request.setResponse(response);
   return request;
 }
コード例 #23
0
  @Override
  public boolean containsHeader(String name) {

    if (response == null) {
      throw new IllegalStateException(sm.getString("responseFacade.nullResponse"));
    }

    return response.containsHeader(name);
  }
コード例 #24
0
  @Override
  public boolean isCommitted() {

    if (response == null) {
      throw new IllegalStateException(sm.getString("responseFacade.nullResponse"));
    }

    return (response.isAppCommitted());
  }
コード例 #25
0
  @Override
  public int getBufferSize() {

    if (response == null) {
      throw new IllegalStateException(sm.getString("responseFacade.nullResponse"));
    }

    return response.getBufferSize();
  }
コード例 #26
0
  @Override
  public String encodeRedirectUrl(String url) {

    if (response == null) {
      throw new IllegalStateException(sm.getString("responseFacade.nullResponse"));
    }

    return response.encodeRedirectURL(url);
  }
コード例 #27
0
  @Override
  public void setCharacterEncoding(String arg0) {

    if (response == null) {
      throw new IllegalStateException(sm.getString("responseFacade.nullResponse"));
    }

    response.setCharacterEncoding(arg0);
  }
コード例 #28
0
  @Override
  public String getContentType() {

    if (response == null) {
      throw new IllegalStateException(sm.getString("responseFacade.nullResponse"));
    }

    return response.getContentType();
  }
  @Test
  public final void testBackupSessionNotInvokedWhenNoSessionIdPresent()
      throws IOException, ServletException {
    when(_request.getRequestedSessionId()).thenReturn(null);
    when(_response.getHeader(eq("Set-Cookie"))).thenReturn(null);

    _sessionTrackerValve.invoke(_request, _response);

    verify(_service, never()).backupSession(anyString(), anyBoolean(), anyString());
  }
コード例 #30
0
  @Override
  public void addDateHeader(String name, long date) {

    if (isCommitted()) return;

    if (Globals.IS_SECURITY_ENABLED) {
      AccessController.doPrivileged(new DateHeaderPrivilegedAction(name, date, true));
    } else {
      response.addDateHeader(name, date);
    }
  }