1package my.package;
2
3/**
4 * Copyright 2009 Stou Sandalski (Siafoo.net)
5 * Copyright 1999-2008 The Apache Software Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 */
20
21import java.io.IOException;
22import java.io.InputStream;
23import java.io.OutputStream;
24import java.util.Enumeration;
25
26import javax.servlet.ServletException;
27import javax.servlet.http.HttpServlet;
28import javax.servlet.http.HttpServletRequest;
29import javax.servlet.http.HttpServletResponse;
30import java.io.BufferedReader;
31import java.io.BufferedWriter;
32import java.io.InputStreamReader;
33import java.io.OutputStreamWriter;
34import java.util.ArrayList;
35import java.util.List;
36
37import org.apache.http.HttpEntity;
38import org.apache.http.HttpResponse;
39import org.apache.http.NameValuePair;
40import org.apache.http.client.HttpClient;
41import org.apache.http.client.entity.UrlEncodedFormEntity;
42import org.apache.http.client.methods.HttpGet;
43import org.apache.http.client.methods.HttpPost;
44import org.apache.http.client.methods.HttpRequestBase;
45import org.apache.http.impl.client.DefaultHttpClient;
46import org.apache.http.message.BasicNameValuePair;
47import org.apache.http.protocol.HTTP;
48
49public class ProxyServlet extends HttpServlet {
50
51 /**
52 *
53 */
54 private static final long serialVersionUID = 8L;
55
56 private static final String targetServer = "http://localhost:5000";
57
58 @Override
59 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
60 handleRequest(req, resp, false);
61 }
62
63 @Override
64 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
65 handleRequest(req, resp, true);
66 }
67
68 @SuppressWarnings("unchecked")
69 protected void handleRequest(HttpServletRequest req, HttpServletResponse resp, boolean isPost) throws ServletException, IOException {
70
71 HttpClient httpclient = new DefaultHttpClient();
72
73 StringBuffer sb = new StringBuffer();
74
75 sb.append(targetServer);
76 sb.append(req.getRequestURI());
77
78 if(req.getQueryString() != null){
79 sb.append("?" + req.getQueryString());
80 }
81
82 HttpRequestBase targetRequest = null;
83
84 if(isPost){
85 System.out.println("POST");
86 HttpPost post = new HttpPost(sb.toString());
87
88 Enumeration<String> paramNames = req.getParameterNames();
89
90 String paramName = null;
91
92 List <NameValuePair> params = new ArrayList <NameValuePair>();
93
94 while(paramNames.hasMoreElements()){
95 paramName = paramNames.nextElement();
96 params.add(new BasicNameValuePair(paramName, req.getParameterValues(paramName)[0]));
97 }
98
99 post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
100 targetRequest = post;
101 }else{
102 System.out.println("GET");
103
104 HttpGet get = new HttpGet(sb.toString());
105 targetRequest = get;
106 }
107
108// // This copies the headers but I never really cared to get it to work properly
109// System.out.println("Request Headers");
110// Enumeration<String> headerNames = req.getHeaderNames();
111// String headerName = null;
112// while(headerNames.hasMoreElements()){
113// headerName = headerNames.nextElement();
114// targetRequest.addHeader(headerName, req.getHeader(headerName));
115// System.out.println(headerName + " : " + req.getHeader(headerName));
116// }
117
118 HttpResponse targetResponse = httpclient.execute(targetRequest);
119 HttpEntity entity = targetResponse.getEntity();
120
121 // Send the Response
122 InputStream input = entity.getContent();
123 OutputStream output = resp.getOutputStream();
124
125 BufferedReader reader = new BufferedReader(new InputStreamReader(input));
126 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
127 String line = reader.readLine();
128
129 while(line != null){
130 writer.write(line + "\n");
131 line = reader.readLine();
132 }
133
134 reader.close();
135 writer.close();
136 httpclient.getConnectionManager().shutdown();
137 }
138
139}
Comments
I have included the Proxy servlet class with the server part of my application project but when I run the application the Proxy fails because it is executing functions (i.e. Sockets) that are not compatible/supported with GWT.
Is it posible to run this servlet in a separate (non GWT) project on the same localhost:8080 port? The GWT environment uses Jetty, and must be running its own servlet container.
Is it possible to have that container kick off the proxy servlet, or is it necessary to separately install tomcat on my localhost Apache/PHP environment.
For production servers you will typically map your GWT frontend and backend to the same server so there is no SOP restriction and so you do not need the servlet. For example your web-app backend lives at example.com/foo and your front end lives at example.com/bar when the user clicks on something in the front end, the front end will post/get to some url inside example.com/foo. Using apache you can easily configure everything to live under the same server.
The servlet works fine with Jetty (See link above for an example project). Also GWT does not pose any restrictions to what server-side code you can run. Are you trying to use this thing with AppEngine?
I simply added your class to the same package as the greet servlet and configured it in the web.xml to match on *.php.
When I run my application in hosted mode the HttpProxy servlet is kicked off when my application attempts to access a php url - just as I would like. But the moment it executes a java library function that is not supported by GWT (i.e. a socket function - and your servlet would not be much good without that) I get an exception indicating that it is not supported.
This is why I have been asking if it is necessary to put it in its own non GWT eclipse project, but then I worry about colissions on localhost:8080.
I hope thats clear, and thanks in advance for you input.
I think my issue was envionment related. I reinstalled J2EE, GWT pluggin and recreated my project from scratch and the problem went away. Also note that I did see that I had included the appengine jars when I created the original project - I did not see the check box on the GWT wizard that includes it by default.
After these changes your proxy worked wonderfully. Thank you for your time and code.
I am posting a recomendation for others having this issue on the SmartGwt forums to use your code.
Would you be able to help me with this. Once I get the resp back in the response object I will be able to use the xml api to extract the data but I am not getting the xml object back.
Also we pretty much gave up on SmartGwt and I have removed it from my computer so I can't really help you too much with it either.
Sorry!