I love the fact that JDK comes with a ScriptEngine. It’s so flexible when you want to evaluate and troubleshoot your application that’s already deployed in an server environment. Add this REST endpoint into a Java EE app, and it will give you instant access to internal states of the app.
package myrestapp;
import java.io.StringReader;
import java.util.logging.Logger;
import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
/**
* Give instant access to your internal application with dynamic scripting.
*
* <p>Example script:
* <pre>
* "sc" + servletContext + ", req=" + request;
* </pre>
*
* <p>Example2
* <pre>
* names = servletContext.getAttributeNames();
* while(names.hasMoreElements()) {
* name = names.nextElement();
* println(name);
* }
* </pre>
*/
@Path("script")
public class ScriptResource {
private static final Logger logger = Logger.getLogger(ScriptResource.class.getName());
@Context
private ServletContext servletContext;
@POST
public String script(@Context HttpServletRequest request, String scriptText) throws Exception {
String engineName = "JavaScript";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName(engineName);
logger.info("Running script text length=" + scriptText.length() + ", engine=" + engine);
Object result = null;
try (StringReader reader = new StringReader(scriptText)) {
Bindings bindings = engine.createBindings();
bindings.put("servletContext", servletContext);
bindings.put("request", request);
result = engine.eval(reader, bindings);
}
logger.info("Result " + result);
return "" + result;
}
}Notice that I gave couple JavaScript examples in the comment area already. You will have access to two binding variables that should give you full access to many internal components of your application.
Need an UI to test out this endpoint? How about give the “Advance Rest Client” Chrome Extension a try? (Thanks to my co-worker’s Chris Griffin tips on this cool extension. It’s really handy tool to have!).
| Reference: | Poking around your REST application with a scriptable endpoint from our JCG partner Zemian Deng at the A Programmer’s Journal blog. |
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Thank you!
We will contact you soon.
👁 Photo of Zemian Deng
Zemian DengOctober 21st, 2014Last Updated: October 20th, 2014
Zemian DengOctober 21st, 2014Last Updated: October 20th, 2014
0 93 1 minute read

This site uses Akismet to reduce spam. Learn how your comment data is processed.