Re: Source Code for Configurable ServletInvokerPortlet

2000-11-02 Thread SCHAECK
t.println("TABLE Border=\"2\" WIDTH=\"65%\"
BGCOLOR=\"#FF\"");
for (int i=0; ivals.length; i++) {
  String name = vals[i];
  out.println("trtd" + name + "/tdtd" +
session.getValue(name) + "/td/tr");
}
out.println("/tableBRBR");
  }
}
// Uncomment this tp obtain a valid HTML document
// out.println("/body/html");
  }

  public void generateWML(HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException {
PrintWriter out;

res.setContentType("text/vnd.wap.wml");
out = res.getWriter ();

// Uncomment these lines to obtain a valid WML document instead of a
fragment
// out.println("?xml version=\"1.0\"?");
// out.println("!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\"
\"http://www.wapforum.org/DTD/wml_1.1.xml\"");
// out.println("wml");
// out.println("card id=\"RequestInfo\" title=\"Request Info\"");

out.println("pbRequest Information/b/p");
out.println("pRemote user: " + req.getRemoteUser() + "/p");
out.println("pRemote address: " + req.getRemoteAddr() + "/p");
out.println("pRemote host: " + req.getRemoteHost() + "/p");

Enumeration e = req.getHeaderNames();
if (e.hasMoreElements()) {
  out.println("pbRequest headers/b/p");
  while (e.hasMoreElements()) {
String name = (String)e.nextElement();
out.println("p" + name + ": " + req.getHeader(name) + "/p");
  }
}

e = req.getParameterNames();
if (e.hasMoreElements()) {
  out.println("pbServlet parameters/b/p");
  while (e.hasMoreElements()) {
String name = (String)e.nextElement();
out.println("p" + name + ": "+ req.getParameter(name) + "/p");
  }
}

HttpSession session = req.getSession(false);
if (session != null) {
  out.println("pbSession Info/b/p");
  out.println("pSession ID" + ": "+ session.getId() + "/p");
  out.println("pLast accessed time" + ": "+ new
Date(session.getLastAccessedTime()).toString() + "/p");
  out.println("Creation time" + ": "+  new
Date(session.getCreationTime()).toString() + "/p");
  String mechanism = "unknown";
  if (req.isRequestedSessionIdFromCookie()) {
mechanism = "cookie";
  } else if (req.isRequestedSessionIdFromURL()) {
mechanism = "url-encoding";
  }
  out.println("pbSession-Tracking" + mechanism + "/b/p");

  String[] vals = session.getValueNames();
  if (vals != null)  {
out.println("pSession values/p");
for (int i=0; ivals.length; i++) {
  String name = vals[i];
  out.println("p" + name + ": "+ session.getValue(name) +
"/p");
}
  }
}

// Uncomment these lines to obtain a valid WML document instead of a
fragment
//out.println("/card/wml");
  }
}

 snap 

Best regards,

Thomas

Thomas Schaeck
IBM Pervasive Computing Division
Phone: +49-(0)7031-16-3479 e-mail: [EMAIL PROTECTED]
Address: IBM Deutschland Entwicklung GmbH,
Schoenaicher Str. 220, 71032 Boeblingen, Germany


Santiago Gala [EMAIL PROTECTED] on 30.10.2000 22:47:53

Please respond to "JetSpeed" [EMAIL PROTECTED]

To:   JetSpeed [EMAIL PROTECTED]
cc:
Subject:  Re: Source Code for Configurable ServletInvokerPortlet






[EMAIL PROTECTED] wrote:

 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"


classnameorg.apache.jetspeed.portal.portlets.ServletInvokerPortlet/classname

 /portlet-entry

 portlet-entry type="ref" parent="ServletInvokerPortlet"
 name="RequestInfoPortlet"
   parameter name="url" value="/RequestInfo/"/
   meta-info
 titleRequest Info/title
 descriptionThis 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 UR

Re: Source Code for Configurable ServletInvokerPortlet

2000-11-02 Thread SCHAECK




Santiago,

thanks for fixing the little second/millisecond bug so quickly.

+1 on having this kind of examples for helping to handle legacy webapps.

But the servlet should not be printing HTML, HEAD, and BODY tags, just the
HTML
fragments. Also, it should possibly check the getContentType() call, and
deliver
HTML or WML as requested by the Portlet. But this last thing is not that
important,
as the current psml allows to specify mime types in the registry.

