1 16 17 package org.apache.cocoon.components.modules.input; 18 19 import org.apache.avalon.framework.configuration.Configuration; 20 import org.apache.avalon.framework.configuration.ConfigurationException; 21 import org.apache.avalon.framework.thread.ThreadSafe; 22 import org.apache.cocoon.environment.ObjectModelHelper; 23 import org.apache.cocoon.environment.Request; 24 25 import java.util.Enumeration ; 26 import java.util.Iterator ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.SortedSet ; 31 import java.util.TreeSet ; 32 33 42 public class HeaderAttributeModule extends AbstractInputModule implements ThreadSafe { 43 44 public Object getAttribute( String name, Configuration modeConf, Map objectModel ) 45 throws ConfigurationException { 46 47 String pname = (String ) this.settings.get("parameter",name); 48 if ( modeConf != null ) { 49 pname = modeConf.getAttribute( "parameter", pname ); 50 pname = modeConf.getChild("parameter").getValue(pname); 52 } 53 return ObjectModelHelper.getRequest(objectModel).getHeader( pname ); 54 } 55 56 57 public Iterator getAttributeNames( Configuration modeConf, Map objectModel ) 58 throws ConfigurationException { 59 60 return new IteratorHelper(ObjectModelHelper.getRequest(objectModel).getHeaderNames()); 61 } 62 63 64 public Object [] getAttributeValues( String name, Configuration modeConf, Map objectModel ) 65 throws ConfigurationException { 66 67 Request request = ObjectModelHelper.getRequest(objectModel); 68 String wildcard = (String ) this.settings.get("parameter", name); 69 if ( modeConf != null ) { 70 wildcard = modeConf.getAttribute( "parameter", wildcard ); 71 wildcard = modeConf.getChild("parameter").getValue(wildcard); 73 } 74 int wildcardIndex = wildcard.indexOf( "*" ); 75 if ( wildcardIndex != -1 ) { 76 79 String prefix = wildcard.substring( 0, wildcardIndex ); 84 String suffix; 85 if ( wildcard.length() >= wildcardIndex + 1 ) { 86 suffix = wildcard.substring( wildcardIndex + 1 ); 87 } else { 88 suffix = ""; 89 } 90 SortedSet names = new TreeSet (); 91 Enumeration allNames = request.getHeaderNames(); 92 93 while (allNames.hasMoreElements()) { 94 String pname = (String ) allNames.nextElement(); 95 if ( pname.startsWith( prefix ) && pname.endsWith( suffix ) ) { 96 names.add(pname); 97 } 98 } 99 100 List values = new LinkedList (); 101 Iterator j = names.iterator(); 102 while (j.hasNext()){ 103 String pname = (String ) j.next(); 104 values.add( request.getHeader( pname ) ); 105 } 106 107 return values.toArray(); 108 109 } else { 110 113 Object value = request.getHeader( wildcard ); 114 if ( value != null && !value.getClass().isArray() ) { 115 Object [] values = new Object [1]; 116 values[0] = value; 117 return values; 118 } else { 119 return (Object []) value; 120 } 121 122 } 123 124 } 125 126 } 127 | Popular Tags |