View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.chukwa.hicc;
19  
20  
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  import java.util.Enumeration;
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServlet;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.hadoop.chukwa.util.XssFilter;
30  
31  public class Iframe extends HttpServlet {
32    public static final long serialVersionUID = 100L;
33  
34    @Override
35    protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
36      resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); 
37    }
38  
39    public void doGet(HttpServletRequest request, HttpServletResponse response)
40        throws IOException, ServletException {
41      String id;
42      String height = "100%";
43      XssFilter xf = null;
44      xf = new XssFilter(request);
45      if (xf.getParameter("boxId") != null) {
46        id = xf.getParameter("boxId");
47      } else {
48        id = "0";
49      }
50      response.setContentType("text/html; chartset=UTF-8//IGNORE");
51      response.setHeader("boxId", xf.getParameter("boxId"));
52      PrintWriter out = response.getWriter();
53      StringBuffer source = new StringBuffer();
54      String requestURL = request.getRequestURL().toString().replaceFirst("iframe/", "");
55      if(requestURL.indexOf("/hicc/")!=-1) {
56         requestURL = requestURL.substring(requestURL.indexOf("/hicc/"));
57      }
58      source.append(requestURL);
59      source.append("?");
60      Enumeration names = request.getParameterNames();
61      while (names.hasMoreElements()) {
62        String key = xf.filter((String) names.nextElement());
63        String[] values = xf.getParameterValues(key);
64        if(values!=null) {
65          for (int i = 0; i < values.length; i++) {
66            source.append(key + "=" + values[i] + "&");
67          }
68          if (key.toLowerCase().intern() == "height".intern()) {
69            height = xf.getParameter(key);
70          }
71        }
72      }
73      StringBuffer output = new StringBuffer();
74      output.append("<html><body><iframe id=\"iframe");
75      output.append(id);
76      output.append("\" name=\"iframe");
77      output.append(id);
78      output.append("\" src=\"");
79      output.append(source);
80      output.append("\" width=\"100%\" height=\"");
81      output.append(height);
82      output.append("\" frameborder=\"0\" style=\"overflow: hidden\"></iframe>");
83      out.println(output.toString());
84    }
85  
86    public void doPost(HttpServletRequest request, HttpServletResponse response)
87        throws IOException, ServletException {
88      doGet(request, response);
89    }
90  }