1
2
3
4
5 package oshi.demo;
6
7 import java.io.BufferedOutputStream;
8 import java.io.BufferedReader;
9 import java.io.IOException;
10 import java.io.InputStreamReader;
11 import java.io.OutputStreamWriter;
12 import java.io.PrintWriter;
13 import java.net.ServerSocket;
14 import java.net.Socket;
15 import java.nio.charset.StandardCharsets;
16 import java.time.Instant;
17 import java.util.Locale;
18 import java.util.StringTokenizer;
19
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import com.fasterxml.jackson.databind.ObjectMapper;
24
25 import oshi.SystemInfo;
26
27
28
29
30
31
32
33
34
35 public class OshiHTTPServer implements Runnable {
36
37 private static final int PORT = 8080;
38
39 private static final Logger logger = LoggerFactory.getLogger(OshiHTTPServer.class);
40
41
42 private Socket connect;
43
44 public OshiHTTPServer(Socket c) {
45 connect = c;
46 logger.debug("Connecton opened.");
47 }
48
49 public static void main(String[] args) {
50 try (ServerSocket serverConnect = new ServerSocket(PORT)) {
51 logger.info("Server started. Listening for connections on port {}", PORT);
52
53
54 while (true) {
55 OshiHTTPServer myServer = new OshiHTTPServer(serverConnect.accept());
56
57
58 Thread thread = new Thread(myServer);
59 thread.start();
60 }
61 } catch (IOException e) {
62 logger.error("Server Connection error: {}", e.getMessage());
63 }
64 }
65
66 @Override
67 public void run() {
68 try (
69 BufferedReader in = new BufferedReader(
70 new InputStreamReader(connect.getInputStream(), StandardCharsets.UTF_8));
71
72 PrintWriter out = new PrintWriter(
73 new OutputStreamWriter(connect.getOutputStream(), StandardCharsets.UTF_8));
74
75 BufferedOutputStream dataOut = new BufferedOutputStream(connect.getOutputStream())) {
76
77
78 String input = in.readLine();
79 if (input == null) {
80 throw new IOException("No characters read from input stream.");
81 }
82
83 StringTokenizer parse = new StringTokenizer(input);
84 String method = parse.nextToken().toUpperCase(Locale.ROOT);
85
86 String fileRequested = parse.nextToken().toLowerCase(Locale.ROOT);
87
88
89 if (!method.equals("GET") && !method.equals("HEAD")) {
90 logger.debug("501 Not Implemented: {}", method);
91 String contentMimeType = "text/html";
92
93
94 out.println("HTTP/1.1 501 Not Implemented");
95 out.println("Server: OSHI HTTP Server");
96 out.println("Date: " + Instant.now());
97 out.println("Content-type: " + contentMimeType);
98 out.println("Content-length: " + 0);
99 out.println();
100 out.flush();
101
102
103 } else {
104
105
106
107 SystemInfo si = new SystemInfo();
108 ObjectMapper mapper = new ObjectMapper();
109 byte[] content = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(si)
110 .getBytes(StandardCharsets.UTF_8);
111
112 if (method.equals("GET")) {
113
114 out.println("HTTP/1.1 200 OK");
115 out.println("Server: OSHI HTTP Server");
116 out.println("Date: " + Instant.now());
117 out.println("Content-type: application/json");
118 out.println("Content-length: " + content.length);
119 out.println();
120 out.flush();
121
122 dataOut.write(content, 0, content.length);
123 dataOut.flush();
124 }
125
126 logger.debug("Data {} returned", fileRequested);
127 }
128 } catch (IOException ioe) {
129 logger.error("Server error: {}", ioe.getMessage());
130 } finally {
131 try {
132
133 connect.close();
134 } catch (Exception e) {
135 logger.error("Error closing connection: {}", e.getMessage());
136 }
137 logger.debug("Connection closed.");
138 }
139 }
140 }