Java Reference
In-Depth Information
import
java.util.logging.*
;
public
class
RequestProcessor
implements
Runnable
{
private
final
static
Logger
logger
=
Logger
.
getLogger
(
RequestProcessor
.
class
.
getCanonicalName
());
private
File
rootDirectory
;
private
String
indexFileName
=
"index.html"
;
private
Socket
connection
;
public
RequestProcessor
(
File
rootDirectory
,
String
indexFileName
,
Socket
connection
)
{
if
(
rootDirectory
.
isFile
())
{
throw
new
IllegalArgumentException
(
"rootDirectory must be a directory, not a file"
);
}
try
{
rootDirectory
=
rootDirectory
.
getCanonicalFile
();
}
catch
(
IOException
ex
)
{
}
this
.
rootDirectory
=
rootDirectory
;
if
(
indexFileName
!=
null
)
this
.
indexFileName
=
indexFileName
;
this
.
connection
=
connection
;
}
@Override
public
void
run
()
{
// for security checks
String
root
=
rootDirectory
.
getPath
();
try
{
OutputStream
raw
=
new
BufferedOutputStream
(
connection
.
getOutputStream
()
);
Writer
out
=
new
OutputStreamWriter
(
raw
);
Reader
in
=
new
InputStreamReader
(
new
BufferedInputStream
(
connection
.
getInputStream
()
),
"US-ASCII"
);
StringBuilder
requestLine
=
new
StringBuilder
();
while
(
true
)
{
int
c
=
in
.
read
();
if
(
c
==
'\r'
||
c
==
'\n'
)
break
;
requestLine
.
append
((
char
)
c
);
}
String
get
=
requestLine
.
toString
();
logger
.
info
(
connection
.
getRemoteSocketAddress
()
+
" "
+
get
);