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.proxy;
19  
20  import java.io.BufferedReader;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  import java.io.InputStreamReader;
24  import java.net.HttpURLConnection;
25  import java.net.URL;
26  import java.net.URLEncoder;
27  import java.nio.charset.Charset;
28  import java.util.Map;
29  
30  import javax.servlet.ServletException;
31  import javax.servlet.ServletOutputStream;
32  import javax.servlet.http.HttpServlet;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  import org.apache.hadoop.chukwa.conf.ChukwaConfiguration;
37  import org.apache.log4j.Logger;
38  
39  /**
40   * HTTP Proxy Servlet for Solr
41   *
42   */
43  public class HttpProxy extends HttpServlet {
44    private static final long serialVersionUID = 7574L;
45    private final String USER_AGENT = "Mozilla/5.0";
46    private final static String SOLR_URL = "chukwa.solr.url";
47    private final static Logger LOG = Logger.getLogger(HttpProxy.class);
48    private String solrUrl = null;
49  
50    public HttpProxy() {
51      super();
52      ChukwaConfiguration conf = new ChukwaConfiguration();
53      solrUrl = conf.get(SOLR_URL);
54    }
55  
56    protected void doGet(HttpServletRequest request, HttpServletResponse response)
57        throws ServletException, IOException {
58      // Create Get request dynamically to remote server
59      String url = solrUrl + request.getPathInfo()
60          + "?" + request.getQueryString();
61  
62      URL obj = new URL(url);
63      HttpURLConnection con = (HttpURLConnection) obj.openConnection();
64  
65      // optional default is GET
66      con.setRequestMethod("GET");
67  
68      // add request header
69      con.setRequestProperty("User-Agent", USER_AGENT);
70  
71      int responseCode = con.getResponseCode();
72      LOG.info("Sending 'GET' request to URL : " + url);
73      LOG.info("Response Code : " + responseCode);
74  
75      BufferedReader in = new BufferedReader(new InputStreamReader(
76          con.getInputStream(), Charset.forName("UTF-8")));
77      String inputLine;
78      StringBuffer response1 = new StringBuffer();
79  
80      ServletOutputStream sout = response.getOutputStream();
81  
82      while ((inputLine = in.readLine()) != null) {
83        response1.append(inputLine);
84        sout.write(inputLine.getBytes(Charset.forName("UTF-8")));
85      }
86      in.close();
87  
88      sout.flush();
89  
90    }
91  
92    protected void doPost(HttpServletRequest request, HttpServletResponse response)
93        throws ServletException, IOException {
94      // Create Post request dynamically to remote server
95      String url = solrUrl + request.getPathInfo();
96  
97      URL obj = new URL(url);
98      HttpURLConnection con = (HttpURLConnection) obj.openConnection();
99  
100     // add reuqest header
101     con.setRequestMethod("POST");
102     con.setRequestProperty("User-Agent", USER_AGENT);
103     con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
104 
105     StringBuilder sb = new StringBuilder();
106     @SuppressWarnings("rawtypes")
107     Map map = request.getParameterMap();
108     for (Object key : request.getParameterMap().keySet()) {
109       if (sb.length() > 0) {
110         sb.append('&');
111       }
112       String keyStr = (String) key;
113       String[] temp = (String[]) map.get(keyStr);
114       for (String s : temp) {
115         sb.append(URLEncoder.encode(keyStr, "UTF-8")).append('=')
116             .append(URLEncoder.encode(s, "UTF-8"));
117       }
118     }
119 
120     String urlParameters = sb.toString();
121 
122     // Send post request
123     con.setDoOutput(true);
124     DataOutputStream wr = new DataOutputStream(con.getOutputStream());
125     wr.writeBytes(urlParameters);
126     wr.flush();
127     wr.close();
128 
129     int responseCode = con.getResponseCode();
130     LOG.debug("\nSending 'POST' request to URL : " + url);
131     LOG.debug("Post parameters : " + urlParameters);
132     LOG.debug("Response Code : " + responseCode);
133 
134     BufferedReader in = new BufferedReader(new InputStreamReader(
135         con.getInputStream(), Charset.forName("UTF-8")));
136     String inputLine;
137     StringBuffer response1 = new StringBuffer();
138 
139     ServletOutputStream sout = response.getOutputStream();
140 
141     while ((inputLine = in.readLine()) != null) {
142       response1.append(inputLine);
143       sout.write(inputLine.getBytes(Charset.forName("UTF-8")));
144     }
145     in.close();
146 
147     sout.flush();
148   }
149 
150 }