1 9 package org.jboss.portlet; 10 11 import org.jboss.portal.format.util.EntityTable; 12 13 import java.io.IOException ; 14 import java.io.PrintWriter ; 15 import java.lang.reflect.InvocationTargetException ; 16 import java.lang.reflect.Method ; 17 import java.lang.reflect.Modifier ; 18 import java.util.Enumeration ; 19 import java.util.Locale ; 20 import java.util.ResourceBundle ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import javax.portlet.ActionRequest; 25 import javax.portlet.ActionResponse; 26 import javax.portlet.Portlet; 27 import javax.portlet.PortletConfig; 28 import javax.portlet.PortletContext; 29 import javax.portlet.PortletException; 30 import javax.portlet.PortletMode; 31 import javax.portlet.PortletSecurityException; 32 import javax.portlet.RenderRequest; 33 import javax.portlet.RenderResponse; 34 import javax.portlet.WindowState; 35 import javax.portlet.PortletPreferences; 36 import javax.portlet.PortletURL; 37 38 44 public class JBossPortlet implements Portlet 45 { 46 47 private static final Class [] ACTION_LOOKUP = new Class []{JBossActionRequest.class,JBossActionResponse.class}; 48 49 50 private PortletConfig config; 51 52 public JBossPortlet() 53 { 54 } 55 56 59 public String getDefaultOperation() 60 { 61 return "main"; 62 } 63 64 67 public String getOperationName() 68 { 69 return "op"; 70 } 71 72 public void init() throws PortletException 73 { 74 } 75 76 public PortletConfig getPortletConfig() 77 { 78 return config; 79 } 80 81 public String getPortletName() 82 { 83 return config.getPortletName(); 84 } 85 86 public PortletContext getPortletContext() 87 { 88 return config.getPortletContext(); 89 } 90 91 94 protected void processAction(JBossActionRequest req, JBossActionResponse resp) throws PortletException, PortletSecurityException, IOException 95 { 96 doDispatch((JBossActionRequest)req,(JBossActionResponse)resp); 97 } 98 99 116 protected void doDispatch(JBossActionRequest req, JBossActionResponse resp) throws PortletException, PortletSecurityException, IOException 117 { 118 Method dispatcher = null; 119 120 String operation = req.getParameter(getOperationName()); 122 123 if (operation != null) 125 { 126 dispatcher = lookupMethod(operation, ACTION_LOOKUP); 127 } 128 129 if (dispatcher == null) 131 { 132 dispatcher = lookupMethod(getDefaultOperation(), ACTION_LOOKUP); 133 } 134 135 if (dispatcher != null) 137 { 138 try 139 { 140 dispatcher.invoke(this, new Object []{req,resp}); 141 } 142 catch (IllegalAccessException e) 143 { 144 throw new PortletException(e); 145 } 146 catch (InvocationTargetException e) 147 { 148 Throwable t = e.getCause(); 149 if (t instanceof PortletException) 150 { 151 throw (PortletException)t; 152 } 153 else if (t instanceof IOException ) 154 { 155 throw (IOException )t; 156 } 157 else if (t instanceof RuntimeException ) 158 { 159 throw (RuntimeException )t; 160 } 161 else if (t instanceof Error ) 162 { 163 throw (Error )t; 164 } 165 else 166 { 167 throw new PortletException("Unexpected exception when dispatching the operation", e); 168 } 169 } 170 } 171 else 172 { 173 throw new PortletException("Nothing to invoke"); 174 } 175 } 176 177 180 public void doEdit(JBossActionRequest req, JBossActionResponse resp) throws PortletException, PortletSecurityException, IOException 181 { 182 PortletPreferences prefs = req.getPreferences(); 183 Map map = prefs.getMap(); 184 for (Iterator i = req.getParameterMap().entrySet().iterator();i.hasNext();) 185 { 186 Map.Entry entry = (Map.Entry )i.next(); 187 String name = (String )entry.getKey(); 188 String [] values = (String [])entry.getValue(); 189 if (map.containsKey(name)) 190 { 191 prefs.setValues(name, values); 192 } 193 } 194 prefs.store(); 195 } 196 197 198 201 protected void render(JBossRenderRequest req, JBossRenderResponse resp) throws PortletException, PortletSecurityException, IOException 202 { 203 resp.setTitle(getTitle(req)); 204 doDispatch((JBossRenderRequest)req, (JBossRenderResponse)resp); 205 } 206 207 210 protected void doDispatch(JBossRenderRequest request, JBossRenderResponse response) throws PortletException, PortletSecurityException, IOException 211 { 212 if (!WindowState.MINIMIZED.equals(request.getWindowState())) 213 { 214 PortletMode portletMode = request.getPortletMode(); 215 if (PortletMode.VIEW.equals(portletMode)) 216 { 217 doView(request, response); 218 } 219 else if (PortletMode.HELP.equals(portletMode)) 220 { 221 doHelp(request, response); 222 } 223 else if (PortletMode.EDIT.equals(portletMode)) 224 { 225 doEdit(request, response); 226 } 227 } 228 } 229 230 233 protected void doView(JBossRenderRequest request, JBossRenderResponse response) throws PortletException, PortletSecurityException, IOException 234 { 235 throw new PortletException(); 236 } 237 238 241 protected void doHelp(JBossRenderRequest request, JBossRenderResponse response) throws PortletException, PortletSecurityException, IOException 242 { 243 throw new PortletException(); 244 } 245 246 249 protected void doEdit(JBossRenderRequest request, JBossRenderResponse response) throws PortletException, PortletSecurityException, IOException 250 { 251 response.setContentType("text/html"); 252 PrintWriter writer = response.getWriter(); 253 254 PortletURL url = response.createActionURL(); 256 url.setParameter(getDefaultOperation(), "doEdit"); 257 258 writer.print("<table> " + 260 "<tr><td class=\"portlet-section-alternate\">" + "Name" + 261 "</td><td class=\"portlet-section-alternate\">" + "Value" + 262 "</td></tr>" + 263 "<form action=\""); 264 writer.print(url.toString()); 265 writer.print("\" method=\"post\">"); 266 267 PortletPreferences prefs = request.getPreferences(); 269 for (Iterator i = prefs.getMap().entrySet().iterator();i.hasNext();) 270 { 271 Map.Entry entry = (Map.Entry )i.next(); 272 String name = (String )entry.getKey(); 273 String [] values = (String [])entry.getValue(); 274 275 name = EntityTable.FULL.convertEntities(name); 277 for (int j = 0; j < values.length; j++) 278 { 279 String value = values[j]; 280 if (value != null) 281 { 282 values[j] = EntityTable.FULL.convertEntities(value); 283 } 284 } 285 286 writer.print("<tr><td class=\"portlet-section-body\">"); 288 writer.print(name); 289 writer.print("</td><td class=\"portlet-section-body\">"); 290 if (prefs.isReadOnly(name)) 291 { 292 writer.print(name); 293 } 294 else 295 { 296 writer.print("<input class=\"portlet-form-input-field\" type=\"text\" name=\""); 297 writer.print(name); 298 writer.print("\" value=\""); 299 writer.print(values.length >= 1 ? values[0] : ""); 300 writer.print("\"/>"); 301 } 302 writer.print("</td></tr>"); 303 } 304 305 writer.print("<tr><td colspan=\"2\" class=\"portlet-section-alternate\">" + 306 "<input class=\"portlet-form-button\" type=\"submit\" value=\"Save\"/>" + 307 "</td></tr>" + 308 "</form></table>"); 309 } 310 311 public ResourceBundle getResourceBundle(Locale locale) 312 { 313 return getPortletConfig().getResourceBundle(locale); 314 } 315 316 protected String getTitle(RenderRequest request) 317 { 318 ResourceBundle bundle = getResourceBundle(request.getLocale()); 319 return bundle.getString("javax.portlet.title"); 320 } 321 322 public String getInitParameter(String name) throws IllegalArgumentException 323 { 324 return getPortletConfig().getInitParameter(name); 325 } 326 327 public Enumeration getInitParameterNames() 328 { 329 return getPortletConfig().getInitParameterNames(); 330 } 331 332 334 public void init(PortletConfig config) throws PortletException 335 { 336 this.config = config; 337 init(); 338 } 339 340 public void processAction(ActionRequest request, ActionResponse response) throws PortletException, PortletSecurityException, IOException 341 { 342 processAction((JBossActionRequest)request,(JBossActionResponse)response); 343 } 344 345 public void render(RenderRequest req, RenderResponse resp) throws PortletException, PortletSecurityException, IOException 346 { 347 render((JBossRenderRequest)req, (JBossRenderResponse)resp); 348 } 349 350 public void destroy() 351 { 352 } 353 354 356 359 private Method lookupMethod(String operation, Class [] parameterTypes) 360 { 361 try 362 { 363 Method m = getClass().getMethod(operation, parameterTypes); 364 if (m.getReturnType() == void.class && 365 Modifier.isPublic(m.getModifiers())) 366 { 367 return m; 368 } 369 } 370 catch (NoSuchMethodException e) 371 { 372 } 374 return null; 375 } 376 } 377 | Popular Tags |