Show ThinkcapCommand.java syntax highlighted
package org.integratedmodelling.thinkcap;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.HashMap;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.velocity.VelocityContext;
import org.integratedmodelling.thinkcap.widget.ThinkcapWidgetManager;
import org.integratedmodelling.thinklab.command.Command;
import org.integratedmodelling.thinklab.exception.ThinklabException;
import org.integratedmodelling.thinklab.exception.ThinklabIOException;
import org.integratedmodelling.thinklab.interfaces.IPlugin;
import org.integratedmodelling.thinklab.interfaces.ISession;
/**
* Wraps a standard IMA command to provide facilitated access to servlet context and session parameters.
* @author Ferdinando Villa, Ecoinformatics Collaboratory, UVM
*
*/
public class ThinkcapCommand extends Command {
public HttpServletRequest request;
public HttpServletResponse response;
public VelocityContext context;
public ThinkcapSession session;
public PrintWriter writer = null;
public ServletOutputStream outputStream = null;
public ServletContext servletContext;
public ThinkcapCommand(HashMap<String, String> args, HttpServletRequest req, HttpServletResponse resp,
VelocityContext ctx) throws ThinklabException {
super(null, args);
request = req;
response = resp;
session = (ThinkcapSession)request.getSession().getAttribute("thinkcap.session.IMTSession");
context = ctx;
}
public ThinkcapCommand(String mode, HashMap<String, String> args, HttpServletRequest req,
HttpServletResponse resp) throws ThinklabException {
super(mode, args);
request = req;
response = resp;
session = (ThinkcapSession)request.getSession().getAttribute("thinkcap.session.IMTSession");
}
public PrintWriter getPrintWriter() throws ThinklabIOException {
if (writer == null) {
try {
writer = response.getWriter();
} catch (IOException e) {
throw new ThinklabIOException("internal: " + e.getMessage());
}
}
return writer;
}
public ServletOutputStream getOutputStream() throws ThinklabIOException {
if (outputStream == null) {
try {
outputStream = response.getOutputStream();
} catch (IOException e) {
throw new ThinklabIOException("internal: " + e.getMessage());
}
}
return outputStream;
}
/**
* Get the full URL for the passed command, with any additional parameters (pass parameter name
* and value in sequence).
* @param command
* @param parameters
* @return
*/
public URL getURLForCommand(String command, String ... parameters) {
return session.getURLForCommand(command, parameters);
}
/**
* Return the widget manager for this session and page context, creating it if necessary.
* @return
*/
public ThinkcapWidgetManager getWidgetManager() {
return session.getWidgetManager();
}
/**
* Get the current HTTP session.
* @return the current HTTP session, dummy.
*/
public HttpSession getHttpSession() {
return request.getSession();
}
/**
* Return the IMT session associated with the command.
* @return the current IMT session. Can't be null, or at least should never be null.
* @see org.integratedmodelling.ima.core.ISession
*/
public ISession getSession() {
return session;
}
public PageSpecs getPageSpecs() {
return (PageSpecs)context.get("pageSpecs");
}
/**
* Log a string wherever logging is done
* @param txt text to be logged on the container's log.
*/
public void log(String txt) {
servletContext.log(txt);
}
public ThinkcapManager getKnowledgeManager() {
return ThinkcapManager.getThinkcapManager();
}
public ThinkCap getThinkcap() {
return ThinkcapManager.getThinkcapManager().getThinkcap();
}
public IPlugin getPlugin(String string) {
return getKnowledgeManager().getPluginRegistry().retrievePlugin(string);
}
}
See more files for this project here