Extending the request info servlet to also support WML is a nice idea, so I
did it
(see below). I also commented out the surrounding tags so that the servlet
now returns fragments suitable for inclusion in portlets.

Also, we should point people using it that it is not the way to go for new
development, just a way to get this servlet you had written before
displayed in a
portlet.

Right, usually content should be rendered by the portlet, e.g. by invoking
a JSP or
executing a stylesheet. We are currently also working on support of
JSPs/servlets
through the Portlet API, we plan to submit a proposal for discussion soon.

 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 {
 if (req.getHeader("accept").indexOf("text/vnd.wap.wml") != -1) {
   generateWML(req, res);

   // add additional content types here
 } else {
  // default to HTML
  generateHTML(req, res);
 }
  }

  public void generateHTML(HttpServletRequest req, HttpServletResponse res)

  throws ServletException, IOException {
PrintWriter out;

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

// Uncomment this tp obtain a valid HTML document
//out.println("HTMLHEADTITLERequest Info
Servlet/TITLE/HEADBODY BGCOLOR=\"#EE\"");

out.println("h4Request Information:/h4");
out.println("TABLE Border=\"2\" WIDTH=\"65%\" BGCOLOR=\"#FF\"");
out.println("trtdRemote user/tdtd" + req.getRemoteUser() +
"/td/tr");
out.println("trtdRemote address/tdtd" + req.getRemoteAddr() +
"/td/tr");
out.println("trtdRemote host/tdtd" + req.getRemoteHost() +
"/td/tr");
out.println("/tableBRBR");

Enumeration e = req.getHeaderNames();
if (e.hasMoreElements()) {
  out.println("h4Request headers:/h4");
  out.println("TABLE Border=\"2\" WIDTH=\"65%\"
BGCOLOR=\"#FF\"");
  while (e.hasMoreElements()) {
String name = (String)e.nextElement();
out.println("trtd" + name + "/tdtd" + req.getHeader(name) +
 "/td/tr");
  }
  out.println("/tableBRBR");
}

e = req.getParameterNames();
if (e.hasMoreElements()) {
  out.println("h4Servlet parameters:/h4");
  out.println("TABLE Border=\"2\" WIDTH=\"65%\"
BGCOLOR=\"#FF\"");
  while (e.hasMoreElements()) {
String name = (String)e.nextElement();
out.println("trtd" + name + "/tdtd" +
req.getParameter(name) + "/td/tr");
  }
  out.println("/tableBRBR");
}

