View Javadoc

1   /*
2    * Copyright 2000-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.jetspeed.portal.portlets;
17  
18  import org.apache.jetspeed.portal.PortletException;
19  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
20  import org.apache.jetspeed.services.logging.JetspeedLogger;
21  
22  import org.apache.turbine.util.RunData;
23  import org.apache.ecs.ConcreteElement;
24  
25  
26  /***
27   * Same as IFramePortlet except that it allows to use basic authentication using current
28   * user name and password (or whatever is provided in portlet preferences)
29   * 
30   * @author <a href="mailto:hoju@visi.com">Jacob Kjome</a>
31   * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a>
32   * @version $Id: BASICAuthIFramePortlet.java,v 1.3 2004/02/23 04:03:34 jford Exp $
33   */
34  public class BASICAuthIFramePortlet extends IFramePortlet
35  {
36  
37      /***
38       * Static initialization of the logger for this class
39       */    
40      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BASICAuthIFramePortlet.class.getName());    
41      
42      private String origSource = null;
43  
44      private static final String USERNAME = "username";
45      private static final String PASSWORD = "password";
46  
47      /***
48       * 
49       * @param runData
50       * @return 
51       */
52      public ConcreteElement getContent(RunData runData)
53      {
54          if (org.apache.jetspeed.util.PortletSessionState.getPortletConfigChanged(this, runData))
55          {
56              try {
57                  this.init();
58              }
59              catch (PortletException pe)
60              {
61                  logger.error("Exception", pe);
62              }
63          }
64          
65          setSource(getSource(runData));
66  
67          return super.getContent(runData);
68      }
69  
70      /***
71       * 
72       * @param runData
73       * @return 
74       */
75      public String getSource(RunData runData)
76      {
77          String source = origSource;
78          if (source == null || source.trim().length() == 0)
79          {
80              return null;
81          }
82  
83          int schemeCount = 8;
84          int index = source.indexOf("https://");
85          if (index == -1)
86          {
87              schemeCount = 7;
88              index = source.indexOf("http://");
89          }
90  
91          if (index != -1)
92          {
93              String user = this.getPortletConfig().getInitParameter(USERNAME);
94              if (user == null || user.trim().length() == 0)
95              {
96                  user = runData.getUser().getUserName();
97              }
98              String pass = this.getPortletConfig().getInitParameter(PASSWORD);
99              if (pass == null || pass.trim().length() == 0)
100             {
101                 pass = runData.getUser().getPassword();
102             }
103             String beginStr = source.substring(0, schemeCount);
104             String endStr   = source.substring(schemeCount);
105             int size = user.length() + pass.length() + source.length() + 2;
106             StringBuffer buff = new StringBuffer(size);
107             source = buff.append(beginStr).append(user).append(":").append(pass).append("@").append(endStr).toString();
108         }
109         return source;
110     }
111 
112     /***
113      * 
114      * @exception PortletException
115      */
116     public void init() throws PortletException 
117     {
118         super.init();
119         origSource = getSource();
120     }
121 
122 }