View Javadoc

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 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  
17  package org.apache.jetspeed.portal.controls;
18  
19  import java.util.Enumeration;
20  import java.util.Iterator;
21  import java.util.Vector;
22  
23  // Jetspeed imports
24  import org.apache.jetspeed.om.registry.PortletControlEntry;
25  import org.apache.jetspeed.om.registry.MediaTypeEntry;
26  import org.apache.jetspeed.om.security.JetspeedUser;
27  import org.apache.jetspeed.portal.Portlet;
28  import org.apache.jetspeed.portal.PortletState;
29  import org.apache.jetspeed.portal.PortletConfig;
30  import org.apache.jetspeed.portal.PortletSet;
31  import org.apache.jetspeed.portal.PortletControl;
32  import org.apache.jetspeed.portal.PortletControlConfig;
33  import org.apache.jetspeed.portal.PortletController;
34  import org.apache.jetspeed.portal.PortletException;
35  import org.apache.jetspeed.portal.portlets.AbstractPortlet;
36  import org.apache.jetspeed.portal.security.portlets.PortletWrapper;
37  import org.apache.jetspeed.services.Registry;
38  import org.apache.jetspeed.services.JetspeedSecurity;
39  import org.apache.jetspeed.util.MimeType;
40  import org.apache.jetspeed.services.resources.JetspeedResources;
41  
42  // Turbine imports
43  import org.apache.turbine.util.RunData;
44  
45  // ECS imports
46  import org.apache.ecs.ElementContainer;
47  import org.apache.ecs.ConcreteElement;
48  import org.apache.ecs.StringElement;
49  
50  /***
51   * The PortletControl acts both as a decoration around a Portlet or PortletSet
52   * and also as the window manager for the enclosed Portlet(Set).
53   * As such it controls the operation that may be performed on this portlet
54   * and whether the portlet content should be displayed or not.
55   * PortletControl also delegates all Portlet and PortletSet methods to its
56   * inner object and can thus be transparently cascaded or substituted to
57   * 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   */
63  public abstract class AbstractPortletControl extends AbstractPortlet
64      implements PortletControl
65  {
66  
67      public static final String  COLOR               = "#CCCCCC";
68      public static final String  BACKGROUND_COLOR    = "#FFFFFF";
69      public static final String  TITLE_COLOR         = "#DDDDDD";
70      public static final String  WIDTH               = "100%";
71  
72      private Portlet                 portlet = null;
73      private PortletControlConfig    conf = null;
74  
75      // PortletControl specifc interface
76      
77      /***
78       * @return the PortletControl specific configuration
79       */
80      public PortletControlConfig getConfig()
81      {
82          return this.conf;
83      }
84  
85      /***
86       * Sets the PortletControl specific configuration
87       * @param conf the new PortletControl configuration
88       */
89      public void setConfig(PortletControlConfig conf)
90      {
91          this.conf=conf;
92      }
93  
94      /***
95       * Sets the portlet to be managed by this control
96       * 
97       * @param portlet the new portlet to be managed by the control
98       */
99      public void setPortlet(Portlet portlet)
100     {
101         this.portlet=portlet;
102     }
103 
104     /***
105      * Retrieves the portlet managed by this control
106      * @return the portlet object managed or null
107      */
108     public Portlet getPortlet()
109     {
110         return this.portlet;
111     }
112 
113     /***
114      * Initializes the control and associates it with a portlet
115      *
116      * @param portlet the portlet to be managed by this control
117      */
118     public void init( Portlet portlet )
119     {
120         this.setPortlet( portlet );
121     }
122 
123     /***
124      * Returns the color to use for displaying the portlet text
125      * 
126      * This method is deprecated and is only useful for ECS
127      * based Control implementation
128      * @deprecated
129      * @return the text color value in HTML format (#RRGGBB)
130      */
131     public String getColor()
132     {
133         return getPortlet().getPortletConfig().getPortletSkin().getTextColor();
134     }
135 
136     /***
137      * Sets the color to use for displaying the portlet text
138      * 
139      * This method is deprecated and is only useful for ECS
140      * based Control implementation
141      * @deprecated
142      * @param color the text color value in HTML format (#RRGGBB)
143      */
144     public void setColor(String color)
145     {
146         getPortlet().getPortletConfig().getPortletSkin().setTextColor( color );
147     }
148 
149     /***
150      * Returns the color to use for displaying the portlet background
151      * 
152      * This method is deprecated and is only useful for ECS
153      * based Control implementation
154      * @deprecated
155      * @return the text color value in HTML format (#RRGGBB)
156      */
157     public String getBackgroundColor()
158     {
159         return getPortlet().getPortletConfig().getPortletSkin().getBackgroundColor();
160     }
161 
162     /***
163      * Sets the color to use for displaying the portlet background
164      * 
165      * This method is deprecated and is only useful for ECS
166      * based Control implementation
167      * @deprecated
168      * @param backgroundColor the background color value in HTML format (#RRGGBB)
169      */
170     public void setBackgroundColor(String backgroundColor)
171     {
172         getPortlet().getPortletConfig().getPortletSkin().setBackgroundColor( backgroundColor );
173     }
174 
175     /***
176      * Returns the color to use for displaying the portlet title
177      * 
178      * This method is deprecated and is only useful for ECS
179      * based Control implementation
180      * @deprecated
181      * @return the text color value in HTML format (#RRGGBB)
182      */
183     public String getTitleColor()
184     {
185         return getPortlet().getPortletConfig().getPortletSkin().getTitleBackgroundColor();
186     }
187 
188     /***
189      * Sets the color to use for displaying the portlet title
190      * 
191      * This method is deprecated and is only useful for ECS
192      * based Control implementation
193      * @deprecated
194      * @param titleColor the title color value in HTML format (#RRGGBB)
195      */
196     public void setTitleColor(String titleColor)
197     {
198         getPortlet().getPortletConfig().getPortletSkin().setTitleBackgroundColor( titleColor );
199     }
200 
201     /***
202      * Returns the width of the managed portlet relative to the size of
203      * portlet control.
204      * This method is deprecated and is only useful for ECS
205      * based Control implementation
206      * @deprecated
207      * @return the size value. May be expressed in percentages (eg, "80%")
208      */
209     public String getWidth() {
210         return getPortlet().getPortletConfig().getSkin( "width", WIDTH );
211     }
212 
213     /***
214      * Sets the width of the managed portlet relative to the size of
215      * portlet control.
216      * 
217      * This method is deprecated and is only useful for ECS
218      * based Control implementation
219      * @deprecated
220      * @param width the width of portlet. Must be a positive, non nul integer
221      */
222     public void setWidth(int width)
223     {
224         getPortlet().getPortletConfig().setSkin( "width", String.valueOf(width) );
225     }
226 
227     /***
228      * Sets the width of the managed portlet relative to the size of
229      * portlet control.
230      * 
231      * This method is deprecated and is only useful for ECS
232      * based Control implementation
233      * @deprecated
234      * @param width the width of portlet. Must be parseable as a positive, non 
235      * nul integer or a percentage
236      */
237     public void setWidth(String width)
238     {
239         getPortlet().getPortletConfig().setSkin( "width", width );
240     }
241 
242 
243     // Portlet interface implementation, delegates everything 
244     // to the managed portlet
245 
246     /***
247      * @return the inner portlet config object
248      */
249     public PortletConfig getPortletConfig()
250     {
251         if (getPortlet()==null) return null;
252         return getPortlet().getPortletConfig();
253     }
254 
255     /***
256      * Updates the managed portlet config object
257      *
258      * @param portletConfig the new portet config object
259      */
260     public void setPortletConfig(PortletConfig portletConfig)
261     {
262         if (getPortlet()==null) return;
263         getPortlet().setPortletConfig(portletConfig);
264     }
265 
266     /***
267      * @return the inner portlet name
268      */
269     public String getName()
270     {   
271         if (getPortlet()==null) return null;
272         return getPortlet().getName();
273     }
274 
275     /***
276      * Updates the managed portlet name
277      *
278      * @param name the new portet name
279      */
280     public void setName(String name)
281     {   
282         if (getPortlet()!=null) getPortlet().setName(name);
283     }
284 
285     /***
286      * @return the inner portlet title
287      */
288     public String getTitle()
289     {
290         if (getPortlet()==null) return null;
291         return getPortlet().getTitle();
292     }
293 
294     /***
295      * Set the title for the inner portlet
296      *
297      * @param title the new portlet title
298      */
299     public void setTitle( String title )
300     {   
301         if (getPortlet()!=null) getPortlet().setTitle(title);
302     }
303 
304     /***
305      * @return the inner portlet description
306      */
307     public String getDescription()
308     {
309         if (getPortlet()==null) return null;
310         return getPortlet().getDescription();
311     }
312 
313     /***
314      * Set the description for the inner portlet
315      *
316      * @param description the new portlet description
317      */
318     public void setDescription( String description )
319     {   
320         if (getPortlet()!=null) getPortlet().setDescription(description);
321     }
322 
323     /***
324      * Inits the inner portlet
325      */
326     public void init() throws PortletException
327     {
328         if (getPortlet()!=null) getPortlet().init();
329     }
330 
331     /***
332      * Returns the content of inner portlet
333      *
334      * @param rundata the RunData for the request
335      */
336     public ConcreteElement getContent(RunData rundata)
337     {
338         Portlet portlet = getPortlet(); 
339         if (portlet == null)
340         {
341             return new ElementContainer();
342         }
343         
344         // Check to see if the portlet allows view
345         // If the current security context disallows view,
346         // do not display the portlet OR the control decorator
347         if (portlet instanceof PortletWrapper)
348         {
349             PortletWrapper wrapper = (PortletWrapper)portlet;
350             if (!wrapper.getAllowView(rundata))
351             {
352                 if (JetspeedResources.getBoolean("defaultportletcontrol.hide.decorator", true))
353                 {
354                     return new StringElement("");
355                 }
356             }
357         }
358         
359         return portlet.getContent( rundata );
360     }
361 
362     /***
363     @see Portlet#supportsType
364     */
365     public boolean supportsType( MimeType mimeType )
366     {
367         boolean allowed = true;
368 
369         if (getPortlet()!=null)
370         {
371             allowed = getPortlet().supportsType( mimeType );
372         }
373         
374         // the inner portlet does not support the type, return immediately...
375         if (allowed)
376         {
377             // we now need to check that the control also supports the type...
378             PortletControlEntry entry = 
379                 (PortletControlEntry)Registry.getEntry(Registry.PORTLET_CONTROL,
380                                                    getConfig().getName() );
381             String baseType = mimeType.toString();
382 
383             if (entry!=null)
384             {
385                 Iterator i = entry.listMediaTypes();
386     
387                 while(i.hasNext())
388                 {
389                     String name = (String)i.next();
390                     MediaTypeEntry media = (MediaTypeEntry)Registry.getEntry(Registry.MEDIA_TYPE, name);
391 
392                     if (media != null)
393                     {
394                         if (baseType.equals(media.getMimeType())) 
395                         {
396                             allowed = true;
397                             break;
398                         }
399                     }
400                 }
401             }
402         }
403         
404         return allowed;
405     }
406 
407     // delegate the portletSet interface to the stored PortletSet
408     // or emulate a 1-element portletSet
409 
410     /***
411      * @return the size of the inner portletSet, 1 if the inner portlet
412      * is not a set, or 0 if there's no inner portlet
413      */
414     public int size()
415     {
416         if (getPortlet() instanceof PortletSet)
417             return ((PortletSet)getPortlet()).size();
418 
419         return ((getPortlet()==null)?0:1);
420     }
421 
422     /***
423      * @return an array of portlet contained within the inner PortletSet
424      * or the inner portlet
425      */
426     public Portlet[] toArray()
427     {
428         if (getPortlet() instanceof PortletSet)
429             return ((PortletSet)getPortlet()).toArray();
430 
431         Portlet[] p = new Portlet[1];
432         p[0]=getPortlet();
433 
434         return p;
435     }
436 
437     /***
438      * Returns the Portlet at the given position if the control manages
439      * a PortletSet, else the inner portlet if the queried position is 0
440      *
441      * @param pos the queried position. Must be strictly smaller than the
442      * value of size()
443      * @return the portlet object stored at this position
444      */
445     public Portlet getPortletAt(int pos)
446     {
447         if (getPortlet() instanceof PortletSet)
448             return ((PortletSet)getPortlet()).getPortletAt(pos);
449 
450         if (pos==0)
451             return getPortlet();
452         else
453             return null;
454     }
455 
456     /***
457      * Returns the elements contained within the inner PortletSet or the
458      * inner portlet
459      *
460      * @return an enumeration of portlets
461      */
462     public Enumeration getPortlets()
463     {
464         if (getPortlet() instanceof PortletSet)
465             return ((PortletSet)getPortlet()).getPortlets();
466 
467         Vector v = new Vector();
468         if (getPortlet()!=null) v.addElement(getPortlet());
469 
470         return v.elements();
471     }
472 
473     /***
474      * Adds a portlet to the inner portletSet
475      *
476      * @param portlet the portlet to add
477      */
478     public synchronized void addPortlet(Portlet portlet)
479     {
480         if (getPortlet() instanceof PortletSet)
481             ((PortletSet)getPortlet()).addPortlet(portlet);
482     }
483 
484     /***
485      * Adds a portlet to the inner portletSet at the explicit position
486      *
487      * @param portlet the portlet to add
488      * @param position the position of the new portlet
489      */
490     public synchronized void addPortlet(Portlet portlet, int position)
491     {
492         if (getPortlet() instanceof PortletSet)
493             ((PortletSet)getPortlet()).addPortlet(portlet,position);
494     }
495 
496     /***
497      * Adds a portlet to the inner portletSet within the given constraints
498      *
499      * @param portlet the portlet to add
500      * @param constraints the portlet layout constraints
501      */
502     public synchronized void addPortlet(Portlet portlet, PortletSet.Constraints constraints)
503     {
504         if (getPortlet() instanceof PortletSet)
505             ((PortletSet)getPortlet()).addPortlet(portlet,constraints);
506     }
507 
508     /***
509      * Adds a portlet to the inner portletSet within the given constraints at the
510      * specific position
511      *
512      * @param portlet the portlet to add
513      * @param constraint the portlet layout constraints
514      * @param position the portlet required position
515      */
516     public synchronized void addPortlet(Portlet portlet, PortletSet.Constraints constraint, int position)
517     {
518         if (getPortlet() instanceof PortletSet)
519             ((PortletSet)getPortlet()).addPortlet(portlet,constraint,position);
520     }
521 
522     /***
523      * @return the controller for the inner portletSet or null
524      */
525     public PortletController getController()
526     {
527         if (getPortlet() instanceof PortletSet)
528             return ((PortletSet)getPortlet()).getController();
529 
530         return null;
531     }
532 
533     /***
534      * Sets the controller for the inner PortletSet. Has no effect if their
535      * is no inner portletSet
536      *
537      * @parama controller the new controller for the inner portletSet
538      */
539     public synchronized void setController(PortletController controller)
540     {
541         if (getPortlet() instanceof PortletSet)
542             ((PortletSet)getPortlet()).setController(controller);
543     }
544 
545     // Delegate PortletState Interface
546     
547     /***
548      * Implements the default close behavior: any authenticated user may
549      * remove a portlet from his page
550      *
551      * @param rundata the RunData object for the current request
552      */
553     public boolean allowClose( RunData rundata )
554     {
555         Portlet p = getPortlet();
556 
557         if (p==null) return false;
558         
559         if ((p instanceof PortletSet)
560             ||(JetspeedSecurity.checkPermission((JetspeedUser) rundata.getUser(),
561                                                 p,
562                                                 JetspeedSecurity.PERMISSION_CLOSE)))
563         {
564             if (p instanceof PortletState)
565             {
566                 return ((PortletState)p).allowClose(rundata);
567             }
568         }
569         
570         return false;           
571     }
572 
573     /***
574      * Returns true if this portlet is currently closed
575      */
576     public boolean isClosed(RunData rundata)
577     {
578         Portlet p = getPortlet();
579         
580         if ( (p!=null) && (p instanceof PortletState) )
581         {
582             return ((PortletState)p).isClosed(rundata);
583         }
584         
585         return false;           
586     }
587 
588     /***
589      * Toggles the portlet state between closed and normal
590      *
591      * @param minimized the new portlet state
592      * @param data the RunData for this request
593      */
594     public void setClosed(boolean close, RunData rundata)
595     {
596         Portlet p = getPortlet();
597         
598         if ( (p!=null) && (p instanceof PortletState) )
599         {
600             ((PortletState)p).setClosed(close, rundata);
601         }
602     }
603 
604     /***
605      * Implements the default info behavior: any authenticated user may
606      * get information on a portlet
607      *
608      * @param rundata the RunData object for the current request
609      */
610     public boolean allowInfo( RunData rundata )
611     {
612         Portlet p = getPortlet();
613         
614         if (p==null) return false;
615         
616         if ((p instanceof PortletSet)
617             ||(JetspeedSecurity.checkPermission((JetspeedUser) rundata.getUser(),
618                                                 p,
619                                                 JetspeedSecurity.PERMISSION_INFO)))
620         {
621             if (p instanceof PortletState)
622             {
623                 return ((PortletState)p).allowInfo(rundata);
624             }
625         }
626         
627         return false;
628     }
629 
630     /***
631      * Implements the default customize behavior: any authenticated user may
632      * customize a portlet
633      *
634      * @param rundata the RunData object for the current request
635      */
636     public boolean allowCustomize( RunData rundata )
637     {
638         Portlet p = getPortlet();
639 
640         if (p==null) return false;
641         
642         if ((p instanceof PortletSet)
643             ||(JetspeedSecurity.checkPermission((JetspeedUser) rundata.getUser(),
644                                                 p,
645                                                 JetspeedSecurity.PERMISSION_CUSTOMIZE)))
646         {
647             if (p instanceof PortletState)
648             {
649                 return ((PortletState)p).allowCustomize(rundata);
650             }
651         }
652         
653         return false;
654     }
655 
656     /***
657      * Implements the default maximize behavior: any authenticated user may
658      * maximize a portlet
659      *
660      * @param rundata the RunData object for the current request
661      */
662     public boolean allowMaximize( RunData rundata )
663     {
664         Portlet p = getPortlet();
665 
666         if (p==null) return false;
667         
668         if ((p instanceof PortletSet)
669             ||(JetspeedSecurity.checkPermission((JetspeedUser) rundata.getUser(),
670                                                 p,
671                                                 JetspeedSecurity.PERMISSION_MAXIMIZE)))
672         {
673             if (p instanceof PortletState)
674             {
675                 return ((PortletState)p).allowMaximize(rundata);
676             }
677         }
678         
679         return false;
680     }
681 
682     /***
683      * Implements the default info behavior: any authenticated user may
684      * minimize a portlet
685      *
686      * @param rundata the RunData object for the current request
687      */
688     public boolean allowMinimize( RunData rundata )
689     {
690         Portlet p = getPortlet();
691 
692         if (p==null) return false;
693         
694         if ((p instanceof PortletSet)
695             ||(JetspeedSecurity.checkPermission((JetspeedUser) rundata.getUser(),
696                                                 p,
697                                                 JetspeedSecurity.PERMISSION_MINIMIZE)))
698         {
699             if (p instanceof PortletState)
700             {
701                 return ((PortletState)p).allowMinimize(rundata);
702             }
703         }
704         
705         return false;
706     }
707 
708     /***
709      * Implements the default info behavior: any authenticated user may
710      * display portlet in print friendly format
711      *
712      * @param rundata the RunData object for the current request
713      */
714     public boolean allowPrintFriendly( RunData rundata )
715     {
716         Portlet p = getPortlet();
717 
718         if (p==null) return false;
719         
720         if ((p instanceof PortletSet)
721             ||(JetspeedSecurity.checkPermission((JetspeedUser) rundata.getUser(),
722                                                 p,
723                                                 JetspeedSecurity.PERMISSION_PRINT_FRIENDLY)))
724         {
725             if (p instanceof PortletState)
726             {
727                 return ((PortletState)p).allowPrintFriendly(rundata);
728             }
729         }
730         
731         return false;
732     }
733 
734     /***
735      * Returns true if this portlet is currently minimized
736      */
737     public boolean isMinimized(RunData rundata)
738     {
739         Portlet p = getPortlet();
740         
741         if ( (p!=null) && (p instanceof PortletState) )
742         {
743             return ((PortletState)p).isMinimized(rundata);
744         }
745         
746         return false;
747     }
748 
749     /***
750     Change the portlet visibility state ( minimized <-> normal )
751 
752     @param minimize True if the portlet change to minimized
753     @param rundata A RunData object
754     */
755     public void setMinimized( boolean minimize, RunData rundata )
756     {
757         Portlet p = getPortlet();
758         
759         if ( (p!=null) && (p instanceof PortletState) )
760         {
761             ((PortletState)p).setMinimized( minimize, rundata);
762         }
763     }
764 
765     public String getID()
766     {
767         return this.portlet.getID();
768     }
769 
770     public void setID(String id)
771     {
772         this.portlet.setID(id);
773     }
774 
775     /***
776     Returns the Portlet with the given id
777     */
778     public Portlet getPortletByID(String id)
779     {
780         Portlet p = getPortlet();
781          if (null == p) 
782             return null;
783 
784         if (p instanceof PortletSet)
785         {
786             Enumeration e = ((PortletSet)p).getPortlets();
787             Portlet portlet = null;
788             while ( e.hasMoreElements() )
789             {
790                 portlet = (Portlet)e.nextElement();
791                 if (portlet.getID().equals(id))
792                     return portlet;
793             }
794             return portlet;
795        }
796        return null;
797     }
798 
799     /***
800     Returns the Portlet with the given name
801     */
802     public Portlet getPortletByName(String name)
803     {
804         Portlet p = getPortlet();
805          if (null == p) 
806             return null;
807 
808         if (p instanceof PortletSet)
809         {
810             Enumeration e = ((PortletSet)p).getPortlets();
811             Portlet portlet = null;
812             while ( e.hasMoreElements() )
813             {
814                 portlet = (Portlet)e.nextElement();
815                 if (portlet.getName().equals(name))
816                     return portlet;
817             }
818             return portlet;
819        }
820        return null;
821     }
822 
823 
824 }