-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponse.java
More file actions
executable file
·65 lines (56 loc) · 1.66 KB
/
Response.java
File metadata and controls
executable file
·65 lines (56 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.io.IOException;
import java.io.File;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
public class Response{
public int code;
public String reasonPhrase;
public Resource resource;
public HashMap<String, String> headers = new HashMap<>();
public String body;
public int size = 0;
public boolean isScript;
private static String HTTP_VERSION = "HTTP/1.1";
private static String SERVER = "Apache/2.4.1 (Unix)";
public Response(Resource resource){
this.resource = resource;
}
public Response(int code, String reasonPhrase){
this.code = code;
this.reasonPhrase = reasonPhrase;
}
public void send(OutputStream client, boolean isScript){
PrintWriter out = new PrintWriter( client, true );
out.println(HTTP_VERSION+" " + code + " " +reasonPhrase);
headers.put("Date",getServerTime());
headers.put("Server",SERVER);
Iterator iterator = headers.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry pair = (Map.Entry)iterator.next();
out.println(pair.getKey() + ": " + pair.getValue());
iterator.remove();
}
if(isScript == false){
out.println("");
}
if(body != null){
out.println(body);
}
}
public String getServerTime() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("PDT"));
return dateFormat.format(calendar.getTime());
}
}