View Javadoc

1   /*
2    * Copyright 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  package org.portletbridge.portlet;
17  
18  import java.io.Serializable;
19  
20  import javax.portlet.PortletPreferences;
21  import javax.portlet.RenderRequest;
22  
23  import org.apache.commons.httpclient.Credentials;
24  import org.apache.commons.httpclient.NTCredentials;
25  import org.apache.commons.httpclient.UsernamePasswordCredentials;
26  import org.portletbridge.PortletBridgeException;
27  import org.portletbridge.ResourceException;
28  
29  public class DefaultBridgeAuthenticator implements BridgeAuthenticator, Serializable {
30  
31      public Credentials getCredentials(RenderRequest request)
32              throws ResourceException {
33          PortletPreferences preferences = request.getPreferences();
34          String configAuthentication = preferences.getValue("authentication",
35                  "none");
36          String configAuthenticationUsername = preferences.getValue(
37                  "authenticationUsername", null);
38          String configAuthenticationPassword = preferences.getValue(
39                  "authenticationPassword", null);
40          String configAuthenticationHost = preferences.getValue(
41                  "authenticationHost", null);
42          String configAuthenticationDomain = preferences.getValue(
43                  "authenticationDomain", null);
44          if (configAuthentication != null
45                  && configAuthentication.trim().length() > 0) {
46              if ("ntlm".equalsIgnoreCase(configAuthentication)) {
47                  return new NTCredentials(configAuthenticationUsername,
48                          configAuthenticationPassword, configAuthenticationHost,
49                          configAuthenticationDomain);
50              } else if ("basic".equalsIgnoreCase(configAuthentication)) {
51                  return
52                          new UsernamePasswordCredentials(
53                                  configAuthenticationUsername,
54                                  configAuthenticationPassword);
55              } else if ("none".equalsIgnoreCase(configAuthentication)) {
56                  return null;
57              } else {
58                  throw new PortletBridgeException("error.configAuthentication");
59              }
60          }
61          return null;
62      }
63  }