1 | /* |
2 | * Copyright 2003,2004 The Apache Software Foundation. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | package org.apache.pluto.driver; |
17 | |
18 | import org.apache.pluto.PortletContainer; |
19 | import org.apache.pluto.driver.services.portal.PageConfig; |
20 | import org.apache.pluto.driver.config.AdminConfiguration; |
21 | import org.apache.commons.logging.Log; |
22 | import org.apache.commons.logging.LogFactory; |
23 | |
24 | import javax.servlet.http.HttpServletRequest; |
25 | import javax.servlet.http.HttpServletResponse; |
26 | import javax.servlet.ServletContext; |
27 | import javax.servlet.ServletException; |
28 | import java.io.IOException; |
29 | import java.text.DecimalFormat; |
30 | |
31 | /** |
32 | * TCK Driver Servlet. |
33 | * |
34 | * @author <a href="mailto:ddewolf@apache.org">David H. DeWolf</a> |
35 | * @author <a href="mailto:zheng@apache.org">ZHENG Zhong</a> |
36 | * @version 1.0 |
37 | * @since Dec 11, 2005 |
38 | */ |
39 | public class TCKDriverServlet extends PortalDriverServlet { |
40 | |
41 | /** Logger. */ |
42 | private static final Log LOG = LogFactory.getLog(TCKDriverServlet.class); |
43 | |
44 | private int pageCounter = 0; |
45 | |
46 | public String getServletInfo() { |
47 | return "Pluto TCK Driver Servlet"; |
48 | } |
49 | |
50 | public void init() { |
51 | ServletContext servletContext = getServletContext(); |
52 | container = (PortletContainer) servletContext.getAttribute( |
53 | AttributeKeys.PORTLET_CONTAINER); |
54 | } |
55 | |
56 | /** |
57 | * Overwrites <code>super.doGet(..)</code>. If <code>portletName</code> |
58 | * (multiple occurrences) parameter is received, the driver is attempting |
59 | * to create a new page. This page must be setup and then redirected to the |
60 | * actual page. Otherwise, the driver calls <code>super.doGet(..)</code> |
61 | * to continue as normal. |
62 | * @param request the incoming servlet request. |
63 | * @param response the incoming servlet response. |
64 | * @throws IOException |
65 | * @throws ServletException |
66 | */ |
67 | public void doGet(HttpServletRequest request, HttpServletResponse response) |
68 | throws IOException, ServletException { |
69 | String[] portletNames = request.getParameterValues("portletName"); |
70 | if (portletNames != null && portletNames.length > 0) { |
71 | debugWithName("Initializing new TCK page..."); |
72 | doSetup(request, response); |
73 | } else { |
74 | debugWithName("No portlet names specified. Continue as normal."); |
75 | super.doGet(request, response); |
76 | } |
77 | } |
78 | |
79 | public void doPost(HttpServletRequest req, HttpServletResponse response) |
80 | throws IOException, ServletException { |
81 | super.doGet(req, response); |
82 | } |
83 | |
84 | |
85 | // Private Methods --------------------------------------------------------- |
86 | |
87 | private void doSetup(HttpServletRequest request, |
88 | HttpServletResponse response) |
89 | throws IOException, ServletException { |
90 | String[] portletNames = request.getParameterValues("portletName"); |
91 | String pageName = request.getParameter("pageName"); |
92 | if (pageName != null) { |
93 | debugWithName("Retrieved page name from request: " + pageName); |
94 | } else { |
95 | debugWithName("Creating page name..."); |
96 | AdminConfiguration adminConfig = (AdminConfiguration) |
97 | getServletContext() |
98 | .getAttribute(AttributeKeys.DRIVER_ADMIN_CONFIG); |
99 | if (adminConfig == null) { |
100 | throw new ServletException("Invalid configuration: " |
101 | + "an AdminConfiguration must be specified " |
102 | + "to run the TCK."); |
103 | } |
104 | |
105 | pageName = (new DecimalFormat("TCK00000")).format(pageCounter++); |
106 | PageConfig pageConfig = new PageConfig(); |
107 | pageConfig.setName(pageName); |
108 | pageConfig.setUri(DEFAULT_PAGE_URI); |
109 | for (int i = 0; i < portletNames.length; i++) { |
110 | debugWithName("Processing portlet name: " + portletNames[i]); |
111 | int index = portletNames[i].indexOf("/"); |
112 | String contextPath = "/" + portletNames[i].substring(0, index); |
113 | String portletName = portletNames[i].substring(index + 1); |
114 | // IU_MOD--START |
115 | // TODO: This might cause a problem without the id |
116 | pageConfig.addPortlet(contextPath, portletName, ""); |
117 | // IU_MOD--END |
118 | adminConfig.getPortletRegistryAdminService() |
119 | .addPortletApplication(contextPath); |
120 | } |
121 | |
122 | adminConfig.getRenderConfigAdminService().addPage(pageConfig); |
123 | debugWithName("Created TCK Page: " + pageName); |
124 | } |
125 | |
126 | // The other possibility would be to redirect to the actual portal. |
127 | // I'm not sure which is better at this point. |
128 | StringBuffer buffer = new StringBuffer(); |
129 | buffer.append(request.getRequestURL().toString()); |
130 | if (!request.getRequestURL().toString().endsWith("/")) { |
131 | buffer.append("/"); |
132 | } |
133 | buffer.append(pageName); |
134 | debugWithName("Sending redirect to: " + buffer.toString()); |
135 | response.sendRedirect(buffer.toString()); |
136 | } |
137 | |
138 | /** |
139 | * Prints debug message with a <code>[Pluto TCK Driver]</code> prefix. |
140 | * @param message message to debug. |
141 | */ |
142 | private void debugWithName(String message) { |
143 | if (LOG.isDebugEnabled()) { |
144 | LOG.debug("[Pluto TCK Driver] " + message); |
145 | } |
146 | } |
147 | |
148 | } |