I've written a configurable ServletInvokerPortlet that invokes
servlets/JSPs.
(It uses the workaround for embedding JSPs in ECS trees recently posted
by Christian Sell, see EcsServletElement.)

Maybe this is useful for others as well:

The URL of the servlet to be invoked can be set by specifying a parameter
named "url" in the portlet entry in the portlet registry section in the
file "jetspeed-config.jcfg":

---- snip ----
<portlet-entry type="abstract" name="ServletInvokerPortlet">

<classname>org.apache.jetspeed.portal.portlets.ServletInvokerPortlet</classname>
</portlet-entry>

<portlet-entry type="ref" parent="ServletInvokerPortlet"
name="RequestInfoPortlet">
  <parameter name="url" value="/RequestInfo/"/>
  <meta-info>
    <title>Request Info</title>
    <description>This portlet shows some request info.</description>
  </meta-info>
</portlet-entry>
---- snap ----

The example defines a portlet named "RequestInfoPortlet" that invokes a
servlet
accessible under the URL "/RequestInfo/" whenever it is displayed.

The entry in default.psml or a personal psml file may look like this:
...
       <entry type="ref" parent="RequestInfoPortlet">
         <layout position="1"/>
       </entry>
...

This is the source for the ServletInvokerPortlet:

---- snip ----
package org.apache.jetspeed.portal.portlets;

import org.apache.ecs.ConcreteElement;
import org.apache.ecs.ElementContainer;
import org.apache.ecs.StringElement;

import org.apache.jetspeed.portal.*;
import org.apache.jetspeed.util.*;

import org.apache.turbine.util.*;

import org.apache.jetspeed.portal.portlets.AbstractPortlet;
import org.apache.jetspeed.util.servlet.EcsServletElement;

/**
 * The ServletInvokerPortlet invokes a servlet or JSP and displays the
result.
 *
 * @author Thomas Schaeck ([EMAIL PROTECTED])
 */
public class ServletInvokerPortlet extends AbstractPortlet {

  /**
   * Returns an ECS concrete element that includes the servlet/JSP.
   *
   * The servlet/JSP will be invoked when the ECS tree is written
   * to the servlet output stream and add its output to the stream.
   */
  public ConcreteElement getContent() {
    // ------------------------------------------------------------------
    // Note: Rundata should not be obtained from the portlet config,
    // but I found no other way to get it in the current JetSpeed version
    // (as of 10/30/200). Normally, this should be per-request info, not
    // per-portlet config info. If a new request comes in before this code
    // is reached and the rundata in the portlet config is overwritten,
    // we might get the wrong request here.
    // ------------------------------------------------------------------
    RunData rundata = this.getPortletConfig().getRunData();
    PortletConfig pc = this.getPortletConfig();

    String servletURL = null;
    try {
      servletURL = (String)
this.getPortletConfig().getInitParameter("url");
      return new EcsServletElement(rundata, servletURL);
    } catch (Exception e) {
      String message = "ServletInvokerPortlet: Error invoking "
                       + servletURL + ": " + e.getMessage();
      Log.error(message, e);
      return new StringElement(message);
    }
  }
}
---- snap ----

Here is a modified version of the code originally posted by Christian Sell.

---- snip ----
package org.apache.jetspeed.util.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import javax.servlet.*;

import org.apache.turbine.util.RunData;
import org.apache.turbine.util.Log;
import org.apache.ecs.ConcreteElement;
import org.apache.ecs.Element;

/**
 * EcsServletElement encapsulates a servlet/JSP within the context of ECS
 * HTML-generation.
 *
 * This is a workaround to allow invoking servlets from JetSpeed Portlets.
 * The servlet will be invoked when traversal of an ECS tree during writing
 * reaches the EcsServlet element.
 *
 * This is a modified version of Christian Sell's original code.
 */
public class EcsServletElement extends ConcreteElement
{
   /** RunData object to obtain HttpServletRequest/Response from. */
   private RunData rundata;

   /** URL of servlet to include */
   private String url;

   /**
    * Construct an ECS element from a given rundata object and URL.
    *
    * @param rundata Rundata object that holds the
HttpServletRequest/Response
    *                objects to be used for servlet invocation.
    * @param url     The URL of the servlet to invoke.
    */
   public EcsServletElement(RunData rundata, String urlString) {
      this.url = urlString;
      this.rundata = rundata;
   }

   public void output(OutputStream out) {
      output(new PrintWriter(out));
   }

