Wiki source code of Portlet Integration

Last modified by Marius Dumitru Florea on 2010/06/16 15:31

Show last authors
1 {{toc/}}
2
3 = Overview of the Portlet Specification 2.0 (JSR286) =
4
5 Portlets provide a componentized user interface for services. In a Service Oriented Architecture one does not write monolithic applications, but separate services that can be orchestrated together into applications. This service orchestration requires componentized UIs for the services, monolithic web UIs based on servlets are no longer sufficient. The Java Portlet Specification allows coordination on the UI layer with different means, such as events, application sessions, and public render parameters, in order to provide a deep and seamless integration between the different services.
6
7 == Portlet Concepts ==
8
9 image:portalPageCreation.png
10
11 * a **portal** is a web based application that commonly provides personalization, authentication, content aggregation from different sources and hosts the presentation layer of information systems
12 * a **portlet** is an application that provides a specific piece of content (information or service) to be included as part of a portal page
13 ** web clients interact with portlets via a request/response paradigm implemented by the portal
14 * a **portlet container** runs portlets and provides them with the required runtime environment
15 ** manages the life cycle of portlets
16 ** provides persistent storage for portlet preferences
17
18 image:elementsOfAPortalPage.png
19
20 * a **portlet mode** indicates the function a portlet is performing in the render method
21 ** standard portlet modes are: view, edit and help
22 ** view: generate markup reflecting the current state of the portlet
23 ** edit: customize the behavior of the portlet
24 ** help: provide help information about the portlet
25 * a **portlet window** is the visual component used to display the content generated by a portlet within portal pages
26 * a **window state** is an indicator of the amount of portal page space that will be assigned to the content generated by a portlet via the render method
27 ** possible window states are: normal, maximized and minimized
28
29 == Portlet Life Cycle ==
30
31 The life cycle of a portlet is expressed through the ##init##, ##processAction##, ##render## and ##destroy## methods of the ##Portlet## interface. Optionally, portlets can implement the ##ResourceServingPortlet## interface to gain an additional phase to their life cycle: ##serveResource##.
32
33 image:requestHandlingSequence.png
34
35 **processAction(ActionRequest, ActionResponse)**
36
37 * triggered **only** by an action URL (e.g. when a form is submitted)
38 * doesn't have access to the response output stream and writer
39 * doesn't have access to the request URL
40 * **can't** create portlet URLs
41 * can issue a response redirect
42 * can access the action request parameters (i.e. submitted data)
43 * can set render request parameters (e.g. pass action request parameters to the render phase)
44 * can dispatch the action request to a servlet **but** the servlet inherits the restrictions, e.g. request URL is ##null## and all content written to the response output stream is ignored
45
46 **render(RenderRequest, RenderResponse)**
47
48 * triggered by a render URL or by an action URL if there was no response redirect issued during the process action phase
49 * has access to the response output stream and writer **but** it must output **only** HTML/WML markup **fragments**
50 * doesn't have access to the request URL
51 * can create action/render/resource portlet URLs
52 * **can't** issue a response redirect
53 * doesn't have access to the action request parameters (i.e. submitted data); if required, they need to be passed as render request parameters during the process action phase
54 * can access render request parameters
55 * can dispatch the render request to a servlet **but** the servlet inherits the restrictions, e.g. request URL is ##null## and response redirects are ignored
56
57 **serveResource(ResourceRequest, ResourceResponse)**
58
59 * triggered **only** by a resource URL
60 * has access to the response output stream and writer and can output **any** content type
61 * doesn't have access to the request URL
62 * can create action/render/resource portlet URLs
63 * **can't** issue a response redirect
64 * can access resource request parameters (i.e. parameters set on the resource portlet URL)
65 * can dispatch the resource request to a servlet **but** the servlet inherits the restrictions, e.g. request URL is ##null## and response redirects are ignored
66
67 image:resourceRequestHandlingSequence.png
68
69 == Portlet URLs ==
70
71 * URLs that reference a portlet
72 * the string representation **may not be a well formed URL** but a special token at the time the portlet is generating its content
73 ** may even be an ECMA script method that may generate the URL at the time the user clicks on the link
74 ** the portlet container is responsible for resolving tokens into real URLs
75 * support application specific parameters
76 ** parameters can be set only using a dedicated API
77 ** the portlet container decides how to encode the parameters in the final URL
78 ** parameters are usually aggregated and encoded as a single query string parameter
79 * hold information about the portlet mode and window state
80 * can be created only during ##render## and ##serveResource## phases (i.e. not during the ##processAction## phase)
81 * missing API for setting the fragment identifier
82 * **action** URLs
83 ** to be used within HTML forms
84 ** action parameters are available only to the action request, within the ##processAction## method of the targeted portlet
85 * **render** URLs
86 ** all the parameters set when constructing a render URL become render parameters of the subsequent render requests for the portlet
87 ** should be accessed via HTTP method GET as they should not change any state on the server
88 ** shouldn't be used within HTML forms
89 ** doesn't trigger ##processAction## method of the targeted portlet
90 * **resource** URLs
91 ** triggers only the ##serveResource## method of the targeted portlet
92 ** when rendering resources the portlet has full control over the output stream and can render binary markup
93
94 == Portlet Constraints ==
95
96 * enforced separation between request processing and response generation
97 * the markup fragment generated by a portlet may be not the only markup returned in the document to the client thus the portlet markup needs to co-exist with whatever other markup the portal produces
98 ** HTML ids must be namespaced to prevent conflicts with ids of elements generated by other portlets within the same portal page
99 ** CSS must affect only the content generated by the portlet
100 ** JavaScript code must be namespaced to prevent overwriting functions or variables defined by other portlets within the same portal page
101 * the same portlet can be included multiple times on the same portal page, possibly without sharing the runtime state
102 ** make sure JavaScript objects aren't shared between instances of the same portlet
103 * portlet URLs can be modified only through a specialized API (e.g. can't append query string parameters to the URL string representation)
104 * when creating a portlet URL we must know how it will be used: form action requires an action URL, resource URL for AJAX requests and render URL for links between different pages
105 * it is desirable to use/integrate with the authentication/authorization mechanism provided by the portal application
106 * ##render## method is triggered on a different HTTP request than ##processAction## in the reference implementation of the portlet specification, i.e. ##render## and ##processAction## methods can be executed in different threads (the portlet container issues a response redirect using a render URL after the process action phase)
107
108 = Overview of the Current Implementation in XWiki Core =
109
110 * http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/web/
111 * based on Java Portlet Specification 1.0 (JSR168)
112 * tightly-coupled with xwiki-core
113 * XWiki request is both a servlet request and a portlet request
114 ** ##interface XWikiRequest extends HttpServletRequest, RenderRequest, ActionRequest##
115 * XWiki response is both a servlet response and a portlet response
116 ** ##interface XWikiResponse extends HttpServletResponse, RenderResponse, ActionResponse##
117 * XWikiPortlet tries to act like the action servlet passing the control to the appropriate XWiki action
118 ** the list of known XWiki actions is hard-coded
119 * XWikiPortlet duplicates code from XWikiAction
120 ** XWiki context initialization
121 ** calling ##action## and ##render## methods of the appropriate XWiki action and handling the returned template name
122 * ##processAction## and ##render## portlet methods execute almost the same code
123 ** the code was written so that ##XWikiAction#action## is called during ##processAction## phase of the portlet and ##XWikiAction#render## is called during ##render## phase, but currently ##XWikiAction#render## is called during both phases and ##XWikiAction#action## is never called
124 * XWikiPortletURLFactory determines the type of portlet URL to create only based on the XWiki action parameter
125 ** the mapping between XWiki action names and portlet URL types is hard-coded
126 ** doesn't create resource URLs (resource handling was added in JSR286)
127 ** behaves as if portlet URLs are well formed URLs
128 * ##XWikiContext#getMode()## is used to distinguish between servlet mode and portlet mode
129 ** there are many places in xwiki-core where context mode is checked in order to execute a different code in portlet mode
130
131 = Integration Solutions =
132
133 == Tightly-Coupled ==
134
135 * http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-containers/xwiki-container-portlet/
136 * transform XWiki actions in components that depend on the container/environment (servlet, portlet, etc.)
137 ** http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-action/
138 * hide container specific URLs behind and abstract XWikiURL notion
139 ** http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-url/
140 ** it might be hard to define an uniform URL interface for all/most of the containers (e.g portlet URLs are of three types)
141 * write a portlet that initializes a portlet container XWiki component and calls the appropriate XWiki action component
142 * difficulties:
143 ** need to specify the portlet URL type when using the portlet URL factory
144 ** have to maintain container specific code
145 ** ##action## and ##render## methods of XWikiAction are currently called on each request so we can't map portlet's ##processAction## and ##render## methods to XWiki's ##action## and ##render## methods respectively
146
147 == Loosely-Coupled ==
148
149 * http://svn.xwiki.org/svnroot/xwiki/contrib/sandbox/xwiki-portlet/
150 * **idea**:
151 ** set a ##dispatchURL## parameter on all portlet URLs
152 ** the portlet reads the dispatch URL from the request and forwards the request and response
153 ** rewrite URLs in the response if content type is HTML
154 * **process action logic**
155 {{code}}
156 extract initial dispatch URL from request
157 do {
158 wrap request and response
159 forward wrapped request and response to the current dispatch URL
160 if (response was redirected) {
161 transform redirect URL in dispatch URL and update the current dispatch URL
162 } else {
163 store response data on session
164 store the session key and the last dispatch URL as render parameters
165 break
166 }
167 } while (true)
168 {{/code}}
169 * **render logic**
170 {{code}}
171 extract response data key from request
172 if (key found) {
173 extract response data from session and remove the key
174 } else {
175 extract dispatch URL from request
176 wrap request and response
177 forward wrapped request and response to the dispatch URL
178 extract response data from wrapped response
179 }
180 rewrite URLs in response data
181 render modified response data
182 {{/code}}
183 * **serve resource logic**
184 {{code}}
185 extract dispatch URL from request
186 wrap request and response
187 forward wrapped request and response to the dispatch URL
188 if (content type is HTML) {
189 rewrite URLs in response data
190 }
191 output (modified) response data
192 {{/code}}
193 * limitation: the servlet application where the request and response are forwarded must reside on the same server and must use a single entry point servlet
194 * URL rewrite logic
195 ** use an XML SAX filter
196 ** use a configurable (Portlet URL type, XWiki URL) mapping
197 {{code}}
198 action=
199 /bin/save/
200 /bin/cancel/
201 /bin/upload/
202
203 resource=
204 /bin/download/
205 /bin/export/
206 /bin/get/
207 /resources/
208 ?xpage=plain&outputSyntax=plain
209 {{/code}}
210 ** rewrite anchor URLs
211 *** determine portlet URL type based on the mapping
212 *** extract dispatch URL from the ##href## attribute
213 ** rewrite form URLs
214 *** use action portlet URL
215 *** extract dispatch URL from the ##action## attribute
216 ** rewrite URLs in JavaScript code (**TODO**)
217 *** use resource portlet URLs for AJAX requests
218 * inform the servlet application that it runs in portlet mode

Get Connected