@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()); } } } }
@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(); } }
@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); }
@Override public void sendError(int sc) throws IOException { if (isCommitted()) throw new IllegalStateException(sm.getString("coyoteResponse.sendError.ise")); response.setAppCommitted(true); response.sendError(sc); }
@Override public void sendRedirect(String location) throws IOException { if (isCommitted()) throw new IllegalStateException(sm.getString("coyoteResponse.sendRedirect.ise")); response.setAppCommitted(true); response.sendRedirect(location); }
@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); }
@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); }
@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(); } }
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(); } }
@Override public void setLocale(Locale loc) { if (isCommitted()) return; response.setLocale(loc); }
@Override public void reset() { if (isCommitted()) throw new IllegalStateException(sm.getString("coyoteResponse.reset.ise")); response.reset(); }
@Override public void addIntHeader(String name, int value) { if (isCommitted()) return; response.addIntHeader(name, value); }
@Override public void setContentLength(int len) { if (isCommitted()) return; response.setContentLength(len); }
@Override public void addCookie(Cookie cookie) { if (isCommitted()) return; response.addCookie(cookie); }
@Override public void setHeader(String name, String value) { if (isCommitted()) return; response.setHeader(name, value); }
/** * 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); } } } }
@Override public void setStatus(int sc, String sm) { if (isCommitted()) return; response.setStatus(sc, sm); }
public void finish() { if (response == null) { throw new IllegalStateException(sm.getString("responseFacade.nullResponse")); } response.setSuspended(true); }
public boolean isFinished() { if (response == null) { throw new IllegalStateException(sm.getString("responseFacade.nullResponse")); } return response.isSuspended(); }
public long getContentWritten() { if (response == null) { throw new IllegalStateException(sm.getString("responseFacade.nullResponse")); } return response.getContentWritten(); }
@Override public void setBufferSize(int size) { if (isCommitted()) throw new IllegalStateException(sm.getString("coyoteResponse.setBufferSize.ise")); response.setBufferSize(size); }
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; }
@Override public boolean containsHeader(String name) { if (response == null) { throw new IllegalStateException(sm.getString("responseFacade.nullResponse")); } return response.containsHeader(name); }
@Override public boolean isCommitted() { if (response == null) { throw new IllegalStateException(sm.getString("responseFacade.nullResponse")); } return (response.isAppCommitted()); }
@Override public int getBufferSize() { if (response == null) { throw new IllegalStateException(sm.getString("responseFacade.nullResponse")); } return response.getBufferSize(); }
@Override public String encodeRedirectUrl(String url) { if (response == null) { throw new IllegalStateException(sm.getString("responseFacade.nullResponse")); } return response.encodeRedirectURL(url); }
@Override public void setCharacterEncoding(String arg0) { if (response == null) { throw new IllegalStateException(sm.getString("responseFacade.nullResponse")); } response.setCharacterEncoding(arg0); }
@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()); }
@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); } }