KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > http > HttpUtil


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.http;
21
22 import java.io.UnsupportedEncodingException JavaDoc;
23
24 /**
25  *
26  * @author Lee David Painter <a HREF="mailto:lee@3sp.com">&lt;lee@3sp.com&gt;</a>
27  */

28 public class HttpUtil {
29
30     private static final String JavaDoc ACCEPTABLE = "ABCDEFGHIJLKMNOPQRSTUVWXYZ" + // ALPHA //$NON-NLS-1$
31
// (UPPER)
32
"abcdefghijklmnopqrstuvwxyz" + // ALPHA (LOWER) //$NON-NLS-1$
33
"0123456789" + // DIGIT //$NON-NLS-1$
34
"_-!.~'()*" + // UNRESERVED //$NON-NLS-1$
35
",;:$+=" + // PUNCT //$NON-NLS-1$
36
"?/@"; // RESERVED //$NON-NLS-1$
37

38     /**
39      * Encode a path suitable for use in a URI.
40      *
41      * @param path path
42      * @param encodeSlash encode forward slashes (/)
43      * @return encoded path
44      */

45     public static String JavaDoc encodePath(String JavaDoc path, boolean encodeSlash, String JavaDoc charset) {
46         /* Encode the string */
47         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
48         byte encoded[];
49         try {
50             if (charset == null)
51                 encoded = path.getBytes();
52             else
53                 encoded = path.getBytes(charset);
54             for (int x = 0; x < encoded.length; x++) {
55                 if (((int) encoded[x] == '%' && encodeSlash) || ACCEPTABLE.indexOf((int) encoded[x]) < 0) {
56                     buffer.append('%');
57                     buffer.append(toHexString(encoded[x]));
58                     continue;
59                 }
60                 buffer.append((char) encoded[x]);
61             }
62         } catch (UnsupportedEncodingException JavaDoc e) {
63             e.printStackTrace();
64             return path;
65         }
66
67         return buffer.toString();
68     }
69
70     /**
71      * <p>
72      * Return the HEX representation of an array of bytes.
73      * </p>
74      *
75      * @param buffer the array of bytes to convert in a HEX {@link String}.
76      * @return a <b>non-null</b> {@link String} instance.
77      */

78     public static String JavaDoc toHexString(byte buffer[]) {
79         char output[] = new char[buffer.length * 2];
80         int position = 0;
81         for (int x = 0; x < buffer.length; x++) {
82             output[position++] = toHexDigit(buffer[x] >> 4);
83             output[position++] = toHexDigit(buffer[x]);
84         }
85         return new String JavaDoc(output);
86     }
87
88     /**
89      * <p>
90      * Return the HEX representation of a byte.
91      * </p>
92      *
93      * @param number the byte to convert in a HEX {@link String}.
94      * @return a <b>non-null</b> 2-characters {@link String} instance.
95      */

96     public static String JavaDoc toHexString(byte number) {
97         char output[] = new char[2];
98         output[0] = toHexDigit((int) (number >> 4));
99         output[1] = toHexDigit((int) (number));
100         return new String JavaDoc(output);
101     }
102
103     /**
104      * <p>
105      * Return the single digit character representing the HEX encoding of the
106      * lower four bits of a given integer.
107      * </p>
108      *
109      * @param number number to conver
110      * @return hex character
111      */

112     private static char toHexDigit(int number) {
113         switch (number & 0x0F) {
114             case 0x00:
115                 return '0';
116             case 0x01:
117                 return '1';
118             case 0x02:
119                 return '2';
120             case 0x03:
121                 return '3';
122             case 0x04:
123                 return '4';
124             case 0x05:
125                 return '5';
126             case 0x06:
127                 return '6';
128             case 0x07:
129                 return '7';
130             case 0x08:
131                 return '8';
132             case 0x09:
133                 return '9';
134             case 0x0A:
135                 return 'A';
136             case 0x0B:
137                 return 'B';
138             case 0x0C:
139                 return 'C';
140             case 0x0D:
141                 return 'D';
142             case 0x0E:
143                 return 'E';
144             case 0x0F:
145                 return 'F';
146         }
147         String JavaDoc message = "Invalid HEX digit " + Integer.toHexString(number); //$NON-NLS-1$
148
throw new IllegalArgumentException JavaDoc(message);
149     }
150
151 }
152
Popular Tags