All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ajax4jsf.framework.resource.ScriptRenderer Maven / Gradle / Ivy

/**
 * Licensed under the Common Development and Distribution License,
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.sun.com/cddl/
 *   
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package org.ajax4jsf.framework.resource;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.ajax4jsf.framework.util.javascript.JSMin;
import org.ajax4jsf.framework.util.message.Messages;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author [email protected] (latest modification by $Author: alexsmirnov $)
 * @version $Revision: 1.7 $ $Date: 2007/01/05 20:24:52 $
 *
 */
public class ScriptRenderer extends OneTimeRenderer {

	private static final String COMPRESS_SCRIPTS_PARAMETER = "org.ajax4jsf.COMPRESS_SCRIPT";
	
	private static final Log _log = LogFactory.getLog(ScriptRenderer.class);

	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.resource.BaseResourceRenderer#getTag()
	 */
	protected String getTag() {
		// TODO Auto-generated method stub
		return "script";
	}

	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.resource.BaseResourceRenderer#getHrefAttr()
	 */
	protected String getHrefAttr() {
		// TODO Auto-generated method stub
		return "src";
	}

	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.resource.BaseResourceRenderer#getCommonAttrs()
	 */
	protected String[][] getCommonAttrs() {
		// TODO Auto-generated method stub
		return new String[][]{{"type",getContentType()}};
	}

	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.resource.ResourceRenderer#getContentType()
	 */
	public String getContentType() {
		// TODO - use configurable encoding ?
		return "text/javascript";
	}

	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.resource.BaseResourceRenderer#send(org.ajax4jsf.framework.resource.InternetResource, org.ajax4jsf.framework.resource.ResourceContext)
	 */
	public int send(InternetResource base, ResourceContext context) throws IOException {
		InputStream in = base.getResourceAsStream(context);
		if (null == in) {
			String message = Messages.getMessage(
					Messages.NO_INPUT_STREAM_ERROR, base.getKey());
			throw new IOException(message);
		}
		OutputStream out = context.getOutputStream();
		// Compress JavaScript output by JSMin ( true by default )
		if( ! "false".equalsIgnoreCase(context.getInitParameter(COMPRESS_SCRIPTS_PARAMETER))){
			CountingOutputStream countingStream = new CountingOutputStream(out);
			JSMin jsmin = new JSMin(in,countingStream);
			try {
				jsmin.jsmin();
			} catch (Exception e) {
				_log.error("Error send script to client for resource "+base.getKey(), e);
			} finally {
				in.close();
				countingStream.flush();
				countingStream.close();
			}
			int written = countingStream.getWritten();
			if(_log.isDebugEnabled()){
				_log.debug("Send "+written+" bytes to client for JavaScript resource "+base.getKey());
			}
			return written;
		} else {
			return sendStream(in, out);
		}
	}

	
}

class CountingOutputStream extends OutputStream {
	private OutputStream outputStream;
	private int written = 0;
	
	CountingOutputStream(OutputStream outputStream) {
		super();
		this.outputStream = outputStream;
	}

	public void close() throws IOException {
		outputStream.close();
	}

	public void flush() throws IOException {
		outputStream.flush();
	}

	public void write(byte[] b, int off, int len) throws IOException {
		outputStream.write(b, off, len);
		written += len;
	}

	public void write(byte[] b) throws IOException {
		outputStream.write(b);
		written += b.length;
	}

	public void write(int b) throws IOException {
		outputStream.write(b);
		written++;
	}

	int getWritten() {
		return written;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy