1 9 package org.jboss.portal.core.invocation; 10 11 import java.util.HashMap ; 12 import java.util.Map ; 13 import java.io.InputStream ; 14 15 import net.sourceforge.wurfl.wall.TagUtil; 16 import net.sourceforge.wurfl.wurflapi.CapabilityMatrix; 17 import net.sourceforge.wurfl.wurflapi.ObjectsManager; 18 import net.sourceforge.wurfl.wurflapi.UAManager; 19 import net.sourceforge.wurfl.wurflapi.WurflSource; 20 21 import org.apache.log4j.Logger; 22 import org.jboss.portal.core.CoreConstants; 23 import org.jboss.portal.server.PortalRequest; 24 import org.jboss.portal.server.PortalResponse; 25 import org.jboss.portal.server.PortalServer; 26 import org.jboss.portal.server.ServerManager; 27 import org.jboss.portal.server.config.Configuration; 28 import org.jboss.portal.server.invocation.AttachmentKey; 29 import org.jboss.portal.server.invocation.Interceptor; 30 import org.jboss.portal.server.invocation.Invocation; 31 32 40 public class ContentTypeInterceptor 41 implements Interceptor 42 { 43 44 private static Logger log = Logger.getLogger(ContentTypeInterceptor.class); 45 46 private static final String MIME_GENERAL_HTML = "text/html"; 47 private static final String MIME_WML = "text/vnd.wap.wml"; 48 private static final String MIME_CHTML = MIME_GENERAL_HTML; 49 private static final String MIME_XHTML = MIME_GENERAL_HTML; 50 51 private static final Map markupToMIMEType = new HashMap (); 52 static 53 { 54 markupToMIMEType.put("wml_1_1", MIME_WML); 55 markupToMIMEType.put("wml_1_2", MIME_WML); 56 markupToMIMEType.put("wml_1_3", MIME_WML); 57 markupToMIMEType.put("html_wi_oma_xhtmlmp_1_0", MIME_XHTML); 58 markupToMIMEType.put("html_wi_w3_xhtmlbasic", MIME_XHTML); 59 markupToMIMEType.put("chtml_generic", MIME_CHTML); 60 markupToMIMEType.put("html_web_4_0", MIME_GENERAL_HTML); 61 markupToMIMEType.put("html_wi_imode_html_1", MIME_GENERAL_HTML); 62 markupToMIMEType.put("html_wi_imode_html_2", MIME_GENERAL_HTML); 63 markupToMIMEType.put("html_wi_imode_html_3", MIME_GENERAL_HTML); 64 markupToMIMEType.put("html_wi_imode_html_4", MIME_GENERAL_HTML); 65 markupToMIMEType.put("html_wi_imode_html_5", MIME_GENERAL_HTML); 66 markupToMIMEType.put("html_wi_imode_compact_generic", MIME_XHTML); 67 } 68 69 private UAManager uam; 70 private CapabilityMatrix cm; 71 private boolean initialized; 72 73 public Object invoke(Invocation invocation) 74 { 75 PortalRequest request = (PortalRequest)invocation.getAttachment(AttachmentKey.PORTAL_REQUEST); 76 PortalResponse response = (PortalResponse)invocation.getAttachment(AttachmentKey.PORTAL_RESPONSE); 77 78 String markup = getMarkup(request); 80 81 String mimeType = null; 83 84 if (markup != null) 86 { 87 mimeType = (String )markupToMIMEType.get(markup); 88 } 89 90 91 if (mimeType == null) 92 { 93 mimeType = MIME_GENERAL_HTML; 94 if (log.isDebugEnabled()) 95 { 96 log.debug("No mimeType type found for markup=" + markup + " using text/html instead"); 97 } 98 } 99 else 100 { 101 if (log.isDebugEnabled()) 102 { 103 log.debug("Found mimeType type " + mimeType + " for markup=" + markup); 104 } 105 } 106 107 if (markup != null) 109 { 110 request.getProperties().setProperty(CoreConstants.REQ_PROP_PREFERRED_MARKUP, markup); 111 } 112 113 response.setContentType(mimeType); 115 116 return invocation.invokeNext(); 118 } 119 120 133 protected String getMarkup(PortalRequest request) 134 { 135 if (!initialized) 137 { 138 PortalServer server = request.getServer(); 140 final ServerManager mgr = server.getManager(); 141 142 WurflSource src = new WurflSource() 144 { 145 public InputStream getWurflInputStream() 146 { 147 Configuration config = mgr.getConfiguration(); 148 InputStream in = config.load("/wurfl.xml"); 149 return in; 150 } 151 public InputStream getWurflPatchInputStream() 152 { 153 return null; 154 } 155 }; 156 157 ObjectsManager.initMyWay(src); 159 uam = ObjectsManager.getUAManagerInstance(); 161 cm = ObjectsManager.getCapabilityMatrixInstance(); 163 initialized = true; 164 } 165 166 String userAgent = TagUtil.getUA(request); 168 String deviceID = uam.getDeviceIDFromUALoose(userAgent); 169 170 String markup = null; 172 if ("generic".equals(deviceID)) 173 { 174 markup = "html_web_4_0"; 175 } 176 else 177 { 178 markup = cm.getCapabilityForDevice(deviceID, "preferred_markup"); 179 } 180 181 if (log.isDebugEnabled()) 182 { 183 if (markup == null) 184 { 185 log.debug("No markup found for ua=" + userAgent + ", devicedID=" + deviceID + " using text/html instead"); 186 } 187 else 188 { 189 log.debug("Found markup=" + markup + ", ua=" + userAgent + ", devicedID=" + deviceID); 190 } 191 } 192 return markup; 193 } 194 195 public static final boolean isAcceptedContentType(String contentType) 196 { 197 return MIME_GENERAL_HTML.equalsIgnoreCase(contentType) | MIME_WML.equalsIgnoreCase(contentType); 198 } 199 } 200 | Popular Tags |