1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int status;
System.out.println("[DelegationServlet] Ma servlet de redirection ");
// // Permet d'observer les flux recus et envoyés
// System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
// System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
// System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");
// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug");
HttpClient client = new HttpClient();
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
NameValuePair[] data =
{ new NameValuePair("userName", "jdutronc"), new NameValuePair("password", "jdutronc"), };
PostMethod post = new PostMethod("http://localhost:8080/Login/login");
post.setRequestBody(data);
Enumeration it = request.getHeaderNames();
if (it != null) {
String str;
while (it.hasMoreElements()) {
str = (String) it.nextElement();
if (str.equalsIgnoreCase("cookie")
|| str.equalsIgnoreCase("referer")
|| str.equalsIgnoreCase("content-length"))
continue;
// System.out.println("<request>" + str + " - " + request.getHeader(str));
post.setRequestHeader(str, request.getHeader(str));
}
}
//post.setRequestHeader();
post.setRequestHeader("referer", "http://localhost:8080/Login/index.jsp");
//post.setParameter()
status = client.executeMethod(post);
InputStream inpost = post.getResponseBodyAsStream();
// BufferedReader in2 = new BufferedReader(new InputStreamReader(inpost));
//
// StringBuffer sb2 = new StringBuffer();
// String s2 = in2.readLine();
// while (s2 != null) {
// sb2.append(s2);
// s2 = in2.readLine();
// }
HttpState state = client.getState();
org.apache.commons.httpclient.Cookie[] httpClientCookies = state.getCookies();
if ((httpClientCookies.length > 0) && (httpClientCookies[0] != null)) {
org.apache.commons.httpclient.Cookie httpClientCookie = httpClientCookies[0];
javax.servlet.http.Cookie delegateCookie =
new javax.servlet.http.Cookie(httpClientCookie.getName(), httpClientCookie.getValue());
// Transfert des informations entre cookie
delegateCookie.setComment(httpClientCookie.getComment());
// delegateCookie.setDomain( httpClientCookie.getDomain() );
// TODO : Quelle est la correspondance entre MaxAge et ExpiryDate ? Si elle existe.
// delegateCookie.setMaxAge( httpClientCookie.getExpiryDate() );
delegateCookie.setPath(httpClientCookie.getPath());
delegateCookie.setSecure(httpClientCookie.getSecure());
delegateCookie.setVersion(httpClientCookie.getVersion());
response.addCookie(delegateCookie);
}
response.sendRedirect("http://localhost:8080/Login/accueil.jsp");
} |
Partager