|
1 // |
|
2 // $Id: check.java,v 1.14 2009-02-24 21:45:14 ylafon Exp $ |
|
3 // From Philippe Le Hegaret (Philippe.Le_Hegaret@sophia.inria.fr) |
|
4 // |
|
5 // (c) COPYRIGHT MIT and INRIA, 1997. |
|
6 // Please first read the full copyright statement in file COPYRIGHT.html |
|
7 |
|
8 package org.w3c.css.servlet; |
|
9 |
|
10 import java.io.IOException; |
|
11 |
|
12 import java.net.URLEncoder; |
|
13 |
|
14 import java.util.Enumeration; |
|
15 |
|
16 import javax.servlet.ServletConfig; |
|
17 import javax.servlet.ServletException; |
|
18 import javax.servlet.http.HttpServlet; |
|
19 import javax.servlet.http.HttpServletRequest; |
|
20 import javax.servlet.http.HttpServletResponse; |
|
21 |
|
22 /** |
|
23 * This class is a servlet to use the validator. |
|
24 * |
|
25 * @version $Revision: 1.14 $ |
|
26 */ |
|
27 public final class check extends HttpServlet { |
|
28 |
|
29 private static String validatorURI = null; |
|
30 |
|
31 |
|
32 /** |
|
33 * Performs the HTTP GET operation. |
|
34 * It redirects to the value contained in "Referer" |
|
35 * |
|
36 * @param req encapsulates the request to the servlet. |
|
37 * @param resp encapsulates the response from the servlet. |
|
38 * @exception ServletException if the request could not be handled. |
|
39 * @exception IOException if detected when handling the request. |
|
40 */ |
|
41 public void doGet(HttpServletRequest req, HttpServletResponse res) |
|
42 throws ServletException, IOException |
|
43 { |
|
44 |
|
45 String uri = req.getHeader("Referer"); |
|
46 String encodeEnc = req.getCharacterEncoding(); |
|
47 if (encodeEnc == null) { |
|
48 encodeEnc = "8859_1"; |
|
49 } |
|
50 uri = URLEncoder.encode(uri, encodeEnc); |
|
51 |
|
52 if (uri == null) { |
|
53 res.setContentType("text/plain"); |
|
54 res.sendError(400, "Referer field empty"); |
|
55 return; |
|
56 } |
|
57 res.setHeader("Cache-Control", "max-age=86400"); |
|
58 res.setHeader("Vary", "Referer"); |
|
59 |
|
60 Enumeration e = req.getParameterNames(); |
|
61 StringBuilder sb = new StringBuilder(validatorURI); |
|
62 boolean first = true; |
|
63 boolean uridone = false; |
|
64 |
|
65 if (e != null) { |
|
66 while (e.hasMoreElements()) { |
|
67 String paramname = (String) e.nextElement(); |
|
68 String paramvalue = req.getParameter(paramname); |
|
69 if ("uri".equals(paramname)) { |
|
70 uridone = true; |
|
71 paramvalue = uri; |
|
72 } |
|
73 if (first) { |
|
74 sb.append('?'); |
|
75 first = false; |
|
76 } else { |
|
77 sb.append('&'); |
|
78 } |
|
79 sb.append(paramname); |
|
80 if (paramvalue != null) { |
|
81 sb.append('='); |
|
82 sb.append(paramvalue); |
|
83 } |
|
84 } |
|
85 } |
|
86 if (!uridone) { |
|
87 if (first) { |
|
88 sb.append("?uri="); |
|
89 } else { |
|
90 sb.append("&uri="); |
|
91 } |
|
92 sb.append(uri); |
|
93 } |
|
94 res.sendRedirect(sb.toString()); |
|
95 } |
|
96 |
|
97 public void init(ServletConfig config) throws ServletException { |
|
98 super.init(config); |
|
99 if (config.getInitParameter("validatorURI") == null) { |
|
100 validatorURI = "validator"; |
|
101 } else { |
|
102 validatorURI = config.getInitParameter("validatorURI"); |
|
103 } |
|
104 } |
|
105 } |