Skip to content

Commit c4de2bd

Browse files
committed
refactored common methods into a Util class
1 parent 18c0887 commit c4de2bd

File tree

5 files changed

+137
-114
lines changed

5 files changed

+137
-114
lines changed

HttpRequest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import java.net.*;
33
import java.util.*;
44
import java.text.SimpleDateFormat;
5+
import static SSHelpers.Util.*;
56

67
public class HttpRequest {
78
/** Help variables */
@@ -102,9 +103,7 @@ public HttpResponse askServerIfvalid(Date lastModified, String etag) {
102103
headers += "If-None-Match: " + etag + CRLF;
103104
} if (lastModified != null) {
104105
// If-Modified-Since: Thu, 13 Oct 2016 00:36:43 GMT\r\n
105-
SimpleDateFormat dateFormat = new SimpleDateFormat ("E, dd MMM yyyy HH:mm:ss z");
106-
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
107-
headers += "If-Modified-Since: " + dateFormat.format(lastModified) + CRLF;
106+
headers += "If-Modified-Since: " + dateToString(lastModified) + CRLF;
108107
}
109108

110109
headers += "Host: " + host + CRLF;

HttpResponse.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import java.io.*;
22
import java.net.*;
33
import java.util.*;
4-
import java.text.SimpleDateFormat;
5-
import java.text.ParseException;
6-
4+
import static SSHelpers.Util.*;
75

86
public class HttpResponse {
97
final static String CRLF = "\r\n";
@@ -228,22 +226,6 @@ public Socket send(Socket host) {
228226
return host;
229227
}
230228

231-
private Date toDate(String dateStr, String format) {
232-
try {
233-
SimpleDateFormat inFormat = new SimpleDateFormat(format);
234-
inFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
235-
return inFormat.parse(dateStr);
236-
} catch (ParseException e) {
237-
System.out.println("Parse date error: " + e + " -- at position:" + e.getErrorOffset());
238-
}
239-
return null;
240-
}
241-
242-
private Date toDate(String dateStr) {
243-
String format = "E, dd MMM yyyy HH:mm:ss z"; // Mon, 13 Jun 2016 21:06:31 GMT
244-
return toDate(dateStr, format);
245-
}
246-
247229
// check if response is still valid using the (now old) expires header
248230
public boolean isExpired() {
249231
if (lastModified != null && expires != null) {

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ clean:
1616
docs: *.java
1717
$(JDOC) $^ -d docs \
1818
-windowtitle $(DOCTITLE) \
19-
-doctitle $(DOCTITLE)
19+
-doctitle $(DOCTITLE) \
20+
-subpackages SSHelpers
2021

2122
docs-clean:
2223
$(RM) docs/*

SCache.java

Lines changed: 16 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
import java.util.*;
44
import java.util.HashMap;
55
import java.text.SimpleDateFormat;
6+
import static SSHelpers.Util.*;
67

78
public class SCache {
9+
// private Helpers helpers = new Helpers();
10+
11+
// A key value store in memory
812
private Map<String, HttpResponse> cacheStore;
13+
// A HttpRequest used to send If-None-Match and If-Modified-Since requests to the server
914
HttpRequest request;
1015

1116
/**
12-
* [SCache description]
17+
* This SCache class contains the cache for the proxy.
18+
* It provides methods to add, validate and get items from the cache.
19+
* The cache uses the URL as a key to lookup items in the cache
1320
*/
1421
public SCache() {
1522
// synchronised HasMap prevents data corruption when reading while writing the Map
@@ -18,7 +25,7 @@ public SCache() {
1825
}
1926

2027
/**
21-
* Add the HttpResponse into the HashMap cache with the url as the key.
28+
* Adds the HttpResponse into the cache with the url as the key.
2229
* @param key The Url
2330
* @param value The HttpResponse
2431
* @return The given value HttpResponse
@@ -29,7 +36,8 @@ public HttpResponse put(String key, HttpResponse value) {
2936
}
3037

3138
/**
32-
* Retrieves the HttpResponse from HashMap cache by looking up the url
39+
* Retrieves the HttpResponse from cache by looking up the url.
40+
* The HttpResponse is checked to make sure it is fresh.
3341
* @param key The Url
3442
* @return The Cached HttpResponse
3543
*/
@@ -50,9 +58,12 @@ public HttpResponse get(String key) {
5058
return value;
5159
}
5260

53-
// check if response is still valid
5461
/**
55-
* Determine if the Cache HttpResonse is still fresh.
62+
* Determines if the cached HttpResonse is still fresh and updates the cache when it is not.
63+
* It checks that the response has not expired by checking the Last-Modified and the Expired header
64+
* in the response. If no expired header is present the server is asked with If-None-Match and the
65+
* If-Modified-Since headers. If the server response status code is not a 304 Not Modified response
66+
* than the new response updates the value in the cache.
5667
* @param requestURL The key in the cache
5768
* @param response The value in the cache
5869
* @return isValid? as in is the item in the cache fresh?
@@ -63,7 +74,6 @@ public boolean isValid(String requestURL, HttpResponse response) {
6374
return true;
6475
}
6576

66-
// HttpRequest check = new HttpRequest(request); // deep copy
6777
request.setHost(getHost(requestURL));
6878
request.setPort(getPort(requestURL));
6979
request.URL = getLocation(requestURL);
@@ -72,7 +82,6 @@ public boolean isValid(String requestURL, HttpResponse response) {
7282
if (newResponse == null) { // if error
7383
return true; // use the cached response if new response has an unrecoverable error.
7484
}
75-
// HttpResponse r = new HttpRequest(request, response.lastModified, response.etag).send(); // ask server
7685
if (newResponse.status == 304) { // Not modified, so still valid
7786
System.out.println("SCACHE: 304 Not modified");
7887
return true;
@@ -81,89 +90,5 @@ public boolean isValid(String requestURL, HttpResponse response) {
8190
put(requestURL, newResponse); // save the new response
8291
return false;
8392
}
84-
85-
// return false;
86-
}
87-
88-
/**
89-
* From a url string get the hostname
90-
* @param url The Url (http://example.com/foo)
91-
* @return The Hostname (example.com)
92-
*/
93-
public static String getHost(String url) {
94-
if (url == null || url.length() <= 0) {
95-
return "";
96-
}
97-
98-
int start = url.indexOf("//");
99-
if (start == -1) {
100-
return "";
101-
}
102-
start += 2;
103-
104-
int end = url.indexOf('/', start);
105-
if (end == -1) {
106-
return "";
107-
}
108-
109-
return url.substring(start, end);
110-
}
111-
112-
/**
113-
* From a utl string get the port number
114-
* @param url The Url (http://example.com:50/foo)
115-
* @return The Port number (50)
116-
*/
117-
public static int getPort(String url) {
118-
if (url == null || url.length() <= 0) {
119-
return 80;
120-
}
121-
122-
int start = url.indexOf("//");
123-
if (start == -1) {
124-
return 80;
125-
}
126-
127-
start += 2;
128-
129-
int end = url.indexOf('/', start);
130-
if (end == -1) {
131-
return 80;
132-
}
133-
134-
int port = url.indexOf(':', start);
135-
if (port == -1) {
136-
return 80;
137-
}
138-
139-
if (port < end) {
140-
end = port;
141-
}
142-
143-
return Integer.parseInt(url.substring(port, end));
144-
}
145-
146-
/**
147-
* From a url string get the location
148-
* @param url The Url (http://example.com/foo)
149-
* @return The location (/foo)
150-
*/
151-
public static String getLocation(String url) {
152-
if (url == null || url.length() <= 0) {
153-
return "";
154-
}
155-
156-
int start = url.indexOf("//");
157-
if (start == -1) {
158-
return "";
159-
}
160-
start += 2;
161-
162-
int end = url.indexOf('/', start);
163-
if (end == -1) {
164-
return "";
165-
}
166-
167-
return url.substring(end);
16893
}
16994
}

SSHelpers/Util.java

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package SSHelpers;
2+
3+
import java.util.Date;
4+
import java.util.TimeZone;
5+
import java.text.ParseException;
6+
import java.text.SimpleDateFormat;
7+
8+
public class Util {
9+
/**
10+
* From a url string get the host-name
11+
* @param url The Url E.g. http://example.com/foo
12+
* @return The Hostname E.g. example.com
13+
*/
14+
public static String getHost(String url) {
15+
if (url == null || url.length() <= 0) {
16+
return "";
17+
}
18+
19+
int start = url.indexOf("//");
20+
if (start == -1) {
21+
return "";
22+
}
23+
start += 2;
24+
25+
int end = url.indexOf('/', start);
26+
if (end == -1) {
27+
return "";
28+
}
29+
30+
return url.substring(start, end);
31+
}
32+
33+
/**
34+
* From a url string get the port number
35+
* @param url The Url E.g. http://example.com:50/foo
36+
* @return The Port number E.g. 50
37+
*/
38+
public static int getPort(String url) {
39+
if (url == null || url.length() <= 0) {
40+
return 80;
41+
}
42+
43+
int start = url.indexOf("//");
44+
if (start == -1) {
45+
return 80;
46+
}
47+
48+
start += 2;
49+
50+
int end = url.indexOf('/', start);
51+
if (end == -1) {
52+
return 80;
53+
}
54+
55+
int port = url.indexOf(':', start);
56+
if (port == -1) {
57+
return 80;
58+
}
59+
60+
if (port < end) {
61+
end = port;
62+
}
63+
64+
return Integer.parseInt(url.substring(port, end));
65+
}
66+
67+
/**
68+
* From a url string get the location
69+
* @param url The Url E.g. http://example.com/foo
70+
* @return The location E.g. /foo
71+
*/
72+
public static String getLocation(String url) {
73+
if (url == null || url.length() <= 0) {
74+
return "";
75+
}
76+
77+
int start = url.indexOf("//");
78+
if (start == -1) {
79+
return "";
80+
}
81+
start += 2;
82+
83+
int end = url.indexOf('/', start);
84+
if (end == -1) {
85+
return "";
86+
}
87+
88+
return url.substring(end);
89+
}
90+
91+
public static Date toDate(String dateStr, String format) {
92+
try {
93+
SimpleDateFormat inFormat = new SimpleDateFormat(format);
94+
inFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
95+
return inFormat.parse(dateStr);
96+
} catch (ParseException e) {
97+
System.out.println("Parse date error: " + e + " -- at position:" + e.getErrorOffset());
98+
}
99+
return null;
100+
}
101+
102+
public static Date toDate(String dateStr) {
103+
String format = "E, dd MMM yyyy HH:mm:ss z"; // Mon, 13 Jun 2016 21:06:31 GMT
104+
return Util.toDate(dateStr, format);
105+
}
106+
107+
public static String dateToString(Date date, String format) {
108+
SimpleDateFormat dateFormat = new SimpleDateFormat (format);
109+
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
110+
return dateFormat.format(date);
111+
}
112+
113+
public static String dateToString(Date date) {
114+
return Util.dateToString(date, "E, dd MMM yyyy HH:mm:ss z");
115+
}
116+
}

0 commit comments

Comments
 (0)