@@ -32,7 +32,7 @@ import util.Failure
32
32
import com .lascala .libs .Enumerator
33
33
34
34
trait HttpResponse {
35
- def lastModified : Date = null
35
+ def lastModified : Date
36
36
def body : ByteString
37
37
def status : ByteString
38
38
def reason : ByteString
@@ -61,6 +61,14 @@ object HttpResponse {
61
61
62
62
def httpDate (date : Date ) = ByteString (httpDateFormat.format(date))
63
63
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
+
64
72
def bytes (rsp : HttpResponse ) = {
65
73
(new ByteStringBuilder ++=
66
74
version ++= SP ++= rsp.status ++= SP ++= rsp.reason ++= CRLF ++=
@@ -103,23 +111,17 @@ trait ChunkedEncodable extends HttpResponse {
103
111
def chunkedData : Enumerator [ByteString ]
104
112
}
105
113
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))
120
122
}
121
123
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 {
123
125
val status = ByteString (" 200" )
124
126
val reason = ByteString (" OK" )
125
127
@@ -142,19 +144,23 @@ object OKResponse {
142
144
case class NotModifiedResponse (body : ByteString = ByteString .empty, shouldKeepAlive : Boolean = false , mimeType : String = " " ) extends HttpResponse {
143
145
val status = ByteString (" 304" )
144
146
val reason = ByteString (" Not Modified" )
147
+ val lastModified = null ;
145
148
}
146
149
147
150
case class NotFoundError (body : ByteString = ByteString .empty, shouldKeepAlive : Boolean = false , mimeType : String = " " ) extends HttpResponse {
148
151
val status = ByteString (" 404" )
149
152
val reason = ByteString (" Not Found" )
153
+ val lastModified = null ;
150
154
}
151
155
152
156
case class MethodNotAllowedError (body : ByteString = ByteString .empty, shouldKeepAlive : Boolean = false , mimeType : String = " " ) extends HttpResponse {
153
157
val status = ByteString (" 405" )
154
158
val reason = ByteString (" Method Not Allowed" )
159
+ val lastModified = null ;
155
160
}
156
161
157
162
case class InternalServerError (body : ByteString = ByteString .empty, shouldKeepAlive : Boolean = false , mimeType : String = " " ) extends HttpResponse {
158
163
val status = ByteString (" 500" )
159
164
val reason = ByteString (" Internal Server Error" )
165
+ val lastModified = null ;
160
166
}
0 commit comments