Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/opencms/flex/jsp/CmsJspTagInfo.java


1   /*
2    * File   : $Source: /usr/local/cvs/opencms/src/com/opencms/flex/jsp/Attic/CmsJspTagInfo.java,v $
3    * Date   : $Date: 2003/05/13 13:18:20 $
4    * Version: $Revision: 1.10.2.1 $
5    *
6    * This library is part of OpenCms -
7    * the Open Source Content Mananagement System
8    *
9    * Copyright (C) 2002 - 2003 Alkacon Software (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, 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 com.opencms.flex.jsp;
33  
34  import com.opencms.boot.CmsBase;
35  import com.opencms.core.A_OpenCms;
36  import com.opencms.flex.cache.CmsFlexController;
37  
38  import java.util.Arrays;
39  import java.util.List;
40  
41  import javax.servlet.ServletRequest;
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.jsp.JspException;
44  import javax.servlet.jsp.tagext.TagSupport;
45  
46  /**
47   * Provides access to OpenCms and System related information.<p>
48   * 
49   * This tag supports the following special "property" values:
50   * <ul>
51   * <li><code>opencms.version</code> returns the current OpenCms version, e.g. <i>5.0 Kaitain</i>
52   * <li><code>opencms.url</code> returns the current request URL, e.g. 
53   * <i>http://localhost:8080/opencms/opencms/index.jsp</i>
54   * <li><code>opencms.uri</code> returns the current request URI, e.g. 
55   * <i>/opencms/opencms/index.jsp</i>
56   * <li><code>opencms.webapp</code> returns the name of the OpenCms web application, e.g. 
57   * <i>opencms</i>
58   * <li><code>opencms.webbasepath</code> returns the name of system path to the OpenCms web 
59   * application, e.g. <i>C:\Java\Tomcat\webapps\opencms\</i> 
60   * <li><code>opencms.request.uri</code> returns the name of the currently requested URI in 
61   * the OpenCms VFS, e.g. <i>/index.jsp</i>
62   * <li><code>opencms.request.element.uri</code> returns the name of the currently processed element, 
63   * which might be a sub-element like a template part, 
64   * in the OpenCms VFS, e.g. <i>/system/modules/org.opencms.welcome/jsptemplates/welcome.jsp</i>
65   * <li><code>opencms.request.folder</code> returns the name of the parent folder of the currently
66   * requested URI in the OpenCms VFS, e.g. <i>/</i>
67   * <li><code>opencms.request.encoding</code> returns the content encoding that has been set
68   * for the currently requested resource, e.g. <i>ISO-8859-1</i>
69   * </ul>
70   * 
71   * All other property values that are passes to the tag as routed to a standard 
72   * <code>System.getProperty(value)</code> call,
73   * so you can also get information about the Java VM environment,
74   * using values like <code>java.vm.version</code> or <code>os.name</code>.<p>
75   * 
76   * If the given property value does not match a key from the special OpenCms values
77   * and also not the system values, a (String) message is returned with a formatted 
78   * error message.<p>
79   *  
80   * @author  Alexander Kandzior (a.kandzior@alkacon.com)
81   * @version $Revision: 1.10.2.1 $
82   */
83  public class CmsJspTagInfo extends TagSupport {
84      
85      // member variables    
86    private String m_property = null;
87  
88      /** Static array with allowed info property values */
89      private static final String[] m_systemProperties =
90          {
91              "opencms.version", // 0
92              "opencms.url", // 1
93              "opencms.uri", // 2
94              "opencms.webapp", // 3
95              "opencms.webbasepath", // 4 
96              "opencms.request.uri", // 5
97              "opencms.request.element.uri", // 6
98              "opencms.request.folder", // 7      
99              "opencms.request.encoding" // 8      
100         };                
101             
102     /** array list of allowed property values for more convenient lookup */
103     private static final List m_userProperty =
104         Arrays.asList(m_systemProperties);
105 
106     /**
107      * Sets the info property name.
108      * 
109      * @param name the info property name to set
110      */
111   public void setProperty(String name) {
112     if (name != null) {
113       m_property = name.toLowerCase();
114     }
115   }
116 
117     /**
118      * Returns the selected info property.
119      * 
120      * @return the selected info property 
121      */
122   public String getProperty() {
123     return m_property != null ? m_property : "";
124   }
125 
126     /**
127      * @see javax.servlet.jsp.tagext.Tag#release()
128      */
129   public void release() {
130     super.release();
131     m_property = null;
132   }
133     
134     /**
135      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
136      */
137   public int doStartTag() throws JspException {
138 
139     ServletRequest req = pageContext.getRequest();
140         
141         // This will always be true if the page is called through OpenCms 
142         if (CmsFlexController.isCmsRequest(req)) {
143 
144             try {       
145                 String result = infoTagAction(m_property, (HttpServletRequest)req);
146                 // Return value of selected property
147                 pageContext.getOut().print(result);
148             } catch (Exception ex) {
149                 System.err.println("Error in Jsp 'info' tag processing: " + ex);
150                 System.err.println(com.opencms.util.Utils.getStackTrace(ex));
151                 throw new JspException(ex);
152             }
153         }
154         return SKIP_BODY;
155     }
156 
157     /**
158      * Returns the selected info property value based on the provided 
159      * parameters.<p>
160      * 
161      * @param property the info property to look up
162      * @param req the currents request
163      * @return the looked up property value 
164      */    
165   public static String infoTagAction(String property, HttpServletRequest req) {   
166     if (property == null) return "+++ Invalid info property selected: null +++";
167 
168         CmsFlexController controller = (CmsFlexController)req.getAttribute(CmsFlexController.ATTRIBUTE_NAME);
169               
170     String result = null;
171     switch (m_userProperty.indexOf(property)) {
172       case 0 : // opencms.version
173         result = A_OpenCms.getVersionName();
174         break;
175       case 1 : // opencms.url
176         result = req.getRequestURL().toString();
177         break;
178       case 2 : // opencms.uri
179         result = req.getRequestURI();
180         break;
181       case 3 : // opencms.webapp
182         result = CmsBase.getWebAppName();
183         break;
184       case 4 : // opencms.webbasepath
185         result = CmsBase.getWebBasePath();
186         break;
187             case 5: // opencms.request.uri
188                 result = controller.getCmsObject().getRequestContext().getUri();
189                 break;   
190             case 6: // opencms.request.element.uri
191                 result = controller.getCurrentRequest().getElementUri();
192                 break;                               
193             case 7: // opencms.request.folder
194                 result = com.opencms.file.CmsResource.getParent(controller.getCmsObject().getRequestContext().getUri());
195                 break;  
196             case 8: // opencms.request.encoding
197                 result = controller.getCmsObject().getRequestContext().getEncoding();
198                 break;          
199             default :
200                 result = System.getProperty(property);
201                 if (result == null) {
202             result = "+++ Invalid info property selected: " + property + " +++";
203                 }
204     }
205         
206         return result;
207   }
208 
209 }