Commit 67de8062 by sistem08user

Cambios

parent b2133aa0
...@@ -6,6 +6,7 @@ import java.io.PrintWriter; ...@@ -6,6 +6,7 @@ import java.io.PrintWriter;
import javax.servlet.Filter; import javax.servlet.Filter;
import javax.servlet.FilterChain; import javax.servlet.FilterChain;
import javax.servlet.FilterConfig; import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import javax.servlet.ServletContextListener;
import javax.servlet.ServletException; import javax.servlet.ServletException;
...@@ -16,6 +17,11 @@ import javax.servlet.http.HttpServletRequest; ...@@ -16,6 +17,11 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
@WebFilter("/*") @WebFilter("/*")
public class RequestFilter implements Filter, ServletContextListener { public class RequestFilter implements Filter, ServletContextListener {
...@@ -50,17 +56,10 @@ public class RequestFilter implements Filter, ServletContextListener { ...@@ -50,17 +56,10 @@ public class RequestFilter implements Filter, ServletContextListener {
if (valid.getBoolean("status")) { if (valid.getBoolean("status")) {
chain.doFilter(request, response); chain.doFilter(request, response);
} else { } else {
if (isAJAXRequest(request)) { redirect(indexPath);
StringBuilder sb = new StringBuilder(); // response.setStatus(response.SC_MOVED_TEMPORARILY);
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><partial-response><redirect url=\"").append(indexPath).append("\"></redirect></partial-response>"); // response.setHeader("Location", indexPath);
response.setHeader("Cache-Control", "no-cache"); // response.sendRedirect(indexPath);
response.setCharacterEncoding("UTF-8");
response.setContentType("text/xml");
PrintWriter pw = response.getWriter();
pw.println(sb.toString());
pw.flush();
}
// response.sendRedirect(indexPath);
} }
} }
} else { } else {
...@@ -78,7 +77,69 @@ public class RequestFilter implements Filter, ServletContextListener { ...@@ -78,7 +77,69 @@ public class RequestFilter implements Filter, ServletContextListener {
response.sendRedirect(indexPath); response.sendRedirect(indexPath);
} }
} }
private void redirect (String indexPath){
try {
String url = indexPath;
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setReadTimeout(5000);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Mozilla");
conn.addRequestProperty("Referer", "google.com");
System.out.println("Request URL ... " + url);
boolean redirect = false;
// normally, 3xx is redirect
int status = conn.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
if (status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER)
redirect = true;
}
System.out.println("Response Code ... " + status);
if (redirect) {
// get redirect url from "location" header field
String newUrl = conn.getHeaderField("Location");
// get the cookie if need, for login
String cookies = conn.getHeaderField("Set-Cookie");
// open the new connnection again
conn = (HttpURLConnection) new URL(newUrl).openConnection();
conn.setRequestProperty("Cookie", cookies);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Mozilla");
conn.addRequestProperty("Referer", "google.com");
System.out.println("Redirect to URL : " + newUrl);
}
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer html = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
html.append(inputLine);
}
in.close();
System.out.println("URL Content... \n" + html.toString());
System.out.println("Done");
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean isAJAXRequest(HttpServletRequest request) { private boolean isAJAXRequest(HttpServletRequest request) {
boolean check = false; boolean check = false;
String facesRequest = request.getHeader("X-Requested-With"); String facesRequest = request.getHeader("X-Requested-With");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment