1 19 20 package org.apache.excalibur.instrument.manager.http.server; 21 22 import java.io.FileNotFoundException ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import org.apache.excalibur.instrument.AbstractLogEnabledInstrumentable; 29 import org.apache.excalibur.instrument.CounterInstrument; 30 import org.apache.excalibur.instrument.ValueInstrument; 31 32 38 public abstract class AbstractHTTPURLHandler 39 extends AbstractLogEnabledInstrumentable 40 implements HTTPURLHandler 41 { 42 43 private String m_path; 44 45 46 private String m_contentType; 47 48 49 private String m_encoding; 50 51 52 private CounterInstrument m_instrumentRequests; 53 54 55 private ValueInstrument m_instrumentRequestTime; 56 57 60 66 public AbstractHTTPURLHandler( String path, String contentType, String 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 79 84 public String getPath() 85 { 86 return m_path; 87 } 88 89 94 public String getContentType() 95 { 96 return m_contentType; 97 } 98 99 104 public String getEncoding() 105 { 106 return m_encoding; 107 } 108 109 116 public final void handleRequest( String path, Map parameters, OutputStream os ) 117 throws IOException 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 137 144 public abstract void doGet( String path, Map parameters, OutputStream os ) 145 throws IOException ; 146 147 public String getParameter( Map params, String name, String defaultValue ) 148 { 149 Object param = params.get( name ); 150 if ( param == null ) 151 { 152 return defaultValue; 153 } 154 else if ( param instanceof String ) 155 { 156 return (String )param; 157 } 158 else 159 { 160 List list = (List )param; 161 return (String )list.get( 0 ); 162 } 163 } 164 165 public String getParameter( Map params, String name ) 166 throws FileNotFoundException 167 { 168 String param = getParameter( params, name, null ); 169 if ( param == null ) 170 { 171 throw new FileNotFoundException ( "The " + name + " parameter was not specified." ); 172 } 173 return param; 174 } 175 176 public boolean getBooleanParameter( Map params, String name ) 177 throws FileNotFoundException 178 { 179 return Boolean.getBoolean( getParameter( params, name ) ); 180 } 181 182 public boolean getBooleanParameter( Map params, String name, boolean defaultValue ) 183 { 184 String 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': case 't': case 'Y': case 'y': return true; 203 } 204 205 return false; 206 } 207 208 public int getIntegerParameter( Map params, String name ) 209 throws FileNotFoundException 210 { 211 try 212 { 213 return Integer.parseInt( getParameter( params, name ) ); 214 } 215 catch ( NumberFormatException e ) 216 { 217 throw new FileNotFoundException ( "The specified " + name + " was invalid." ); 218 } 219 } 220 221 public int getIntegerParameter( Map params, String name, int defaultValue ) 222 { 223 String 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 e ) 234 { 235 return defaultValue; 236 } 237 } 238 239 public long getLongParameter( Map params, String name ) 240 throws FileNotFoundException 241 { 242 try 243 { 244 return Long.parseLong( getParameter( params, name ) ); 245 } 246 catch ( NumberFormatException e ) 247 { 248 throw new FileNotFoundException ( "The specified " + name + " was invalid." ); 249 } 250 } 251 252 public long getLongParameter( Map params, String name, long defaultValue ) 253 { 254 String 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 e ) 265 { 266 return defaultValue; 267 } 268 } 269 270 public String [] getParameters( Map params, String name ) 271 { 272 Object param = params.get( name ); 273 if ( param == null ) 274 { 275 return new String [0]; 276 } 277 else if ( param instanceof String ) 278 { 279 return new String [] { (String )param }; 280 } 281 else 282 { 283 List list = (List )param; 284 String [] ary = new String [list.size()]; 285 list.toArray( ary ); 286 return ary; 287 } 288 } 289 290 public int[] getIntegerParameters( Map params, String name, int defaultValue ) 291 { 292 String [] 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 e ) 302 { 303 iValues[i] = defaultValue; 304 } 305 } 306 307 return iValues; 308 } 309 310 public long[] getLongParameters( Map params, String name, long defaultValue ) 311 { 312 String [] 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 e ) 322 { 323 lValues[i] = defaultValue; 324 } 325 } 326 327 return lValues; 328 } 329 } 330 331 | Popular Tags |