ポートレットの設定
設定は、ポートレットによって設定され、カスタマイズ情報に保管されます。 デフォルトでは、PortletServingServlet サーブレットが、各ポートレット・ウィンドウのポートレットの設定を Cookie に保管します。 しかし、それらの設定を保管する場所は、セッション、.xml ファイル、 またはデータベースのいずれかに変更することができます。
ポートレット設定の Cookie での保管
Cookie の属性は、以下のように定義されます。
- パス
- context/portlet-name/portletwindow
- 名前:
- Cookie の名前には、固定値 PortletPreferenceCookie があります。
- 値
- Cookie の値には、次の構造にマッピングすることによって、設定のリストが含まれます。
*['/' pref-name *['=' pref-value]] - すべての設定は、「/」で始まり、その後に設定の名前が続きます。 設定に 1 つ以上の値がある場合、名前の後に続く値は「=」文字で区切られます。 ヌル値は、ストリング「#*!0_NULL_0!*#」で表されます。 例えば、Cookie の値は /locations=raleigh=boeblingen/regions=nc=bw のようになります。
ポートレット設定保管のカスタマイズ
Cookie を処理する方法をオーバーライドして、設定をセッション、 .xml ファイル、またはデータベースに保管できます。 保管をカスタマイズするには、 ポートレットを呼び出す前に、要求と応答をラップする新規エントリー・ポイントとして、 フィルター、サーブレットまたは JavaServer Pages ファイルを作成する必要があります。 設定を Cookie ではなくセッションに保管するように PortletServingServlet の振る舞いを変更する方法を理解するために、 以下のサンプル・ラッパーを検討します。
以下は、メインのサーブレットがポートレットの呼び出しを管理する方法の例です。
public class DispatchServlet extends HttpServlet
{
...
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
// create wrappers to change preference storage
RequestProxy req = new RequestProxy(request);
ResponseProxy resp = new ResponseProxy(request, response);
// create url prefix to always return to this servlet
...
req.setAttribute("com.ibm.wsspi.portlet.url.prefix", urlPrefix);
// prepare portlet url
String portletPath = request.getPathInfo();
...
// include portlet using wrappers
RequestDispatcher rd = getServletContext().getRequestDispatcher(modifiedPortletPath);
rd.include(req, resp);
}
}
次の例では、要求ラッパーが、Cookie の処理を変更して、セッションから設定を取り出します。
public class RequestWrapper extends HttpServletRequestWrapper
{
...
public Cookie[] getCookies() {
Cookie[] cookies = (Cookie[]) session.getAttribute("SessionPreferences");
return cookies;
}
}
次の例では、応答ラッパーが、Cookie の処理を変更して、設定をセッションに保管します。
public class ResponseProxy extends HttpServletResponseWrapper
{
...
public void addCookie(Cookie cookie) {
Cookie[] oldCookies = (Cookie[]) session.getAttribute("SessionPreferences");
int newPos = (oldCookies == null) ? 0 : oldCookies.length;
Cookie[] newCookies = new Cookie[newPos+1];
session.setAttribute("SessionPreferences", newCookies);
if (oldCookies != null) {
System.arraycopy(oldCookies, 0, newCookies, 0, oldCookies.length);
}
newCookies[newPos] = cookie;
}
}