1 16 package org.apache.cocoon.webapps.session.components; 17 18 import org.apache.avalon.framework.component.Component; 19 import org.apache.avalon.framework.configuration.Configurable; 20 import org.apache.avalon.framework.configuration.Configuration; 21 import org.apache.avalon.framework.configuration.ConfigurationException; 22 import org.apache.avalon.framework.context.Context; 23 import org.apache.avalon.framework.context.ContextException; 24 import org.apache.avalon.framework.context.Contextualizable; 25 import org.apache.avalon.framework.logger.AbstractLogEnabled; 26 import org.apache.avalon.framework.thread.ThreadSafe; 27 import org.apache.cocoon.components.ContextHelper; 28 import org.apache.cocoon.environment.Request; 29 import org.apache.cocoon.webapps.session.MediaManager; 30 31 37 public final class DefaultMediaManager 38 extends AbstractLogEnabled 39 implements MediaManager, Configurable, ThreadSafe, Contextualizable, Component { 40 41 42 protected PreparedMediaType[] allMediaTypes; 43 44 45 protected String defaultMediaType; 46 47 48 protected String [] mediaTypeNames; 49 50 51 protected Context context; 52 53 54 57 public void contextualize(Context context) throws ContextException { 58 this.context = context; 59 } 60 61 64 public void configure(Configuration myConfiguration) 65 throws ConfigurationException { 66 Configuration mediaConf = myConfiguration.getChild("mediatypes", false); 68 if (mediaConf == null) { 69 this.defaultMediaType = "html"; 71 } else { 72 this.defaultMediaType = mediaConf.getAttribute("default", "html"); 73 } 74 this.mediaTypeNames = new String [1]; 75 this.mediaTypeNames[0] = this.defaultMediaType; 76 boolean found; 77 int i; 78 String name; 79 80 Configuration[] childs = mediaConf.getChildren("media"); 81 PreparedMediaType[] array = new PreparedMediaType[0]; 82 PreparedMediaType[] copy; 83 Configuration current; 84 if (childs != null) { 85 for(int x = 0; x < childs.length; x++) { 86 current = childs[x]; 87 copy = new PreparedMediaType[array.length + 1]; 88 System.arraycopy(array, 0, copy, 0, array.length); 89 array = copy; 90 name = current.getAttribute("name"); 91 array[array.length-1] = new PreparedMediaType(name, current.getAttribute("useragent")); 92 found = false; 93 i = 0; 94 while ( i < this.mediaTypeNames.length && found == false) { 95 found = this.mediaTypeNames[i].equals(name); 96 i++; 97 } 98 if (found == false) { 99 String [] newStrings = new String [this.mediaTypeNames.length + 1]; 100 System.arraycopy(this.mediaTypeNames, 0, newStrings, 0, this.mediaTypeNames.length); 101 newStrings[newStrings.length-1] = name; 102 this.mediaTypeNames = newStrings; 103 } 104 } 105 } 106 this.allMediaTypes = array; 107 } 108 109 112 public boolean testMedia(String value) { 113 boolean result = false; 115 116 Request request = ContextHelper.getRequest(this.context); 117 118 String useragent = request.getHeader("User-Agent"); 119 PreparedMediaType theMedia = null; 120 int i, l; 121 i = 0; 122 l = this.allMediaTypes.length; 123 while (i < l && theMedia == null) { 124 if (useragent.indexOf(this.allMediaTypes[i].useragent) == -1) { 125 i++; 126 } else { 127 theMedia = this.allMediaTypes[i]; 128 } 129 } 130 if (theMedia != null) { 131 result = theMedia.name.equals(value); 132 } else { 133 result = this.defaultMediaType.equals(value); 134 } 135 136 return result; 137 } 138 139 142 public String [] getMediaTypes() { 143 return this.mediaTypeNames; 145 } 146 147 150 public String getMediaType() { 151 Request request = ContextHelper.getRequest( this.context ); 153 String useragent = request.getHeader("User-Agent"); 155 PreparedMediaType media = null; 156 if (useragent != null) { 157 int i, l; 158 i = 0; 159 l = this.allMediaTypes.length; 160 while (i < l && media == null) { 161 if (useragent.indexOf(this.allMediaTypes[i].useragent) == -1) { 162 i++; 163 } else { 164 media = this.allMediaTypes[i]; 165 } 166 } 167 } 168 return (media == null ? this.defaultMediaType : media.name); 169 } 170 171 } 172 173 174 177 final class PreparedMediaType { 178 179 String name; 180 String useragent; 181 182 PreparedMediaType(String name, String useragent) { 183 this.name = name; 184 this.useragent = useragent; 185 } 186 } 187 | Popular Tags |