KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > invocation > ContentTypeInterceptor


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.invocation;
10
11 import java.util.HashMap JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.io.InputStream JavaDoc;
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 /**
33  * The selection of content type for the response. The selection is based on the functionality
34  * provided by the WALL library of the <a HREF="http://wurfl.sourceforge.net">WURFL project</a>.
35  *
36  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
37  * @author <a HREF="mailto:arturas.baranauskas@centras.lt">Arturas Baranauskas</a>
38  * @version $Revision: 1.5 $
39  */

40 public class ContentTypeInterceptor
41       implements Interceptor
42 {
43
44    private static Logger log = Logger.getLogger(ContentTypeInterceptor.class);
45
46    private static final String JavaDoc MIME_GENERAL_HTML = "text/html";
47    private static final String JavaDoc MIME_WML = "text/vnd.wap.wml";
48    private static final String JavaDoc MIME_CHTML = MIME_GENERAL_HTML;
49    private static final String JavaDoc MIME_XHTML = MIME_GENERAL_HTML;
50
51    private static final Map JavaDoc markupToMIMEType = new HashMap JavaDoc();
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 JavaDoc invoke(Invocation invocation)
74    {
75       PortalRequest request = (PortalRequest)invocation.getAttachment(AttachmentKey.PORTAL_REQUEST);
76       PortalResponse response = (PortalResponse)invocation.getAttachment(AttachmentKey.PORTAL_RESPONSE);
77
78       // Get the markup for this request according to the user agent
79
String JavaDoc markup = getMarkup(request);
80
81       // The MIME type.
82
String JavaDoc mimeType = null;
83
84       // Selecting a proper MIME type according to the markup
85
if (markup != null)
86       {
87          mimeType = (String JavaDoc)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       // Populate info
108
if (markup != null)
109       {
110          request.getProperties().setProperty(CoreConstants.REQ_PROP_PREFERRED_MARKUP, markup);
111       }
112
113       // Set content type
114
response.setContentType(mimeType);
115
116       // Continue invocation
117
return invocation.invokeNext();
118    }
119
120    /**
121     * The content type selection is based on the User Agent which comes
122     * in a HTTP header. The WALL then returns the prefered markup (WML/
123     * cHTML/xHTML) for that particular User Agent. The "prefered_markup"
124     * capability of each User Agent is stored in the "wurfl.xml" and the
125     * ContentTypeInterceptor is expecting this file to be found
126     * in the root of the portal's web application.
127     *
128     * @param request we need it: a) to get the portal's web application
129     * root and initialize the WALL; b) to get the User agent.
130     * @return returns the MIME type depending on User Agent's "preferred_markup"
131     * capability.
132     */

133    protected String JavaDoc getMarkup(PortalRequest request)
134    {
135       // Initialize if needed
136
if (!initialized)
137       {
138          // Get servlet context
139
PortalServer server = request.getServer();
140          final ServerManager mgr = server.getManager();
141
142          //
143
WurflSource src = new WurflSource()
144          {
145             public InputStream JavaDoc getWurflInputStream()
146             {
147                Configuration config = mgr.getConfiguration();
148                InputStream JavaDoc in = config.load("/wurfl.xml");
149                return in;
150             }
151             public InputStream JavaDoc getWurflPatchInputStream()
152             {
153                return null;
154             }
155          };
156
157          //Initalizing the WURFL and selecting a proper markup.
158
ObjectsManager.initMyWay(src);
159          // Let's get the User Agent Manager's instance
160
uam = ObjectsManager.getUAManagerInstance();
161          // Let's get the Capability Matrix's instance
162
cm = ObjectsManager.getCapabilityMatrixInstance();
163          initialized = true;
164       }
165
166       // Retrieving the prefered markup.
167
String JavaDoc userAgent = TagUtil.getUA(request);
168       String JavaDoc deviceID = uam.getDeviceIDFromUALoose(userAgent);
169       
170       // By default we pick HTML 4.0 as markup
171
String JavaDoc 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 JavaDoc contentType)
196    {
197       return MIME_GENERAL_HTML.equalsIgnoreCase(contentType) | MIME_WML.equalsIgnoreCase(contentType);
198    }
199 }
200
Popular Tags