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

[톰캣 구현하기 - 3, 4단계] 달리 미션 제출합니다 #442

Merged
merged 20 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
4 changes: 2 additions & 2 deletions study/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ handlebars:

server:
tomcat:
accept-count: 1
max-connections: 1
accept-count: 100
max-connections: 250
threads:
max: 2
2 changes: 1 addition & 1 deletion study/src/test/java/thread/stage0/SynchronizationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private static final class SynchronizedMethods {

private int sum = 0;

public void calculate() {
synchronized public void calculate() {
setSum(getSum() + 1);
}

Expand Down
6 changes: 3 additions & 3 deletions study/src/test/java/thread/stage0/ThreadPoolsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ void testNewFixedThreadPool() {
executor.submit(logWithSleep("hello fixed thread pools"));

// 올바른 값으로 바꿔서 테스트를 통과시키자.
final int expectedPoolSize = 0;
final int expectedQueueSize = 0;
final int expectedPoolSize = 2;
final int expectedQueueSize = 1;

assertThat(expectedPoolSize).isEqualTo(executor.getPoolSize());
assertThat(expectedQueueSize).isEqualTo(executor.getQueue().size());
Expand All @@ -46,7 +46,7 @@ void testNewCachedThreadPool() {
executor.submit(logWithSleep("hello cached thread pools"));

// 올바른 값으로 바꿔서 테스트를 통과시키자.
final int expectedPoolSize = 0;
final int expectedPoolSize = 3;
final int expectedQueueSize = 0;

assertThat(expectedPoolSize).isEqualTo(executor.getPoolSize());
Expand Down
2 changes: 1 addition & 1 deletion study/src/test/java/thread/stage2/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AppTest {
*/
@Test
void test() throws Exception {
final var NUMBER_OF_THREAD = 10;
final var NUMBER_OF_THREAD = 250;
var threads = new Thread[NUMBER_OF_THREAD];

for (int i = 0; i < NUMBER_OF_THREAD; i++) {
Expand Down
41 changes: 41 additions & 0 deletions tomcat/src/main/java/nextstep/jwp/config/LoginFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package nextstep.jwp.config;

import nextstep.jwp.db.InMemorySession;
import org.apache.coyote.http11.filter.Filter;
import org.apache.coyote.http11.filter.FilterChain;
import org.apache.coyote.http11.request.Request;
import org.apache.coyote.http11.response.HttpStatus;
import org.apache.coyote.http11.response.Response;
import org.apache.coyote.http11.util.Resource;

public class LoginFilter implements Filter {

@Override
public void doFilter(Request request, Response response, FilterChain filterChain) {
final String uri = request.getPath();
final var cookie = request.getCookie();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시 자료형을 나타낼 때 var를 사용하는 경우와 그렇지 않은 경우는 어떤 기준이신가요?

Copy link
Author

@waterricecake waterricecake Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 원래는 var를 안쓸려고 했는데 여기에 추가한 것을 깜빡했네요;;;;
var를 안써봐서 확실한 기준이 아직은 없네요;;
만약 사용한다면 메서드나 기능적인면 혹은 역할이 바뀌지 않는 객체이지만 후에 type이 변경될 수 있을때 사용할 것 같습니다.
0ad5111


if (uri.equals("/login") && cookie.containsKey("JSESSIONID")) {
validKey(cookie.get("JSESSIONID"), response);
}
filterChain.doFilter(request, response);
}

private void validKey(String jSessionId, Response response) {
if (InMemorySession.isLogin(jSessionId)) {
response
.setStatus(HttpStatus.FOUND)
.setContentType("html")
.setLocation("/index.html")
.setResponseBody(Resource.getFile("index.html"))
.setFiltered(true);
return;
}
response
.setStatus(HttpStatus.UNAUTHORIZED)
.setContentType("html")
.setResponseBody(Resource.getFile("401.html"))
.setLocation("/401.html")
.setFiltered(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package nextstep.jwp.controller;

import org.apache.coyote.http11.controller.AbstractController;
import org.apache.coyote.http11.request.Request;
import org.apache.coyote.http11.response.HttpStatus;
import org.apache.coyote.http11.response.Response;

public class HelloWorldController extends AbstractController {
@Override
protected void doGet(Request request, Response response) {
response.setStatus(HttpStatus.OK)
.setContentType("html")
.setResponseBody("Hello world!");
}
}
67 changes: 21 additions & 46 deletions tomcat/src/main/java/nextstep/jwp/controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -1,68 +1,43 @@
package nextstep.jwp.controller;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import nextstep.jwp.db.InMemorySession;
import nextstep.jwp.db.InMemoryUserRepository;
import nextstep.jwp.exception.UnauthorizedException;
import nextstep.jwp.model.AuthUser;
import nextstep.jwp.model.User;
import org.apache.coyote.http11.controller.AbstractController;
import org.apache.coyote.http11.request.Request;
import org.apache.coyote.http11.response.HttpStatus;
import org.apache.coyote.http11.response.Response;
import org.apache.coyote.http11.servlet.Servlet;
import org.apache.coyote.http11.util.Resource;

public class LoginController {
public class LoginController extends AbstractController {

public static Response login(Request request){
@Override
protected void doPost(Request request, Response response) {
Map<String, String> body = request.getBody();
AuthUser authUser = AuthUser.from(body);
User user = InMemoryUserRepository.findByAccount(authUser.getAccount()).orElseThrow(()->new UnauthorizedException("해당 유저가 없습니다."));
if(!user.checkPassword(authUser.getPassword())){
User user = InMemoryUserRepository.findByAccount(body.get("account"))
.orElseThrow(() -> new UnauthorizedException("해당 유저가 없습니다."));
if (!user.checkPassword(body.get("password"))) {
throw new UnauthorizedException("아이디 및 패스워드가 틀렸습니다.");
}
String jSessionId = InMemorySession.login(user);
Map<String,String> cookie = new HashMap<>();
if(!request.getCookie().containsKey("JSESSIONID")){
cookie.put("JSESSIONID",jSessionId);
Map<String, String> cookie = new HashMap<>();
if (!request.getCookie().containsKey("JSESSIONID")) {
cookie.put("JSESSIONID", jSessionId);
}
return Response.builder()
.status(HttpStatus.FOUND)
.contentType("html")
.cookie(cookie)
.location("index.html")
.responseBody(getFile("index.html"))
.build();
response.setStatus(HttpStatus.FOUND)
.setContentType("html")
.setCookie(cookie)
.setLocation("index.html")
.setResponseBody(Resource.getFile("index.html"));
}

public static Response signUp(Request request){
Map<String, String> requestBody = request.getBody();
final String account = requestBody.get("account");
final String password = requestBody.get("password");
final String email = requestBody.get("email");
User user = new User(account,password,email);
InMemoryUserRepository.save(user);
return Response.builder()
.status(HttpStatus.FOUND)
.contentType("html")
.location("index.html")
.responseBody(getFile("index.html"))
.build();
}


private static String getFile(String fileName){
try {
final var fileUrl = Servlet.class.getClassLoader().getResource("static/" + fileName);
final var fileBytes = Files.readAllBytes(new File(fileUrl.getFile()).toPath());
return new String(fileBytes);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (NullPointerException e){
return "";
}
@Override
protected void doGet(Request request, Response response) {
response.setStatus(HttpStatus.OK)
.setContentType("html")
.setResponseBody(Resource.getFile("login.html"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package nextstep.jwp.controller;

import java.util.Map;
import nextstep.jwp.db.InMemoryUserRepository;
import nextstep.jwp.model.User;
import org.apache.coyote.http11.controller.AbstractController;
import org.apache.coyote.http11.request.Request;
import org.apache.coyote.http11.response.HttpStatus;
import org.apache.coyote.http11.response.Response;
import org.apache.coyote.http11.util.Resource;

public class RegisterController extends AbstractController {
@Override
public void doPost(Request request, Response response) {
Map<String, String> requestBody = request.getBody();
final String account = requestBody.get("account");
final String password = requestBody.get("password");
final String email = requestBody.get("email");
User user = new User(account, password, email);
InMemoryUserRepository.save(user);
response.setStatus(HttpStatus.FOUND)
.setContentType("html")
.setLocation("index.html")
.setResponseBody(Resource.getFile("index.html"));
}


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기 개행이 두줄 입니다!😱

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Override
protected void doGet(Request request, Response response) {
response.setStatus(HttpStatus.OK)
.setContentType("html")
.setResponseBody(Resource.getFile("register.html"));
}
}
46 changes: 9 additions & 37 deletions tomcat/src/main/java/nextstep/jwp/controller/ViewController.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,20 @@
package nextstep.jwp.controller;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.apache.coyote.http11.controller.AbstractController;
import org.apache.coyote.http11.request.Request;
import org.apache.coyote.http11.response.HttpStatus;
import org.apache.coyote.http11.response.Response;
import org.apache.coyote.http11.servlet.Servlet;
import org.apache.coyote.http11.util.Resource;

public class ViewController {
public static Response getLogin(Request request){
return Response.builder()
.status(HttpStatus.OK)
.contentType("html")
.responseBody(getFile("login.html"))
.build();
}

public static Response getRegister(Request request){
return Response.builder()
.status(HttpStatus.OK)
.contentType("html")
.responseBody(getFile("register.html"))
.build();
}
public class ViewController extends AbstractController {

public static Response getVoid(Request request){
return Response.builder()
.status(HttpStatus.OK)
.responseBody("Hello world!")
.contentType("html")
.build();
}
@Override
protected void doGet(Request request, Response response) {

String fileName = request.getPath();

private static String getFile(String fileName){
try {
final var fileUrl = Servlet.class.getClassLoader().getResource("static/" + fileName);
final var fileBytes = Files.readAllBytes(new File(fileUrl.getFile()).toPath());
return new String(fileBytes);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (NullPointerException e){
return "";
}
response.setStatus(HttpStatus.OK)
.setContentType(fileName.split("\\.")[1])
.setResponseBody(Resource.getFile(fileName));
}
}
12 changes: 6 additions & 6 deletions tomcat/src/main/java/nextstep/jwp/db/InMemorySession.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import nextstep.jwp.exception.UnauthorizedException;
import nextstep.jwp.model.User;

public class InMemorySession {
private static final Map<User, UUID> session = new HashMap<>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

한줄 개행 추가부탁합니다!
혹시 UUID가 키 값이 아니고 User가 키 값인 이유가 있을까요? 저라면 UUID를 키값으로 가지고 저희가 필요한 정보인 User를 값으로 했을 것 같습니다. 이 부분에 대해서 달리의 의견이 궁금합니다!

Copy link
Author

@waterricecake waterricecake Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 아코 말이 맞는 것 같네요! 단순히 user가 key를 가지는 것이라 생각했는데 생각해보니 uuid를 통해 user를 찾는 것이니 이런형식이 맞겠군요.
3bd9ea4


public static String login(User user){
public static String login(User user) {
UUID uuid = UUID.randomUUID();
session.put(user,uuid);
session.put(user, uuid);
return uuid.toString();
}
public static boolean isLogin(String id){
for(UUID uuid: session.values()){
if(uuid.toString().equals(id)){

public static boolean isLogin(String id) {
for (UUID uuid : session.values()) {
if (uuid.toString().equals(id)) {
return true;
}
}
Expand Down
11 changes: 6 additions & 5 deletions tomcat/src/main/java/nextstep/jwp/db/InMemoryUserRepository.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
package nextstep.jwp.db;

import java.util.concurrent.atomic.AtomicLong;
import nextstep.jwp.model.User;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import nextstep.jwp.model.User;

public class InMemoryUserRepository {

private static final Map<String, User> database = new ConcurrentHashMap<>();
private static final AtomicLong autoIncrementId = new AtomicLong(1L);

static {
final User user = new User(autoIncrementId.get(), "gugu", "password", "hkkang@woowahan.com");
database.put(user.getAccount(), user);
}

private InMemoryUserRepository() {
}

public static void save(User user) {
database.put(user.getAccount(), user.putId(autoIncrementId.addAndGet(1L)));
}

public static Optional<User> findByAccount(String account) {
return Optional.ofNullable(database.get(account));
}

private InMemoryUserRepository() {}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package nextstep.jwp.exception;

public class UnauthorizedException extends RuntimeException{
public class UnauthorizedException extends RuntimeException {
public UnauthorizedException(String message) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도 개행 추가 부탁드립니다!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super(message);
}
Expand Down
25 changes: 0 additions & 25 deletions tomcat/src/main/java/nextstep/jwp/model/AuthUser.java

This file was deleted.

4 changes: 2 additions & 2 deletions tomcat/src/main/java/nextstep/jwp/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public String getAccount() {
return account;
}

public User putId(Long id){
return new User(id,account,password,email);
public User putId(Long id) {
return new User(id, account, password, email);
}


Expand Down
Loading