This project has retired. For details please refer to its
Attic page.
TestPortlet xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.tiles.test.portlet;
22
23 import java.io.IOException;
24 import java.io.PrintWriter;
25
26 import javax.portlet.ActionRequest;
27 import javax.portlet.ActionResponse;
28 import javax.portlet.GenericPortlet;
29 import javax.portlet.PortletContext;
30 import javax.portlet.PortletException;
31 import javax.portlet.PortletRequestDispatcher;
32 import javax.portlet.PortletSession;
33 import javax.portlet.PortletURL;
34 import javax.portlet.ProcessAction;
35 import javax.portlet.RenderRequest;
36 import javax.portlet.RenderResponse;
37
38 import org.apache.tiles.TilesContainer;
39 import org.apache.tiles.access.TilesAccess;
40 import org.apache.tiles.request.Request;
41 import org.apache.tiles.request.portlet.RenderPortletRequest;
42
43
44
45
46
47
48 public class TestPortlet extends GenericPortlet {
49
50
51 @Override
52 protected void doView(RenderRequest request, RenderResponse response)
53 throws PortletException, IOException {
54 PortletSession portletSession = request.getPortletSession();
55 String definition = (String) portletSession.getAttribute("definition");
56 if (definition != null) {
57 portletSession.removeAttribute("definition");
58 TilesContainer container = getCurrentContainer(request,
59 getPortletContext());
60 Request currentRequest = new RenderPortletRequest(container
61 .getApplicationContext(), getPortletContext(), request,
62 response);
63 if (container.isValidDefinition(definition, currentRequest)) {
64 container.render(definition, currentRequest);
65 addBackLink(response);
66 } else {
67 PortletRequestDispatcher dispatcher = getPortletContext()
68 .getRequestDispatcher(
69 "/WEB-INF/jsp/nosuchdefinition.jsp");
70 dispatcher.forward(request, response);
71 addBackLink(response);
72 }
73 } else {
74 PortletRequestDispatcher dispatcher = getPortletContext()
75 .getRequestDispatcher("/WEB-INF/jsp/index.jsp");
76 dispatcher.forward(request, response);
77 }
78 }
79
80
81
82
83
84
85
86
87 @ProcessAction(name = "showDefinition")
88 public void showDefinition(ActionRequest request, ActionResponse response) {
89 request.getPortletSession().setAttribute("definition",
90 request.getParameter("definition"));
91 }
92
93
94
95
96
97
98
99 private void addBackLink(RenderResponse response) throws IOException {
100 PrintWriter writer = response.getWriter();
101 writer.append("<a href=\"");
102 PortletURL url = response.createRenderURL();
103 writer.append(url.toString());
104 writer.append("\"> Back to definition selection</a>");
105 }
106
107
108
109
110
111
112
113
114
115 private static TilesContainer getCurrentContainer(
116 javax.portlet.PortletRequest request, PortletContext context) {
117 TilesContainer container = (TilesContainer) request
118 .getAttribute(TilesAccess.CURRENT_CONTAINER_ATTRIBUTE_NAME);
119 if (container == null) {
120 container = getContainer(context);
121 request.setAttribute(TilesAccess.CURRENT_CONTAINER_ATTRIBUTE_NAME,
122 container);
123 }
124
125 return container;
126 }
127
128
129
130
131
132
133
134
135
136
137 private static TilesContainer getContainer(PortletContext context, String key) {
138 if (key == null) {
139 key = TilesAccess.CONTAINER_ATTRIBUTE;
140 }
141 return (TilesContainer) context.getAttribute(key);
142 }
143
144
145
146
147
148
149
150
151 private static TilesContainer getContainer(PortletContext context) {
152 return getContainer(context, TilesAccess.CONTAINER_ATTRIBUTE);
153 }
154 }