Skip to content

Commit d93f6fd

Browse files
committed
Remove OKFileResponse by refactoring.
This patch is mostly contributed by Eun Woo Song. See the comments in 8cbc9c6.
1 parent 732a034 commit d93f6fd

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

src/main/scala/com/lascala/http/HttpResponse.scala

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import util.Failure
3232
import com.lascala.libs.Enumerator
3333

3434
trait HttpResponse {
35-
def lastModified: Date = null
35+
def lastModified: Date
3636
def body: ByteString
3737
def status: ByteString
3838
def reason: ByteString
@@ -61,6 +61,14 @@ object HttpResponse {
6161

6262
def httpDate(date: Date) = ByteString(httpDateFormat.format(date))
6363

64+
def readFile(file: File) = {
65+
val resource = new Array[Byte](file.length.toInt)
66+
val in = new FileInputStream(file)
67+
in.read(resource)
68+
in.close()
69+
ByteString(resource)
70+
}
71+
6472
def bytes(rsp: HttpResponse) = {
6573
(new ByteStringBuilder ++=
6674
version ++= SP ++= rsp.status ++= SP ++= rsp.reason ++= CRLF ++=
@@ -103,23 +111,17 @@ trait ChunkedEncodable extends HttpResponse {
103111
def chunkedData: Enumerator[ByteString]
104112
}
105113

106-
case class OKFileResponse(file: File, shouldKeepAlive: Boolean = true) extends HttpResponse {
107-
val body = readFile(file)
108-
val mimeType = new Tika().detect(file)
109-
val status = ByteString("200")
110-
val reason = ByteString("OK")
111-
112-
override def lastModified = new Date(file.lastModified)
113-
def readFile(file: File) = {
114-
val resource = new Array[Byte](file.length.toInt)
115-
val in = new FileInputStream(file)
116-
in.read(resource)
117-
in.close()
118-
ByteString(resource)
119-
}
114+
object OKResponse {
115+
import HttpResponse._
116+
117+
def withFile(file: File) = OKResponse(
118+
body = readFile(file),
119+
shouldKeepAlive = true,
120+
mimeType = new Tika().detect(file),
121+
lastModified = new Date(file.lastModified))
120122
}
121123

122-
case class OKResponse(body: ByteString, shouldKeepAlive: Boolean = true, mimeType: String = "text/html") extends HttpResponse {
124+
case class OKResponse(body: ByteString, shouldKeepAlive: Boolean = true, mimeType: String = "text/html", lastModified: Date = null) extends HttpResponse {
123125
val status = ByteString("200")
124126
val reason = ByteString("OK")
125127

@@ -142,19 +144,23 @@ object OKResponse {
142144
case class NotModifiedResponse(body: ByteString = ByteString.empty, shouldKeepAlive: Boolean = false, mimeType: String = "") extends HttpResponse {
143145
val status = ByteString("304")
144146
val reason = ByteString("Not Modified")
147+
val lastModified = null;
145148
}
146149

147150
case class NotFoundError(body: ByteString = ByteString.empty, shouldKeepAlive: Boolean = false, mimeType: String = "") extends HttpResponse {
148151
val status = ByteString("404")
149152
val reason = ByteString("Not Found")
153+
val lastModified = null;
150154
}
151155

152156
case class MethodNotAllowedError(body: ByteString = ByteString.empty, shouldKeepAlive: Boolean = false, mimeType: String = "") extends HttpResponse {
153157
val status = ByteString("405")
154158
val reason = ByteString("Method Not Allowed")
159+
val lastModified = null;
155160
}
156161

157162
case class InternalServerError(body: ByteString = ByteString.empty, shouldKeepAlive: Boolean = false, mimeType: String = "") extends HttpResponse {
158163
val status = ByteString("500")
159164
val reason = ByteString("Internal Server Error")
165+
val lastModified = null;
160166
}

src/main/scala/common/main.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class RequestHandler extends Actor {
5454
case file if file.isFile() =>
5555
headers.find(_.name.toLowerCase == "if-modified-since") match {
5656
case Some(d) if HttpResponse.httpDateFormat.parse(d.value).compareTo(new java.util.Date(file.lastModified)) != -1 => sender ! NotModifiedResponse()
57-
case _ => sender ! OKFileResponse(file, true)
57+
case _ => sender ! OKResponse.withFile(file)
5858
}
5959
case _ => sender ! NotFoundError()
6060
}

0 commit comments

Comments
 (0)