com.github.marceloemanoel.vraptor.jscontroller.web.JsController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vraptor-js-controller Show documentation
Show all versions of vraptor-js-controller Show documentation
vraptor-js-controller helps interaction through ajax
The newest version!
package com.github.marceloemanoel.vraptor.jscontroller.web;
import org.apache.tools.ant.filters.StringInputStream;
import com.github.marceloemanoel.vraptor.jscontroller.Controller;
import com.github.marceloemanoel.vraptor.jscontroller.generators.JsGenerator;
import com.github.marceloemanoel.vraptor.jscontroller.generators.velocity.MinifiedJsGenerator;
import com.github.marceloemanoel.vraptor.jscontroller.vraptor.VRaptorControllerDiscover;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.interceptor.download.Download;
import br.com.caelum.vraptor.interceptor.download.InputStreamDownload;
/**
* This controller generates all the javascript needed to use the other controllers via javascript.
* It should be injected on the pages.
* @author marceloemanoel
*/
@Resource
@Path("/js")
public class JsController {
private static final String JS_EXTENSION = ".js";
private static final String MINIFIED_JS_EXTENSION = "-min" + JS_EXTENSION;
private static final String CONTENT_TYPE = "text/javascript; charset=UTF-8";
private Result result;
private VRaptorControllerDiscover discover;
private JsGenerator generator;
public JsController(Result result, VRaptorControllerDiscover discover, JsGenerator generator) {
this.result = result;
this.discover = discover;
this.generator = generator;
}
@Get
@Path("/{controllerName}")
public Download controller(String controllerName){
Controller controller = discover.find(controllerName);
if(controller == null){
result.notFound();
return null;
}
return download(generator.generate(controller), controller.getName()+JS_EXTENSION);
}
private Download download(String javascript, String fileName) {
StringInputStream inputstream = new StringInputStream(javascript);
return new InputStreamDownload(inputstream, CONTENT_TYPE, fileName, false, javascript.length());
}
@Get
@Path("/min/{controllerName}")
public Download minifiedController(String controllerName){
Controller controller = discover.find(controllerName);
if(controller == null){
result.notFound();
return null;
}
MinifiedJsGenerator minifiedJsGenerator = new MinifiedJsGenerator(generator);
return download(minifiedJsGenerator.generate(controller), controller.getName()+MINIFIED_JS_EXTENSION);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy