-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[톰캣 구현하기 1 & 2단계] 달리(김동연) 미션 제출합니다 (#351)
* feat: ioStream 학습 * feat: fileTest 학습 * docs: 기능 구현 목록 작성 * feat: get method 응답 기능 구현 * feat: queryString 파싱 기능 구현 * feat: 로그인 redirect 구현 * feat: 로그인 redirect 구현 * feat: Session 구현 * refactor : 구조 변경 * fix : 로그인 페이지 오류 수정 * style : 코드 컨벤션 준수 및 사용하지 않는 메서드 제거 * style : cookie header로 이동 * refactor: 중복된 file 코드 통합 * feat : auto-increment id 추가 * test : 테스트 추가 * refactor: equals 재정의
- Loading branch information
1 parent
68db530
commit 841e70a
Showing
27 changed files
with
968 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,16 @@ | ||
# 톰캣 구현하기 | ||
|
||
## 요구사항 | ||
|
||
- [x] GET /index.html 응답 | ||
- [x] CSS 지원 | ||
- [x] Query String 파싱 | ||
|
||
- [x] HTTP Status Code 302 | ||
- [x] POST 방식으로 회원가입 | ||
- [x] Cookie에 JSESSIONID 값 저장하기 | ||
- [x] Session 구현하기 | ||
|
||
## 기능 목록 | ||
- [x] header에서 입력 url을 추출한다 | ||
- [x] resource에 해당 정적 html 파일을 가져온다 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
tomcat/src/main/java/nextstep/jwp/controller/LoginController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
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.request.Request; | ||
import org.apache.coyote.http11.response.HttpStatus; | ||
import org.apache.coyote.http11.response.Response; | ||
import org.apache.coyote.http11.servlet.Servlet; | ||
|
||
public class LoginController { | ||
|
||
public static Response login(Request request){ | ||
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())){ | ||
throw new UnauthorizedException("아이디 및 패스워드가 틀렸습니다."); | ||
} | ||
String jSessionId = InMemorySession.login(user); | ||
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(); | ||
} | ||
|
||
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 ""; | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
tomcat/src/main/java/nextstep/jwp/controller/ViewController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package nextstep.jwp.controller; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
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; | ||
|
||
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 static Response getVoid(Request request){ | ||
return Response.builder() | ||
.status(HttpStatus.OK) | ||
.responseBody("Hello world!") | ||
.contentType("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 ""; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package nextstep.jwp.db; | ||
|
||
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<>(); | ||
|
||
public static String login(User user){ | ||
UUID uuid = UUID.randomUUID(); | ||
session.put(user,uuid); | ||
return uuid.toString(); | ||
} | ||
public static boolean isLogin(String id){ | ||
for(UUID uuid: session.values()){ | ||
if(uuid.toString().equals(id)){ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package nextstep.jwp.db; | ||
|
||
import java.util.concurrent.atomic.AtomicLong; | ||
import nextstep.jwp.model.User; | ||
|
||
import java.util.Map; | ||
|
@@ -9,14 +10,14 @@ | |
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(1L, "gugu", "password", "[email protected]"); | ||
final User user = new User(autoIncrementId.get(), "gugu", "password", "[email protected]"); | ||
database.put(user.getAccount(), user); | ||
} | ||
|
||
public static void save(User user) { | ||
database.put(user.getAccount(), user); | ||
database.put(user.getAccount(), user.putId(autoIncrementId.addAndGet(1L))); | ||
} | ||
|
||
public static Optional<User> findByAccount(String account) { | ||
|
7 changes: 7 additions & 0 deletions
7
tomcat/src/main/java/nextstep/jwp/exception/UnauthorizedException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package nextstep.jwp.exception; | ||
|
||
public class UnauthorizedException extends RuntimeException{ | ||
public UnauthorizedException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package nextstep.jwp.model; | ||
|
||
import java.util.Map; | ||
|
||
public class AuthUser { | ||
private final String account; | ||
private final String password; | ||
|
||
public AuthUser(String account, String password) { | ||
this.account = account; | ||
this.password = password; | ||
} | ||
|
||
public static AuthUser from(Map<String, String> query){ | ||
return new AuthUser(query.get("account"),query.get("password")); | ||
} | ||
|
||
public String getAccount() { | ||
return account; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
} |
Oops, something went wrong.