Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/webapp runner #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions 01_web/http-server/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ru.netology</groupId>
<artifactId>http-server</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
1 change: 1 addition & 0 deletions 01_web/http-server/public/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('it works!');
14 changes: 14 additions & 0 deletions 01_web/http-server/public/classic.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Classic Demo</h1>
<p>Current time is: {time}</p>
</body>
</html>
16 changes: 16 additions & 0 deletions 01_web/http-server/public/events.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p>You clicked: <span data-id="counter">0</span></p>
<button data-action="inc">Click me</button>

<script src="events.js"></script>
</body>
</html>
8 changes: 8 additions & 0 deletions 01_web/http-server/public/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let count = 0;
const counterEl = document.querySelector('[data-id="counter"]');
const incEl = document.querySelector('[data-action="inc"]');

incEl.addEventListener('click', () => {
count++;
counterEl.textContent = `${count}`;
});
18 changes: 18 additions & 0 deletions 01_web/http-server/public/forms.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Forms Demo</h1>
<form>
<input name="login" placeholder="Login">
<input name="password" type="password" placeholder="Password">
<button>Login</button>
</form>
</body>
</html>
13 changes: 13 additions & 0 deletions 01_web/http-server/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>HTML Document</title>
</head>
<body>
<h1>HTML Document</h1>
</body>
</html>
14 changes: 14 additions & 0 deletions 01_web/http-server/public/links.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<a href="./spring.png">PNG Logo</a>
<a href="./spring.svg">SVG Logo</a>
</body>
</html>
16 changes: 16 additions & 0 deletions 01_web/http-server/public/resources.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>HTML Document</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>HTML Document</h1>
<img src="spring.png">
<script src="app.js"></script>
</body>
</html>
Binary file added 01_web/http-server/public/spring.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions 01_web/http-server/public/spring.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions 01_web/http-server/public/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
color: #6EB149;
font-family: Arial, serif;
}
7 changes: 7 additions & 0 deletions 01_web/http-server/src/main/java/ru/netology/Handler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.netology;

import java.io.BufferedOutputStream;

public interface Handler {
public void handle(Request request, BufferedOutputStream responseStream);
}
10 changes: 10 additions & 0 deletions 01_web/http-server/src/main/java/ru/netology/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.netology;

public class Main {
public static void main(String[] args) {
final var server = new Server();
server.listen();
}
}


4 changes: 4 additions & 0 deletions 01_web/http-server/src/main/java/ru/netology/Request.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ru.netology;

public class Request {
}
102 changes: 102 additions & 0 deletions 01_web/http-server/src/main/java/ru/netology/Server.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package ru.netology;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.Executors;

public class Server {

private final List<String> validPaths = List.of("/index.html", "/spring.svg", "/spring.png", "/resources.html", "/styles.css", "/app.js", "/links.html", "/forms.html", "/classic.html", "/events.html", "/events.js");

public void listen() {
var poolExecutor = Executors.newFixedThreadPool(64);

try (final var serverSocket = new ServerSocket(9999)) {
log("Server start!");
while (true) {
Socket socket = serverSocket.accept();
poolExecutor.submit(() -> handle(socket));
}
} catch (IOException e) {
e.printStackTrace(System.out);
}
}

public void handle(Socket socket) {
try (final var in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
final var out = new BufferedOutputStream(socket.getOutputStream())
) {
log("client connected: " + socket.getRemoteSocketAddress());
// read only request line for simplicity
// must be in form GET /path HTTP/1.1
final var requestLine = in.readLine();
final var parts = requestLine.split(" ");

if (parts.length != 3) {
// just close socket
log("Invalid request");
return;
}

final var path = parts[1];
if (!validPaths.contains(path)) {
out.write((
"HTTP/1.1 404 Not Found\r\n" +
"Content-Length: 0\r\n" +
"Connection: close\r\n" +
"\r\n"
).getBytes());
out.flush();
return;
}

final var filePath = Path.of(".", "public", path);
final var mimeType = Files.probeContentType(filePath);
// special case for classic
if (path.equals("/classic.html")) {
final var template = Files.readString(filePath);
final var content = template.replace(
"{time}",
LocalDateTime.now().toString()
).getBytes();
out.write((
"HTTP/1.1 200 OK\r\n" +
"Content-Type: " + mimeType + "\r\n" +
"Content-Length: " + content.length + "\r\n" +
"Connection: close\r\n" +
"\r\n"
).getBytes());
out.write(content);
out.flush();
log("classic.html loaded");
return;
}

final var length = Files.size(filePath);
out.write((
"HTTP/1.1 200 OK\r\n" +
"Content-Type: " + mimeType + "\r\n" +
"Content-Length: " + length + "\r\n" +
"Connection: close\r\n" +
"\r\n"
).getBytes());
Files.copy(filePath, out);
log(filePath.getFileName() + " loaded");
out.flush();
} catch (IOException e) {
e.printStackTrace(System.out);
}
}

private void log(String message) {
System.out.println("[" + Thread.currentThread().getName() + "] " + message);
}
}
Binary file not shown.
Binary file not shown.
56 changes: 56 additions & 0 deletions 04_servlets/servlets/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ru.netology</groupId>
<artifactId>servlets</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>copy</goal></goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.heroku</groupId>
<artifactId>webapp-runner</artifactId>
<version>9.0.27.1</version>
<destFileName>webapp-runner.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ru.netology.controller;

import com.google.gson.Gson;
import ru.netology.model.Post;
import ru.netology.service.PostService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Reader;

public class PostController {
public static final String APPLICATION_JSON = "application/json";
private final PostService service;

public PostController(PostService service) {
this.service = service;
}

public void all(HttpServletResponse response) throws IOException {
response.setContentType(APPLICATION_JSON);
final var data = service.all();
final var gson = new Gson();
response.getWriter().print(gson.toJson(data));
}

public void getById(long id, HttpServletResponse response) throws IOException {
// TODO: deserialize request & serialize response
response.setContentType(APPLICATION_JSON);
final var gson = new Gson();
final var post = service.getById(id);
response.getWriter().print(gson.toJson(post));
}

public void save(Reader body, HttpServletResponse response) throws IOException {
response.setContentType(APPLICATION_JSON);
final var gson = new Gson();
final var post = gson.fromJson(body, Post.class);
final var data = service.save(post);
response.getWriter().print(gson.toJson(data));
}

public void removeById(long id, HttpServletResponse response) {
// TODO: deserialize request & serialize response
response.setContentType(APPLICATION_JSON);
service.removeById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.netology.exception;

public class NotFoundException extends RuntimeException {
public NotFoundException() {
}

public NotFoundException(String message) {
super(message);
}

public NotFoundException(String message, Throwable cause) {
super(message, cause);
}

public NotFoundException(Throwable cause) {
super(cause);
}

public NotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Loading