KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > jsp > CmsJspTagInfo


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspTagInfo.java,v $
3  * Date : $Date: 2006/03/27 14:52:19 $
4  * Version: $Revision: 1.22 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.jsp;
33
34 import org.opencms.file.CmsResource;
35 import org.opencms.flex.CmsFlexController;
36 import org.opencms.i18n.CmsMessageContainer;
37 import org.opencms.main.CmsLog;
38 import org.opencms.main.OpenCms;
39
40 import java.util.Arrays JavaDoc;
41 import java.util.List JavaDoc;
42
43 import javax.servlet.ServletRequest JavaDoc;
44 import javax.servlet.http.HttpServletRequest JavaDoc;
45 import javax.servlet.jsp.JspException JavaDoc;
46 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
47
48 import org.apache.commons.logging.Log;
49
50 /**
51  * Provides access to OpenCms and System related information.<p>
52  *
53  * This tag supports the following special "property" values:
54  * <ul>
55  * <li><code>opencms.version</code> returns the current OpenCms version, e.g. <i>5.0 Kaitain</i>
56  * <li><code>opencms.url</code> returns the current request URL, e.g.
57  * <i>http://localhost:8080/opencms/opencms/index.jsp</i>
58  * <li><code>opencms.uri</code> returns the current request URI, e.g.
59  * <i>/opencms/opencms/index.jsp</i>
60  * <li><code>opencms.webapp</code> returns the name of the OpenCms web application, e.g.
61  * <i>opencms</i>
62  * <li><code>opencms.webbasepath</code> returns the name of system path to the OpenCms web
63  * application, e.g. <i>C:\Java\Tomcat\webapps\opencms\</i>
64  * <li><code>opencms.request.uri</code> returns the name of the currently requested URI in
65  * the OpenCms VFS, e.g. <i>/index.jsp</i>
66  * <li><code>opencms.request.element.uri</code> returns the name of the currently processed element,
67  * which might be a sub-element like a template part,
68  * in the OpenCms VFS, e.g. <i>/system/modules/org.opencms.welcome/jsptemplates/welcome.jsp</i>
69  * <li><code>opencms.request.folder</code> returns the name of the parent folder of the currently
70  * requested URI in the OpenCms VFS, e.g. <i>/</i>
71  * <li><code>opencms.request.encoding</code> returns the content encoding that has been set
72  * for the currently requested resource, e.g. <i>ISO-8859-1</i>
73  * </ul>
74  *
75  * All other property values that are passes to the tag as routed to a standard
76  * <code>System.getProperty(value)</code> call,
77  * so you can also get information about the Java VM environment,
78  * using values like <code>java.vm.version</code> or <code>os.name</code>.<p>
79  *
80  * If the given property value does not match a key from the special OpenCms values
81  * and also not the system values, a (String) message is returned with a formatted
82  * error message.<p>
83  *
84  * @author Alexander Kandzior
85  *
86  * @version $Revision: 1.22 $
87  *
88  * @since 6.0.0
89  */

