KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > webrender > service > JavaScriptService


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.webrender.service;
31
32 import java.io.IOException JavaDoc;
33
34 import nextapp.echo2.webrender.Connection;
35 import nextapp.echo2.webrender.Service;
36 import nextapp.echo2.webrender.util.GZipCompressor;
37 import nextapp.echo2.webrender.util.JavaScriptCompressor;
38 import nextapp.echo2.webrender.util.Resource;
39
40 /**
41  * A service which renders <code>JavaScript</code> resource files.
42  */

43 public class JavaScriptService
44 implements Service {
45     
46     /**
47      * Creates a new <code>JavaScript</code> service from the specified
48      * resource in the <code>CLASSPATH</code>.
49      *
50      * @param id the <code>Service</code> id
51      * @param resourceName the <code>CLASSPATH</code> resource name containing
52      * the JavaScript content
53      * @return the created <code>JavaScriptService</code>
54      */

55     public static JavaScriptService forResource(String JavaDoc id, String JavaDoc resourceName) {
56         String JavaDoc content = Resource.getResourceAsString(resourceName);
57         return new JavaScriptService(id, content);
58     }
59
60     /** <code>Service</code> identifier. */
61     private String JavaDoc id;
62     
63     /** The JavaScript content in plain text. */
64     private String JavaDoc content;
65     
66     /** The JavaScript content in GZip compressed form. */
67     private byte[] gzipContent;
68     
69     /**
70      * Creates a new <code>JavaScriptService</code>.
71      *
72      * @param id the <code>Service</code> id
73      * @param content the <code>JavaScript content</code>
74      */

75     public JavaScriptService(String JavaDoc id, String JavaDoc content) {
76         super();
77         this.id = id;
78         this.content = JavaScriptCompressor.compress(content);
79         try {
80             gzipContent = GZipCompressor.compress(this.content);
81         } catch (IOException JavaDoc ex) {
82             // Should not occur.
83
throw new RuntimeException JavaDoc("Exception compressing JavaScript source.", ex);
84         }
85     }
86     
87     /**
88      * @see nextapp.echo2.webrender.Service#getId()
89      */

90     public String JavaDoc getId() {
91         return id;
92     }
93     
94     /**
95      * <code>DO_NOT_CACHE</code> is returned for <code>JavaScript</code>
96      * to avoid possibility of ever running out-of-date JavaScript in the
97      * event an application is updated and redeployed.
98      *
99      * @see nextapp.echo2.webrender.Service#getVersion()
100      */

101     public int getVersion() {
102         return DO_NOT_CACHE;
103     }
104     
105     /**
106      * @see nextapp.echo2.webrender.Service#service(nextapp.echo2.webrender.Connection)
107      */

108     public void service(Connection conn)
109     throws IOException JavaDoc {
110         String JavaDoc userAgent = conn.getRequest().getHeader("user-agent");
111         if (userAgent == null || userAgent.indexOf("MSIE") != -1) {
112             // Due to behavior detailed Microsoft Knowledge Base Article Id 312496,
113
// all HTTP compression support is disabled for this browser.
114
// Due to the fact that ClientProperties information is not necessarily
115
// available at this stage, browsers which provide deceitful user-agent
116
// headers will also be affected.
117
servicePlain(conn);
118         } else {
119             String JavaDoc acceptEncoding = conn.getRequest().getHeader("accept-encoding");
120             if (acceptEncoding != null && acceptEncoding.indexOf("gzip") != -1) {
121                 serviceGZipCompressed(conn);
122             } else {
123                 servicePlain(conn);
124             }
125         }
126     }
127     
128     /**
129      * Renders the JavaScript resource using GZip encoding.
130      *
131      * @param conn the relevant <code>Connection</code>
132      */

133     private void serviceGZipCompressed(Connection conn)
134     throws IOException JavaDoc {
135         conn.getResponse().setContentType("text/plain");
136         conn.getResponse().setHeader("Content-Encoding", "gzip");
137         conn.getOutputStream().write(gzipContent);
138     }
139     
140     /**
141      * Renders the JavaScript resource WITHOUT using GZip encoding.
142      *
143      * @param conn the relevant <code>Connection</code>
144      */

145     private void servicePlain(Connection conn)
146     throws IOException JavaDoc {
147         conn.getResponse().setContentType("text/plain");
148         conn.getWriter().print(content);
149     }
150 }
151
Popular Tags