This project has retired. For details please refer to its Attic page.
TestPortlet xref
View Javadoc

1   /*
2    * $Id: TestPortlet.java 939055 2010-04-28 19:16:04Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Test Portlet.
45   *
46   * @version $Rev: 939055 $ $Date: 2010-04-29 05:16:04 +1000 (Thu, 29 Apr 2010) $
47   */
48  public class TestPortlet extends GenericPortlet {
49  
50      /** {@inheritDoc} */
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       * Puts the definition name in a session attribue.
83       *
84       * @param request The portlet request.
85       * @param response The portlet response.
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       * Adds a link to the response to go back.
95       *
96       * @param response The portlet response.
97       * @throws IOException If something goes wrong.
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      * Returns the current container that has been set, or the default one.
109      *
110      * @param request The request to use.
111      * @param context The portlet context to use.
112      * @return The current Tiles container to use in web pages.
113      * @since 2.1.0
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      * Returns a specific Tiles container.
130      *
131      * @param context The portlet context to use.
132      * @param key The key under which the container is stored. If null, the
133      * default container will be returned.
134      * @return The requested Tiles container.
135      * @since 2.1.2
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      * Returns the default Tiles container.
146      *
147      * @param context The portlet context to use.
148      * @return The default Tiles container.
149      * @since 2.1.2
150      */
151     private static TilesContainer getContainer(PortletContext context) {
152         return getContainer(context, TilesAccess.CONTAINER_ATTRIBUTE);
153     }
154 }