Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
proyecto-estandar
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Israel Gutierrez Salazar
proyecto-estandar
Commits
67de8062
Commit
67de8062
authored
Apr 19, 2018
by
sistem08user
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cambios
parent
b2133aa0
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
71 additions
and
10 deletions
+71
-10
RequestFilter.java
src/java/demojsoncrud/servlets/RequestFilter.java
+71
-10
No files found.
src/java/demojsoncrud/servlets/RequestFilter.java
View file @
67de8062
...
@@ -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,16 +56,9 @@ public class RequestFilter implements Filter, ServletContextListener {
...
@@ -50,16 +56,9 @@ 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
.
setCharacterEncoding
(
"UTF-8"
);
response
.
setContentType
(
"text/xml"
);
PrintWriter
pw
=
response
.
getWriter
();
pw
.
println
(
sb
.
toString
());
pw
.
flush
();
}
// response.sendRedirect(indexPath);
// response.sendRedirect(indexPath);
}
}
}
}
...
@@ -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"
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment