1 17 18 package org.apache.tomcat.util.http; 19 20 import java.io.PrintWriter ; 21 import java.io.StringWriter ; 22 import java.util.Enumeration ; 23 24 import org.apache.tomcat.util.buf.MessageBytes; 25 26 28 29 50 51 67 68 69 70 94 public class MimeHeaders { 95 98 public static final int DEFAULT_HEADER_SIZE=8; 99 100 103 private MimeHeaderField[] headers = new 104 MimeHeaderField[DEFAULT_HEADER_SIZE]; 105 106 109 private int count; 110 111 114 public MimeHeaders() { 115 } 116 117 120 public void recycle() { 122 clear(); 123 } 124 125 128 public void clear() { 129 for (int i = 0; i < count; i++) { 130 headers[i].recycle(); 131 } 132 count = 0; 133 } 134 135 138 public String toString() { 139 StringWriter sw = new StringWriter (); 140 PrintWriter pw = new PrintWriter (sw); 141 pw.println("=== MimeHeaders ==="); 142 Enumeration e = names(); 143 while (e.hasMoreElements()) { 144 String n = (String )e.nextElement(); 145 pw.println(n + " = " + getHeader(n)); 146 } 147 return sw.toString(); 148 } 149 150 152 155 public int size() { 156 return count; 157 } 158 159 163 public MessageBytes getName(int n) { 164 return n >= 0 && n < count ? headers[n].getName() : null; 165 } 166 167 171 public MessageBytes getValue(int n) { 172 return n >= 0 && n < count ? headers[n].getValue() : null; 173 } 174 175 177 public int findHeader( String name, int starting ) { 178 184 for (int i = starting; i < count; i++) { 186 if (headers[i].getName().equalsIgnoreCase(name)) { 187 return i; 188 } 189 } 190 return -1; 191 } 192 193 195 200 public Enumeration names() { 201 return new NamesEnumerator(this); 202 } 203 204 public Enumeration values(String name) { 205 return new ValuesEnumerator(this, name); 206 } 207 208 210 211 215 private MimeHeaderField createHeader() { 216 MimeHeaderField mh; 217 int len = headers.length; 218 if (count >= len) { 219 MimeHeaderField tmp[] = new MimeHeaderField[count * 2]; 221 System.arraycopy(headers, 0, tmp, 0, len); 222 headers = tmp; 223 } 224 if ((mh = headers[count]) == null) { 225 headers[count] = mh = new MimeHeaderField(); 226 } 227 count++; 228 return mh; 229 } 230 231 234 public MessageBytes addValue( String name ) { 235 MimeHeaderField mh = createHeader(); 236 mh.getName().setString(name); 237 return mh.getValue(); 238 } 239 240 244 public MessageBytes addValue(byte b[], int startN, int len) 245 { 246 MimeHeaderField mhf=createHeader(); 247 mhf.getName().setBytes(b, startN, len); 248 return mhf.getValue(); 249 } 250 251 253 public MessageBytes addValue(char c[], int startN, int len) 254 { 255 MimeHeaderField mhf=createHeader(); 256 mhf.getName().setChars(c, startN, len); 257 return mhf.getValue(); 258 } 259 260 265 public MessageBytes setValue( String name ) { 266 for ( int i = 0; i < count; i++ ) { 267 if(headers[i].getName().equalsIgnoreCase(name)) { 268 for ( int j=i+1; j < count; j++ ) { 269 if(headers[j].getName().equalsIgnoreCase(name)) { 270 removeHeader(j--); 271 } 272 } 273 return headers[i].getValue(); 274 } 275 } 276 MimeHeaderField mh = createHeader(); 277 mh.getName().setString(name); 278 return mh.getValue(); 279 } 280 281 287 public MessageBytes getValue(String name) { 288 for (int i = 0; i < count; i++) { 289 if (headers[i].getName().equalsIgnoreCase(name)) { 290 return headers[i].getValue(); 291 } 292 } 293 return null; 294 } 295 296 public String getHeader(String name) { 299 MessageBytes mh = getValue(name); 300 return mh != null ? mh.toString() : null; 301 } 302 303 309 public void removeHeader(String name) { 310 313 for (int i = 0; i < count; i++) { 314 if (headers[i].getName().equalsIgnoreCase(name)) { 315 removeHeader(i--); 316 } 317 } 318 } 319 320 324 private void removeHeader(int idx) { 325 MimeHeaderField mh = headers[idx]; 326 327 mh.recycle(); 328 headers[idx] = headers[count - 1]; 329 headers[count - 1] = mh; 330 count--; 331 } 332 333 } 334 335 342 class NamesEnumerator implements Enumeration { 343 int pos; 344 int size; 345 String next; 346 MimeHeaders headers; 347 348 NamesEnumerator(MimeHeaders headers) { 349 this.headers=headers; 350 pos=0; 351 size = headers.size(); 352 findNext(); 353 } 354 355 private void findNext() { 356 next=null; 357 for( ; pos< size; pos++ ) { 358 next=headers.getName( pos ).toString(); 359 for( int j=0; j<pos ; j++ ) { 360 if( headers.getName( j ).equalsIgnoreCase( next )) { 361 next=null; 363 break; 364 } 365 } 366 if( next!=null ) { 367 break; 369 } 370 } 371 pos++; 374 } 375 376 public boolean hasMoreElements() { 377 return next!=null; 378 } 379 380 public Object nextElement() { 381 String current=next; 382 findNext(); 383 return current; 384 } 385 } 386 387 390 class ValuesEnumerator implements Enumeration { 391 int pos; 392 int size; 393 MessageBytes next; 394 MimeHeaders headers; 395 String name; 396 397 ValuesEnumerator(MimeHeaders headers, String name) { 398 this.name=name; 399 this.headers=headers; 400 pos=0; 401 size = headers.size(); 402 findNext(); 403 } 404 405 private void findNext() { 406 next=null; 407 for( ; pos< size; pos++ ) { 408 MessageBytes n1=headers.getName( pos ); 409 if( n1.equalsIgnoreCase( name )) { 410 next=headers.getValue( pos ); 411 break; 412 } 413 } 414 pos++; 415 } 416 417 public boolean hasMoreElements() { 418 return next!=null; 419 } 420 421 public Object nextElement() { 422 MessageBytes current=next; 423 findNext(); 424 return current.toString(); 425 } 426 } 427 428 class MimeHeaderField { 429 MimeHeaderField next; 433 MimeHeaderField prev; 434 435 protected final MessageBytes nameB = MessageBytes.newInstance(); 436 protected final MessageBytes valueB = MessageBytes.newInstance(); 437 438 441 public MimeHeaderField() { 442 } 443 444 public void recycle() { 445 nameB.recycle(); 446 valueB.recycle(); 447 next=null; 448 } 449 450 public MessageBytes getName() { 451 return nameB; 452 } 453 454 public MessageBytes getValue() { 455 return valueB; 456 } 457 } 458 | Popular Tags |