| Index: trunk/lucene-search-3/src/main/java/org/wikimedia/lsearch/frontend/HttpHandler.java |
| — | — | @@ -14,23 +14,31 @@ |
| 15 | 15 | import java.net.URI; |
| 16 | 16 | import java.net.URISyntaxException; |
| 17 | 17 | import java.util.HashMap; |
| | 18 | + |
| | 19 | +import org.apache.log4j.Level; |
| 18 | 20 | import org.apache.log4j.Logger; |
| 19 | 21 | |
| 20 | 22 | /** |
| 21 | 23 | * Simple HTTP 1.1 handler, used for Index and Search daemons |
| 22 | 24 | * for more info about the protocol see handle() method |
| 23 | 25 | * |
| | 26 | + * |
| | 27 | + * NOTE: we are using old JDK 1.1 classes which don't have |
| | 28 | + * a very good unicode support, so extra care needs to be taken |
| | 29 | + * so everything is converted out right. The old classes are used |
| | 30 | + * because they enable better byte-oriented operations. |
| | 31 | + * |
| | 32 | + * $LastChangedDate:$ |
| | 33 | + * $LastChangedRevision:$ |
| | 34 | + * $LastChangedBy:$ |
| | 35 | + * |
| 24 | 36 | * @author Brion Vibber |
| 25 | 37 | * |
| 26 | 38 | */ |
| 27 | 39 | abstract public class HttpHandler extends Thread { |
| 28 | 40 | protected static org.apache.log4j.Logger log = Logger.getLogger(HttpHandler.class); |
| 29 | 41 | |
| 30 | | - /** NOTE: we are using old JDK 1.1 classes which don't have |
| 31 | | - * a very good unicode support, so extra care needs to be taken |
| 32 | | - * so everything is converted out right. The old classes are used |
| 33 | | - * because they enable better byte-oriented operations. |
| 34 | | - */ |
| | 42 | + |
| 35 | 43 | /** Client input stream */ |
| 36 | 44 | DataInputStream istrm; |
| 37 | 45 | /** Client output stream */ |
| — | — | @@ -52,6 +60,10 @@ |
| 53 | 61 | protected String charset = "none"; |
| 54 | 62 | boolean headersSent; |
| 55 | 63 | |
| | 64 | + /** |
| | 65 | + * collection of the headers |
| | 66 | + */ |
| | 67 | + @SuppressWarnings("rawtypes") |
| 56 | 68 | protected HashMap<String, Comparable> headers; |
| 57 | 69 | |
| 58 | 70 | protected static HttpMonitor monitor = null; |
| — | — | @@ -63,7 +75,9 @@ |
| 64 | 76 | istrm = new DataInputStream(new BufferedInputStream(s.getInputStream())); |
| 65 | 77 | ostrm = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream(),"utf-8"))); |
| 66 | 78 | } catch (IOException e) { |
| 67 | | - log.error("I/O in opening http socket.",e); |
| | 79 | + if (log.isEnabledFor(Level.ERROR)){ |
| | 80 | + log.error("I/O in opening http socket.",e); |
| | 81 | + } |
| 68 | 82 | } |
| 69 | 83 | } |
| 70 | 84 | |
| — | — | @@ -91,7 +105,7 @@ |
| 92 | 106 | public boolean isKeepAlive(){ |
| 93 | 107 | if(version.equals("HTTP/1.0")){ |
| 94 | 108 | if(headers.get("Connection")!=null && |
| 95 | | - ((String)headers.get("Connection")).equalsIgnoreCase("Keep-Alive")) |
| | 109 | + ((String)headers.get("Connection")).equalsIgnoreCase("keep-alive")) |
| 96 | 110 | return true; |
| 97 | 111 | else |
| 98 | 112 | return false; |
| — | — | @@ -112,12 +126,19 @@ |
| 113 | 127 | do{ |
| 114 | 128 | headersSent = false; |
| 115 | 129 | handle(); |
| 116 | | - log.debug("request handled."); |
| | 130 | + if (log.isEnabledFor(Level.DEBUG)){ |
| | 131 | + log.debug("request handled."); |
| | 132 | + } |
| | 133 | + |
| 117 | 134 | } while(isKeepAlive()); |
| 118 | | - log.debug("No keep-alive, closing connection ... "); |
| | 135 | + if (log.isEnabledFor(Level.DEBUG)){ |
| | 136 | + log.debug("No keep-alive, closing connection ... "); |
| | 137 | + } |
| 119 | 138 | } catch (Exception e) { |
| 120 | 139 | e.printStackTrace(); |
| 121 | | - log.error(e.getMessage(),e); |
| | 140 | + if (log.isEnabledFor(Level.DEBUG)){ |
| | 141 | + log.error(e.getMessage(),e); |
| | 142 | + } |
| 122 | 143 | } finally { |
| 123 | 144 | if (!headersSent) { |
| 124 | 145 | sendError(500, "Internal server error", "An internal error occurred: no header sent."); |
| — | — | @@ -133,7 +154,7 @@ |
| 134 | 155 | /** |
| 135 | 156 | * Simple HTTP protocol; Used for search (GET) and indexing (POST) |
| 136 | 157 | * GET requests syntax: |
| 137 | | - * URL path format: /operation/database/searchterm |
| | 158 | + * URL path format: /operation/database/search-term |
| 138 | 159 | * The path should be URL-encoded UTF-8 (standard IRI). |
| 139 | 160 | * |
| 140 | 161 | * Additional parameters may be specified in a query string: |
| — | — | @@ -148,7 +169,9 @@ |
| 149 | 170 | * typically, this means: /updatePage?db=entest&title=Main%20Page |
| 150 | 171 | * and the POST content is article text |
| 151 | 172 | */ |
| | 173 | + @SuppressWarnings("rawtypes") |
| 152 | 174 | protected void handle() { |
| | 175 | + |
| 153 | 176 | headers = new HashMap<String, Comparable>(); |
| 154 | 177 | |
| 155 | 178 | // parse first line |
| — | — | @@ -176,9 +199,10 @@ |
| 177 | 200 | try { |
| 178 | 201 | uri = new URI("http://localhost:8123" + rawUri); |
| 179 | 202 | } catch (URISyntaxException e) { |
| 180 | | - sendError(400, "Bad Request", |
| 181 | | - "Couldn't make sense of the given URI."); |
| 182 | | - log.warn("Bad URI in request: " + rawUri,e); |
| | 203 | + sendError(400, "Bad Request", "Couldn't make sense of the given URI."); |
| | 204 | + if (log.isEnabledFor(Level.WARN)) { |
| | 205 | + log.warn("Bad URI in request: " + rawUri,e); |
| | 206 | + } |
| 183 | 207 | return; |
| 184 | 208 | } |
| 185 | 209 | |
| — | — | @@ -203,7 +227,9 @@ |
| 204 | 228 | |
| 205 | 229 | protected void sendHeaders(int code, String message, int contentLen) { |
| 206 | 230 | if (headersSent) { |
| 207 | | - log.warn("Asked to send headers, but already sent! ("+code+" "+message+")"); |
| | 231 | + if (log.isEnabledFor(Level.WARN)){ |
| | 232 | + log.warn("Asked to send headers, but already sent! ("+code+" "+message+")"); |
| | 233 | + } |
| 208 | 234 | return; |
| 209 | 235 | } |
| 210 | 236 | sendOutputLine("HTTP/1.1 "+code+" "+message); |
| — | — | @@ -232,7 +258,9 @@ |
| 233 | 259 | |
| 234 | 260 | /** Send single line to client. The lines are buffered and sent out in chunks */ |
| 235 | 261 | protected void sendOutputLine(String sout) { |
| 236 | | - log.debug(">>>"+sout); |
| | 262 | + if (log.isEnabledFor(Level.DEBUG)){ |
| | 263 | + log.debug(">>>"+sout); |
| | 264 | + } |
| 237 | 265 | // write to buffer instead directly to stream! |
| 238 | 266 | char[] s = (sout+"\r\n").toCharArray(); |
| 239 | 267 | if(bufLength + s.length >= outputBuffer.length) |
| — | — | @@ -247,7 +275,9 @@ |
| 248 | 276 | |
| 249 | 277 | /** Sending raw data to client */ |
| 250 | 278 | protected void sendBytes(char[] data){ |
| 251 | | - log.debug(">>> Writing "+data.length+" bytes of data"); |
| | 279 | + if (log.isEnabledFor(Level.DEBUG)){ |
| | 280 | + log.debug(">>> Writing "+data.length+" bytes of data"); |
| | 281 | + } |
| 252 | 282 | flushOutput(); |
| 253 | 283 | ostrm.write(data); |
| 254 | 284 | } |
| — | — | @@ -266,7 +296,9 @@ |
| 267 | 297 | //log.error("Internal error, read "+read+" bytes istead of "+contentLength+" from POST request"); |
| 268 | 298 | return data; |
| 269 | 299 | } catch (IOException e) { |
| 270 | | - log.warn("Could not send raw data in bytes to output stream.",e); |
| | 300 | + if (log.isEnabledFor(Level.WARN)){ |
| | 301 | + log.warn("Could not send raw data in bytes to output stream.",e); |
| | 302 | + } |
| 271 | 303 | } |
| 272 | 304 | return null; |
| 273 | 305 | } |
| — | — | @@ -278,9 +310,13 @@ |
| 279 | 311 | try { |
| 280 | 312 | sin = istrm.readLine(); |
| 281 | 313 | } catch (IOException e) { |
| 282 | | - log.warn("I/O problem in reading from stream",e); |
| | 314 | + if (log.isEnabledFor(Level.WARN)){ |
| | 315 | + log.warn("I/O problem in reading from stream",e); |
| | 316 | + } |
| 283 | 317 | } |
| 284 | | - log.debug("<<<"+ sin); |
| | 318 | + if (log.isEnabledFor(Level.DEBUG)){ |
| | 319 | + log.debug("<<<"+ sin); |
| | 320 | + } |
| 285 | 321 | return sin; |
| 286 | 322 | } |
| 287 | 323 | |