KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xmlrpc > util > HttpUtil


1 /*
2  * Copyright 1999,2005 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
18 package org.apache.xmlrpc.util;
19
20 import org.apache.commons.codec.binary.Base64;
21 import org.apache.commons.codec.EncoderException;
22
23 /**
24  * Provides utility functions useful in HTTP communications
25  *
26  * @author <a HREF="mailto:rhoegg@isisnetworks.net">Ryan Hoegg</a>
27  */

28 public class HttpUtil
29 {
30     private static final Base64 base64;
31     
32     static {
33         base64 = new Base64();
34     }
35     
36     private HttpUtil()
37     {
38         // private because currently we only offer static methods.
39
}
40     
41     public static String JavaDoc encodeBasicAuthentication(String JavaDoc user, String JavaDoc password)
42     {
43         String JavaDoc auth;
44         if (user == null || password == null)
45         {
46             auth = null;
47         }
48         else
49         {
50             try
51             {
52                 Object JavaDoc bytes = (user + ':' + password).getBytes();
53                 auth = new String JavaDoc((byte[]) base64.encode(bytes)).trim();
54             }
55             catch (EncoderException e)
56             {
57                 // EncoderException is never thrown in the body of
58
// Base64.encode(byte[]) in Commons Codec 1.1.
59
throw new RuntimeException JavaDoc("Possibly incompatible version of '"
60                                            + Base64.class.getName() +
61                                            "' used: " + e);
62             }
63         }
64         return auth;
65     }
66 }
67
Popular Tags