   /**
    * Add element to the designated PrintWriter.
    */
   public void output(PrintWriter out) {
      ServletContext ctx = rundata.getServletContext();
      RequestDispatcher dispatcher = ctx.getRequestDispatcher(url);
      try {
         // Include the servlet or JSP.
         dispatcher.include(rundata.getRequest(),rundata.getResponse());
      } catch (Exception e) {
         String message = "JSPPortlet: Could not include the following URL:
"
         + url + " : " + e.getMessage();
         Log.error( message, e );
         out.print(message);
      }
   }
}
---- snap ----

This is the example servlet I used. It prints some of the request info in a
table.

---- snip ----
import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 * This example servlet returns some information about the incoming
request.
 *
 * @author Thomas Schaeck ([EMAIL PROTECTED])
 */
public class RequestInfoServlet extends HttpServlet
{
  public void doGet (HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException {
    PrintWriter out;

    res.setContentType("text/html");
    out = res.getWriter ();

    out.println("<HTML><HEAD><TITLE>Example Servlet</TITLE>" +
"</HEAD><BODY BGCOLOR=\"#FFFFEE\">");
    out.println("<h4>Request Information:</h4>");
    out.println("<TABLE Border=\"2\" WIDTH=\"65%\" BGCOLOR=\"#DDDDFF\">");
    out.println("<tr><td>Remote user</td><td>" + req.getRemoteUser() +
"</td></tr>");
    out.println("<tr><td>Remote address</td><td>" + req.getRemoteAddr() +
"</td></tr>");
    out.println("<tr><td>Remote host</td><td>" + req.getRemoteHost() +
"</td></tr>");
    out.println("</table><BR><BR>");

    Enumeration e = req.getHeaderNames();
    if (e.hasMoreElements()) {
      out.println("<h4>Request headers:</h4>");
      out.println("<TABLE Border=\"2\" WIDTH=\"65%\"
BGCOLOR=\"#DDDDFF\">");
      while (e.hasMoreElements()) {
        String name = (String)e.nextElement();
        out.println("<tr><td>" + name + "</td><td>" + req.getHeader(name) +
"</td></tr>");
      }
      out.println("</table><BR><BR>");
    }

    e = req.getParameterNames();
    if (e.hasMoreElements()) {
      out.println("<h4>Servlet parameters:</h4>");
      out.println("<TABLE Border=\"2\" WIDTH=\"65%\"
BGCOLOR=\"#DDDDFF\">");
      while (e.hasMoreElements()) {
        String name = (String)e.nextElement();
        out.println("<tr><td>" + name + "</td><td>" +
req.getParameter(name) + "</td></tr>");
      }
      out.println("</table><BR><BR>");
    }

    HttpSession session = req.getSession(false);
    if (session != null) {
      out.println("<h4>Session information:</h4>");
      out.println("<TABLE Border=\"2\" WIDTH=\"65%\"
BGCOLOR=\"#DDDDFF\">");
      out.println("<tr><td>Session ID</td><td>" + session.getId());
      out.println("<tr><td>Last accessed time</td><td>" + new
Date(session.getLastAccessedTime()).toString() + "</td></tr>");
      out.println("<tr><td>Creation time</td><td>" +  new
Date(session.getCreationTime()).toString() + "</td></tr>");
      String mechanism = "unknown";
      if (req.isRequestedSessionIdFromCookie()) {
        mechanism = "cookie";
      } else if (req.isRequestedSessionIdFromURL()) {
        mechanism = "url-encoding";
      }
      out.println("Session-tracking mechanism" + mechanism);
      out.println("</table><BR><BR>");

      String[] vals = session.getValueNames();
      if (vals != null)  {
        out.println("<h4>Session values</h4>");
        out.println("<TABLE Border=\"2\" WIDTH=\"65%\"
BGCOLOR=\"#DDDDFF\">");
        for (int i=0; i<vals.length; i++) {
          String name = vals[i];
          out.println("<tr><td>" + name + "</td><td>" +
session.getValue(name) + "</td></tr>");
        }
        out.println("</table><BR><BR>");
      }
    }
    out.println("</body></html>");
  }
---- snap ----

Best regards,

Thomas

Thomas Schaeck
IBM Pervasive Computing Division
e-mail: [EMAIL PROTECTED]
Address: IBM Deutschland Entwicklung GmbH,
Schoenaicher Str. 220, 71032 Boeblingen, Germany




--
--------------------------------------------------------------
Please read the FAQ! <http://java.apache.org/faq/>
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Archives and Other:  <http://java.apache.org/main/mail.html>
Problems?:           [EMAIL PROTECTED]

Reply via email to