1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.portlets.pam;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.LinkedHashMap;
23 import java.util.List;
24 import java.util.Locale;
25 import java.util.StringTokenizer;
26
27 import javax.portlet.ActionRequest;
28 import javax.portlet.ActionResponse;
29 import javax.portlet.PortletConfig;
30 import javax.portlet.PortletContext;
31 import javax.portlet.PortletException;
32 import javax.portlet.PortletMode;
33 import javax.portlet.PortletSession;
34 import javax.portlet.RenderRequest;
35 import javax.portlet.RenderResponse;
36
37 import org.apache.jetspeed.CommonPortletServices;
38 import org.apache.jetspeed.components.portletregistry.FailedToStorePortletDefinitionException;
39 import org.apache.jetspeed.components.portletregistry.PortletRegistry;
40 import org.apache.jetspeed.components.portletregistry.RegistryException;
41 import org.apache.jetspeed.om.common.GenericMetadata;
42 import org.apache.jetspeed.om.common.LocalizedField;
43 import org.apache.jetspeed.om.common.MutableDescription;
44 import org.apache.jetspeed.om.common.MutableDisplayName;
45 import org.apache.jetspeed.om.common.MutableLanguage;
46 import org.apache.jetspeed.om.common.ParameterComposite;
47 import org.apache.jetspeed.om.common.SecurityRoleRefComposite;
48 import org.apache.jetspeed.om.common.UserAttribute;
49 import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
50 import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
51 import org.apache.jetspeed.om.common.preference.PreferenceComposite;
52 import org.apache.jetspeed.page.PageManager;
53 import org.apache.jetspeed.portlets.pam.beans.PortletApplicationBean;
54 import org.apache.jetspeed.search.SearchEngine;
55 import org.apache.pluto.om.common.SecurityRoleRef;
56 import org.apache.pluto.om.portlet.ContentType;
57 import org.apache.portals.bridges.beans.TabBean;
58 import org.apache.portals.bridges.common.GenericServletPortlet;
59
60 /***
61 * This portlet is a tabbed editor user interface for editing both portlet defintions
62 * and portlet applications.
63 *
64 * @author <a href="mailto:jford@apache.com">Jeremy Ford</a>
65 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
66 * @version $Id: PortletApplicationDetail.java 348264 2005-11-22 22:06:45Z taylor $
67 */
68 public class PortletApplicationDetail extends GenericServletPortlet
69 {
70 private static final String PORTLET_ACTION = "portlet_action";
71 private final String VIEW_PA = "portletApplication";
72 private final String VIEW_PD = "portletDefinition";
73
74 private static final String PORTLET_APP_ACTION_PREFIX = "portlet_app.";
75 private static final String PORTLET_ACTION_PREFIX = "portlet.";
76
77 private PortletContext context;
78 private PortletRegistry registry;
79 private PageManager pageManager;
80 private SearchEngine searchEngine;
81 private LinkedHashMap paTabMap = new LinkedHashMap();
82 private LinkedHashMap pdTabMap = new LinkedHashMap();
83
84 public void init(PortletConfig config)
85 throws PortletException
86 {
87 super.init(config);
88 context = getPortletContext();
89 registry = (PortletRegistry)context.getAttribute(CommonPortletServices.CPS_REGISTRY_COMPONENT);
90 searchEngine = (SearchEngine) context.getAttribute(CommonPortletServices.CPS_SEARCH_COMPONENT);
91 pageManager = (PageManager) context.getAttribute(CommonPortletServices.CPS_PAGE_MANAGER_COMPONENT);
92 if (null == registry)
93 {
94 throw new PortletException("Failed to find the Portlet Registry on portlet initialization");
95 }
96
97 TabBean tb1 = new TabBean("pa_details");
98 TabBean tb2 = new TabBean("pa_metadata");
99 TabBean tb3 = new TabBean("pa_portlets");
100 TabBean tb4 = new TabBean("pa_user_attribtues");
101
102 paTabMap.put(tb1.getId(), tb1);
103 paTabMap.put(tb2.getId(), tb2);
104 paTabMap.put(tb3.getId(), tb3);
105 paTabMap.put(tb4.getId(), tb4);
106
107 TabBean tb_1 = new TabBean("pd_details");
108 TabBean tb_2 = new TabBean("pd_metadata");
109 TabBean tb_3 = new TabBean("pd_preferences");
110 TabBean tb_4 = new TabBean("pd_languages");
111 TabBean tb_5 = new TabBean("pd_parameters");
112 TabBean tb_6 = new TabBean("pd_security");
113 TabBean tb_7 = new TabBean("pd_content_type");
114
115 pdTabMap.put(tb_1.getId(), tb_1);
116 pdTabMap.put(tb_2.getId(), tb_2);
117 pdTabMap.put(tb_3.getId(), tb_3);
118 pdTabMap.put(tb_4.getId(), tb_4);
119 pdTabMap.put(tb_5.getId(), tb_5);
120 pdTabMap.put(tb_6.getId(), tb_6);
121 pdTabMap.put(tb_7.getId(), tb_7);
122 }
123
124 public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
125
126 {
127 response.setContentType("text/html");
128
129 String paName = (String)
130 request.getPortletSession().getAttribute(PortletApplicationResources.PAM_CURRENT_PA,
131 PortletSession.APPLICATION_SCOPE);
132
133 MutablePortletApplication pa = registry.getPortletApplication(paName);
134
135 if (null != pa)
136 {
137 request.setAttribute(VIEW_PA, new PortletApplicationBean(pa));
138
139 String pdefName = (String) request.getPortletSession().getAttribute(PortletApplicationResources.REQUEST_SELECT_PORTLET, PortletSession.APPLICATION_SCOPE);
140 PortletDefinitionComposite pdef = (PortletDefinitionComposite) pa.getPortletDefinitionByName(pdefName);
141
142 request.setAttribute(VIEW_PD, pdef);
143
144 request.setAttribute("tabs", paTabMap.values());
145 request.setAttribute("portlet_tabs", pdTabMap.values());
146
147 TabBean selectedTab = (TabBean) request.getPortletSession().getAttribute(PortletApplicationResources.REQUEST_SELECT_TAB, PortletSession.APPLICATION_SCOPE);
148 if(selectedTab == null)
149 {
150 selectedTab = (TabBean) paTabMap.values().iterator().next();
151 }
152
153
154 if(selectedTab.getId().equals("pa_portlets"))
155 {
156 TabBean selectedPortletTab = (TabBean) request.getPortletSession().getAttribute("selected_portlet_tab");
157 if(selectedPortletTab == null)
158 {
159 selectedPortletTab = (TabBean) pdTabMap.values().iterator().next();
160 }
161 if(selectedPortletTab.getId().equals("pd_security")) {
162 setupSecurityContraintContext(request, null, pdef);
163 }
164 request.setAttribute("selected_portlet_tab", selectedPortletTab);
165 }
166 else if(selectedTab.getId().equals("pa_details"))
167 {
168 setupSecurityContraintContext(request, pa, null);
169 }
170
171 request.setAttribute(PortletApplicationResources.REQUEST_SELECT_TAB, selectedTab);
172 }
173
174 super.doView(request, response);
175 }
176
177 private void setupSecurityContraintContext(RenderRequest request, MutablePortletApplication pa, PortletDefinitionComposite pdef) throws PortletException
178 {
179 try
180 {
181 List securityContraintRefList = pageManager.getPageSecurity().getSecurityConstraintsDefs();
182 request.setAttribute("securityContraintRefList", securityContraintRefList);
183 if(pdef == null)
184 {
185 request.setAttribute("currentSecurityConstraintRef", pa.getJetspeedSecurityConstraint());
186 }
187 else
188 {
189 request.setAttribute("currentSecurityConstraintRef", pdef.getJetspeedSecurityConstraint());
190 }
191 }
192 catch (Exception e)
193 {
194 throw new PortletException("Failed to retrieve security constraint references from "
195 + (pdef == null ? "portlet application " + pa.getName() : "portlet definition " + pdef.getName()));
196 }
197 }
198
199
200 public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException, IOException
201 {
202 String paName = (String)
203 actionRequest.getPortletSession().getAttribute(PortletApplicationResources.PAM_CURRENT_PA,
204 PortletSession.APPLICATION_SCOPE);
205
206 String selectedPortlet = actionRequest.getParameter(PortletApplicationResources.REQUEST_SELECT_PORTLET);
207 if(selectedPortlet != null)
208 {
209 actionRequest.getPortletSession().setAttribute(PortletApplicationResources.REQUEST_SELECT_PORTLET, selectedPortlet, PortletSession.APPLICATION_SCOPE);
210 }
211
212 String selectedTab = actionRequest.getParameter(PortletApplicationResources.REQUEST_SELECT_TAB);
213 if(selectedTab != null)
214 {
215 TabBean tab = (TabBean) paTabMap.get(selectedTab);
216 actionRequest.getPortletSession().setAttribute(PortletApplicationResources.REQUEST_SELECT_TAB, tab, PortletSession.APPLICATION_SCOPE);
217 }
218
219 String selectedPortletTab = actionRequest.getParameter("selected_portlet_tab");
220 if(selectedPortletTab != null)
221 {
222 TabBean tab = (TabBean) pdTabMap.get(selectedPortletTab);
223 actionRequest.getPortletSession().setAttribute("selected_portlet_tab", tab);
224 }
225
226 String action = actionRequest.getParameter(PORTLET_ACTION);
227 if(action != null)
228 {
229 MutablePortletApplication pa = registry.getPortletApplication(paName);
230 if(isAppAction(action))
231 {
232 action = getAction(PORTLET_APP_ACTION_PREFIX, action);
233
234 if(action.endsWith("metadata"))
235 {
236 processMetadataAction(actionRequest, actionResponse, pa, null, action);
237 }
238 else if(action.endsWith("user_attribute"))
239 {
240 processUserAttributeAction(actionRequest, actionResponse, pa, action);
241 }
242 else if(action.endsWith("edit_security_constraint"))
243 {
244 processSecurityRef(actionRequest, actionResponse, pa, null, action);
245 }
246 searchEngine.update(pa);
247 }
248 else if(isPortletAction(action))
249 {
250 action = getAction(PORTLET_ACTION_PREFIX, action);
251 String pdefName = (String) actionRequest.getPortletSession().getAttribute(PortletApplicationResources.REQUEST_SELECT_PORTLET, PortletSession.APPLICATION_SCOPE);
252
253 try
254 {
255 PortletDefinitionComposite pdef = (PortletDefinitionComposite) pa.getPortletDefinitionByName(pdefName);
256 if(action.endsWith("metadata"))
257 {
258 processMetadataAction(actionRequest, actionResponse, null, pdef, action);
259 }
260 else if(action.endsWith("portlet"))
261 {
262 processPortletAction(actionRequest, actionResponse, pa, pdef, action);
263 }
264 else if(action.endsWith("preference"))
265 {
266 processPreferenceAction(actionRequest, actionResponse, pa, pdef, action);
267 }
268 else if(action.endsWith("language"))
269 {
270 processLanguage(actionRequest, actionResponse, pa, pdef, action);
271 }
272 else if(action.endsWith("parameter"))
273 {
274 processParameter(actionRequest, actionResponse, pa, pdef, action);
275 }
276 else if(action.endsWith("security"))
277 {
278 processSecurity(actionRequest, actionResponse, pa, pdef, action);
279 }
280 else if(action.endsWith("content_type"))
281 {
282 processContentType(actionRequest, actionResponse, pa, pdef, action);
283 }
284 else if(action.endsWith("edit_security_constraint")) {
285 processSecurityRef(actionRequest, actionResponse, null, pdef, action);
286 }
287 searchEngine.update(pdef);
288 }
289 catch (RegistryException e)
290 {
291 throw new PortletException("A Registry action has failed. "+e.getMessage());
292 }
293 }
294 }
295 }
296
297 private boolean isAppAction(String action)
298 {
299 return action.startsWith(PORTLET_APP_ACTION_PREFIX);
300 }
301
302 private boolean isPortletAction(String action)
303 {
304 return action.startsWith(PORTLET_ACTION_PREFIX);
305 }
306
307 private String getAction(String prefix, String action)
308 {
309 return action.substring(prefix.length());
310 }
311
312 /***
313 * @param actionRequest
314 * @param actionResponse
315 * @param pa
316 * @param action
317 */
318 private void processUserAttributeAction(ActionRequest actionRequest, ActionResponse actionResponse, MutablePortletApplication mpa, String action)
319 throws PortletException, IOException
320 {
321 boolean modified = false;
322 if(action.equals("edit_user_attribute"))
323 {
324 String userAttrName = "";
325
326 Iterator userAttrIter = mpa.getUserAttributes().iterator();
327 while (userAttrIter.hasNext())
328 {
329 UserAttribute userAttr = (UserAttribute) userAttrIter.next();
330
331 userAttrName = userAttr.getName();
332 String description = actionRequest.getParameter(userAttrName + ":description");
333 if(!userAttr.getDescription().equals(description))
334 {
335 userAttr.setDescription(description);
336 modified = true;
337 }
338 }
339 }
340 else if(action.equals("add_user_attribute"))
341 {
342 String userAttrName = actionRequest.getParameter("user_attr_name");
343 String userAttrDesc = actionRequest.getParameter("user_attr_desc");
344 if (userAttrName != null && userAttrName.trim().length() > 0)
345 {
346 mpa.addUserAttribute(userAttrName.trim(), userAttrDesc);
347 modified = true;
348 }
349 }
350 else if(action.equals("remove_user_attribute"))
351 {
352 String[] userAttrNames = actionRequest.getParameterValues("user_attr_id");
353 if(userAttrNames != null)
354 {
355 String userAttrName = "";
356 int count = 0;
357 Iterator userAttrIter = mpa.getUserAttributes().iterator();
358 while (userAttrIter.hasNext())
359 {
360 UserAttribute userAttr = (UserAttribute) userAttrIter.next();
361 for(int ix = 0; ix < userAttrNames.length; ix++)
362 {
363 userAttrName = userAttrNames[ix];
364 if(userAttr.getName().equals(userAttrName))
365 {
366 userAttrIter.remove();
367 count++;
368 break;
369 }
370 }
371 }
372 modified = count > 0;
373 }
374 }
375
376 if(modified)
377 {
378 try
379 {
380 registry.updatePortletApplication(mpa);
381 }
382 catch(RegistryException e)
383 {
384 throw new PortletException("Failed to update portlet application while performing action " + action, e);
385 }
386 }
387 }
388
389 /***
390 * @param actionRequest
391 * @param actionResponse
392 * @param pa
393 * @param action
394 * @throws PortletException
395 * @throws IOException
396 */
397 private void processMetadataAction(ActionRequest actionRequest,
398 ActionResponse actionResponse,
399 MutablePortletApplication pa,
400 PortletDefinitionComposite pd,
401 String action)
402 throws PortletException, IOException
403 {
404 GenericMetadata meta = null;
405
406 if (pd != null)
407 {
408 meta = pd.getMetadata();
409 }
410 else if(pa != null)
411 {
412 meta = pa.getMetadata();
413 }
414
415 if (meta == null)
416 {
417 return;
418 }
419
420 boolean modified = false;
421 if(action.equals("edit_metadata"))
422 {
423 Iterator fieldsIter = meta.getFields().iterator();
424 while (fieldsIter.hasNext())
425 {
426 LocalizedField field = (LocalizedField) fieldsIter.next();
427 String id = field.getId().toString();
428 String value = actionRequest.getParameter(id + ":value");
429 if (value != null)
430 {
431 if (!value.equals(field.getValue()))
432 {
433 field.setValue(value);
434 modified = true;
435 }
436 }
437 }
438 }
439 else if (action.equals("remove_metadata"))
440 {
441 String[] ids = actionRequest.getParameterValues("metadata_id");
442 if (ids != null)
443 {
444 Iterator fieldsIter = meta.getFields().iterator();
445 int count = 0;
446 while (fieldsIter.hasNext())
447 {
448 LocalizedField field = (LocalizedField) fieldsIter.next();
449 String id = field.getId().toString();
450
451 for(int i=0; i<ids.length; i++)
452 {
453 String mid = ids[i];
454 if(mid.equals(id))
455 {
456 fieldsIter.remove();
457 count++;
458 break;
459 }
460 }
461 }
462 modified = count > 0;
463 }
464 }
465 else if(action.equals("add_metadata"))
466 {
467 String name = actionRequest.getParameter("name");
468 String value = actionRequest.getParameter("value");
469 String localeParam = actionRequest.getParameter("locale");
470
471 if(localeParam == null || name.trim().length() == 0)
472 {
473 localeParam = "en";
474 }
475 Locale locale = new Locale(localeParam);
476
477 if (name != null && name.trim().length() > 0)
478 {
479 meta.addField(locale, name, value);
480 modified = true;
481 }
482 }
483
484 if (modified)
485 {
486 try {
487 if (pd == null)
488 {
489 registry.updatePortletApplication(pa);
490 }
491 else
492 {
493 registry.savePortletDefinition(pd);
494 }
495 }
496 catch(RegistryException e)
497 {
498 throw new PortletException("Failed to perform action " + action + " on "
499 + (pd == null ? "portlet application " + pa.getName() : "portlet definition " + pd.getName()) );
500 }
501 }
502 }
503
504 private void processPortletAction(ActionRequest actionRequest, ActionResponse actionResponse, MutablePortletApplication pa,
505 PortletDefinitionComposite portlet, String action) throws RegistryException
506 {
507 if(action.equals("edit_portlet"))
508 {
509 String displayNameParam = actionRequest.getParameter("display_name");
510 if(displayNameParam == null)
511 {
512 int index = 0;
513 Iterator displayNameIter = portlet.getDisplayNameSet().iterator();
514 while (displayNameIter.hasNext())
515 {
516 MutableDisplayName displayName = (MutableDisplayName) displayNameIter.next();
517 displayNameParam = actionRequest.getParameter("display_name:" + index);
518
519
520 if(displayNameParam != null)
521 {
522 if(displayNameParam.length() == 0)
523 {
524 displayNameIter.remove();
525 }
526 else if(!displayNameParam.equals(displayName.getDisplayName()))
527 {
528 displayName.setDisplayName(displayNameParam);
529 }
530 }
531 index++;
532 }
533 String expirationCache = actionRequest.getParameter("expirationCache");
534 if (expirationCache != null)
535 {
536 try
537 {
538 Integer.parseInt(expirationCache);
539 portlet.setExpirationCache(expirationCache);
540 }
541 catch (NumberFormatException e)
542 {}
543 }
544 }
545 else
546 {
547 String locale = actionRequest.getParameter("locale");
548 portlet.addDisplayName(new Locale(locale), displayNameParam);
549 }
550 }
551 else if(action.equals("remove_portlet"))
552 {
553
554 }
555 else if(action.equals("add_portlet"))
556 {
557
558 }
559 registry.savePortletDefinition(portlet);
560 }
561
562 /***
563 * @param actionRequest
564 * @param actionResponse
565 * @param pa
566 * @param pdef
567 * @param action
568 * @throws RegistryException
569 */
570 private void processPreferenceAction(ActionRequest actionRequest, ActionResponse actionResponse, MutablePortletApplication pa, PortletDefinitionComposite portlet, String action) throws RegistryException
571 {
572 if(action.equals("add_preference"))
573 {
574 String name = actionRequest.getParameter("name");
575 String value = actionRequest.getParameter("value");
576
577 PreferenceComposite pref = (PreferenceComposite) portlet.getPreferenceSet().get(name);
578 if(pref == null)
579 {
580 portlet.addPreference(name, new String[] { value });
581 }
582 else
583 {
584 pref.addValue(value);
585 }
586 }
587 else if(action.equals("edit_preference"))
588 {
589 String[] prefNames = actionRequest.getParameterValues("pref_edit_id");
590 for (int i = 0; i < prefNames.length; i++)
591 {
592 String prefName = prefNames[i];
593 PreferenceComposite prefComp = (PreferenceComposite) portlet.getPreferenceSet().get(prefName);
594 String[] values = prefComp.getValueArray();
595 for (int j = 0; j < values.length; j++)
596 {
597 String value = values[j];
598 String newValue = actionRequest.getParameter(prefName + ":" + j);
599 if(!value.equals(newValue))
600 {
601 prefComp.setValueAt(j, newValue);
602 }
603 }
604 }
605 }
606 else if(action.equals("remove_preference"))
607 {
608 String[] prefNames = actionRequest.getParameterValues("pref_remove_id");
609
610 Iterator prefIter = portlet.getPreferenceSet().iterator();
611 while (prefIter.hasNext())
612 {
613 PreferenceComposite pref = (PreferenceComposite) prefIter.next();
614 String name = pref.getName();
615
616 for(int i=0; i<prefNames.length; i++)
617 {
618 String prefName = prefNames[i];
619 if(name.equals(prefName))
620 {
621 prefIter.remove();
622 break;
623 }
624 }
625 }
626
627 }
628 registry.savePortletDefinition(portlet);
629 }
630
631 /***
632 * @param actionRequest
633 * @param actionResponse
634 * @param pa
635 * @param pdef
636 * @param action
637 * @throws RegistryException
638 */
639 private void processLanguage(ActionRequest actionRequest, ActionResponse actionResponse, MutablePortletApplication pa, PortletDefinitionComposite portlet, String action) throws RegistryException
640 {
641 if(action.equals("add_language"))
642 {
643 String title = actionRequest.getParameter("title");
644 String shortTitle = actionRequest.getParameter("short_title");
645 String keywords = actionRequest.getParameter("keyword");
646 String locale = actionRequest.getParameter("locale");
647
648 portlet.addLanguage(title, shortTitle, keywords, new Locale(locale));
649 }
650 else if(action.equals("remove_language"))
651 {
652 String[] removeIds = actionRequest.getParameterValues("language_remove_id");
653 if(removeIds != null)
654 {
655 int id = 0;
656 Iterator langIter = portlet.getLanguageSet().iterator();
657 while (langIter.hasNext())
658 {
659 langIter.next();
660
661 int currentId = id++;
662 for(int i=0; i<removeIds.length; i++)
663 {
664 String removeId = removeIds[i];
665 String tempId = "" + currentId;
666 if(removeId.equals(tempId))
667 {
668 langIter.remove();
669 break;
670 }
671 }
672 }
673 }
674 }
675 else if(action.equals("edit_language"))
676 {
677 String[] editIds = actionRequest.getParameterValues("language_edit_id");
678 if(editIds != null)
679 {
680
681
682
683 String id;
684 int index=0;
685 Iterator langIter = portlet.getLanguageSet().iterator();
686 while (langIter.hasNext())
687 {
688 id = editIds[index];
689
690 String title = actionRequest.getParameter("title:" + id);
691 String shortTitle = actionRequest.getParameter("short_title:" + id);
692
693
694 MutableLanguage lang = (MutableLanguage) langIter.next();
695
696 if(!lang.getTitle().equals(title))
697 {
698 lang.setTitle(title);
699 }
700
701 Iterator keywordIter = lang.getKeywords();
702 int keywordIndex = 0;
703 ArrayList keywordList = new ArrayList();
704
705 while (keywordIter.hasNext())
706 {
707 keywordIter.next();
708 String keywordParam = actionRequest.getParameter("keyword:" + id + ":" + keywordIndex);
709
710 if(keywordParam != null && keywordParam.length() > 0)
711 {
712 keywordList.add(keywordParam);
713 }
714
715 keywordIndex++;
716 }
717
718 lang.setKeywords(keywordList);
719 if(lang.getShortTitle() == null || !lang.getShortTitle().equals(shortTitle))
720 {
721 lang.setShortTitle(shortTitle);
722 }
723 index++;
724 }
725 }
726 }
727
728 registry.savePortletDefinition(portlet);
729 }
730
731 /***
732 * @param actionRequest
733 * @param actionResponse
734 * @param pa
735 * @param pdef
736 * @param action
737 * @throws FailedToStorePortletDefinitionException
738 */
739 private void processParameter(ActionRequest actionRequest, ActionResponse actionResponse, MutablePortletApplication pa, PortletDefinitionComposite portlet, String action) throws FailedToStorePortletDefinitionException
740 {
741 if(action.equals("add_parameter"))
742 {
743 String name = actionRequest.getParameter("name");
744 if(name != null)
745 {
746 String description = actionRequest.getParameter("description");
747 String locale = actionRequest.getParameter("locale");
748
749 ParameterComposite parameter = (ParameterComposite)portlet.getInitParameterSet().get(name);
750 if(parameter == null)
751 {
752 String value = actionRequest.getParameter("value");
753 parameter = portlet.addInitParameter(name, value, description, new Locale(locale));
754 }
755 else
756 {
757 parameter.addDescription(new Locale(locale), description);
758 }
759 }
760 }
761 else if(action.equals("edit_parameter"))
762 {
763 String[] paramIds = actionRequest.getParameterValues("parameter_edit_id");
764
765 if(paramIds != null)
766 {
767 for(int i=0; i<paramIds.length; i++)
768 {
769 String paramId = paramIds[i];
770 ParameterComposite param = (ParameterComposite) portlet.getInitParameterSet().get(paramId);
771
772 String value = actionRequest.getParameter(paramId + ":value");
773 param.setValue(value);
774
775 int index = 0;
776 Iterator descIter = param.getDescriptionSet().iterator();
777 while (descIter.hasNext())
778 {
779 MutableDescription description = (MutableDescription) descIter.next();
780 String descParam = actionRequest.getParameter(paramId + ":description:" + index);
781
782
783 if(descParam != null)
784 {
785 if(descParam.length() == 0)
786 {
787 descIter.remove();
788 }
789 else if(!descParam.equals(description.getDescription()))
790 {
791 description.setDescription(descParam);
792 }
793 }
794 index++;
795 }
796 }
797 }
798 }
799 else if(action.equals("remove_parameter"))
800 {
801 String[] paramIds = actionRequest.getParameterValues("parameter_remove_id");
802
803 if(paramIds != null)
804 {
805 Iterator paramIter = portlet.getInitParameterSet().iterator();
806 while (paramIter.hasNext())
807 {
808 ParameterComposite param = (ParameterComposite) paramIter.next();
809
810 for(int i=0; i<paramIds.length; i++)
811 {
812 String paramId = paramIds[i];
813 if(param.getName().equals(paramId))
814 {
815 paramIter.remove();
816 break;
817 }
818 }
819 }
820 }
821 }
822 registry.savePortletDefinition(portlet);
823 }
824
825 /***
826 * @param actionRequest
827 * @param actionResponse
828 * @param pa
829 * @param pdef
830 * @param action
831 * @throws FailedToStorePortletDefinitionException
832 */
833 private void processSecurity(ActionRequest actionRequest, ActionResponse actionResponse, MutablePortletApplication pa, PortletDefinitionComposite portlet, String action) throws FailedToStorePortletDefinitionException
834 {
835 if(action.equals("add_security"))
836 {
837 String name = actionRequest.getParameter("name");
838
839 if(name != null)
840 {
841 String link = actionRequest.getParameter("link");
842
843 SecurityRoleRefComposite securityRoleRef = (SecurityRoleRefComposite) portlet.getInitSecurityRoleRefSet().get(name);
844 if(securityRoleRef == null && link != null)
845 {
846 securityRoleRef = (SecurityRoleRefComposite) portlet.addSecurityRoleRef(name, link);
847 }
848
849 if(securityRoleRef != null)
850 {
851 String description = actionRequest.getParameter("description");
852 if(description != null && description.length() > 0)
853 {
854 String locale = actionRequest.getParameter("locale");
855 if(locale == null)
856 {
857 locale = "en";
858 }
859 securityRoleRef.addDescription(new Locale(locale), description);
860 }
861 }
862 }
863 }
864 else if(action.equals("edit_security"))
865 {
866 Iterator securityIter = portlet.getInitSecurityRoleRefSet().iterator();
867 while (securityIter.hasNext())
868 {
869 SecurityRoleRefComposite secRef = (SecurityRoleRefComposite) securityIter.next();
870 String name = secRef.getRoleName();
871
872
873
874 String link = actionRequest.getParameter(name + ":link");
875
876 if(!secRef.getRoleLink().equals(link))
877 {
878 secRef.setRoleLink(link);
879 }
880
881 int index = 0;
882 Iterator descIter = secRef.getDescriptionSet().iterator();
883 while (descIter.hasNext())
884 {
885 MutableDescription description = (MutableDescription) descIter.next();
886 String descParam = actionRequest.getParameter(name + ":description:" + index);
887
888
889 if(descParam != null)
890 {
891 if(descParam.length() == 0)
892 {
893 descIter.remove();
894 }
895 else if(!descParam.equals(description.getDescription()))
896 {
897 description.setDescription(descParam);
898 }
899 }
900
901 index++;
902 }
903 }
904 }
905 else if(action.equals("remove_security"))
906 {
907 String[] securityIds = actionRequest.getParameterValues("security_remove_id");
908 if(securityIds != null)
909 {
910 for(int i=0; i<securityIds.length; i++)
911 {
912 String id = securityIds[i];
913 SecurityRoleRef secRef = portlet.getInitSecurityRoleRefSet().get(id);
914 portlet.getInitSecurityRoleRefSet().remove(secRef);
915 }
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932 }
933 }
934 registry.savePortletDefinition(portlet);
935 }
936
937 private void processSecurityRef(ActionRequest actionRequest, ActionResponse actionResponse, MutablePortletApplication pa, PortletDefinitionComposite pdef, String action) throws PortletException
938 {
939 String ref = actionRequest.getParameter("security-constraint-ref");
940 String currentRef = "";
941 if(pa != null)
942 {
943 currentRef = pa.getJetspeedSecurityConstraint();
944 }
945 else
946 {
947 currentRef = pdef.getJetspeedSecurityConstraint();
948 }
949 if(currentRef == null) {
950 currentRef = "";
951 }
952 if(!currentRef.equals(ref)) {
953 if(ref.length() == 0) {
954 ref = null;
955 }
956
957 try
958 {
959 if(pa != null)
960 {
961 pa.setJetspeedSecurityConstraint(ref);
962 registry.updatePortletApplication(pa);
963 }
964 else
965 {
966 pdef.setJetspeedSecurityConstraint(ref);
967 registry.savePortletDefinition(pdef);
968 }
969 }
970 catch(RegistryException e)
971 {
972 throw new PortletException("Failed to perform action " + action + " on "
973 + (pdef == null ? "portlet application " + pa.getName() : "portlet definition " + pdef.getName()) );
974 }
975 }
976 }
977
978 /***
979 * @param actionRequest
980 * @param actionResponse
981 * @param pa
982 * @param pdef
983 * @param action
984 * @throws FailedToStorePortletDefinitionException
985 */
986 private void processContentType(ActionRequest actionRequest, ActionResponse actionResponse,
987 MutablePortletApplication pa, PortletDefinitionComposite portlet, String action) throws FailedToStorePortletDefinitionException
988 {
989 if(action.equals("add_content_type"))
990 {
991 String contentType = actionRequest.getParameter("content_type");
992 if(contentType != null)
993 {
994 ArrayList allModes = new ArrayList();
995 String[] modes = actionRequest.getParameterValues("mode");
996 if(modes != null)
997 {
998 for(int i=0; i<modes.length; i++)
999 {
1000 String mode = modes[i];
1001
1002 allModes.add(new PortletMode(mode));
1003 }
1004 }
1005
1006 String customModes = actionRequest.getParameter("custom_modes");
1007 StringTokenizer tok = new StringTokenizer(customModes, ",");
1008 while (tok.hasMoreTokens())
1009 {
1010
1011 allModes.add(tok.nextToken());
1012 }
1013
1014 portlet.addContentType(contentType, allModes);
1015 }
1016 }
1017 else if(action.equals("remove_content_type"))
1018 {
1019 String[] contentIds = actionRequest.getParameterValues("content_type_remove_id");
1020 if(contentIds != null)
1021 {
1022 Iterator contentIter = portlet.getContentTypeSet().iterator();
1023 while (contentIter.hasNext())
1024 {
1025 ContentType contentType = (ContentType) contentIter.next();
1026 for(int i=0; i<contentIds.length; i++)
1027 {
1028 String id = contentIds[i];
1029 if(contentType.getContentType().equals(id))
1030 {
1031 contentIter.remove();
1032 break;
1033 }
1034 }
1035 }
1036 }
1037 }
1038 registry.savePortletDefinition(portlet);
1039
1040 }
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255 }