90 public class CmsJspTagInfo extends TagSupport JavaDoc {
91
92     /** Serial version UID required for safe serialization. */
93     private static final long serialVersionUID = -3881095296148023924L;
94
95     /** The log object for this class. */
96     private static final Log LOG = CmsLog.getLog(CmsJspTagInfo.class);
97
98     /** Static array with allowed info property values. */
99     private static final String JavaDoc[] SYSTEM_PROPERTIES = {"opencms.version", // 0
100
"opencms.url", // 1
101
"opencms.uri", // 2
102
"opencms.webapp", // 3
103
"opencms.webbasepath", // 4
104
"opencms.request.uri", // 5
105
"opencms.request.element.uri", // 6
106
"opencms.request.folder", // 7
107
"opencms.request.encoding", // 8
108
"opencms.request.locale" // 9
109
};
110
111     /** Array list of allowed property values for more convenient lookup. */
112     private static final List JavaDoc SYSTEM_PROPERTIES_LIST = Arrays.asList(SYSTEM_PROPERTIES);
113
114     // member variables
115
private String JavaDoc m_property;
116
117     /**
118      * Returns the selected info property value based on the provided
119      * parameters.<p>
120      *
121      * @param property the info property to look up
122      * @param req the currents request
123      * @return the looked up property value
124      */

125     public static String JavaDoc infoTagAction(String JavaDoc property, HttpServletRequest JavaDoc req) {
126
127         if (property == null) {
128             CmsMessageContainer errMsgContainer = Messages.get().container(Messages.GUI_ERR_INVALID_INFO_PROP_0);
129             return Messages.getLocalizedMessage(errMsgContainer, req);
130         }
131         CmsFlexController controller = CmsFlexController.getController(req);
132
133         String JavaDoc result = null;
134         switch (SYSTEM_PROPERTIES_LIST.indexOf(property)) {
135             case 0: // opencms.version
136
result = OpenCms.getSystemInfo().getVersionName();
137                 break;
138             case 1: // opencms.url
139
result = req.getRequestURL().toString();
140                 break;
141             case 2: // opencms.uri
142
result = req.getRequestURI();
143                 break;
144             case 3: // opencms.webapp
145
result = OpenCms.getSystemInfo().getWebApplicationName();
146                 break;
147             case 4: // opencms.webbasepath
148
result = OpenCms.getSystemInfo().getWebApplicationRfsPath();
149                 break;
150             case 5: // opencms.request.uri
151
result = controller.getCmsObject().getRequestContext().getUri();
152                 break;
153             case 6: // opencms.request.element.uri
154
result = controller.getCurrentRequest().getElementUri();
155                 break;
156             case 7: // opencms.request.folder
157
result = CmsResource.getParentFolder(controller.getCmsObject().getRequestContext().getUri());
158                 break;
159             case 8: // opencms.request.encoding
160
result = controller.getCmsObject().getRequestContext().getEncoding();
161                 break;
162             case 9: // opencms.request.locale
163
result = controller.getCmsObject().getRequestContext().getLocale().toString();
164                 break;
165             default:
166                 result = System.getProperty(property);
167                 if (result == null) {
168                     CmsMessageContainer errMsgContainer = Messages.get().container(
169                         Messages.GUI_ERR_INVALID_INFO_PROP_1,
170                         property);
171                     return Messages.getLocalizedMessage(errMsgContainer, req);
172                 }
173         }
174
175         return result;
176     }
177
178     /**
179      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
180      */

181     public int doStartTag() throws JspException JavaDoc {
182
183         ServletRequest JavaDoc req = pageContext.getRequest();
184
185         // This will always be true if the page is called through OpenCms
186
if (CmsFlexController.isCmsRequest(req)) {
187
188             try {
189                 String JavaDoc result = infoTagAction(m_property, (HttpServletRequest JavaDoc)req);
190                 // Return value of selected property
191
pageContext.getOut().print(result);
192             } catch (Exception JavaDoc ex) {
193                 if (LOG.isErrorEnabled()) {
194                     LOG.error(Messages.get().getBundle().key(Messages.ERR_PROCESS_TAG_1, "info"), ex);
195                 }
196                 throw new JspException JavaDoc(ex);
197             }
198         }
199         return SKIP_BODY;
200     }
201
202     /**
203      * Returns the selected info property.
204      *
205      * @return the selected info property
206      */

207     public String JavaDoc getProperty() {
208
209         return m_property != null ? m_property : "";
210     }
211
212     /**
213      * @see javax.servlet.jsp.tagext.Tag#release()
214      */

215     public void release() {
216
217         super.release();
218         m_property = null;
219     }
220
221     /**
222      * Sets the info property name.
223      *
224      * @param name the info property name to set
225      */

226     public void setProperty(String JavaDoc name) {
227
228         if (name != null) {
229             m_property = name.toLowerCase();
230         }
231     }
232
233 }
234
Popular Tags