Newer
Older
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package code;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.security.KeyStore;
import java.util.concurrent.Executors;
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;
public class Server {
public static int port = 443;
public static final String VERSION = "1.0";
public static void main(String[] args) throws IOException {
if (args.length > 1 && args[0].equals("-port"))
try {
port = Integer.parseInt(args[1]);
} catch (Exception e) {
e.printStackTrace();
}
HttpsServer server = HttpsServer.create(new InetSocketAddress(port), 0);
// initialise the HTTPS server
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
// initialise the keystore
char[] password = "simulator".toCharArray();
KeyStore ks = KeyStore.getInstance("JKS");
FileInputStream fis = new FileInputStream("testkey.jks");
ks.load(fis, password);
// setup the key manager factory
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, password);
// setup the trust manager factory
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
// setup the HTTPS context and parameters
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
server.setHttpsConfigurator(new HttpsConfigurator(sslContext) {
public void configure(HttpsParameters params) {
try {
// 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");
}
}
});
//API del server
server.createContext("/index/", new AllURIs());
server.createContext("/bulloni/", new Handler());
server.createContext("/utensili/", new Handler());
server.createContext("/vernici/", new Handler());
server.createContext("/altro/", new Handler());
server.createContext("/", new Home());
server.setExecutor((ThreadPoolExecutor) Executors.newFixedThreadPool(5));
server.start();
System.out.println("server running on localhost:"+port);
} catch (Exception e) {
System.out.println("Failed to create HTTPS server on port " + port + " of localhost");
e.printStackTrace();
}
}
// public static void main(String[] args) throws IOException {//main HTTP
// if (args.length > 1 && args[0].equals("-port"))
// try {
// port = Integer.parseInt(args[1]);
// } catch (Exception e) {
// e.printStackTrace();
// }
// ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(5);//Executors.newWorkStealingPool() doesn't work
// HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
//
// server.createContext("/index/", new AllURIs());
// server.createContext("/bulloni/", new Handler());
// server.createContext("/utensili/", new Handler());
// server.createContext("/vernici/", new Handler());
// server.createContext("/altro/", new Handler());
// server.createContext("/", new Home());
//
// server.setExecutor(threadPoolExecutor);
// server.start();
//
// System.out.println("server running on localhost:"+port);
//
// }
public static String getLocalPage(URI pageid) {
String line;
String pageIDString = pageid.toString();
String page = "./html" + pageIDString.substring(0, pageIDString.length() - 1) + ".txt";// entro nella cartella
// html e leggo il file
// txt
String answer = "";
if (getExtension(page).length() == 0)
page += ".html";
try {
FileReader fileReader = new FileReader(page);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null) {
answer += (line + "\n");
}
bufferedReader.close();
} 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";
}
return answer;
}
private static String getExtension(String file) {
int i = file.length() - 1;
while (i > 0 && file.charAt(i) != '.' && file.charAt(i) != '/')
i--;
if (file.charAt(i) == '.')
return file.substring(i + 1);
else
return "";
}
}