View Javadoc

1   /*
2    * Copyright 2003,2004,2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.pluto.portlet.admin.taglib;
17  
18  import java.io.IOException;
19  import java.io.PrintWriter;
20  
21  import javax.portlet.PortletRequest;
22  import javax.portlet.PortletSession;
23  import javax.servlet.jsp.JspException;
24  import javax.servlet.jsp.JspTagException;
25  import javax.servlet.jsp.JspWriter;
26  import javax.servlet.jsp.tagext.TagSupport;
27  
28  import org.apache.pluto.Constants;
29  import org.apache.pluto.portlet.admin.PlutoAdminConstants;
30  import org.apache.pluto.portlet.admin.bean.PortletMessage;
31  import org.apache.pluto.portlet.admin.bean.PortletMessageType;
32  
33  /***
34   * Tag to print an error, info, alert, status or success message in a portlet
35   * as defined by Cascading Syle Sheet classes in Appendix PLT C of the
36   * Java Portlet Specification (JSR-168) version 1.0 (pg 116).
37   * <p>
38   * This class uses the PortletMessage and PortletMessageType classes to
39   * encapsulates message information. It also can handle a Throwable
40   * put in the session (see below), using the getMessage() for the message
41   * and printing out the stack trace in an HTML comment. All messages are
42   * wrapped in a &lt;p&gt; element containing a 'class' attribute with a
43   * spec-defined CSS value, which assumes these are defined in a CSS style
44   * sheet used by the JSP page. Right now this class can only handle one
45   * message per JSP page.
46   * </p>
47   * <p>
48   * There are three ways a message can be displayed using this tag:<br/>
49   * 1. Use the tag's message attribute on the JSP page to set the message
50   * as a String or expression that evaluates to a String. Optionally, you
51   * can use the messageType attribute. Valid messageType attributes
52   * are info, error, status, success and alert. The default message
53   * type is status.<br/>
54   * 2. Put a message in the portlet session encapsulated in a PortletMessage
55   * object with a PlutoAdminConstants.MESSAGE_ATTR session key. This class
56   * takes care of getting the attribute from the session, using its message and
57   * type data and removing the session attribute.<br/>
58   * 3. Put a Throwable in the portlet session with a PlutoAdminConstants.ERROR_ATTR
59   * key.  This class takes care of getting the attribute from the session,
60   * using its data and removing the session attribute. Throwables are always
61   * given a messageType of error.<br/>
62   * </p>
63   *
64   * @author Craig Doremus
65   * @see org.apache.pluto.portlet.admin.bean.PortletMessage
66   * @see org.apache.pluto.portlet.admin.bean.PortletMessageType
67   * @see org.apache.pluto.portlet.admin.PlutoAdminConstants
68   */
69  public class MessageTag extends TagSupport {
70  
71  	private String message;
72  	private String messageType = "status";
73  	/* User-defined CSS class */
74  	private String cssClass = "portlet-msg-info";
75  
76  	/************* JSR-168 defined CSS classes for messages ************/
77  	/*** CSS class for Error messages. Example: Portlet not available*/
78  public static final String MSG_ERROR_CSS_CLASS = "portlet-msg-error";
79  	/*** CSS class for Help messages, general additional information, etc. Example: Info about */
80  	public static final String MSG_INFO_CSS_CLASS = "portlet-msg-info";
81  	/*** CSS class for Status of the current operation. Example: Progress: 80% */
82  	public static final String MSG_STATUS_CSS_CLASS = "portlet-msg-status";
83  	/*** CSS class for Warning messages. Example: Timeout occurred, try again later */
84  	public static final String MSG_ALERT_CSS_CLASS = "portlet-msg-alert";
85  	/*** CSS class for Verification of the successful completion of a task. Example: Operation completed successfully */
86  	public static final String MSG_SUCCESS_CSS_CLASS = "portlet-msg-success";
87  	/* ************************************************************* */
88  
89  	/***
90  	 * Does the work of the tag.
91  	 */
92  	public int doStartTag()	throws JspException {
93  
94      PortletRequest request = (PortletRequest)pageContext.getRequest().getAttribute(Constants.PORTLET_REQUEST);
95      PortletSession session = request.getPortletSession();
96      Throwable error = (Throwable)session.getAttribute(PlutoAdminConstants.ERROR_ATTR);
97  		session.removeAttribute(PlutoAdminConstants.ERROR_ATTR);
98      PortletMessage oMsg = (PortletMessage)session.getAttribute(PlutoAdminConstants.MESSAGE_ATTR);
99  		session.removeAttribute(PlutoAdminConstants.MESSAGE_ATTR);
100 
101     try {
102       JspWriter out = pageContext.getOut();
103       if (message != null ) {
104       	if (cssClass == null) {
105       		cssClass = PortletMessageType.getTypeByName(messageType).CssClass;
106       	}
107 	      out.print(wrapHtml(message, cssClass));
108 	    } else if (oMsg != null ) {
109         out.print(wrapHtml(oMsg.getMessage(), oMsg.getType().CssClass));
110 	    } else if (error != null ) {
111 	    	StringBuffer sb = new StringBuffer();
112 	    	sb.append(error.getMessage());
113         if (error.getCause() != null) {
114         	sb.append("<br>Underlying Exception cause: ");
115   	    	sb.append(error.getCause().getMessage());
116         }
117         out.print(wrapHtml(sb.toString(), MSG_ERROR_CSS_CLASS));
118         //print out the stack trace in an HTML comment
119        	out.println("<!-- " + PlutoAdminConstants.LS);
120         PrintWriter writer = new PrintWriter(out, true);
121         error.printStackTrace(writer);
122        	out.print(PlutoAdminConstants.LS + "-->");
123 	    }
124     } catch (IOException e) {
125       throw new JspTagException("Error in tag MessageTag: " + e.toString());
126     }
127 			return SKIP_BODY;
128 	  }
129 
130 	private String wrapHtml(String msg, String css){
131 		StringBuffer sb = new StringBuffer();
132 		sb.append("<p class=\"" + css + "\">");
133     sb.append(msg);
134     sb.append("</p>");
135     return sb.toString();
136 	}
137 
138 	/***
139 	 * @param message The error message
140 	 */
141 	public void setMessage(String message) {
142 		this.message = message;
143 	}
144 
145 	/***
146 	 * @param cssClass The cssClass to set.
147 	 */
148 	public void setCssClass(String cssClass) {
149 		this.cssClass = cssClass;
150 	}
151 	/***
152 	 * @param messageType The messageType to set.
153 	 */
154 	public void setMessageType(String messageType) {
155 		this.messageType = messageType;
156 	}
157 }