1/*2 * Copyright 2000-2001,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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.portal.controls;
1819import java.util.Enumeration;
20import java.util.Iterator;
21import java.util.Vector;
2223// Jetspeed imports24import org.apache.jetspeed.om.registry.PortletControlEntry;
25import org.apache.jetspeed.om.registry.MediaTypeEntry;
26import org.apache.jetspeed.om.security.JetspeedUser;
27import org.apache.jetspeed.portal.Portlet;
28import org.apache.jetspeed.portal.PortletState;
29import org.apache.jetspeed.portal.PortletConfig;
30import org.apache.jetspeed.portal.PortletSet;
31import org.apache.jetspeed.portal.PortletControl;
32import org.apache.jetspeed.portal.PortletControlConfig;
33import org.apache.jetspeed.portal.PortletController;
34import org.apache.jetspeed.portal.PortletException;
35import org.apache.jetspeed.portal.portlets.AbstractPortlet;
36import org.apache.jetspeed.portal.security.portlets.PortletWrapper;
37import org.apache.jetspeed.services.Registry;
38import org.apache.jetspeed.services.JetspeedSecurity;
39import org.apache.jetspeed.util.MimeType;
40import org.apache.jetspeed.services.resources.JetspeedResources;
4142// Turbine imports43import org.apache.turbine.util.RunData;
4445// ECS imports46import org.apache.ecs.ElementContainer;
47import org.apache.ecs.ConcreteElement;
48import org.apache.ecs.StringElement;
4950/***51 * The PortletControl acts both as a decoration around a Portlet or PortletSet52 * and also as the window manager for the enclosed Portlet(Set).53 * As such it controls the operation that may be performed on this portlet54 * and whether the portlet content should be displayed or not.55 * PortletControl also delegates all Portlet and PortletSet methods to its56 * inner object and can thus be transparently cascaded or substituted to57 * a simple portlet wherever in a PSML object tree.58 *59 * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>60 * @author <a href="mailto:sgala@apache.org">Santiago Gala</a>61 * @version $Id: AbstractPortletControl.java,v 1.22 2004/03/29 21:16:27 taylor Exp $62 */63publicabstractclassAbstractPortletControlextendsAbstractPortlet64 implements PortletControl65 {
6667publicstaticfinal String COLOR = "#CCCCCC";
68publicstaticfinal String BACKGROUND_COLOR = "#FFFFFF";
69publicstaticfinal String TITLE_COLOR = "#DDDDDD";
70publicstaticfinal String WIDTH = "100%";
7172privatePortlet portlet = null;
73privatePortletControlConfig conf = null;
7475// PortletControl specifc interface7677/***78 * @return the PortletControl specific configuration79 */80publicPortletControlConfig getConfig()
81 {
82returnthis.conf;
83 }
8485/***86 * Sets the PortletControl specific configuration87 * @param conf the new PortletControl configuration88 */89publicvoid setConfig(PortletControlConfig conf)
90 {
91this.conf=conf;
92 }
9394/***95 * Sets the portlet to be managed by this control96 * 97 * @param portlet the new portlet to be managed by the control98 */99publicvoid setPortlet(Portlet portlet)
100 {
101this.portlet=portlet;
102 }
103104/***105 * Retrieves the portlet managed by this control106 * @return the portlet object managed or null107 */108publicPortlet getPortlet()
109 {
110returnthis.portlet;
111 }
112113/***114 * Initializes the control and associates it with a portlet115 *116 * @param portlet the portlet to be managed by this control117 */118publicvoid init( Portlet portlet )
119 {
120this.setPortlet( portlet );
121 }
122123/***124 * Returns the color to use for displaying the portlet text125 * 126 * This method is deprecated and is only useful for ECS127 * based Control implementation128 * @deprecated129 * @return the text color value in HTML format (#RRGGBB)130 */131public String getColor()
132 {
133return getPortlet().getPortletConfig().getPortletSkin().getTextColor();
134 }
135136/***137 * Sets the color to use for displaying the portlet text138 * 139 * This method is deprecated and is only useful for ECS140 * based Control implementation141 * @deprecated142 * @param color the text color value in HTML format (#RRGGBB)143 */144publicvoid setColor(String color)
145 {
146 getPortlet().getPortletConfig().getPortletSkin().setTextColor( color );
147 }
148149/***150 * Returns the color to use for displaying the portlet background151 * 152 * This method is deprecated and is only useful for ECS153 * based Control implementation154 * @deprecated155 * @return the text color value in HTML format (#RRGGBB)156 */157public String getBackgroundColor()
158 {
159return getPortlet().getPortletConfig().getPortletSkin().getBackgroundColor();
160 }
161162/***163 * Sets the color to use for displaying the portlet background164 * 165 * This method is deprecated and is only useful for ECS166 * based Control implementation167 * @deprecated168 * @param backgroundColor the background color value in HTML format (#RRGGBB)169 */170publicvoid setBackgroundColor(String backgroundColor)
171 {
172 getPortlet().getPortletConfig().getPortletSkin().setBackgroundColor( backgroundColor );
173 }
174175/***176 * Returns the color to use for displaying the portlet title177 * 178 * This method is deprecated and is only useful for ECS179 * based Control implementation180 * @deprecated181 * @return the text color value in HTML format (#RRGGBB)182 */183public String getTitleColor()
184 {
185return getPortlet().getPortletConfig().getPortletSkin().getTitleBackgroundColor();
186 }
187188/***189 * Sets the color to use for displaying the portlet title190 * 191 * This method is deprecated and is only useful for ECS192 * based Control implementation193 * @deprecated194 * @param titleColor the title color value in HTML format (#RRGGBB)195 */196publicvoid setTitleColor(String titleColor)
197 {
198 getPortlet().getPortletConfig().getPortletSkin().setTitleBackgroundColor( titleColor );
199 }
200201/***202 * Returns the width of the managed portlet relative to the size of203 * portlet control.204 * This method is deprecated and is only useful for ECS205 * based Control implementation206 * @deprecated207 * @return the size value. May be expressed in percentages (eg, "80%")208 */209public String getWidth() {
210return getPortlet().getPortletConfig().getSkin( "width", WIDTH );
211 }
212213/***214 * Sets the width of the managed portlet relative to the size of215 * portlet control.216 * 217 * This method is deprecated and is only useful for ECS218 * based Control implementation219 * @deprecated220 * @param width the width of portlet. Must be a positive, non nul integer221 */222publicvoid setWidth(int width)
223 {
224 getPortlet().getPortletConfig().setSkin( "width", String.valueOf(width) );
225 }
226227/***228 * Sets the width of the managed portlet relative to the size of229 * portlet control.230 * 231 * This method is deprecated and is only useful for ECS232 * based Control implementation233 * @deprecated234 * @param width the width of portlet. Must be parseable as a positive, non 235 * nul integer or a percentage236 */237publicvoid setWidth(String width)
238 {
239 getPortlet().getPortletConfig().setSkin( "width", width );
240 }
241242243// Portlet interface implementation, delegates everything 244// to the managed portlet245246/***247 * @return the inner portlet config object248 */249publicPortletConfig getPortletConfig()
250 {
251if (getPortlet()==null) returnnull;
252return getPortlet().getPortletConfig();
253 }
254255/***256 * Updates the managed portlet config object257 *258 * @param portletConfig the new portet config object259 */260publicvoid setPortletConfig(PortletConfig portletConfig)
261 {
262if (getPortlet()==null) return;
263 getPortlet().setPortletConfig(portletConfig);
264 }
265266/***267 * @return the inner portlet name268 */269public String getName()
270 {
271if (getPortlet()==null) returnnull;
272return getPortlet().getName();
273 }
274275/***276 * Updates the managed portlet name277 *278 * @param name the new portet name279 */280publicvoid setName(String name)
281 {
282if (getPortlet()!=null) getPortlet().setName(name);
283 }
284285/***286 * @return the inner portlet title287 */288public String getTitle()
289 {
290if (getPortlet()==null) returnnull;
291return getPortlet().getTitle();
292 }
293294/***295 * Set the title for the inner portlet296 *297 * @param title the new portlet title298 */299publicvoid setTitle( String title )
300 {
301if (getPortlet()!=null) getPortlet().setTitle(title);
302 }
303304/***305 * @return the inner portlet description306 */307public String getDescription()
308 {
309if (getPortlet()==null) returnnull;
310return getPortlet().getDescription();
311 }
312313/***314 * Set the description for the inner portlet315 *316 * @param description the new portlet description317 */318publicvoid setDescription( String description )
319 {
320if (getPortlet()!=null) getPortlet().setDescription(description);
321 }
322323/***324 * Inits the inner portlet325 */326publicvoid init() throws PortletException
327 {
328if (getPortlet()!=null) getPortlet().init();
329 }
330331/***332 * Returns the content of inner portlet333 *334 * @param rundata the RunData for the request335 */336public ConcreteElement getContent(RunData rundata)
337 {
338Portlet portlet = getPortlet();
339if (portlet == null)
340 {
341returnnew ElementContainer();
342 }
343344// Check to see if the portlet allows view345// If the current security context disallows view,346// do not display the portlet OR the control decorator347if (portlet instanceof PortletWrapper)
348 {
349PortletWrapper wrapper = (PortletWrapper)portlet;
350if (!wrapper.getAllowView(rundata))
351 {
352if (JetspeedResources.getBoolean("defaultportletcontrol.hide.decorator", true))
353 {
354returnnew StringElement("");
355 }
356 }
357 }
358359return portlet.getContent( rundata );
360 }
361362/***363 @see Portlet#supportsType364 */365publicboolean supportsType( MimeType mimeType )
366 {
367boolean allowed = true;
368369if (getPortlet()!=null)
370 {
371 allowed = getPortlet().supportsType( mimeType );
372 }
373374// the inner portlet does not support the type, return immediately...375if (allowed)
376 {
377// we now need to check that the control also supports the type...378PortletControlEntry entry =
379 (PortletControlEntry)Registry.getEntry(Registry.PORTLET_CONTROL,
380 getConfig().getName() );
381 String baseType = mimeType.toString();
382383if (entry!=null)
384 {
385 Iterator i = entry.listMediaTypes();
386387while(i.hasNext())
388 {
389 String name = (String)i.next();
390MediaTypeEntry media = (MediaTypeEntry)Registry.getEntry(Registry.MEDIA_TYPE, name);
391392if (media != null)
393 {
394if (baseType.equals(media.getMimeType()))
395 {
396 allowed = true;
397break;
398 }
399 }
400 }
401 }
402 }
403404return allowed;
405 }
406407// delegate the portletSet interface to the stored PortletSet408// or emulate a 1-element portletSet409410/***411 * @return the size of the inner portletSet, 1 if the inner portlet412 * is not a set, or 0 if there's no inner portlet413 */414publicint size()
415 {
416if (getPortlet() instanceof PortletSet)
417return ((PortletSet)getPortlet()).size();
418419return ((getPortlet()==null)?0:1);
420 }
421422/***423 * @return an array of portlet contained within the inner PortletSet424 * or the inner portlet425 */426publicPortlet[] toArray()
427 {
428if (getPortlet() instanceof PortletSet)
429return ((PortletSet)getPortlet()).toArray();
430431Portlet[] p = newPortlet[1];
432 p[0]=getPortlet();
433434return p;
435 }
436437/***438 * Returns the Portlet at the given position if the control manages439 * a PortletSet, else the inner portlet if the queried position is 0440 *441 * @param pos the queried position. Must be strictly smaller than the442 * value of size()443 * @return the portlet object stored at this position444 */445publicPortlet getPortletAt(int pos)
446 {
447if (getPortlet() instanceof PortletSet)
448return ((PortletSet)getPortlet()).getPortletAt(pos);
449450if (pos==0)
451return getPortlet();
452else453returnnull;
454 }
455456/***457 * Returns the elements contained within the inner PortletSet or the458 * inner portlet459 *460 * @return an enumeration of portlets461 */462public Enumeration getPortlets()
463 {
464if (getPortlet() instanceof PortletSet)
465return ((PortletSet)getPortlet()).getPortlets();
466467 Vector v = new Vector();
468if (getPortlet()!=null) v.addElement(getPortlet());
469470return v.elements();
471 }
472473/***474 * Adds a portlet to the inner portletSet475 *476 * @param portlet the portlet to add477 */478publicsynchronizedvoid addPortlet(Portlet portlet)
479 {
480if (getPortlet() instanceof PortletSet)
481 ((PortletSet)getPortlet()).addPortlet(portlet);
482 }
483484/***485 * Adds a portlet to the inner portletSet at the explicit position486 *487 * @param portlet the portlet to add488 * @param position the position of the new portlet489 */490publicsynchronizedvoid addPortlet(Portlet portlet, int position)
491 {
492if (getPortlet() instanceof PortletSet)
493 ((PortletSet)getPortlet()).addPortlet(portlet,position);
494 }
495496/***497 * Adds a portlet to the inner portletSet within the given constraints498 *499 * @param portlet the portlet to add500 * @param constraints the portlet layout constraints501 */502publicsynchronizedvoid addPortlet(Portlet portlet, PortletSet.Constraints constraints)
503 {
504if (getPortlet() instanceof PortletSet)
505 ((PortletSet)getPortlet()).addPortlet(portlet,constraints);
506 }
507508/***509 * Adds a portlet to the inner portletSet within the given constraints at the510 * specific position511 *512 * @param portlet the portlet to add513 * @param constraint the portlet layout constraints514 * @param position the portlet required position515 */516publicsynchronizedvoid addPortlet(Portlet portlet, PortletSet.Constraints constraint, int position)
517 {
518if (getPortlet() instanceof PortletSet)
519 ((PortletSet)getPortlet()).addPortlet(portlet,constraint,position);
520 }
521522/***523 * @return the controller for the inner portletSet or null524 */525publicPortletController getController()
526 {
527if (getPortlet() instanceof PortletSet)
528return ((PortletSet)getPortlet()).getController();
529530returnnull;
531 }
532533/***534 * Sets the controller for the inner PortletSet. Has no effect if their535 * is no inner portletSet536 *537 * @parama controller the new controller for the inner portletSet538 */539publicsynchronizedvoid setController(PortletController controller)
540 {
541if (getPortlet() instanceof PortletSet)
542 ((PortletSet)getPortlet()).setController(controller);
543 }
544545// Delegate PortletState Interface546547/***548 * Implements the default close behavior: any authenticated user may549 * remove a portlet from his page550 *551 * @param rundata the RunData object for the current request552 */553publicboolean allowClose( RunData rundata )
554 {
555Portlet p = getPortlet();
556557if (p==null) return false;
558559if ((p instanceof PortletSet)
560 ||(JetspeedSecurity.checkPermission((JetspeedUser) rundata.getUser(),
561 p,
562 JetspeedSecurity.PERMISSION_CLOSE)))
563 {
564if (p instanceof PortletState)
565 {
566return ((PortletState)p).allowClose(rundata);
567 }
568 }
569570return false;
571 }
572573/***574 * Returns true if this portlet is currently closed575 */576publicboolean isClosed(RunData rundata)
577 {
578Portlet p = getPortlet();
579580if ( (p!=null) && (p instanceof PortletState) )
581 {
582return ((PortletState)p).isClosed(rundata);
583 }
584585return false;
586 }
587588/***589 * Toggles the portlet state between closed and normal590 *591 * @param minimized the new portlet state592 * @param data the RunData for this request593 */594publicvoid setClosed(boolean close, RunData rundata)
595 {
596Portlet p = getPortlet();
597598if ( (p!=null) && (p instanceof PortletState) )
599 {
600 ((PortletState)p).setClosed(close, rundata);
601 }
602 }
603604/***605 * Implements the default info behavior: any authenticated user may606 * get information on a portlet607 *608 * @param rundata the RunData object for the current request609 */610publicboolean allowInfo( RunData rundata )
611 {
612Portlet p = getPortlet();
613614if (p==null) return false;
615616if ((p instanceof PortletSet)
617 ||(JetspeedSecurity.checkPermission((JetspeedUser) rundata.getUser(),
618 p,
619 JetspeedSecurity.PERMISSION_INFO)))
620 {
621if (p instanceof PortletState)
622 {
623return ((PortletState)p).allowInfo(rundata);
624 }
625 }
626627return false;
628 }
629630/***631 * Implements the default customize behavior: any authenticated user may632 * customize a portlet633 *634 * @param rundata the RunData object for the current request635 */636publicboolean allowCustomize( RunData rundata )
637 {
638Portlet p = getPortlet();
639640if (p==null) return false;
641642if ((p instanceof PortletSet)
643 ||(JetspeedSecurity.checkPermission((JetspeedUser) rundata.getUser(),
644 p,
645 JetspeedSecurity.PERMISSION_CUSTOMIZE)))
646 {
647if (p instanceof PortletState)
648 {
649return ((PortletState)p).allowCustomize(rundata);
650 }
651 }
652653return false;
654 }
655656/***657 * Implements the default maximize behavior: any authenticated user may658 * maximize a portlet659 *660 * @param rundata the RunData object for the current request661 */662publicboolean allowMaximize( RunData rundata )
663 {
664Portlet p = getPortlet();
665666if (p==null) return false;
667668if ((p instanceof PortletSet)
669 ||(JetspeedSecurity.checkPermission((JetspeedUser) rundata.getUser(),
670 p,
671 JetspeedSecurity.PERMISSION_MAXIMIZE)))
672 {
673if (p instanceof PortletState)
674 {
675return ((PortletState)p).allowMaximize(rundata);
676 }
677 }
678679return false;
680 }
681682/***683 * Implements the default info behavior: any authenticated user may684 * minimize a portlet685 *686 * @param rundata the RunData object for the current request687 */688publicboolean allowMinimize( RunData rundata )
689 {
690Portlet p = getPortlet();
691692if (p==null) return false;
693694if ((p instanceof PortletSet)
695 ||(JetspeedSecurity.checkPermission((JetspeedUser) rundata.getUser(),
696 p,
697 JetspeedSecurity.PERMISSION_MINIMIZE)))
698 {
699if (p instanceof PortletState)
700 {
701return ((PortletState)p).allowMinimize(rundata);
702 }
703 }
704705return false;
706 }
707708/***709 * Implements the default info behavior: any authenticated user may710 * display portlet in print friendly format711 *712 * @param rundata the RunData object for the current request713 */714publicboolean allowPrintFriendly( RunData rundata )
715 {
716Portlet p = getPortlet();
717718if (p==null) return false;
719720if ((p instanceof PortletSet)
721 ||(JetspeedSecurity.checkPermission((JetspeedUser) rundata.getUser(),
722 p,
723 JetspeedSecurity.PERMISSION_PRINT_FRIENDLY)))
724 {
725if (p instanceof PortletState)
726 {
727return ((PortletState)p).allowPrintFriendly(rundata);
728 }
729 }
730731return false;
732 }
733734/***735 * Returns true if this portlet is currently minimized736 */737publicboolean isMinimized(RunData rundata)
738 {
739Portlet p = getPortlet();
740741if ( (p!=null) && (p instanceof PortletState) )
742 {
743return ((PortletState)p).isMinimized(rundata);
744 }
745746return false;
747 }
748749/***750 Change the portlet visibility state ( minimized <-> normal )751752 @param minimize True if the portlet change to minimized753 @param rundata A RunData object754 */755publicvoid setMinimized( boolean minimize, RunData rundata )
756 {
757Portlet p = getPortlet();
758759if ( (p!=null) && (p instanceof PortletState) )
760 {
761 ((PortletState)p).setMinimized( minimize, rundata);
762 }
763 }
764765public String getID()
766 {
767returnthis.portlet.getID();
768 }
769770publicvoid setID(String id)
771 {
772this.portlet.setID(id);
773 }
774775/***776 Returns the Portlet with the given id777 */778publicPortlet getPortletByID(String id)
779 {
780Portlet p = getPortlet();
781if (null == p)
782returnnull;
783784if (p instanceof PortletSet)
785 {
786 Enumeration e = ((PortletSet)p).getPortlets();
787Portlet portlet = null;
788while ( e.hasMoreElements() )
789 {
790 portlet = (Portlet)e.nextElement();
791if (portlet.getID().equals(id))
792return portlet;
793 }
794return portlet;
795 }
796returnnull;
797 }
798799/***800 Returns the Portlet with the given name801 */802publicPortlet getPortletByName(String name)
803 {
804Portlet p = getPortlet();
805if (null == p)
806returnnull;
807808if (p instanceof PortletSet)
809 {
810 Enumeration e = ((PortletSet)p).getPortlets();
811Portlet portlet = null;
812while ( e.hasMoreElements() )
813 {
814 portlet = (Portlet)e.nextElement();
815if (portlet.getName().equals(name))
816return portlet;
817 }
818return portlet;
819 }
820returnnull;
821 }
822823824 }