📄 jkcoyotehandler.java
字号:
Response res=req.getResponse(); if( log.isDebugEnabled() ) log.debug("doRead " + chunk.getBytes() + " " + chunk.getOffset() + " " + chunk.getLength()); MsgContext ep=(MsgContext)res.getNote( epNote ); JkInputStream jkIS=(JkInputStream)ep.getNote( inputStreamNote ); // return jkIS.read( chunk.getBytes(), chunk.getOffset(), chunk.getLength()); return jkIS.doRead( chunk ); } // -------------------- Jk handler implementation -------------------- // Jk Handler mehod public int invoke( Msg msg, MsgContext ep ) throws IOException { if( logTime.isDebugEnabled() ) ep.setLong( MsgContext.TIMER_PRE_REQUEST, System.currentTimeMillis()); org.apache.coyote.Request req=(org.apache.coyote.Request)ep.getRequest(); org.apache.coyote.Response res=req.getResponse(); res.setHook( this ); if( log.isDebugEnabled() ) log.debug( "Invoke " + req + " " + res + " " + req.requestURI().toString()); res.setOutputBuffer( this ); req.setInputBuffer( this ); if( ep.getNote( headersMsgNote ) == null ) { Msg msg2=new MsgAjp(); ep.setNote( headersMsgNote, msg2 ); } res.setNote( epNote, ep ); ep.setStatus( JK_STATUS_HEAD ); try { adapter.service( req, res ); } catch( Exception ex ) { ex.printStackTrace(); } ep.setStatus( JK_STATUS_NEW ); req.recycle(); res.recycle(); return OK; } private void appendHead(org.apache.coyote.Response res) throws IOException { if( log.isDebugEnabled() ) log.debug("COMMIT sending headers " + res + " " + res.getMimeHeaders() ); C2BConverter c2b=(C2BConverter)res.getNote( utfC2bNote ); if( c2b==null ) { c2b=new C2BConverter( "UTF8" ); res.setNote( utfC2bNote, c2b ); } MsgContext ep=(MsgContext)res.getNote( epNote ); MsgAjp msg=(MsgAjp)ep.getNote( headersMsgNote ); msg.reset(); msg.appendByte(HandlerRequest.JK_AJP13_SEND_HEADERS); msg.appendInt( res.getStatus() ); // s->b conversion, message msg.appendBytes( null ); // XXX add headers MimeHeaders headers=res.getMimeHeaders(); String contentType = res.getContentType(); if( contentType != null ) { headers.setValue("Content-Type").setString(contentType); } String contentLanguage = res.getContentLanguage(); if( contentLanguage != null ) { headers.setValue("Content-Language").setString(contentLanguage); } int contentLength = res.getContentLength(); if( contentLength >= 0 ) { headers.setValue("Content-Length").setInt(contentLength); } int numHeaders = headers.size(); msg.appendInt(numHeaders); for( int i=0; i<numHeaders; i++ ) { MessageBytes hN=headers.getName(i); // no header to sc conversion - there's little benefit // on this direction c2b.convert ( hN ); msg.appendBytes( hN ); MessageBytes hV=headers.getValue(i); c2b.convert( hV ); msg.appendBytes( hV ); } ep.setType( JkHandler.HANDLE_SEND_PACKET ); ep.getSource().invoke( msg, ep ); } // -------------------- Coyote Action implementation -------------------- public void action(ActionCode actionCode, Object param) { try { if( actionCode==ActionCode.ACTION_COMMIT ) { if( log.isDebugEnabled() ) log.debug("COMMIT " ); org.apache.coyote.Response res=(org.apache.coyote.Response)param; if( res.isCommitted() ) { if( log.isInfoEnabled() ) log.info("Response already commited " ); } else { appendHead( res ); } } else if( actionCode==ActionCode.ACTION_RESET ) { if( log.isInfoEnabled() ) log.info("RESET " ); } else if( actionCode==ActionCode.ACTION_CLIENT_FLUSH ) { if( log.isDebugEnabled() ) log.debug("CLIENT_FLUSH " ); org.apache.coyote.Response res=(org.apache.coyote.Response)param; MsgContext ep=(MsgContext)res.getNote( epNote ); ep.setType( JkHandler.HANDLE_FLUSH ); ep.getSource().invoke( null, ep ); } else if( actionCode==ActionCode.ACTION_CLOSE ) { if( log.isDebugEnabled() ) log.debug("CLOSE " ); org.apache.coyote.Response res=(org.apache.coyote.Response)param; MsgContext ep=(MsgContext)res.getNote( epNote ); if( ep.getStatus()== JK_STATUS_CLOSED ) { // Double close - it may happen with forward if( log.isDebugEnabled() ) log.debug("Double CLOSE - forward ? " + res.getRequest().requestURI() ); return; } if( !res.isCommitted() ) this.action( ActionCode.ACTION_COMMIT, param ); MsgAjp msg=(MsgAjp)ep.getNote( headersMsgNote ); msg.reset(); msg.appendByte( HandlerRequest.JK_AJP13_END_RESPONSE ); msg.appendByte( 1 ); ep.setType( JkHandler.HANDLE_SEND_PACKET ); ep.getSource().invoke( msg, ep ); ep.setType( JkHandler.HANDLE_FLUSH ); ep.getSource().invoke( msg, ep ); ep.setStatus(JK_STATUS_CLOSED ); if( logTime.isDebugEnabled() ) logTime(res.getRequest(), res); } else if( actionCode==ActionCode.ACTION_REQ_SSL_ATTRIBUTE ) { org.apache.coyote.Request req=(org.apache.coyote.Request)param; // Extract SSL certificate information (if requested) MessageBytes certString = (MessageBytes)req.getNote(WorkerEnv.SSL_CERT_NOTE); if( certString != null ) { byte[] certData = certString.getByteChunk().getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(certData); // Fill the first element. X509Certificate jsseCerts[] = null; try { CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate(bais); jsseCerts = new X509Certificate[1]; jsseCerts[0] = cert; } catch(java.security.cert.CertificateException e) { log.error("Certificate convertion failed" , e ); return; } req.setAttribute(SSLSupport.CERTIFICATE_KEY, jsseCerts); } } else if( actionCode==ActionCode.ACTION_REQ_HOST_ATTRIBUTE ) { org.apache.coyote.Request req=(org.apache.coyote.Request)param; // If remoteHost not set by JK, get it's name from it's remoteAddr if( req.remoteHost().isNull()) req.remoteHost().setString(InetAddress.getByName(req.remoteAddr().toString()).getHostName()); // } else if( actionCode==ActionCode.ACTION_POST_REQUEST ) { } else if( actionCode==ActionCode.ACTION_ACK ) { if( log.isDebugEnabled() ) log.debug("ACK " ); // What should we do here ? Who calls it ? } } catch( Exception ex ) { log.error( "Error in action code ", ex ); } } private void logTime(Request req, Response res ) { // called after the request // org.apache.coyote.Request req=(org.apache.coyote.Request)param; // Response res=req.getResponse(); MsgContext ep=(MsgContext)res.getNote( epNote ); String uri=req.requestURI().toString(); if( uri.indexOf( ".gif" ) >0 ) return; ep.setLong( MsgContext.TIMER_POST_REQUEST, System.currentTimeMillis()); long t1= ep.getLong( MsgContext.TIMER_PRE_REQUEST ) - ep.getLong( MsgContext.TIMER_RECEIVED ); long t2= ep.getLong( MsgContext.TIMER_POST_REQUEST ) - ep.getLong( MsgContext.TIMER_PRE_REQUEST ); logTime.debug("Time pre=" + t1 + "/ service=" + t2 + " " + res.getContentLength() + " " + uri ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -