import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletContext;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.portlet.ResourceURL;
/**
* <a href="JSPPortlet.java.html"><b><i>View Source</i></b></a>
*
* @author Brian Wing Shun Chan
*
*/
public class JSPPortlet extends GenericPortlet {
public void init() throws PortletException {
editJSP = getInitParameter("edit-jsp");
helpJSP = getInitParameter("help-jsp");
viewJSP = getInitParameter("view-jsp");
_log.info("JSP portlet initialized");
}
@Override
public void destroy() {
_log.info("JSP portlet destroyed");
super.destroy();
}
public void doEdit(RenderRequest req, RenderResponse res)
throws IOException, PortletException {
if (req.getPreferences() == null) {
super.doEdit(req, res);
}
else {
include(editJSP, req, res);
}
}
public void doHelp(RenderRequest req, RenderResponse res)
throws IOException, PortletException {
_log.info("INFO level log");
forward(helpJSP, req, res);
}
public void doView(RenderRequest req, RenderResponse res)
throws IOException, PortletException {
_log.fine("Debug level log");
_log.warning("WARN level log");
_log.severe("ERROR level log");
_log.info("INFO level log");
String contentType = req.getResponseContentType();
res.setContentType(contentType);
// set portletbridge title if its set.
ResourceBundle bundle = getPortletConfig().getResourceBundle(req
.getLocale());
if (bundle != null) {
String title = null;
try {
title = bundle.getString("javax.portlet.title");
res.setTitle(title);
} catch (Exception e) {
// Ignore MissingResourceException
}
}
res.setProperty(RenderResponse.EXPIRATION_CACHE, "0");
PrintWriter writer = res.getWriter();
writer.write("<div id='"+res.getNamespace()+"'>");
writer.write("<h3><b>Hello World view mode</b></h3>");
writer.write("<p>Portal server:"+getPortletConfig().getPortletContext().getServerInfo()+"</p>");
if(_log.isLoggable(Level.FINE)){
writer.write("<p>Debug log level enabled</p>");
}
if(_log.isLoggable(Level.WARNING)){
writer.write("<p>Warn log level enabled</p>");
}
if(_log.isLoggable(Level.INFO)){
writer.write("<p>Info log level enabled</p>");
}
writeImage(res, writer, "cardemo.jpg");
writeImage(res, writer, "nonexistent.jpg");
writer.write("</div>");
writer.flush();
// throw new PortletException("Test exception");
}
private void writeImage(RenderResponse res, PrintWriter writer, String name) {
writer.write("<span>/images/" + name);
writer.write("<img alt='" + name + "' src='");
String resourceID = "/images/" + name;
ResourceURL resourceURL = res.createResourceURL();
resourceURL.setResourceID(resourceID);
writer.write(resourceURL.toString());
writer.write("'/></span>");
}
public void processAction(ActionRequest req, ActionResponse res)
throws IOException, PortletException {
}
@Override
public void serveResource(ResourceRequest request, ResourceResponse response)
throws PortletException, IOException {
try {
String target = request.getResourceID();
_log.info("Serve resource "+target);
PortletContext portletContext = getPortletConfig()
.getPortletContext();
PortletRequestDispatcher dispatcher = portletContext.getRequestDispatcher(target);
if (null != dispatcher) {
String mimeType = portletContext.getMimeType(target);
if(null == mimeType){
int lastIndexOfSlash = target.lastIndexOf('/');
if(lastIndexOfSlash >=0){
target = target.substring(lastIndexOfSlash+1);
}
int indexOfQuestion = target.indexOf('?');
if(indexOfQuestion >=0){
target = target.substring(0,indexOfQuestion);
}
mimeType = portletContext.getMimeType(target);
}
if(null != mimeType){
response.setContentType(mimeType);
}
IncludeResourceResponse iresponse = new IncludeResourceResponse(response);
dispatcher.include(request, iresponse);
_log.info("Finish to Serve resource " + target);
} else {
_log.severe("No request dispatcher!");
}
} catch (Exception e) {
if (e instanceof PortletException) {
PortletException pe = (PortletException) e;
throw pe;
}
if (e instanceof IOException) {
IOException ie = (IOException) e;
throw ie;
}
throw new PortletException(e);
}
}
protected void include(String path, RenderRequest req, RenderResponse res)
throws IOException, PortletException {
PortletRequestDispatcher prd =
getPortletContext().getRequestDispatcher(path);
if (prd == null) {
_log.severe(path + " is not a valid include");
}
else {
prd.include(req, res);
}
}
protected void forward(String path, RenderRequest req, RenderResponse res)
throws IOException, PortletException {
PortletRequestDispatcher prd =
getPortletContext().getRequestDispatcher(path);
if (prd == null) {
_log.severe(path + " is not a valid include");
}
else {
prd.forward(req, res);
}
}
protected String editJSP;
protected String helpJSP;
protected String viewJSP;
private static Logger _log = Logger.getLogger("org.jboss.portletbridge.HelloPortlet");
}