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
6edf9f5a
Commit
6edf9f5a
authored
2 years ago
by
A C
Browse files
Options
Downloads
Patches
Plain Diff
TLS
parent
f9a10f5c
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
domainManager/Domain/src/code/Domain.java
+116
-54
116 additions, 54 deletions
domainManager/Domain/src/code/Domain.java
domainManager/Domain/src/code/Helper.java
+1
-1
1 addition, 1 deletion
domainManager/Domain/src/code/Helper.java
with
117 additions
and
55 deletions
domainManager/Domain/src/code/Domain.java
+
116
−
54
View file @
6edf9f5a
package
code
;
package
code
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.net.HttpURLConnection
;
import
java.net.HttpURLConnection
;
import
java.net.InetSocketAddress
;
import
java.net.InetSocketAddress
;
import
java.net.URL
;
import
java.net.URL
;
import
java.security.KeyStore
;
import
java.sql.SQLException
;
import
java.sql.SQLException
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.ThreadPoolExecutor
;
import
java.util.concurrent.ThreadPoolExecutor
;
import
javax.net.ssl.KeyManagerFactory
;
import
javax.net.ssl.SSLContext
;
import
javax.net.ssl.SSLEngine
;
import
javax.net.ssl.SSLParameters
;
import
javax.net.ssl.TrustManagerFactory
;
import
com.sun.net.httpserver.HttpsConfigurator
;
import
com.sun.net.httpserver.HttpsParameters
;
import
com.sun.net.httpserver.HttpsServer
;
import
org.json.JSONArray
;
import
org.json.JSONArray
;
import
org.json.JSONException
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
import
org.json.JSONObject
;
...
@@ -18,71 +29,123 @@ import db.DBC;
...
@@ -18,71 +29,123 @@ import db.DBC;
public
class
Domain
{
public
class
Domain
{
static
public
int
port
=
3001
;
static
public
int
port
=
3001
;
public
static
void
main
(
String
[]
args
)
throws
IOException
,
JSONException
{
public
static
void
main
(
String
[]
args
)
throws
IOException
,
JSONException
{
ThreadPoolExecutor
threadPoolExecutor
=
(
ThreadPoolExecutor
)
Executors
.
newCachedThreadPool
();
ThreadPoolExecutor
threadPoolExecutor
=
(
ThreadPoolExecutor
)
Executors
.
newCachedThreadPool
();
HttpServer
server
=
HttpServer
.
create
(
new
InetSocketAddress
(
port
),
0
);
// HttpServer server=HttpServer.create(new InetSocketAddress(port),0);
//chiamata per popolare moduli
HttpsServer
server
=
HttpsServer
.
create
(
new
InetSocketAddress
(
port
),
0
);
// port gets set here
// HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
URL
url
=
new
URL
(
"https://gitlab.di.unipmn.it/alfredo/iotlabgw.edu-al.unipmn.it/-/raw/main/index.json"
);
HttpURLConnection
con
=
(
HttpURLConnection
)
url
.
openConnection
();
// initialise the HTTPS server
try
{
Helper
.
setConnectionSettings
(
con
,
"GET"
);
SSLContext
sslContext
=
SSLContext
.
getInstance
(
"TLS"
);
// leggo risposta
// initialise the keystore
int
status
=
con
.
getResponseCode
();
char
[]
password
=
"miapasswd"
.
toCharArray
();
//controllare ToDo
KeyStore
ks
=
KeyStore
.
getInstance
(
"JKS"
);
String
content
=
Helper
.
getResponseFromConnection
(
con
);
FileInputStream
fis
=
new
FileInputStream
(
"../lig.keystore"
);
con
.
disconnect
();
ks
.
load
(
fis
,
password
);
//manipolazione per ottenere i campi dei moduli
// setup the key manager factory
riempiModuli
(
content
);
KeyManagerFactory
kmf
=
KeyManagerFactory
.
getInstance
(
"SunX509"
);
kmf
.
init
(
ks
,
password
);
server
.
setExecutor
(
threadPoolExecutor
);
// setup the trust manager factory
server
.
createContext
(
"/install/"
,
new
InstallHandler
());
TrustManagerFactory
tmf
=
TrustManagerFactory
.
getInstance
(
"SunX509"
);
server
.
createContext
(
"/start/"
,
new
StartHandler
());
tmf
.
init
(
ks
);
server
.
createContext
(
"/stop/"
,
new
StopHandler
());
server
.
createContext
(
"/delete/"
,
new
DeleteHandler
());
// setup the HTTPS context and parameters
server
.
createContext
(
"/secured/domains/"
,
new
TokenHandler
());
sslContext
.
init
(
kmf
.
getKeyManagers
(),
tmf
.
getTrustManagers
(),
null
);
server
.
createContext
(
"/secured/services"
,
new
ServicesHandler
());
server
.
setHttpsConfigurator
(
new
HttpsConfigurator
(
sslContext
)
{
server
.
createContext
(
"/secured/priviledges"
,
new
PriviledgesHandler
());
@Override
public
void
configure
(
HttpsParameters
params
)
{
server
.
start
();
try
{
System
.
out
.
println
(
"Domain in ascolto su "
+
Helper
.
getSelfURL
());
// initialise the SSL context
SSLContext
context
=
getSSLContext
();
SSLEngine
engine
=
context
.
createSSLEngine
();
params
.
setNeedClientAuth
(
false
);
params
.
setCipherSuites
(
engine
.
getEnabledCipherSuites
());
params
.
setProtocols
(
engine
.
getEnabledProtocols
());
// Set the SSL parameters
SSLParameters
sslParameters
=
context
.
getSupportedSSLParameters
();
params
.
setSSLParameters
(
sslParameters
);
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
"Failed to create HTTPS port"
);
ex
.
printStackTrace
();
}
}
});
// chiamata per popolare moduli
URL
url
=
new
URL
(
"https://gitlab.di.unipmn.it/alfredo/iotlabgw.edu-al.unipmn.it/-/raw/main/index.json"
);
HttpURLConnection
con
=
(
HttpURLConnection
)
url
.
openConnection
();
Helper
.
setConnectionSettings
(
con
,
"GET"
);
// leggo risposta
int
status
=
con
.
getResponseCode
();
// controllare ToDo
String
content
=
Helper
.
getResponseFromConnection
(
con
);
con
.
disconnect
();
// manipolazione per ottenere i campi dei moduli
riempiModuli
(
content
);
server
.
setExecutor
(
threadPoolExecutor
);
// API del server
server
.
createContext
(
"/install/"
,
new
InstallHandler
());
server
.
createContext
(
"/start/"
,
new
StartHandler
());
server
.
createContext
(
"/stop/"
,
new
StopHandler
());
server
.
createContext
(
"/delete/"
,
new
DeleteHandler
());
server
.
createContext
(
"/secured/domains/"
,
new
TokenHandler
());
server
.
createContext
(
"/secured/services"
,
new
ServicesHandler
());
server
.
createContext
(
"/secured/priviledges"
,
new
PriviledgesHandler
());
server
.
start
();
System
.
out
.
println
(
"Domain in ascolto su "
+
Helper
.
getSelfURL
());
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"Failed to create HTTPS server on port "
+
port
+
Helper
.
getSelfURL
());
e
.
printStackTrace
();
}
}
}
private
static
void
riempiModuli
(
String
content
)
throws
JSONException
{
private
static
void
riempiModuli
(
String
content
)
throws
JSONException
{
JSONObject
obj
=
new
JSONObject
(
content
);
JSONObject
obj
=
new
JSONObject
(
content
);
JSONArray
jr
=
obj
.
getJSONArray
(
"response"
);
JSONArray
jr
=
obj
.
getJSONArray
(
"response"
);
String
[]
s1
=
new
String
[
jr
.
length
()];
String
[]
s1
=
new
String
[
jr
.
length
()];
for
(
int
i
=
0
;
i
<
jr
.
length
();
i
++)
{
for
(
int
i
=
0
;
i
<
jr
.
length
();
i
++)
{
s1
[
i
]
=
jr
.
getString
(
i
);
s1
[
i
]
=
jr
.
getString
(
i
);
}
}
for
(
int
i
=
0
;
i
<
s1
.
length
;
i
++)
{
for
(
int
i
=
0
;
i
<
s1
.
length
;
i
++)
{
String
remZip
=
s1
[
i
].
substring
(
0
,
s1
[
i
].
length
()
-
4
);
String
remZip
=
s1
[
i
].
substring
(
0
,
s1
[
i
].
length
()
-
4
);
String
[]
curr
=
remZip
.
split
(
"-"
);
String
[]
curr
=
remZip
.
split
(
"-"
);
for
(
int
j
=
0
;
j
<
curr
.
length
;
j
++)
{
for
(
int
j
=
0
;
j
<
curr
.
length
;
j
++)
{
if
(
curr
.
length
>
1
)
{
if
(
curr
.
length
>
1
)
{
String
a
=
curr
[
0
];
String
a
=
curr
[
0
];
String
b
=
curr
[
1
];
String
b
=
curr
[
1
];
try
{
try
{
//inserimento modulo non arduino
//
inserimento modulo non arduino
DBC
.
fillModules
(
a
,
b
,
s1
[
i
]);
DBC
.
fillModules
(
a
,
b
,
s1
[
i
]);
}
catch
(
SQLException
|
JSONException
e
)
{
}
catch
(
SQLException
|
JSONException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
}
}
else
{
else
{
String
a
=
curr
[
0
];
String
a
=
curr
[
0
];
try
{
try
{
//inserimento modulo arduino
//
inserimento modulo arduino
DBC
.
fillModules
(
a
,
"Arduino1"
,
s1
[
i
]);
DBC
.
fillModules
(
a
,
"Arduino1"
,
s1
[
i
]);
}
catch
(
SQLException
|
JSONException
e
)
{
}
catch
(
SQLException
|
JSONException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
...
@@ -91,5 +154,4 @@ public class Domain {
...
@@ -91,5 +154,4 @@ public class Domain {
}
}
}
}
}
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
domainManager/Domain/src/code/Helper.java
+
1
−
1
View file @
6edf9f5a
...
@@ -26,7 +26,7 @@ public class Helper {
...
@@ -26,7 +26,7 @@ public class Helper {
private
static
String
keycloakRealm
=
"test00"
;
private
static
String
keycloakRealm
=
"test00"
;
private
static
String
keycloakURL
=
"http://localhost:8080/"
;
private
static
String
keycloakURL
=
"http://localhost:8080/"
;
private
static
String
cloudappURL
=
"http://localhost:3002/"
;
private
static
String
cloudappURL
=
"http://localhost:3002/"
;
private
static
String
selfURL
=
"http://localhost:3001"
;
private
static
String
selfURL
=
"http
s
://localhost:3001"
;
public
static
void
sendCors
(
HttpExchange
exchange
,
int
stato
,
String
response
)
throws
IOException
{
public
static
void
sendCors
(
HttpExchange
exchange
,
int
stato
,
String
response
)
throws
IOException
{
Headers
headers
=
exchange
.
getResponseHeaders
();
Headers
headers
=
exchange
.
getResponseHeaders
();
...
...
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