Show StreamResource.java syntax highlighted
package org.integratedmodelling.thinkcap.basecommands;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.servlet.ServletOutputStream;
import org.integratedmodelling.thinkcap.ThinkcapAction;
import org.integratedmodelling.thinkcap.ThinkcapAnswer;
import org.integratedmodelling.thinkcap.ThinkcapCommand;
import org.integratedmodelling.thinkcap.ThinkcapPlugin;
import org.integratedmodelling.thinklab.command.CommandDeclaration;
import org.integratedmodelling.thinklab.command.CommandPattern;
import org.integratedmodelling.thinklab.exception.ThinklabException;
import org.integratedmodelling.thinklab.KnowledgeManager;
import org.integratedmodelling.thinklab.interfaces.IAction;
/**
* This command finds a resource identified by its name in the plugin area,
* caches it, and outputs it as an HTTP stream with the MIME type implied by the filename extension.
*
* Usage in templates is:
*
* <code>
* <a href="StreamResource.cmd?ref=plugin/resname.ext">....</a>
* </code>
*
* Should work with any resource the MIME type of which can be inferred by JAF from the name: images,
* style sheets, HTML files etc., as long as it has been recognized and cached by ThinkcapPlugin as something
* to make available.
*
* @author Ferdinando Villa
*
*/
public class StreamResource extends CommandPattern {
public class StreamResourceAction extends ThinkcapAction {
@Override
public void execute(ThinkcapCommand command, ThinkcapAnswer value) throws ThinklabException {
String img = command.getArgumentAsString("res");
File f = ThinkcapPlugin.getResource(img);
command.response.setContentType(ThinkcapPlugin.getMimeType(f));
/* retrieve stream from plugin area */
try {
BufferedInputStream input =
new BufferedInputStream(new FileInputStream(f));
ServletOutputStream output = command.getOutputStream();
while (true) {
int data = input.read();
if (data < 0)
break;
output.write(data);
}
output.flush();
} catch (Exception e) {
throw new ThinklabException(e);
}
}
}
@Override
public CommandDeclaration createCommand() throws ThinklabException {
CommandDeclaration ret = new CommandDeclaration("StreamResource", "");
try {
ret.addMandatoryArgument("ref", "",
KnowledgeManager.KM().getTextType().getSemanticType());
} catch (ThinklabException e) {
}
return ret;
}
@Override
public IAction createAction() {
return new StreamResourceAction();
}
}
See more files for this project here