HttpSession session = req.getSession(false);
if (session != null) {
  out.println("h4Session information:/h4");
  out.println("TABLE Border=\"2\" WIDTH=\"65%\"
BGCOLOR=\"#FF\"");
  out.println("trtdSession ID/tdtd" + session.getId());
  out.println("trtdLast accessed time/tdtd" + new
Date(session.getLastAccessedTime()).toString() + "/td/tr");
  out.println("trtdCreation time/tdtd" +  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("/tableBRBR");

  String[] vals = session.getValueNames();
  if (vals != null)  {
out.println("h4Session values/h4");
out.println("TABLE Border=\"2\" WIDTH=\"65%\"
BGCOLOR=\"#FF\"");
for (int i=0; ivals.length; i++) {
  String name = vals[i];
  out.println("trtd" + name + "/tdtd" +
session.getValue(name) + "/td/tr");
}
out.println("/tableBRBR");
  }
}
// Uncomment this tp obtain a valid HTML document
// out.println("/body/html");
  }

  public void generateWML(HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException {
PrintWriter out;

res.setContentType("text/vnd.wap.wml");
out = res.getWriter ();

// Uncomment these lines to obtain a valid WML document instead of a
fragment
// out.println("?xml version=\"1.0\"?");
// out.println("!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\"

Re: Source Code for Configurable ServletInvokerPortlet

2000-11-02 Thread sbelt

Sometime back (a few months ago, i believe) I sent code to Jon which did the
same thing. It never made it into the distribution - perhaps there were bugs
in it, or perhaps it was not well written.

But I can tell you some of what I learned in the process:

- your hrefs=, src=, etc tags need to be updated to include absolute URLS
for links, images, etc... to still function. My version included code to
parse the HTML and update these parameters automatically.
- Any javascript in your legacy page will possibly break when displaed as a
portlet.
- IE is forgiving about html tags (for example, you can have a TD tag
without a /TD tag). This sometimes causes problems in a portlet-version.

If you are interested, I'll look for the code I submitted which you could
cut-paste into your code if you think it would be useful.

Steve B.

- Original Message -
From: [EMAIL PROTECTED]
To: "JetSpeed" [EMAIL PROTECTED]
Sent: Tuesday, October 31, 2000 3:18 PM
Subject: Re: Source Code for Configurable ServletInvokerPortlet





 Santiago,

 thanks for fixing the little second/millisecond bug so quickly.

 +1 on having this kind of examples for helping to handle legacy webapps.

 But the servlet should not be printing HTML, HEAD, and BODY tags, just
the
 HTML
 fragments. Also, it should possibly check the getContentType() call, and
 deliver
 HTML or WML as requested by the Portlet. But this last thing is not that
 important,
 as the current psml allows to specify mime types in the registry.

 I just posted the serlvet as an example for a legacy servlet; HTML, HEAD
 and BODY
 tags were in there to verify that the portlet actually works for legacy
 servlets
 that return complete HTML pages. Netscape and IE seem to ignore the HTML,
 HEAD and
 BODY tags when they appear in a table, so it worked.

 Extending the request info servlet to also support WML is a nice idea, so
I
 just
 did it (see below). I also commented out the surrounding tags so that the
 servlet
 now returns fragments suitable for inclusion in portlets.

 Also, we should point people using it that it is not the way to go for
new
 development, just a way to get this servlet you had written before
 displayed in a
 portlet.

 Right, usually content should be rendered by the portlet, e.g. by invoking
 a JSP or
 executing a stylesheet. We are currently also working on support of
 JSPs/servlets
 through the Portlet API, we plan to submit a proposal for discussion soon.

  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 {
  if (req.getHeader("accept").indexOf("text/vnd.wap.wml") != -1) {
generateWML(req, res);

// add additional content types here
  } else {
   // default to HTML
   generateHTML(req, res);
  }
   }

   public void generateHTML(HttpServletRequest req, HttpServletResponse
res)

   throws ServletException, IOException {
 PrintWriter out;

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

 // Uncomment this tp obtain a valid HTML document
 file://out.println("HTMLHEADTITLERequest Info
 Servlet/TITLE/HEADBODY BGCOLOR=\"#EE\"");

 out.println("h4Request Information:/h4");
 out.println("TABLE Border=\"2\" WIDTH=\"65%\" BGCOLOR=\"#FF\"");
 out.println("trtdRemote user/tdtd" + req.getRemoteUser() +
 "/td/tr");
 out.println("trtdRemote address/tdtd" + req.getRemoteAddr() +
 "/td/tr");
 out.println("trtdRemote host/tdtd" + req.getRemoteHost() +
 "/td/tr");
 out.println("/tableBRBR");

 Enumeration e = req.getHeaderNames();
 if (e.hasMoreElements()) {
   out.println("h4Request headers:/h4");
   out.println("TABLE Border=\"2\" WIDTH=\"65%\"
 BGCOLOR=\"#FF\"");
   while (e.hasMoreElements()) {
 String name = (String)e.nextElement();
 out.println("trtd" + name + "/tdtd" + req.getHeader(name)
+
  "/td/tr");
   }
   out.println("/tableBRBR");
 }

 e = req.getParameterNames();
 if (e.hasMoreElements()) {
   out.println("h4Servlet parameters:/h4");
   out.println("TABLE Border=\"2\" WIDTH=\"65%\"
 BGCOLOR=\"#FF\"");
   while (e.hasMoreElements()) {
 String name = (String)e.nextElement();
 out.println("trtd" + name + "/tdtd" +
 req.getParameter(name) + "/td/tr");
   

Re: Source Code for Configurable ServletInvokerPortlet

2000-10-30 Thread Santiago Gala



[EMAIL PROTECTED] wrote:

 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"

 classnameorg.apache.jetspeed.portal.portlets.ServletInvokerPortlet/classname
 /portlet-entry

 portlet-entry type="ref" parent="ServletInvokerPortlet"
 name="RequestInfoPortlet"
   parameter name="url" value="/RequestInfo/"/
   meta-info
 titleRequest Info/title
 descriptionThis 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();