Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
pissirSmartHome
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Alfredo Chissotti
pissirSmartHome
Commits
b0a63b8a
Commit
b0a63b8a
authored
2 years ago
by
Alfredo Chissotti
Browse files
Options
Downloads
Patches
Plain Diff
keycloack x server
parent
70459fdb
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
WebServer/src/code/Home.java
+59
-9
59 additions, 9 deletions
WebServer/src/code/Home.java
with
59 additions
and
9 deletions
WebServer/src/code/Home.java
+
59
−
9
View file @
b0a63b8a
...
...
@@ -9,6 +9,9 @@ import java.net.URI;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
import
com.sun.net.httpserver.HttpExchange
;
import
com.sun.net.httpserver.HttpHandler
;
...
...
@@ -17,8 +20,9 @@ public class Home implements HttpHandler {
@Override
public
void
handle
(
HttpExchange
exchange
)
throws
IOException
{
URI
requestURI
=
exchange
.
getRequestURI
();
if
(
requestURI
.
compareTo
(
URI
.
create
(
"/"
))
!=
0
)
{
boolean
wantsHome
=
Helper
.
compareText
(
requestURI
.
toString
(),
URI
.
create
(
"/"
).
toString
());
boolean
wantsKeycloak
=
Helper
.
compareText
(
requestURI
.
toString
(),
URI
.
create
(
"/keycloack.json"
).
toString
());
if
(!
wantsHome
||
!
wantsKeycloak
)
{
String
error
=
"Invalid URI"
;
OutputStream
os
=
exchange
.
getResponseBody
();
exchange
.
sendResponseHeaders
(
400
,
error
.
getBytes
().
length
);
...
...
@@ -29,14 +33,27 @@ public class Home implements HttpHandler {
String
requestMethod
=
exchange
.
getRequestMethod
();
if
(
Helper
.
compareText
(
requestMethod
,
"GET"
))
{
String
response
=
getHomePage
();
List
<
String
>
strlist
=
new
ArrayList
<>();
strlist
.
add
(
"text/html"
);
exchange
.
getResponseHeaders
().
put
(
"content-type"
,
strlist
);
exchange
.
sendResponseHeaders
(
200
,
response
.
getBytes
().
length
);
OutputStream
os
=
exchange
.
getResponseBody
();
os
.
write
(
response
.
getBytes
());
os
.
close
();
String
response
=
null
;
if
(
wantsHome
){
response
=
getHomePage
();
strlist
.
add
(
"text/html"
);
}
else
if
(
wantsKeycloak
){
response
=
getKeycloak
();
strlist
.
add
(
"text/json"
);
}
if
(!
Helper
.
compareText
(
response
,
"fail"
)){
exchange
.
getResponseHeaders
().
put
(
"content-type"
,
strlist
);
exchange
.
sendResponseHeaders
(
200
,
response
.
getBytes
().
length
);
OutputStream
os
=
exchange
.
getResponseBody
();
os
.
write
(
response
.
getBytes
());
os
.
close
();
}
else
{
exchange
.
sendResponseHeaders
(
500
,
response
.
getBytes
().
length
);
OutputStream
os
=
exchange
.
getResponseBody
();
os
.
write
(
response
.
getBytes
());
os
.
close
();
}
}
else
{
Helper
.
methodNotAllowed
(
exchange
);
}
...
...
@@ -94,6 +111,39 @@ public class Home implements HttpHandler {
return
answer
.
toString
();
}
private
static
String
getKeycloak
(){
String
page
=
Server
.
CLIENT_PATH
+
"/keycloak.json"
;
BufferedReader
bufferedReader
=
null
;
StringBuilder
answer
=
new
StringBuilder
();
try
{
FileReader
fileReader
=
new
FileReader
(
page
);
bufferedReader
=
new
BufferedReader
(
fileReader
);
String
line
;
while
((
line
=
bufferedReader
.
readLine
())
!=
null
)
{
answer
.
append
(
line
.
trim
());
}
JSONObject
js
=
new
JSONObject
(
answer
.
toString
());
return
js
.
toString
();
}
catch
(
FileNotFoundException
ex
)
{
System
.
out
.
println
(
"Unable to open file '"
+
page
+
"'"
);
return
"fail"
;
}
catch
(
IOException
ex
)
{
System
.
out
.
println
(
"Error reading file '"
+
page
+
"'"
);
return
"fail"
;
}
catch
(
JSONException
e
)
{
System
.
out
.
println
(
"The file doesn't contain a JSON '"
+
page
+
"'"
);
return
"fail"
;
}
finally
{
try
{
if
(
bufferedReader
!=
null
)
bufferedReader
.
close
();
}
catch
(
IOException
ex
){
System
.
out
.
println
(
"Error closing bufferedReader"
);
}
}
}
private
static
String
getExtension
(
String
file
)
{
int
i
=
file
.
length
()
-
1
;
while
(
i
>
0
&&
file
.
charAt
(
i
)
!=
'.'
&&
file
.
charAt
(
i
)
!=
'/'
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment