1 16 package org.mortbay.http.ajp; 17 18 import java.io.IOException ; 19 import java.net.InetAddress ; 20 import java.net.Socket ; 21 22 import org.apache.commons.logging.Log; 23 import org.mortbay.log.LogFactory; 24 import org.mortbay.http.HttpConnection; 25 import org.mortbay.http.HttpHandler; 26 import org.mortbay.http.HttpListener; 27 import org.mortbay.http.HttpMessage; 28 import org.mortbay.http.HttpRequest; 29 import org.mortbay.http.HttpServer; 30 import org.mortbay.util.InetAddrPort; 31 import org.mortbay.util.ThreadedServer; 32 33 34 45 public class AJP13Listener extends ThreadedServer implements HttpListener 46 { 47 private static Log log=LogFactory.getLog(AJP13Listener.class); 48 49 50 private HttpServer _server; 51 private boolean _lastOut=false; 52 private boolean _lastLow=false; 53 private String _integralScheme=HttpMessage.__SSL_SCHEME; 54 private String _confidentialScheme=HttpMessage.__SSL_SCHEME; 55 private int _integralPort=0; 56 private int _confidentialPort=0; 57 private boolean _identifyListener=false; 58 private int _bufferSize=8192; 59 private int _bufferReserve=512; 60 private String [] _remoteServers; 61 private HttpHandler _handler; 62 63 64 public AJP13Listener() 65 { 66 } 67 68 69 public AJP13Listener(InetAddrPort address) 70 { 71 super(address); 72 } 73 74 75 public void setHttpServer(HttpServer server) 76 { 77 _server=server; 78 } 79 80 81 public HttpServer getHttpServer() 82 { 83 return _server; 84 } 85 86 87 public int getBufferSize() 88 { 89 return _bufferSize; 90 } 91 92 93 public void setBufferSize(int size) 94 { 95 _bufferSize=size; 96 if (_bufferSize>8192) 97 log.warn("AJP Data buffer > 8192: "+size); 98 } 99 100 101 public int getBufferReserve() 102 { 103 return _bufferReserve; 104 } 105 106 107 public void setBufferReserve(int size) 108 { 109 _bufferReserve=size; 110 } 111 112 113 public boolean getIdentifyListener() 114 { 115 return _identifyListener; 116 } 117 118 119 124 public void setIdentifyListener(boolean identifyListener) 125 { 126 _identifyListener=identifyListener; 127 } 128 129 130 public String getDefaultScheme() 131 { 132 return HttpMessage.__SCHEME; 133 } 134 135 136 public void start() throws Exception 137 { 138 super.start(); 139 log.info("Started AJP13Listener on "+getInetAddrPort()); 140 log.info("NOTICE: AJP13 is not a secure protocol. Please protect the port "+getInetAddrPort()); 141 } 142 143 144 public void stop() throws InterruptedException 145 { 146 super.stop(); 147 log.info("Stopped AJP13Listener on "+getInetAddrPort()); 148 } 149 150 151 154 public String [] getRemoteServers() 155 { 156 return _remoteServers; 157 } 158 159 160 169 public void setRemoteServers(String [] servers) 170 { 171 _remoteServers=servers; 172 } 173 174 175 182 public void handleConnection(Socket socket) throws IOException 183 { 184 if (_remoteServers!=null&&_remoteServers.length>0) 186 { 187 boolean match=false; 188 InetAddress inetAddress=socket.getInetAddress(); 189 String hostAddr=inetAddress.getHostAddress(); 190 String hostName=inetAddress.getHostName(); 191 for (int i=0; i<_remoteServers.length; i++) 192 { 193 if (hostName.equals(_remoteServers[i])||hostAddr.equals(_remoteServers[i])) 194 { 195 match=true; 196 break; 197 } 198 } 199 if (!match) 200 { 201 log.warn("AJP13 Connection from un-approved host: "+inetAddress); 202 return; 203 } 204 } 205 206 socket.setTcpNoDelay(true); 208 socket.setSoTimeout(getMaxIdleTimeMs()); 209 AJP13Connection connection=createConnection(socket); 210 try 211 { 212 connection.handle(); 213 } 214 finally 215 { 216 connection.destroy(); 217 } 218 } 219 220 221 228 protected AJP13Connection createConnection(Socket socket) throws IOException 229 { 230 return new AJP13Connection(this,socket.getInputStream(),socket.getOutputStream(),socket,getBufferSize()); 231 } 232 233 234 241 public void customizeRequest(HttpConnection connection, HttpRequest request) 242 { 243 if (_identifyListener) 244 request.setAttribute(HttpListener.ATTRIBUTE,getName()); 245 246 Socket socket=(Socket )(connection.getConnection()); 247 customizeRequest(socket,request); 248 } 249 250 251 258 protected void customizeRequest(Socket socket, HttpRequest request) 259 { 260 } 261 262 263 268 public void persistConnection(HttpConnection connection) 269 { 270 } 271 272 273 276 public boolean isLowOnResources() 277 { 278 boolean low=getThreads()==getMaxThreads()&&getIdleThreads()<getMinThreads(); 279 if (low&&!_lastLow) 280 log.info("LOW ON THREADS: "+this); 281 else if (!low&&_lastLow) 282 { 283 log.info("OK on threads: "+this); 284 _lastOut=false; 285 } 286 _lastLow=low; 287 return low; 288 } 289 290 291 294 public boolean isOutOfResources() 295 { 296 boolean out=getThreads()==getMaxThreads()&&getIdleThreads()==0; 297 if (out&&!_lastOut) 298 log.warn("OUT OF THREADS: "+this); 299 300 _lastOut=out; 301 return out; 302 } 303 304 305 public boolean isIntegral(HttpConnection connection) 306 { 307 return ((AJP13Connection)connection).isSSL(); 308 } 309 310 311 public boolean isConfidential(HttpConnection connection) 312 { 313 return ((AJP13Connection)connection).isSSL(); 314 } 315 316 317 public String getIntegralScheme() 318 { 319 return _integralScheme; 320 } 321 322 323 public void setIntegralScheme(String integralScheme) 324 { 325 _integralScheme=integralScheme; 326 } 327 328 329 public int getIntegralPort() 330 { 331 return _integralPort; 332 } 333 334 335 public void setIntegralPort(int integralPort) 336 { 337 _integralPort=integralPort; 338 } 339 340 341 public String getConfidentialScheme() 342 { 343 return _confidentialScheme; 344 } 345 346 347 public void setConfidentialScheme(String confidentialScheme) 348 { 349 _confidentialScheme=confidentialScheme; 350 } 351 352 353 public int getConfidentialPort() 354 { 355 return _confidentialPort; 356 } 357 358 359 public void setConfidentialPort(int confidentialPort) 360 { 361 _confidentialPort=confidentialPort; 362 } 363 364 365 public HttpHandler getHttpHandler() 366 { 367 return _handler; 368 } 369 370 371 public void setHttpHandler(HttpHandler handler) 372 { 373 _handler=handler; 374 } 375 } 376 | Popular Tags |