KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > adaptor > http > HttpUtil


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8 package mx4j.tools.adaptor.http;
9
10 /**
11  * Utility methods for the HTTP adaptor
12  *
13  * @version $Revision: 1.3 $
14  */

15 public class HttpUtil
16 {
17
18    /**
19     * Gets a message apropriate for a give HTTP code
20     *
21     * @param code Reference Code
22     * @return The result message
23     * @see HttpConstants
24     */

25    public static String JavaDoc getCodeMessage(int code)
26    {
27       switch (code)
28       {
29          case HttpConstants.STATUS_OKAY:
30             return "OK";
31          case HttpConstants.STATUS_NO_CONTENT:
32             return "No Content";
33          case HttpConstants.STATUS_MOVED_PERMANENTLY:
34             return "Moved Permanently";
35          case HttpConstants.STATUS_MOVED_TEMPORARILY:
36             return "Moved Temporarily";
37          case HttpConstants.STATUS_BAD_REQUEST:
38             return "Bad Request";
39          case HttpConstants.STATUS_FORBIDDEN:
40             return "Forbidden";
41          case HttpConstants.STATUS_NOT_FOUND:
42             return "Not Found";
43          case HttpConstants.STATUS_NOT_ALLOWED:
44             return "Method Not Allowed";
45          case HttpConstants.STATUS_INTERNAL_ERROR:
46             return "Internal Server Error";
47          case HttpConstants.STATUS_AUTHENTICATE:
48             return "Authentication requested";
49          case HttpConstants.STATUS_NOT_IMPLEMENTED:
50             return "Not Implemented";
51          default:
52             return "Unknown Code (" + code + ")";
53       }
54    }
55
56
57    /**
58     * Makes a path canonical
59     *
60     * @param path Target path
61     * @return The canonicalized path
62     */

63    public static String JavaDoc canonicalizePath(String JavaDoc path)
64    {
65       char[] chars = path.toCharArray();
66       int length = chars.length;
67       int idx;
68       int odx = 0;
69       while ((idx = indexOf(chars, length, '/', odx)) < length - 1)
70       {
71          int ndx = indexOf(chars, length, '/', idx + 1);
72          int kill = -1;
73          if (ndx == idx + 1)
74          {
75             kill = 1;
76          }
77          else if ((ndx >= idx + 2) && (chars[idx + 1] == '.'))
78          {
79             if (ndx == idx + 2)
80             {
81                kill = 2;
82             }
83             else if ((ndx == idx + 3) && (chars[idx + 2] == '.'))
84             {
85                kill = 3;
86                while ((idx > 0) && (chars[--idx] != '/'))
87                {
88                   ++kill;
89                }
90             }
91          }
92          if (kill == -1)
93          {
94             odx = ndx;
95          }
96          else if (idx + kill >= length)
97          {
98             length = odx = idx + 1;
99          }
100          else
101          {
102             length -= kill;
103             System.arraycopy(chars, idx + 1 + kill,
104                              chars, idx + 1, length - idx - 1);
105             odx = idx;
106          }
107       }
108       return new String JavaDoc(chars, 0, length);
109    }
110
111
112    protected static int indexOf(char[] chars, int length, char chr, int from)
113    {
114       while ((from < length) && (chars[from] != chr))
115       {
116          ++from;
117       }
118       return from;
119    }
120
121    /**
122     * Returns whether a boolean variable is in the variables. It tries to find
123     * it. If not found the the default is used. If found is tested to check if
124     * it is <code>true</code> or <code>1</code> and the answer is true.
125     * Otherwise is false
126     */

127    public static boolean booleanVariableValue(HttpInputStream in, String JavaDoc variable, boolean defaultValue)
128    {
129       if (in.getVariables().containsKey(variable))
130       {
131          String JavaDoc result = (String JavaDoc)in.getVariables().get(variable);
132          return result.equals("true") || result.equals("1");
133       }
134       return defaultValue;
135    }
136 }
137
Popular Tags