Commit c675e93f authored by Mohamad Bashar Desoki's avatar Mohamad Bashar Desoki

simple web app

parent 06b0dc91
...@@ -5,32 +5,58 @@ ...@@ -5,32 +5,58 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="ad904d3c-917a-41ec-8997-0c459fd2925c" name="Changes" comment=""> <list default="true" id="ad904d3c-917a-41ec-8997-0c459fd2925c" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/src/main/java/org/ds/WebServer.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/org/ds/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/ds/Main.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/resources/index.html" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/index.html" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" /> <option name="LAST_RESOLUTION" value="IGNORE" />
</component> </component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings"> <component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" /> <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component> </component>
<component name="ProjectColorInfo"><![CDATA[{ <component name="ProjectColorInfo">{
"associatedIndex": 1 &quot;associatedIndex&quot;: 1
}]]></component> }</component>
<component name="ProjectId" id="2tREiXPX513LksO5LzaY0KttqM7" /> <component name="ProjectId" id="2tREiXPX513LksO5LzaY0KttqM7" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" /> <component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState"> <component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent">{
"keyToString": { &quot;keyToString&quot;: {
"RunOnceActivity.ShowReadmeOnStart": "true", &quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
"git-widget-placeholder": "master", &quot;git-widget-placeholder&quot;: &quot;master&quot;,
"kotlin-language-version-configured": "true" &quot;kotlin-language-version-configured&quot;: &quot;true&quot;
} }
}]]></component> }</component>
<component name="RunManager">
<configuration default="true" type="JetRunConfigurationType">
<module name="simple-web-app" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration default="true" type="KotlinStandaloneScriptRunConfigurationType">
<module name="simple-web-app" />
<option name="filePath" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" /> <component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager"> <component name="TaskManager">
<task active="true" id="Default" summary="Default task"> <task active="true" id="Default" summary="Default task">
...@@ -42,4 +68,15 @@ ...@@ -42,4 +68,15 @@
</task> </task>
<servers /> <servers />
</component> </component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
</project> </project>
\ No newline at end of file
...@@ -14,4 +14,13 @@ ...@@ -14,4 +14,13 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.12.1</version>
</dependency>
</dependencies>
</project> </project>
\ No newline at end of file
...@@ -2,6 +2,14 @@ package org.ds; ...@@ -2,6 +2,14 @@ package org.ds;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Hello world!"); if (args.length != 2) {
System.out.println("java -jar (jar name) PORT_NUMBER SERVER_NAME");
}
int currentServerPort = Integer.parseInt(args[0]);
String serverName = args[1];
WebServer webServer = new WebServer(currentServerPort, serverName);
webServer.startServer();
} }
} }
\ No newline at end of file
package org.ds;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
public class WebServer {
private static final String STATUS_ENDPOINT = "/status";
private static final String HOME_PAGE_ENDPOINT = "/";
private static final String HTML_PAGE = "index.html";
private final int port;
private HttpServer server;
private final String serverName;
public WebServer(int port, String serverName) {
this.port = port;
this.serverName = serverName;
}
public void startServer() {
try {
this.server = HttpServer.create(new InetSocketAddress(port), 0);
} catch (IOException e) {
e.printStackTrace();
return;
}
server.createContext(STATUS_ENDPOINT, this::handleStatusCheckRequest);
server.createContext(HOME_PAGE_ENDPOINT, this::handleHomePageRequest);
server.setExecutor(Executors.newFixedThreadPool(8));
System.out.println(String.format("Started server %s on port %d ", serverName, port));
server.start();
}
private void handleHomePageRequest(HttpExchange exchange) throws IOException {
if (!exchange.getRequestMethod().equalsIgnoreCase("get")) {
exchange.close();
return;
}
System.out.println(String.format("%s received a request", this.serverName));
exchange.getResponseHeaders().add("Content-Type", "text/html");
exchange.getResponseHeaders().add("Cache-Control", "no-cache");
byte[] response = loadHtml(HTML_PAGE);
sendResponse(response, exchange);
}
/**
* Loads the HTML page to be fetched to the web browser
*
* @param htmlFilePath - The relative path to the html file
* @throws IOException
*/
private byte[] loadHtml(String htmlFilePath) throws IOException {
InputStream htmlInputStream = getClass().getResourceAsStream(htmlFilePath);
if (htmlInputStream == null) {
return new byte[]{};
}
Document document = Jsoup.parse(htmlInputStream, "UTF-8", "");
String modifiedHtml = modifyHtmlDocument(document);
return modifiedHtml.getBytes();
}
/**
* Fills the server's name and local time in theHTML document
*
* @param document - original HTML document
*/
private String modifyHtmlDocument(Document document) {
Element serverNameElement = document.selectFirst("#server_name");
serverNameElement.appendText(serverName);
return document.toString();
}
private void handleStatusCheckRequest(HttpExchange exchange) throws IOException {
if (!exchange.getRequestMethod().equalsIgnoreCase("get")) {
exchange.close();
return;
}
System.out.println("Received a health check");
String responseMessage = "Server is alive\n";
sendResponse(responseMessage.getBytes(), exchange);
}
private void sendResponse(byte[] responseBytes, HttpExchange exchange) throws IOException {
exchange.sendResponseHeaders(200, responseBytes.length);
OutputStream outputStream = exchange.getResponseBody();
outputStream.write(responseBytes);
outputStream.flush();
outputStream.close();
}
}
<!DOCTYPE html>
<html>
<head>
<title>Distributed Search</title>
<meta http-equiv="cache-control" content="no-cache"/>
</head>
<body style="background: #e6f3ff;">
<h1 style="color:blue; text-align: center; font-style: bold" id="server_name_title">Welcome to </h1>
<h1 style="color:#00b4ff; text-align: center; font-style: italic" id="server_name"></h1>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment