| 1 |
package org.astrogrid.samp.web; |
| 2 |
|
| 3 |
import java.net.ServerSocket; |
| 4 |
import java.io.ByteArrayOutputStream; |
| 5 |
import java.io.IOException; |
| 6 |
import java.io.OutputStream; |
| 7 |
import java.io.PrintStream; |
| 8 |
import java.io.UnsupportedEncodingException; |
| 9 |
import java.util.Iterator; |
| 10 |
import java.util.Map; |
| 11 |
|
| 12 |
/** |
| 13 |
* CorsHttpServer subclass which performs logging to a given print stream |
| 14 |
* at the HTTP level. Logging is not done through the logging system. |
| 15 |
* |
| 16 |
* @author Mark Taylor |
| 17 |
* @since 2 Feb 2011 |
| 18 |
*/ |
| 19 |
public class LoggingCorsHttpServer extends CorsHttpServer { |
| 20 |
|
| 21 |
private final PrintStream out_; |
| 22 |
private int iSeq_; |
| 23 |
|
| 24 |
/** |
| 25 |
* Constructor. |
| 26 |
* |
| 27 |
* @param socket socket hosting the service |
| 28 |
* @param auth defines which domains requests will be permitted from |
| 29 |
* @param out destination stream for logging |
| 30 |
*/ |
| 31 |
public LoggingCorsHttpServer( ServerSocket socket, OriginAuthorizer auth, |
| 32 |
PrintStream out ) throws IOException { |
| 33 |
super( socket, auth ); |
| 34 |
out_ = out; |
| 35 |
} |
| 36 |
|
| 37 |
public Response serve( Request request ) { |
| 38 |
int iseq; |
| 39 |
synchronized ( this ) { |
| 40 |
iseq = ++iSeq_; |
| 41 |
} |
| 42 |
logRequest( request, iseq ); |
| 43 |
return new LoggedResponse( super.serve( request ), iseq, |
| 44 |
"POST".equals( request.getMethod() ) ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Logs a given request. |
| 49 |
* |
| 50 |
* @param request HTTP request |
| 51 |
* @param iseq index of the request; unique integer for each request |
| 52 |
*/ |
| 53 |
private void logRequest( Request request, int iseq ) { |
| 54 |
StringBuffer sbuf = new StringBuffer(); |
| 55 |
sbuf.append( '\n' ); |
| 56 |
appendBanner( sbuf, '>', iseq ); |
| 57 |
sbuf.append( request.getMethod() ) |
| 58 |
.append( ' ' ) |
| 59 |
.append( request.getUrl() ) |
| 60 |
.append( '\n' ); |
| 61 |
appendHeaders( sbuf, request.getHeaderMap() ); |
| 62 |
byte[] body = request.getBody(); |
| 63 |
if ( body != null && body.length > 0 ) { |
| 64 |
sbuf.append( '\n' ); |
| 65 |
try { |
| 66 |
sbuf.append( new String( request.getBody(), "utf-8" ) ); |
| 67 |
} |
| 68 |
catch ( UnsupportedEncodingException e ) { |
| 69 |
throw new AssertionError( "No utf-8??" ); |
| 70 |
} |
| 71 |
} |
| 72 |
out_.println( sbuf ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Adds a line to the given stringbuffer which indicates information |
| 77 |
* relating to a given sequence number follows. |
| 78 |
* |
| 79 |
* @param sbuf string buffer to add to |
| 80 |
* @param c filler character |
| 81 |
* @param iseq sequence number |
| 82 |
*/ |
| 83 |
private void appendBanner( StringBuffer sbuf, char c, int iseq ) { |
| 84 |
String label = Integer.toString( iseq ); |
| 85 |
int nc = 75 - label.length(); |
| 86 |
for ( int i = 0; i < nc; i++ ) { |
| 87 |
sbuf.append( c ); |
| 88 |
} |
| 89 |
sbuf.append( ' ' ) |
| 90 |
.append( label ) |
| 91 |
.append( '\n' ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Adds HTTP header information to a string buffer. |
| 96 |
* |
| 97 |
* @param sbuf buffer to add lines to |
| 98 |
* @param map header key->value pair map |
| 99 |
*/ |
| 100 |
private void appendHeaders( StringBuffer sbuf, Map map ) { |
| 101 |
for ( Iterator it = map.entrySet().iterator(); it.hasNext(); ) { |
| 102 |
Map.Entry entry = (Map.Entry) it.next(); |
| 103 |
sbuf.append( entry.getKey() ) |
| 104 |
.append( ": " ) |
| 105 |
.append( entry.getValue() ) |
| 106 |
.append( '\n' ); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* HTTP response which will log its content at an appropriate time. |
| 112 |
*/ |
| 113 |
private class LoggedResponse extends Response { |
| 114 |
private final Response base_; |
| 115 |
private final boolean logBody_; |
| 116 |
private final String headText_; |
| 117 |
private String bodyText_; |
| 118 |
|
| 119 |
/** |
| 120 |
* Constructor. |
| 121 |
* |
| 122 |
* @param base response on which this one is based |
| 123 |
* @param iseq sequence number of request that this is a response to |
| 124 |
* @param logBody true iff the body of the response is to be logged |
| 125 |
*/ |
| 126 |
LoggedResponse( Response base, int iseq, boolean logBody ) { |
| 127 |
super( base.getStatusCode(), base.getStatusPhrase(), |
| 128 |
base.getHeaderMap() ); |
| 129 |
base_ = base; |
| 130 |
logBody_ = logBody; |
| 131 |
StringBuffer sbuf = new StringBuffer(); |
| 132 |
sbuf.append( '\n' ); |
| 133 |
appendBanner( sbuf, '<', iseq ); |
| 134 |
sbuf.append( getStatusCode() ) |
| 135 |
.append( ' ' ) |
| 136 |
.append( getStatusPhrase() ) |
| 137 |
.append( '\n' ); |
| 138 |
appendHeaders( sbuf, getHeaderMap() ); |
| 139 |
headText_ = sbuf.toString(); |
| 140 |
} |
| 141 |
|
| 142 |
public void writeBody( final OutputStream out ) throws IOException { |
| 143 |
|
| 144 |
// This method captures the logging output as well as writing |
| 145 |
// the body text to the given stream. The purpose of doing it |
| 146 |
// like this rather than writing the logging information |
| 147 |
// directly is so that this method can be harmlessly called |
| 148 |
// multiple times (doesn't normally happen, but can do sometimes). |
| 149 |
|
| 150 |
// Prepare an object (an OutputStream) to which you can write |
| 151 |
// the body content and then use its toString method to get |
| 152 |
// the loggable text. This loggable text is either the |
| 153 |
// body content itself, or an indication of how many bytes it |
| 154 |
// contained, depending on the logBody_ flag. |
| 155 |
final OutputStream lout = logBody_ |
| 156 |
? (OutputStream) new ByteArrayOutputStream() { |
| 157 |
public String toString() { |
| 158 |
String txt; |
| 159 |
try { |
| 160 |
txt = new String( buf, 0, count, "utf-8" ); |
| 161 |
} |
| 162 |
catch ( UnsupportedEncodingException e ) { |
| 163 |
txt = e.toString(); |
| 164 |
} |
| 165 |
return "\n" + txt + "\n"; |
| 166 |
} |
| 167 |
} |
| 168 |
: (OutputStream) new CountOutputStream() { |
| 169 |
public String toString() { |
| 170 |
return count_ > 0 |
| 171 |
? "<" + count_ + " bytes of output omitted>\n" |
| 172 |
: ""; |
| 173 |
} |
| 174 |
}; |
| 175 |
|
| 176 |
// Prepare an output stream which writes both to the normal |
| 177 |
// response destination and to the content logging object we've |
| 178 |
// just set up. |
| 179 |
OutputStream teeOut = new OutputStream() { |
| 180 |
public void write( byte[] b ) throws IOException { |
| 181 |
lout.write( b ); |
| 182 |
out.write( b ); |
| 183 |
} |
| 184 |
public void write( byte[] b, int off, int len ) |
| 185 |
throws IOException { |
| 186 |
lout.write( b, off, len ); |
| 187 |
out.write( b, off, len ); |
| 188 |
} |
| 189 |
public void write( int b ) throws IOException { |
| 190 |
lout.write( b ); |
| 191 |
out.write( b ); |
| 192 |
} |
| 193 |
}; |
| 194 |
|
| 195 |
// Write the body content to the response output stream, |
| 196 |
// and store the loggable output. |
| 197 |
String slog; |
| 198 |
try { |
| 199 |
base_.writeBody( teeOut ); |
| 200 |
slog = lout.toString(); |
| 201 |
} |
| 202 |
catch ( IOException e ) { |
| 203 |
slog = "log error? " + e + "\n"; |
| 204 |
} |
| 205 |
bodyText_ = slog; |
| 206 |
} |
| 207 |
|
| 208 |
public void writeResponse( final OutputStream out ) throws IOException { |
| 209 |
super.writeResponse( out ); |
| 210 |
out_.print( headText_ + bodyText_ ); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* OutputStream subclass which counts the number of bytes it is being |
| 216 |
* asked to write, but otherwise does nothing. |
| 217 |
*/ |
| 218 |
private static class CountOutputStream extends OutputStream { |
| 219 |
long count_; // number of bytes counted so far |
| 220 |
public void write( byte[] b ) { |
| 221 |
count_ += b.length; |
| 222 |
} |
| 223 |
public void write( byte[] b, int off, int len ) { |
| 224 |
count_ += len; |
| 225 |
} |
| 226 |
public void write( int b ) { |
| 227 |
count_++; |
| 228 |
} |
| 229 |
} |
| 230 |
} |