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

com.github.searls.jasmine.coffee.HandlesRequestsForCoffee Maven / Gradle / Ivy

Go to download

A JavaScript unit test plugin that processes JavaScript sources and Jasmine specs, prepares test runner HTML files, executes Jasmine specs headlessly with HtmlUnit, and produces JUnit XML reports

There is a newer version: 1.1.2
Show newest version
package com.github.searls.jasmine.coffee;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.http.HttpHeaders;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.util.resource.Resource;

import com.github.searls.jasmine.format.BuildsJavaScriptToWriteFailureHtml;

public class HandlesRequestsForCoffee {

	private CoffeeScript coffeeScript = new CoffeeScript();
	private BuildsJavaScriptToWriteFailureHtml buildsJavaScriptToWriteFailureHtml = new BuildsJavaScriptToWriteFailureHtml(); 
	
	public void handle(Request baseRequest, HttpServletResponse response, Resource resource) throws IOException {
		baseRequest.setHandled(true);
		String javascript = compileCoffee(resource);
		setHeaders(response, resource, javascript);
		writeResponse(response, javascript);
	}

	private void writeResponse(HttpServletResponse response, String javascript) throws IOException {
		response.getWriter().write(javascript);
	}

	private void setHeaders(HttpServletResponse response, Resource resource, String javascript) {
		response.setContentType("text/javascript");
		response.setDateHeader(HttpHeaders.LAST_MODIFIED, resource.lastModified());
		response.setHeader(HttpHeaders.CONTENT_LENGTH,Integer.toString(javascript.length()));
	}

	private String compileCoffee(Resource resource) {
		try {
			return coffeeScript.compile(IOUtils.toString(resource.getInputStream()));
		} catch(Exception e) {
			return buildsJavaScriptToWriteFailureHtml.build("CoffeeScript Error: failed to compile "+resource.getName()+". 
Error message:

"+e.getMessage()+""); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy