new admin templates! with many thanks to init...
[mir.git] / source / mir / util / HTTPRequestParser.java
1 package mir.util;
2
3 import javax.servlet.http.HttpServletRequest;
4
5 public class HTTPRequestParser {
6   private HttpServletRequest request;
7   private String encoding;
8
9   public HTTPRequestParser(HttpServletRequest aRequest) {
10     this(aRequest, aRequest.getCharacterEncoding());
11   }
12
13   public HTTPRequestParser(HttpServletRequest aRequest, String anEncoding) {
14     request = aRequest;
15     encoding = anEncoding;
16   }
17
18   public boolean hasParameter(String aName) {
19     return request.getParameter(aName)!=null;
20   }
21
22   public String getParameterWithDefault(String aName, String aDefault) {
23     if (hasParameter(aName))
24       return getParameter(aName);
25     else
26       return aDefault;
27   }
28
29   public String getParameter(String aName) {
30     try {
31       String result = request.getParameter(aName);
32       String requestEncoding = request.getCharacterEncoding();
33       if (requestEncoding==null)
34         requestEncoding = "ISO-8859-1";
35
36       if (result != null && encoding!=null && !encoding.equals(requestEncoding)) {
37         result = new String(result.getBytes(requestEncoding), encoding);
38       }
39
40       return result;
41     }
42     catch (Throwable t) {
43       throw new RuntimeException("HTTPRequestParser.getParameter: " + t.getMessage());
44     }
45   }
46
47   public int getIntegerWithDefault(String aName, int aDefault) {
48     int result = aDefault;
49     String value = getParameter(aName);
50
51     try {
52       result = Integer.parseInt(value);
53     }
54     catch (Throwable t) {
55     }
56     return result;
57   }
58 }