1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package openr66.protocol.http;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Set;
29
30 import org.jboss.netty.buffer.ChannelBuffer;
31 import org.jboss.netty.buffer.ChannelBuffers;
32 import org.jboss.netty.channel.Channel;
33 import org.jboss.netty.handler.codec.http.Cookie;
34 import org.jboss.netty.handler.codec.http.CookieDecoder;
35 import org.jboss.netty.handler.codec.http.CookieEncoder;
36 import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
37 import org.jboss.netty.handler.codec.http.HttpHeaders;
38 import org.jboss.netty.handler.codec.http.HttpRequest;
39 import org.jboss.netty.handler.codec.http.HttpResponse;
40 import org.jboss.netty.handler.codec.http.HttpResponseStatus;
41 import org.jboss.netty.handler.codec.http.HttpVersion;
42
43
44
45
46
47
48
49
50 public class HttpWriteCacheEnable {
51
52
53
54
55
56
57
58
59 public static void writeFile(HttpRequest request, Channel channel, String filename, String cookieNameToRemove) {
60
61 HttpResponse response;
62 if (request.containsHeader(HttpHeaders.Names.IF_MODIFIED_SINCE)) {
63 response = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
64 HttpResponseStatus.NOT_MODIFIED);
65 handleCookies(request, response, cookieNameToRemove);
66 channel.write(response);
67 return;
68 }
69 File file = new File(filename);
70 byte [] bytes = new byte[(int) file.length()];
71 FileInputStream fileInputStream;
72 try {
73 fileInputStream = new FileInputStream(file);
74 } catch (FileNotFoundException e) {
75 response = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
76 HttpResponseStatus.NOT_FOUND);
77 handleCookies(request, response, cookieNameToRemove);
78 channel.write(response);
79 return;
80 }
81 try {
82 fileInputStream.read(bytes);
83 } catch (IOException e) {
84 response = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
85 HttpResponseStatus.NOT_FOUND);
86 handleCookies(request, response, cookieNameToRemove);
87 channel.write(response);
88 return;
89 }
90 ChannelBuffer buf = ChannelBuffers.copiedBuffer(bytes);
91
92 response = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
93 HttpResponseStatus.OK);
94 response.setContent(buf);
95 response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buf.readableBytes()));
96 ArrayList<String> cache_control = new ArrayList<String>(2);
97 cache_control.add(HttpHeaders.Values.PUBLIC);
98 cache_control.add(HttpHeaders.Values.MAX_AGE+"="+31536000);
99 response.setHeader(HttpHeaders.Names.CACHE_CONTROL, cache_control);
100 response.setHeader(HttpHeaders.Names.LAST_MODIFIED, "Tue, 15 Nov 1994 12:45:26 GMT");
101 handleCookies(request, response, cookieNameToRemove);
102
103 channel.write(response);
104 }
105
106
107
108
109
110
111 public static void handleCookies(HttpRequest request, HttpResponse response, String cookieNameToRemove) {
112 String cookieString = request.getHeader(HttpHeaders.Names.COOKIE);
113 if (cookieString != null) {
114 CookieDecoder cookieDecoder = new CookieDecoder();
115 Set<Cookie> cookies = cookieDecoder.decode(cookieString);
116 if(!cookies.isEmpty()) {
117
118 CookieEncoder cookieEncoder = new CookieEncoder(true);
119
120 for (Cookie cookie : cookies) {
121 if (cookie.getName().equalsIgnoreCase(cookieNameToRemove)) {
122 } else {
123 cookieEncoder.addCookie(cookie);
124 response.addHeader(HttpHeaders.Names.SET_COOKIE, cookieEncoder.encode());
125 cookieEncoder = new CookieEncoder(true);
126 }
127 }
128 }
129 }
130 }
131
132 }