KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > thauvin > google > taglibs > KeySupport


1 /*
2  * @(#)KeySupport.java
3  *
4  * Copyright (c) 2002-2003, Erik C. Thauvin (erik@thauvin.net)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * Neither the name of the author nor the names of its contributors may be
19  * used to endorse or promote products derived from this software without
20  * specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $Id: KeySupport.java,v 1.2 2003/10/10 03:10:30 ethauvin Exp $
35  *
36  */

37 package net.thauvin.google.taglibs;
38
39 import net.thauvin.google.TagUtility;
40
41 import javax.servlet.jsp.*;
42 import javax.servlet.jsp.tagext.*;
43
44
45 /**
46  * Implements support for specifying the key used for authorization in the
47  * Google API.
48  * <p>
49  * The key can be specified using one the following:
50  * <ul>
51  * <li>The {@link #key key} attribute.</li>
52  * <li>The {@link net.thauvin.google.TagUtility#KEY_PARAM key} request parameter.</li>
53  * <li>The {@link net.thauvin.google.TagUtility#KEY_CONTEXT_PARAM key} context
54  * parameter.</li>
55  * <li>The {@link net.thauvin.google.TagUtility#KEY_CONTEXT_PARAM key} application scope
56  * attribute.</li>
57  * </ul>
58  *
59  * @author Erik C. Thauvin
60  * @created April 25, 2002
61  * @version $Revision: 1.2 $
62  * @since 1.0
63  */

64 public abstract class KeySupport extends BodyTagSupport
65 {
66     /**
67      * The key attribute.
68      */

69     protected String JavaDoc key = null;
70
71     /**
72      * Sets the key attribute.
73      *
74      * @param key The new attribute value.
75      * @see #getKey
76      */

77     public final void setKey(String JavaDoc key)
78     {
79         this.key = key;
80     }
81
82     /**
83      * Release method.
84      */

85     public void release()
86     {
87         super.release();
88
89         // Reset the key value
90
key = null;
91
92         // Reset the values
93
reset();
94     }
95
96     /**
97      * Returns the key attribute.
98      *
99      * @return The attribute value.
100      * @see #setKey(String)
101      */

102     protected String JavaDoc getKey()
103     {
104         if (TagUtility.isValidString(key, true))
105         {
106             return key;
107         }
108         else
109         {
110             String JavaDoc keyParam =
111                 TagUtility.getParameter(pageContext.getRequest(),
112                                         TagUtility.KEY_PARAM);
113
114             if (TagUtility.isValidString(keyParam, true))
115             {
116                 return keyParam;
117             }
118
119             keyParam =
120                 pageContext.getServletContext().getInitParameter(TagUtility.KEY_CONTEXT_PARAM);
121
122             if (TagUtility.isValidString(keyParam, true))
123             {
124                 return keyParam;
125             }
126
127             try
128             {
129                 keyParam =
130                     (String JavaDoc)(pageContext.getAttribute(TagUtility.KEY_CONTEXT_PARAM,
131                                                       PageContext.APPLICATION_SCOPE));
132
133                 if (TagUtility.isValidString(keyParam, true))
134                 {
135                     return keyParam;
136                 }
137             }
138             catch (NullPointerException JavaDoc e)
139             {
140                 ; // Do nothing
141
}
142         }
143
144         return "";
145     }
146
147     /**
148      * Reset the values.
149      */

150     protected void reset()
151     {
152         //super.reset();
153
}
154 }
155
Popular Tags