commit 29ce5b819a3eb3bb6c4914b8f6b4ade3ff43aba8 Author: Daniil Gentili Date: Sat Nov 4 16:36:04 2017 +0300 First public version diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..a782717 --- /dev/null +++ b/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project WarehouseServer. + + + diff --git a/manifest.mf b/manifest.mf new file mode 100644 index 0000000..1574df4 --- /dev/null +++ b/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml new file mode 100644 index 0000000..6c95bd3 --- /dev/null +++ b/nbproject/build-impl.xml @@ -0,0 +1,1452 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties new file mode 100644 index 0000000..fa3ecbe --- /dev/null +++ b/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=6fa89b9d +build.xml.script.CRC32=54c0d1b5 +build.xml.stylesheet.CRC32=8064a381@1.80.1.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=6fa89b9d +nbproject/build-impl.xml.script.CRC32=cfaf6078 +nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48 diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..e4940de --- /dev/null +++ b/nbproject/project.properties @@ -0,0 +1,83 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=WarehouseServer +application.vendor=root +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/WarehouseServer.jar +dist.javadoc.dir=${dist.dir}/javadoc +endorsed.classpath= +excludes= +includes=** +jar.compress=false +javac.classpath=\ + ${reference.HTTP.jar}:\ + ${reference.WarehouseLibrary.jar} +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.external.vm=true +javac.processorpath=\ + ${javac.classpath} +javac.source=1.8 +javac.target=1.8 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=warehouseserver.WarehouseServer +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +project.HTTP=../HTTPPayloads +project.license=gpl30 +project.WarehouseLibrary=../WarehouseLibrary +reference.HTTP.jar=${project.HTTP}/dist/HTTP.jar +reference.WarehouseLibrary.jar=${project.WarehouseLibrary}/dist/WarehouseLibrary.jar +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 0000000..4c20db8 --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,33 @@ + + + org.netbeans.modules.java.j2seproject + + + WarehouseServer + + + + + + + + + + HTTP + jar + + jar + clean + jar + + + WarehouseLibrary + jar + + jar + clean + jar + + + + diff --git a/src/warehouseserver/ConnectionRunnable.java b/src/warehouseserver/ConnectionRunnable.java new file mode 100644 index 0000000..7cb128f --- /dev/null +++ b/src/warehouseserver/ConnectionRunnable.java @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2017 Daniil Gentili + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package warehouseserver; + +import Payloads.ServerException; +import Payloads.RequestPayload; +import Payloads.ResponsePayload; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.Socket; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author Studente + */ +public class ConnectionRunnable implements Runnable { + + BufferedReader input; + BufferedWriter output; + Socket client; + Boolean run = true; + String path = "/root/NetBeansProjects/WarehouseServer/src/warehouseserver/warehouse.txt"; + + public ConnectionRunnable(Socket client) throws IOException { + this.client = client; + input = new BufferedReader(new InputStreamReader(client.getInputStream())); + output = new BufferedWriter(new OutputStreamWriter(client.getOutputStream())); + } + + + @Override + public void run() { + String buffer = ""; + while (run) { + try { + buffer = this.handlePayload(new RequestPayload(input)).toString(); + } catch (IOException ex) { + Logger.getLogger(ConnectionRunnable.class.getName()).log(Level.SEVERE, null, ex); + buffer = new ResponsePayload(500, "Internal server error").toString(); + } catch (ServerException e) { + buffer = e.getPayload().toString(); + } + try { + output.write(buffer); + output.flush(); + } catch (IOException ex) { + Logger.getLogger(ConnectionRunnable.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + + private ResponsePayload handlePayload(RequestPayload request) throws ServerException, IOException { + if (!"HTTP".equals(request.getProtocol())) { + throw new ServerException(request, 400, "Bad request", "Wrong protocol"); + } + run = request.shouldKeepAlive(); + + switch (request.getMethod()) { + case "GET": + if (!"/".equals(request.getURI())) { + throw new ServerException(request, 404, "File not found", "Database not found"); + } + return new ResponsePayload(request, 200, "OK", getDatabase()); + case "POST": + if (!"/".equals(request.getURI())) { + throw new ServerException(request, 404, "File not found", "Database not found"); + } + return new ResponsePayload(request, 200, "OK", getDatabase()); + default: + throw new ServerException(request, 400, "Bad Request", "Bad HTTP method"); + + } + + } + + synchronized private String getDatabase() throws IOException { + return String.join("\n", Files.readAllLines(Paths.get(path))); + } + +} diff --git a/src/warehouseserver/MultiServer.java b/src/warehouseserver/MultiServer.java new file mode 100644 index 0000000..0f92d81 --- /dev/null +++ b/src/warehouseserver/MultiServer.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2017 Daniil Gentili + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package warehouseserver; + +import java.io.IOException; +import java.net.ServerSocket; +import java.util.concurrent.ExecutorService; +import static java.util.concurrent.Executors.newCachedThreadPool; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author Studente + */ +public class MultiServer { + private Integer port; + + MultiServer(Integer port) { + this.port = port; + } + public void start() { + try { + ExecutorService pool = newCachedThreadPool(); + ServerSocket server = new ServerSocket(port); + while (true) { + pool.submit(new ConnectionRunnable(server.accept())); + } + } catch (IOException ex) { + Logger.getLogger(MultiServer.class.getName()).log(Level.SEVERE, null, ex); + } + } +} diff --git a/src/warehouseserver/WarehouseServer.java b/src/warehouseserver/WarehouseServer.java new file mode 100644 index 0000000..927ddf7 --- /dev/null +++ b/src/warehouseserver/WarehouseServer.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2017 Daniil Gentili + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package warehouseserver; + +/** + * + * @author Studente + */ +public class WarehouseServer { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + new MultiServer(9090).start(); + } +} diff --git a/src/warehouseserver/warehouse.txt b/src/warehouseserver/warehouse.txt new file mode 100644 index 0000000..bac9868 --- /dev/null +++ b/src/warehouseserver/warehouse.txt @@ -0,0 +1,2 @@ +1;Twilight Sparkle plushie;49.99;20 +666;Tempest Shadow plushie;79.99;10