Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 771 | blopes | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
||
| 3 | * contributor license agreements. See the NOTICE file distributed with |
||
| 4 | * this work for additional information regarding copyright ownership. |
||
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
||
| 6 | * (the "License"); you may not use this file except in compliance with |
||
| 7 | * the License. You may obtain a copy of the License at |
||
| 8 | * |
||
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
| 10 | * |
||
| 11 | * Unless required by applicable law or agreed to in writing, software |
||
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
| 14 | * See the License for the specific language governing permissions and |
||
| 15 | * limitations under the License. |
||
| 16 | */ |
||
| 17 | import java.io.IOException; |
||
| 18 | import java.io.PrintWriter; |
||
| 19 | import java.util.Enumeration; |
||
| 20 | import java.util.Locale; |
||
| 21 | import java.util.ResourceBundle; |
||
| 22 | |||
| 23 | import javax.servlet.ServletException; |
||
| 24 | import javax.servlet.http.HttpServlet; |
||
| 25 | import javax.servlet.http.HttpServletRequest; |
||
| 26 | import javax.servlet.http.HttpServletResponse; |
||
| 27 | import javax.servlet.http.HttpSession; |
||
| 28 | |||
| 29 | import org.apache.tomcat.util.json.JSONFilter; |
||
| 30 | |||
| 31 | import util.CookieFilter; |
||
| 32 | import util.HTMLFilter; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Example servlet showing request headers |
||
| 36 | */ |
||
| 37 | public class RequestHeaderExample extends HttpServlet { |
||
| 38 | |||
| 39 | private static final long serialVersionUID = 1L; |
||
| 40 | |||
| 41 | @Override |
||
| 42 | public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { |
||
| 43 | if (prefersJSON(request.getHeader("Accept"))) { |
||
| 44 | renderJSON(request, response); |
||
| 45 | } else { |
||
| 46 | renderHTML(request, response); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Returns true if the client appears to prefer a JSON response, false otherwise. Note that this method is not very |
||
| 52 | * pedantic and uses only a very lazy algorithm for checking whether JSON is "preferred". |
||
| 53 | * |
||
| 54 | * @param acceptHeader The value of the HTTP "Accept" header from the client. |
||
| 55 | * |
||
| 56 | * @return true if the client appears to prefer a JSON response, false otherwise. |
||
| 57 | */ |
||
| 58 | protected boolean prefersJSON(String acceptHeader) { |
||
| 59 | if (null == acceptHeader) { |
||
| 60 | return false; |
||
| 61 | } |
||
| 62 | // mime/type, mime/type;q=n, ... |
||
| 63 | |||
| 64 | // Don't bother with the q-factor. |
||
| 65 | // This is not expected to be 100% accurate or spec-compliant |
||
| 66 | String[] accepts = acceptHeader.split(","); |
||
| 67 | for (String accept : accepts) { |
||
| 68 | if (accept.contains("application/json")) { |
||
| 69 | return true; |
||
| 70 | } |
||
| 71 | |||
| 72 | // text/html, application/html, etc. |
||
| 73 | if (accept.contains("html")) { |
||
| 74 | return false; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | return false; |
||
| 78 | } |
||
| 79 | |||
| 80 | protected void renderHTML(HttpServletRequest request, HttpServletResponse response) throws IOException { |
||
| 81 | ResourceBundle rb = ResourceBundle.getBundle("LocalStrings", request.getLocale()); |
||
| 82 | |||
| 83 | response.setContentType("text/html"); |
||
| 84 | response.setCharacterEncoding("UTF-8"); |
||
| 85 | |||
| 86 | PrintWriter out = response.getWriter(); |
||
| 87 | out.println("<!DOCTYPE html><html>"); |
||
| 88 | out.println("<head>"); |
||
| 89 | out.println("<meta charset=\"UTF-8\" />"); |
||
| 90 | |||
| 91 | String title = rb.getString("requestheader.title"); |
||
| 92 | out.println("<title>" + title + "</title>"); |
||
| 93 | out.println("</head>"); |
||
| 94 | out.println("<body bgcolor=\"white\">"); |
||
| 95 | |||
| 96 | out.println("<a href=\"../reqheaders.html\">"); |
||
| 97 | out.println( |
||
| 98 | "<img src=\"../images/code.gif\" height=24 " + "width=24 align=right border=0 alt=\"view code\"></a>"); |
||
| 99 | out.println("<a href=\"../index.html\">"); |
||
| 100 | out.println( |
||
| 101 | "<img src=\"../images/return.gif\" height=24 " + "width=24 align=right border=0 alt=\"return\"></a>"); |
||
| 102 | |||
| 103 | out.println("<h3>" + title + "</h3>"); |
||
| 104 | out.println("<table border=0>"); |
||
| 105 | Enumeration<String> e = request.getHeaderNames(); |
||
| 106 | while (e.hasMoreElements()) { |
||
| 107 | String headerName = e.nextElement(); |
||
| 108 | String headerValue = request.getHeader(headerName); |
||
| 109 | out.println("<tr><td bgcolor=\"#CCCCCC\">"); |
||
| 110 | out.println(HTMLFilter.filter(headerName)); |
||
| 111 | out.println("</td><td>"); |
||
| 112 | if (headerName.toLowerCase(Locale.ENGLISH).contains("cookie")) { |
||
| 113 | HttpSession session = request.getSession(false); |
||
| 114 | String sessionId = null; |
||
| 115 | if (session != null) { |
||
| 116 | sessionId = session.getId(); |
||
| 117 | } |
||
| 118 | out.println(HTMLFilter.filter(CookieFilter.filter(headerValue, sessionId))); |
||
| 119 | } else { |
||
| 120 | out.println(HTMLFilter.filter(headerValue)); |
||
| 121 | } |
||
| 122 | out.println("</td></tr>"); |
||
| 123 | } |
||
| 124 | out.println("</table>"); |
||
| 125 | } |
||
| 126 | |||
| 127 | protected void renderJSON(HttpServletRequest request, HttpServletResponse response) throws IOException { |
||
| 128 | response.setContentType("application/json"); |
||
| 129 | response.setCharacterEncoding("UTF-8"); |
||
| 130 | |||
| 131 | PrintWriter out = response.getWriter(); |
||
| 132 | |||
| 133 | out.append('['); |
||
| 134 | Enumeration<String> e = request.getHeaderNames(); |
||
| 135 | while (e.hasMoreElements()) { |
||
| 136 | String headerName = e.nextElement(); |
||
| 137 | String headerValue = request.getHeader(headerName); |
||
| 138 | |||
| 139 | out.append("{\"").append(JSONFilter.escape(headerName)).append("\":\""); |
||
| 140 | |||
| 141 | |||
| 142 | if (headerName.toLowerCase(Locale.ENGLISH).contains("cookie")) { |
||
| 143 | HttpSession session = request.getSession(false); |
||
| 144 | String sessionId = null; |
||
| 145 | if (session != null) { |
||
| 146 | sessionId = session.getId(); |
||
| 147 | } |
||
| 148 | out.append(JSONFilter.escape(CookieFilter.filter(headerValue, sessionId))); |
||
| 149 | } else { |
||
| 150 | out.append(JSONFilter.escape(headerValue)); |
||
| 151 | } |
||
| 152 | out.append("\"}"); |
||
| 153 | |||
| 154 | if (e.hasMoreElements()) { |
||
| 155 | out.append(','); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | out.print("]"); |
||
| 160 | } |
||
| 161 | |||
| 162 | @Override |
||
| 163 | public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { |
||
| 164 | doGet(request, response); |
||
| 165 | } |
||
| 166 | |||
| 167 | } |
||
| 168 |