View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
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   * Utility class to write external file with cache enable properties
46   * 
47   * @author Frederic Bregier
48   *
49   */
50  public class HttpWriteCacheEnable {
51  
52      /**
53       * Write a file, taking into account cache enabled and removing session cookie
54       * @param request
55       * @param channel
56       * @param filename
57       * @param cookieNameToRemove
58       */
59      public static void writeFile(HttpRequest request, Channel channel, String filename, String cookieNameToRemove) {
60          // Convert the response content to a ChannelBuffer.
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          // Build the response object.
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);//1 year
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         // Write the response.
103         channel.write(response);
104     }
105     /**
106      * Remove the given named cookie
107      * @param request
108      * @param response
109      * @param cookieNameToRemove
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                 // Reset the sessions if necessary.
118                 CookieEncoder cookieEncoder = new CookieEncoder(true);
119                 // Remove all Session for images
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 }