KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > instrument > manager > http > server > AbstractHTTPURLHandler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package org.apache.excalibur.instrument.manager.http.server;
21
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.excalibur.instrument.AbstractLogEnabledInstrumentable;
29 import org.apache.excalibur.instrument.CounterInstrument;
30 import org.apache.excalibur.instrument.ValueInstrument;
31
32 /**
33  *
34  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
35  * @version CVS $Revision: 1.5 $ $Date: 2004/02/29 18:11:04 $
36  * @since 4.1
37  */

38 public abstract class AbstractHTTPURLHandler
39     extends AbstractLogEnabledInstrumentable
40     implements HTTPURLHandler
41 {
42     /** The path handled by this handler. */
43     private String JavaDoc m_path;
44     
45     /** The content type. */
46     private String JavaDoc m_contentType;
47     
48     /** The encoding. */
49     private String JavaDoc m_encoding;
50     
51     /** Number of requests. */
52     private CounterInstrument m_instrumentRequests;
53     
54     /** Time it takes to process each request. */
55     private ValueInstrument m_instrumentRequestTime;
56     
57     /*---------------------------------------------------------------
58      * Constructors
59      *-------------------------------------------------------------*/

60     /**
61      * Creates a new AbstractHTTPURLHandler.
62      *
63      * @param path The path handled by this handler.
64      * @param contentType The content type.
65      */

66     public AbstractHTTPURLHandler( String JavaDoc path, String JavaDoc contentType, String JavaDoc encoding )
67     {
68         m_path = path;
69         m_contentType = contentType;
70         m_encoding = encoding;
71         
72         addInstrument( m_instrumentRequests = new CounterInstrument( "requests" ) );
73         addInstrument( m_instrumentRequestTime = new ValueInstrument( "request-time" ) );
74     }
75     
76     /*---------------------------------------------------------------
77      * HTTPURLHandler Methods
78      *-------------------------------------------------------------*/

79     /**
80      * Returns the path handled by this handler.
81      *
82      * @return The path handled by this handler.
83      */

84     public String JavaDoc getPath()
85     {
86         return m_path;
87     }
88     
89     /**
90      * Returns the content type.
91      *
92      * @return The content type.
93      */

94     public String JavaDoc getContentType()
95     {
96         return m_contentType;
97     }
98     
99     /**
100      * Return the encoding to use.
101      *
102      * @return the encoding.
103      */

104     public String JavaDoc getEncoding()
105     {
106         return m_encoding;
107     }
108     
109     /**
110      * Handles the specified request.
111      *
112      * @param The full path being handled.
113      * @param parameters A Map of the parameters in the request.
114      * @param os The OutputStream to write the result to.
115      */

116     public final void handleRequest( String JavaDoc path, Map JavaDoc parameters, OutputStream JavaDoc os )
117         throws IOException JavaDoc
118     {
119         long start = System.currentTimeMillis();
120         try
121         {
122             doGet( path, parameters, os );
123         }
124         finally
125         {
126             m_instrumentRequests.increment();
127             if ( m_instrumentRequestTime.isActive() )
128             {
129                 m_instrumentRequestTime.setValue( (int)( System.currentTimeMillis() - start ) );
130             }
131         }
132     }
133             
134     /*---------------------------------------------------------------
135      * Methods
136      *-------------------------------------------------------------*/

137     /**
138      * Handles the specified request.
139      *
140      * @param The full path being handled.
141      * @param parameters A Map of the parameters in the request.
142      * @param os The OutputStream to write the result to.
143      */

144     public abstract void doGet( String JavaDoc path, Map JavaDoc parameters, OutputStream JavaDoc os )
145         throws IOException JavaDoc;
146     
147     public String JavaDoc getParameter( Map JavaDoc params, String JavaDoc name, String JavaDoc defaultValue )
148     {
149         Object JavaDoc param = params.get( name );
150         if ( param == null )
151         {
152             return defaultValue;
153         }
154         else if ( param instanceof String JavaDoc )
155         {
156             return (String JavaDoc)param;
157         }
158         else
159         {
160             List JavaDoc list = (List JavaDoc)param;
161             return (String JavaDoc)list.get( 0 );
162         }
163     }
164     
165     public String JavaDoc getParameter( Map JavaDoc params, String JavaDoc name )
166         throws FileNotFoundException JavaDoc
167     {
168         String JavaDoc param = getParameter( params, name, null );
169         if ( param == null )
170         {
171             throw new FileNotFoundException JavaDoc( "The " + name + " parameter was not specified." );
172         }
173         return param;
174     }
175     
176     public boolean getBooleanParameter( Map JavaDoc params, String JavaDoc name )
177         throws FileNotFoundException JavaDoc
178     {
179         return Boolean.getBoolean( getParameter( params, name ) );
180     }
181     
182     public boolean getBooleanParameter( Map JavaDoc params, String JavaDoc name, boolean defaultValue )
183     {
184         String JavaDoc value = getParameter( params, name, null );
185         if ( value == null )
186         {
187             return defaultValue;
188         }
189         
190         if ( value.length() < 1 )
191         {
192             return false;
193         }
194         
195         char c = value.charAt( 0 );
196         switch ( c )
197         {
198         case 'T': // TRUE
199
case 't': // true
200
case 'Y': // YES
201
case 'y': // yes
202
return true;
203         }
204         
205         return false;
206     }
207     
208     public int getIntegerParameter( Map JavaDoc params, String JavaDoc name )
209         throws FileNotFoundException JavaDoc
210     {
211         try
212         {
213             return Integer.parseInt( getParameter( params, name ) );
214         }
215         catch ( NumberFormatException JavaDoc e )
216         {
217             throw new FileNotFoundException JavaDoc( "The specified " + name + " was invalid." );
218         }
219     }
220     
221     public int getIntegerParameter( Map JavaDoc params, String JavaDoc name, int defaultValue )
222     {
223         String JavaDoc value = getParameter( params, name, null );
224         if ( value == null )
225         {
226             return defaultValue;
227         }
228         
229         try
230         {
231             return Integer.parseInt( value );
232         }
233         catch ( NumberFormatException JavaDoc e )
234         {
235             return defaultValue;
236         }
237     }
238     
239     public long getLongParameter( Map JavaDoc params, String JavaDoc name )
240         throws FileNotFoundException JavaDoc
241     {
242         try
243         {
244             return Long.parseLong( getParameter( params, name ) );
245         }
246         catch ( NumberFormatException JavaDoc e )
247         {
248             throw new FileNotFoundException JavaDoc( "The specified " + name + " was invalid." );
249         }
250     }
251     
252     public long getLongParameter( Map JavaDoc params, String JavaDoc name, long defaultValue )
253     {
254         String JavaDoc value = getParameter( params, name, null );
255         if ( value == null )
256         {
257             return defaultValue;
258         }
259         
260         try
261         {
262             return Long.parseLong( value );
263         }
264         catch ( NumberFormatException JavaDoc e )
265         {
266             return defaultValue;
267         }
268     }
269     
270     public String JavaDoc[] getParameters( Map JavaDoc params, String JavaDoc name )
271     {
272         Object JavaDoc param = params.get( name );
273         if ( param == null )
274         {
275             return new String JavaDoc[0];
276         }
277         else if ( param instanceof String JavaDoc )
278         {
279             return new String JavaDoc[] { (String JavaDoc)param };
280         }
281         else
282         {
283             List JavaDoc list = (List JavaDoc)param;
284             String JavaDoc[] ary = new String JavaDoc[list.size()];
285             list.toArray( ary );
286             return ary;
287         }
288     }
289     
290     public int[] getIntegerParameters( Map JavaDoc params, String JavaDoc name, int defaultValue )
291     {
292         String JavaDoc[] values = getParameters( params, name );
293         int[] iValues = new int[values.length];
294         
295         for ( int i = 0; i < values.length; i++ )
296         {
297             try
298             {
299                 iValues[i] = Integer.parseInt( values[i] );
300             }
301             catch ( NumberFormatException JavaDoc e )
302             {
303                 iValues[i] = defaultValue;
304             }
305         }
306         
307         return iValues;
308     }
309     
310     public long[] getLongParameters( Map JavaDoc params, String JavaDoc name, long defaultValue )
311     {
312         String JavaDoc[] values = getParameters( params, name );
313         long[] lValues = new long[values.length];
314         
315         for ( int i = 0; i < values.length; i++ )
316         {
317             try
318             {
319                 lValues[i] = Long.parseLong( values[i] );
320             }
321             catch ( NumberFormatException JavaDoc e )
322             {
323                 lValues[i] = defaultValue;
324             }
325         }
326         
327         return lValues;
328     }
329 }
330
331
Popular Tags