001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.webrender.service;
031:
032: import java.io.IOException;
033:
034: import nextapp.echo2.webrender.Connection;
035: import nextapp.echo2.webrender.Service;
036: import nextapp.echo2.webrender.util.GZipCompressor;
037: import nextapp.echo2.webrender.util.JavaScriptCompressor;
038: import nextapp.echo2.webrender.util.Resource;
039:
040: /**
041: * A service which renders <code>JavaScript</code> resource files.
042: */
043: public class JavaScriptService implements Service {
044:
045: /**
046: * Creates a new <code>JavaScript</code> service from the specified
047: * resource in the <code>CLASSPATH</code>.
048: *
049: * @param id the <code>Service</code> id
050: * @param resourceName the <code>CLASSPATH</code> resource name containing
051: * the JavaScript content
052: * @return the created <code>JavaScriptService</code>
053: */
054: public static JavaScriptService forResource(String id,
055: String resourceName) {
056: String content = Resource.getResourceAsString(resourceName);
057: return new JavaScriptService(id, content);
058: }
059:
060: /** <code>Service</code> identifier. */
061: private String id;
062:
063: /** The JavaScript content in plain text. */
064: private String content;
065:
066: /** The JavaScript content in GZip compressed form. */
067: private byte[] gzipContent;
068:
069: /**
070: * Creates a new <code>JavaScriptService</code>.
071: *
072: * @param id the <code>Service</code> id
073: * @param content the <code>JavaScript content</code>
074: */
075: public JavaScriptService(String id, String content) {
076: super ();
077: this .id = id;
078: this .content = JavaScriptCompressor.compress(content);
079: try {
080: gzipContent = GZipCompressor.compress(this .content);
081: } catch (IOException ex) {
082: // Should not occur.
083: throw new RuntimeException(
084: "Exception compressing JavaScript source.", ex);
085: }
086: }
087:
088: /**
089: * @see nextapp.echo2.webrender.Service#getId()
090: */
091: public String getId() {
092: return id;
093: }
094:
095: /**
096: * <code>DO_NOT_CACHE</code> is returned for <code>JavaScript</code>
097: * to avoid possibility of ever running out-of-date JavaScript in the
098: * event an application is updated and redeployed.
099: *
100: * @see nextapp.echo2.webrender.Service#getVersion()
101: */
102: public int getVersion() {
103: return DO_NOT_CACHE;
104: }
105:
106: /**
107: * @see nextapp.echo2.webrender.Service#service(nextapp.echo2.webrender.Connection)
108: */
109: public void service(Connection conn) throws IOException {
110: String 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 acceptEncoding = conn.getRequest().getHeader(
120: "accept-encoding");
121: if (acceptEncoding != null
122: && acceptEncoding.indexOf("gzip") != -1) {
123: serviceGZipCompressed(conn);
124: } else {
125: servicePlain(conn);
126: }
127: }
128: }
129:
130: /**
131: * Renders the JavaScript resource using GZip encoding.
132: *
133: * @param conn the relevant <code>Connection</code>
134: */
135: private void serviceGZipCompressed(Connection conn)
136: throws IOException {
137: conn.getResponse().setContentType("text/plain");
138: conn.getResponse().setHeader("Content-Encoding", "gzip");
139: conn.getOutputStream().write(gzipContent);
140: }
141:
142: /**
143: * Renders the JavaScript resource WITHOUT using GZip encoding.
144: *
145: * @param conn the relevant <code>Connection</code>
146: */
147: private void servicePlain(Connection conn) throws IOException {
148: conn.getResponse().setContentType("text/plain");
149: conn.getWriter().print(content);
150: }
151: }
|