View Javadoc

1   //========================================================================
2   //Copyright 2006-2008 Mort Bay Consulting Pty. Ltd.
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   //http://www.apache.org/licenses/LICENSE-2.0
8   //Unless required by applicable law or agreed to in writing, software
9   //distributed under the License is distributed on an "AS IS" BASIS,
10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  //See the License for the specific language governing permissions and
12  //limitations under the License.
13  //========================================================================
14  
15  package org.mortbay.jetty.client.security;
16  
17  
18  import java.io.IOException;
19  
20  import org.mortbay.io.Buffer;
21  import org.mortbay.io.ByteArrayBuffer;
22  import org.mortbay.jetty.HttpHeaders;
23  import org.mortbay.jetty.client.HttpExchange;
24  import org.mortbay.jetty.security.B64Code;
25  import org.mortbay.util.StringUtil;
26  
27  /**
28   * Sets authentication headers for BASIC authentication challenges
29   * 
30   * @author jesse
31   */
32  public class BasicAuthorization implements Authorization
33  {
34      private Buffer _authorization;
35      
36      public BasicAuthorization(Realm realm) throws IOException
37      {
38          String authenticationString = "Basic " + B64Code.encode( realm.getPrincipal() + ":" + realm.getCredentials(), StringUtil.__ISO_8859_1);
39          _authorization= new ByteArrayBuffer(authenticationString);
40      }
41      
42      /**
43       * BASIC authentication is of the form
44       * 
45       * encoded credentials are of the form: username:password
46       * 
47       * 
48       */
49      public void setCredentials( HttpExchange exchange ) throws IOException
50      {
51          exchange.setRequestHeader( HttpHeaders.AUTHORIZATION_BUFFER, _authorization);
52      }
53  }