1 15 16 package org.jboss.portal.common; 17 18 import javax.activation.MimeType ; 19 import javax.activation.MimeTypeParseException ; 20 import java.util.Arrays ; 21 import java.util.Collections ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 36 public final class MediaType 37 { 38 private static Map allowedTypes = new HashMap (); 39 private static Map supportedExtensions = new HashMap (); 40 41 44 public static final MediaType ANY = 45 new MediaType("*", "*", new String []{}); 46 47 50 public static final MediaType XHTML = new MediaType("application", "xhtml+xml", 51 new String []{"xhtml"}); 52 53 56 public static final MediaType HTML = new MediaType("text", "html", 57 new String []{"html", "htm"}, 58 new MediaType[]{XHTML}); 59 60 63 public static final MediaType FORM = 64 new MediaType("application", "x-www-form-urlencoded", new String []{}); 65 66 69 public static final MediaType XML = new MediaType("text", "xml", new String []{"xml"}, 70 new MediaType[]{XHTML}); 71 72 75 public static final MediaType WML = new MediaType("text", "vnd.wap.wml", 76 new String []{"wml"}); 77 78 81 public static final MediaType CSS = new MediaType("text", "css", 82 new String []{"css"}); 83 84 87 public static final MediaType TEXT = new MediaType("text", "plain", 88 new String []{"txt"}); 89 90 93 public static final MediaType JS = new MediaType("text", "javascript", 94 new String []{"js"}); 95 96 99 public static final MediaType SVG = new MediaType("image", "svg+xml", 100 new String []{"svg"}); 101 102 105 public static final MediaType JPEG = new MediaType("image", "jpeg", 106 new String []{"jpeg", "jpg"}); 107 108 111 public static final MediaType GIF = new MediaType("image", "gif", 112 new String []{"gif"}); 113 114 117 public static final MediaType PNG = new MediaType("image", "png", 118 new String []{"png"}); 119 120 123 public static final MediaType WBMP = new MediaType("image", "vnd.wap.wbmp", 124 new String []{"wbpm"}); 125 126 129 public static final MediaType RSS = new MediaType("application", "rss+xml", 130 new String []{}); 131 132 135 public static final MediaType ICO = new MediaType("application", "octet-stream", 136 new String []{"ico"}); 137 private MimeType m_mimeType = null; 138 private MediaType[] m_allowedSubTypes; 139 140 146 private MediaType(String primaryType, String subType, String [] extensions) 147 { 148 try 149 { 150 m_mimeType = new MimeType (primaryType, subType); 151 m_allowedSubTypes = null; 152 allowedTypes.put(m_mimeType.getBaseType(), this); 153 for (int i = 0; i < extensions.length; i++) 154 { 155 supportedExtensions.put(extensions[i], this); 156 } 157 } 158 catch (MimeTypeParseException e) 159 { 160 System.out.println(e.getMessage()); 162 } 163 } 164 165 172 private MediaType(String primaryType, String subType, String [] extensions, MediaType[] allowedSubTypes) 173 { 174 this(primaryType, subType, extensions); 175 if (m_mimeType != null) 177 { 178 m_allowedSubTypes = allowedSubTypes; 179 } 180 } 181 182 190 public static MediaType parseMimeType(String mimeType) throws MimeTypeParseException 191 { 192 if (mimeType == null || "".equals(mimeType)) 193 { 194 throw new IllegalArgumentException ("no valid mime type provided"); 195 } 196 197 String type = mimeType.trim().toLowerCase(); 198 if (allowedTypes.keySet().contains(type)) 199 { 200 return (MediaType)allowedTypes.get(type); 201 } 202 203 throw new MimeTypeParseException ("Type [" + mimeType + "] not supported"); 204 } 205 206 215 public static MediaType parseMimeTypeByExtension(String extension) 216 throws MimeTypeParseException 217 { 218 if (extension == null || "".equals(extension)) 219 { 220 throw new IllegalArgumentException ("no valid mime type provided [" + extension + "]"); 221 } 222 223 String ext = extension.trim().toLowerCase(); 224 if (supportedExtensions.keySet().contains(ext)) 225 { 226 return (MediaType)supportedExtensions.get(ext); 227 } 228 229 throw new MimeTypeParseException ("Extension [" + extension + "] not supported"); 230 } 231 232 238 public static List getAllowedSubTypes(MediaType mimeType) 239 { 240 if (mimeType.m_allowedSubTypes == null) 241 { 242 return Collections.EMPTY_LIST; 243 } 244 else 245 { 246 return Collections.unmodifiableList(Arrays.asList(mimeType.m_allowedSubTypes)); 247 } 248 } 249 250 255 public List getAllowedSubTypes() 256 { 257 if (m_allowedSubTypes == null) 258 { 259 return Collections.EMPTY_LIST; 260 } 261 else 262 { 263 return Collections.unmodifiableList(Arrays.asList(m_allowedSubTypes)); 264 } 265 } 266 267 273 public String toString() 274 { 275 return m_mimeType.getBaseType(); 276 } 277 278 285 public boolean equals(Object o) 286 { 287 if (this == o) 288 { 289 return true; 290 } 291 if (!(o instanceof MediaType)) 292 { 293 return false; 294 } 295 296 final MediaType type = (MediaType)o; 297 298 return m_mimeType.equals(type.m_mimeType); 299 } 300 301 307 public int hashCode() 308 { 309 return m_mimeType.hashCode(); 310 } 311 } 312 | Popular Tags |