-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseFactory.java
More file actions
executable file
·162 lines (146 loc) · 4.46 KB
/
ResponseFactory.java
File metadata and controls
executable file
·162 lines (146 loc) · 4.46 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import java.util.Scanner;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
public class ResponseFactory {
Response response;
Request request;
MimeTypes mimetypes;
public Response getResponse(Request request, Resource resource, MimeTypes mimetypes) throws IOException, ParseException,ServerException{
response = new Response(resource);
this.request = request;
this.mimetypes = mimetypes;
if(resource.isScript() == true){
executeScript(resource);
}else if( (fileExists(resource.absolutePath())) == false && (request.getVerb().equals("PUT")) == false ){
fileNotFound();
}else{
identifyVerb(request.getVerb(),resource);
}
return response;
}
public Boolean fileExists(String path){
File file = new File(path);
return file.exists();
}
public void identifyVerb(String verb, Resource resource) throws IOException, ParseException, ServerException{
switch ( verb ) {
case "PUT": createFile(resource.absolutePath());
break;
case "DELETE": deleteFile(resource.absolutePath());
break;
case "POST": sendFileContents(resource.absolutePath(),true);
break;
case "GET": checkLastModified(resource.absolutePath());
break;
case "HEAD": sendFileContents(resource.absolutePath(),false);
break;
default: badRequest();
}
}
public void createFile(String path) throws IOException {
File file = new File(path);
file.getParentFile().mkdirs();
file.createNewFile();
response.headers.put("Content-Location", path);
response.code = 201;
response.reasonPhrase = "Created";
}
public void deleteFile(String path){
File file = new File(path);
file.delete();
response.code = 204;
response.reasonPhrase = "No Content";
}
public void sendFileContents(String path, Boolean sendBody) throws IOException{
String line;
File file = new File(path);
Scanner scanner = new Scanner(file);
while(scanner.hasNext()){
line = scanner.nextLine();
response.body += (line + "\n");
}
byte bytes[] = response.body.getBytes("UTF-8");
response.headers.put("Content-Length", Integer.toString(bytes.length));
if( sendBody == false ){
response.body = null;
}
response.size = bytes.length;
getContentType(path);
OkResponse();
}
public void checkLastModified(String path)throws IOException, ParseException{
File file;
String ifModifiedSince = request.getHeaderContent("If-Modified-Since");
SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
Date ifModifiedSinceDate;
Date timeModified;
try{
file = new File(path);
}catch(Exception e){
fileNotFound();
return;
}
timeModified = new Date(file.lastModified());
try{
ifModifiedSinceDate = formatter.parse(ifModifiedSince);
}catch(Exception e){
notModified(path);
return;
}
if(timeModified.after(ifModifiedSinceDate)){
sendFileContents(path,true);
}else{
notModified(path);
response.headers.put("Last-Modified", formatter.format(timeModified));
}
}
public void getContentType(String path){
File file = new File(path);
String name = file.getName();
String extension = "";
int i = name.lastIndexOf('.');
if (i > 0) {
extension = name.substring(i+1);
System.out.println(extension);
}
response.headers.put("Content-Type", mimetypes.lookUp(extension));
}
public void OkResponse(){
response.code = 200;
response.reasonPhrase = "OK";
}
public void badRequest() throws ServerException{
throw new ServerException();
}
public void fileNotFound(){
response.code = 404;
response.reasonPhrase = "File Not Found";
}
public void notModified(String path){
response.headers.put("Content-Location", path);
response.code = 304;
response.reasonPhrase = "Not Modified";
}
public void executeScript(Resource resource) throws IOException {
response.code = 200;
response.reasonPhrase = "OK";
ProcessBuilder build = new ProcessBuilder(resource.absolutePath());
Process process = build.start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = stdInput.readLine()) != null){
stringBuilder.append(line);
}
response.body = stringBuilder.toString();
Map<String, String> env = build.environment();
env.put("SERVER_PROTOCOL", "HTTP/1.1");
env.put("QUERY_STRING", resource.absolutePath());
response.isScript = true;
}
}