-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebClient.java
More file actions
387 lines (346 loc) · 14.3 KB
/
WebClient.java
File metadata and controls
387 lines (346 loc) · 14.3 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
* WebClient Class
*
* This class initiates a TCP connection to a remote server, downloads a requested resource
* (file), and then writes the file to the local working directory.
*/
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.util.logging.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WebClient {
private static final Logger logger = Logger.getLogger("WebClient"); // global logger
// The following regex was constructed with help from the website: https://regex101.com/
// Used for parsing URL. protocol hostname port pathname
private static final String URL_REGEX = "([a-zA-Z]+)://([^:/]+):?(\\d*)?/(\\S+)";
private static final String STRING_TO_BYTE_CHARSET = "US-ASCII";
// numbers chosen are arbitrary
private static final int NO_PORT = 0;
private static final int NO_BYTE = -1;
// numbers follow standard Java convention
private static final int SUCCESSFUL_TERMINATION = 0;
private static final int UNSUCCESSFUL_TERMINATION = -1;
/* currently, we lowercase the parsed protocol from the URL; however, we may change
that implementation in the future, in which case we do not want to have to change
every single string comparison in our program.
*/
private static final String HTTP = "http";
private static final String HTTPS = "https";
// as specified in assignment
private static final int HTTP_DEFAULT_PORT = 80;
private static final int HTTPS_DEFAULT_PORT = 443;
private static final String SUCCESS_STATUS_PHRASE = "OK";
private static final int SUCCESS_STATUS_CODE = 200;
// url variables
private String protocol;
private String hostname;
private int port;
private String pathname;
private String filename;
// connection variables
private Socket socket;
private InputStream inputStream;
private OutputStream outputStream;
private FileOutputStream fileOutputStream;
private static final int EOF = -1;
private static final int OFFSET = 0;
// arbitrary
private static final int BUFFER_SIZE = 4096;
/**
* Default no-arg constructor
*/
public WebClient() {
protocol = null;
hostname = null;
port = NO_PORT;
pathname = null;
filename = null;
socket = null;
inputStream = null;
outputStream = null;
}
/**
* Close all opened streams, sockets, and other resources before terminating the program.
*
* @param resources all resources which need to be closed
*/
private void closeGracefully(Closeable... resources) {
/*
* We need to surround this with a try-catch block because the closing itself can raise
* an IOException. In this case, if closing fails, there is nothing else we can do. We must also
* ensure the resource is not null. This is because other parts of the program instantiate certain
* resources to null before reassignment.
*/
try {
for (Closeable resource : resources) {
if (resource != null) {
resource.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Parses the provided URL to obtain the protocol, hostname, port, and pathname.
*
* @param url URL of the object to be downloaded. It is a fully qualified URL.
*/
private void parseUrl(String url) {
Pattern pattern = Pattern.compile(URL_REGEX);
Matcher matcher = pattern.matcher(url);
/*
* The previous line merely created a matcher object; we must invoke the
* find method to get the matcher to actually search the provided string. If
* the find succeeds, then we know we have found all of our mandatory capturing
* groups (protocol, hostname, pathname), and can perform an additional check
* to see if we also found a port.
*/
if (matcher.find()) {
/*
Note that group 0 is the entire string; individual capture groups are
indexed starting from 1. Since protocol and hostname are case-insensitive,
let's just make them lowercase.
*/
protocol = matcher.group(1).toLowerCase();
hostname = matcher.group(2).toLowerCase();
String portAsString = matcher.group(3);
// assign a default port if none is provided
if (portAsString.isEmpty()) {
switch (protocol) {
case HTTP:
port = HTTP_DEFAULT_PORT;
break;
case HTTPS:
port = HTTPS_DEFAULT_PORT;
break;
}
} else {
port = Integer.valueOf(portAsString);
}
pathname = matcher.group(4);
// the file is always the last parth of the pathname
String[] subdirectories = pathname.split("/");
filename = subdirectories[subdirectories.length - 1];
}
}
/**
* Establishes a connection with a host at a specific port number. Assumes the
* hostname and port have been defined by some other part of the program.
* @throws IOException
* @throws UnknownHostException
*/
private void establishConnection() throws UnknownHostException, IOException {
switch (protocol) {
case HTTP:
socket = new Socket(hostname, port);
break;
// if the protocol is HTTPS, we need a Secure Socket Layer (SSL) Socket.
case HTTPS:
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
socket = (SSLSocket) factory.createSocket(hostname, port);
break;
}
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
}
/**
* Create a properly formatted HTTP GET request message.
*
* @return A request message that is ready to send to the server.
*/
private String constructGetRequest() {
String httpMethod = "GET";
String httpVersion = "HTTP/1.1";
String hostHeader = "Host: " + hostname + "\r\n";
String connectionHeader = "Connection: close\r\n";
/*
* A request message has these three "components"; this is why the code is broken up
* in a similar manner, but these could just as easily be combined into a single string.
*/
String requestLine = String.format("%s /%s %s\r\n", httpMethod, pathname, httpVersion);
String headerLines = hostHeader + connectionHeader;
String endOfHeaderLines = "\r\n";
String request = requestLine + headerLines + endOfHeaderLines;
return request;
}
/**
* Send a properly formatted HTTP GET request message.
*
* @param getRequest Properly formatted GET request.
* @throws IOException
*/
private void sendGetRequest(String getRequest) throws IOException {
byte[] getRequestBytes = getRequest.getBytes(STRING_TO_BYTE_CHARSET);
outputStream.write(getRequestBytes);
/*
* flush to ensure request is actually written to the socket.
*/
outputStream.flush();
System.out.println(getRequest);
}
/**
* Check if the response was successful and if so, read the payload.
*
* @throws IOException
*/
private void readResponse() throws IOException {
if (wasRequestSuccessful()) {
readPayload();
}
}
/**
* Reads the response byte by byte, checking for the status code and
* status phrase to see if the request was successful, and parsing all the
* headers from the response.
*
* @throws IOException
* @return A boolean denoting whether or not the request was successful.
*/
private boolean wasRequestSuccessful() throws IOException {
/*
initially, we have not read any bytes from the response. We need
both prevByte and currByte because we need to keep track of two
bytes sequentially (to see if we've encountered \r\n).
*/
int prevByte = NO_BYTE;
int currByte = NO_BYTE;
/* the response status line is formatted as: protocol statusCode statusPhrase
when we split on the status line, we use an index of 0 and 1 to access the code and phrase
respectively
*/
int STATUS_CODE = 1;
int STATUS_PHRASE = 2;
/*
* We haven't yet read the response, so the current line and the response are empty.
* In addition, we need a separate boolean for knowing if we are on the firstLine, because this is how
* we know to check for the status code and phrase.
*/
String currLine = "";
boolean readFirstLine = false;
String response = "";
// assume the request was successful unless we find otherwise
boolean requestWasSuccessful = true;
while ((currByte = inputStream.read()) != EOF) {
currLine += (char) currByte;
/*
* Java implicitly converts the char to its ASCII value when
* comparing with prevByte and currByte. This is why we are able
* to make this comparison.
*/
if (prevByte == '\r' && currByte == '\n') {
response += currLine;
/*
* Check that both the status code and status phrase are what we expect.
* We need to trim the status_phrase to get rid of extra white space before
* or after. We don't need to trim on the status_code since Integer.valueOf
* will automatically deal with that white space anyway.
*/
if (!readFirstLine) {
String[] statusLine = currLine.split(" ");
if (Integer.valueOf(statusLine[STATUS_CODE]) != SUCCESS_STATUS_CODE ||
!(statusLine[STATUS_PHRASE].trim().equals(SUCCESS_STATUS_PHRASE))
) {
/*
if the request was unsuccessful, we set the boolean to false but
do not immediately break out of the loop. This is because we still
need to finish reading all headers.
*/
requestWasSuccessful = false;
}
readFirstLine = true;
}
/*
* If we've encountered a line consisting solely of \r\n, this means
* we've found the end of our header lines. We can exit the loop
* as we no longer want to parse the remainder of the response (doing
* so would mean we are reading the payload).
*/
if (currLine.equals("\r\n")) {
break;
}
/*
* If you've found the end of a regular header line, then "reset"
* it to begin reading the next header line. This is to ensure that
* we do not add duplicate or redundant info to our response when
* printing to console.
*/
currLine = "";
prevByte = NO_BYTE;
currByte = NO_BYTE;
}
prevByte = currByte;
}
System.out.println(response);
return requestWasSuccessful;
}
/**
* Reads the payload of the response and creates a local file.
*
* @throws IOException
*/
private void readPayload() throws IOException {
/*
* The numBytes tells us how many bytes we actually read from the server; this may
* be different from the buffer size (ie. if the number of bytes remaining is <
* buffer.length). This is why we cannot specify buffer.length as the number of bytes being written
* to the file, as we would get an IndexOutOfBounds exception when we reach the end.
*/
int numBytes = 0;
byte[] buffer = new byte[BUFFER_SIZE];
fileOutputStream = new FileOutputStream(filename);
while ((numBytes = inputStream.read(buffer)) != EOF) {
fileOutputStream.write(buffer, OFFSET, numBytes);
}
/*
* we are only going to open the file after the entire file has been written; therefore, we can flush at the end.
* There is no urgency when outputting to file.
*/
fileOutputStream.flush();
}
/**
* Downloads the object specified by the parameter url.
*
* @param url URL of the object to be downloaded. It is a fully qualified URL.
*/
public void getObject(String url) {
boolean wasSuccessful = true;
try {
parseUrl(url);
establishConnection();
sendGetRequest(constructGetRequest());
readResponse();
}
catch (UnknownHostException e) {
wasSuccessful = false;
e.printStackTrace();
}
catch (IOException e) {
wasSuccessful = false;
e.printStackTrace();
}
/*
* We must always close our resources, regardless of whether or not
* the request was successful. We close them in reverse chronological
* order.
*/
finally {
closeGracefully(
fileOutputStream,
outputStream,
inputStream,
socket
);
if (wasSuccessful) {
System.exit(SUCCESSFUL_TERMINATION);
}
else {
System.exit(UNSUCCESSFUL_TERMINATION);
}
}
}
}