Skip to content

Commit

Permalink
Merge pull request #65 from sanhee/refactor/attribute/main
Browse files Browse the repository at this point in the history
Attribute 리팩토링
  • Loading branch information
sanhee authored Dec 2, 2021
2 parents 2c1ce14 + 752d4db commit a4f4cf2
Show file tree
Hide file tree
Showing 10 changed files with 360 additions and 194 deletions.
72 changes: 72 additions & 0 deletions src/main/java/webserver/http/attribute/Attributes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package webserver.http.attribute;

import util.HttpRequestUtils;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;

public class Attributes {
private final Map<String, String> attributes = new LinkedHashMap<>();

public static Attributes from(Map<String, String> attributes) {
return new Attributes().addAll(attributes);
}

public static Attributes from(String headerText) {
Map<String, String> attributes = new LinkedHashMap<>();

String[] splittedHeaderTexts = headerText.split(System.lineSeparator());
for (String splittedHeaderText : splittedHeaderTexts) {
HttpRequestUtils.Pair pair = HttpRequestUtils.parseHeader(splittedHeaderText);

if (pair != null) {
attributes.put(pair.getKey(), pair.getValue());
}
}

return from(attributes);
}

public Attributes add(String key, String value) {
attributes.put(key, value);
return this;
}

public Attributes addAll(Map<String, String> attributes) {
this.attributes.putAll(attributes);
return this;
}

public String get(String key) {
return attributes.get(key);
}

public String getOrDefault(String key, String defaultValue) {
return attributes.getOrDefault(key, defaultValue);
}

public String toHeaderText() {
StringJoiner headerText = new StringJoiner("\r\n");
for (Map.Entry<String, String> each : attributes.entrySet()) {
headerText.add(each.getKey() + ": " + each.getValue());
}

return headerText.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Attributes that = (Attributes) o;
return Objects.equals(attributes, that.attributes);
}

@Override
public int hashCode() {
return Objects.hash(attributes);
}

}
31 changes: 7 additions & 24 deletions src/main/java/webserver/http/header/Header.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
package webserver.http.header;

import util.HttpRequestUtils;
import webserver.http.attribute.Attributes;

import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;

public abstract class Header {
private Map<String, String> attributes;
private Attributes attributes;

protected Header(Map<String, String> attributes) {
protected Header(Attributes attributes) {
this.attributes = attributes;
}

protected static Map<String, String> attributeFrom(String headerText) {
Map<String, String> attributes = new LinkedHashMap<>();

String[] splittedHeaderTexts = headerText.split(System.lineSeparator());
for (String splittedHeaderText : splittedHeaderTexts) {
HttpRequestUtils.Pair pair = HttpRequestUtils.parseHeader(splittedHeaderText);

if (pair != null) {
attributes.put(pair.getKey(), pair.getValue());
}
}

return attributes;
}

public Map<String, String> getAttributes() {
public Attributes getAttributes() {
return attributes;
}

Expand All @@ -37,9 +20,9 @@ public byte[] getBytes() {

sb.append(getStatusLine()).append(System.lineSeparator());

for (Map.Entry<String, String> entry : getAttributes().entrySet()) {
sb.append(entry.getKey() + ": " + entry.getValue() + System.lineSeparator());
}
String attributesString = attributes.toHeaderText();

sb.append(attributesString + (!attributesString.isEmpty() ? "\r\n" : ""));

sb.append(System.lineSeparator());

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/webserver/http/header/RequestHeader.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package webserver.http.header;

import util.HttpRequestUtils;
import webserver.http.attribute.Attributes;
import webserver.http.statusline.RequestStatusLine;

import java.util.List;
import java.util.Map;

public class RequestHeader extends Header {
private RequestStatusLine statusLine;

protected RequestHeader(RequestStatusLine statusLine, Map<String, String> attributes) {
protected RequestHeader(RequestStatusLine statusLine, Attributes attributes) {
super(attributes);
this.statusLine = statusLine;
}

public static RequestHeader of(List<String> statusLine, Map<String, String> attributes) {
public static RequestHeader of(List<String> statusLine, Attributes attributes) {
return new RequestHeader(RequestStatusLine.from(statusLine), attributes);
}

public static RequestHeader from(String headerText) {
String[] splittedHeaderTexts = headerText.split(System.lineSeparator());
List<String> statusLine = HttpRequestUtils.parseStatusLine(splittedHeaderTexts[0]);

return RequestHeader.of(statusLine, attributeFrom(headerText));
return RequestHeader.of(statusLine, Attributes.from(headerText));
}

public String getPath() {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/webserver/http/header/ResponseHeader.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package webserver.http.header;

import util.HttpRequestUtils;
import webserver.http.attribute.Attributes;
import webserver.http.statusline.ResponseStatusLine;

import java.util.List;
import java.util.Map;

public class ResponseHeader extends Header {

private ResponseStatusLine statusLine;

protected ResponseHeader(ResponseStatusLine statusLine, Map<String, String> attributes) {
protected ResponseHeader(ResponseStatusLine statusLine, Attributes attributes) {
super(attributes);
this.statusLine = statusLine;
}

public static ResponseHeader of(List<String> statusLine, Map<String, String> attributes) {
public static ResponseHeader of(List<String> statusLine, Attributes attributes) {
return new ResponseHeader(ResponseStatusLine.from(statusLine), attributes);
}

public static ResponseHeader from(String headerText) {
String[] splittedHeaderTexts = headerText.split(System.lineSeparator());
List<String> statusLine = HttpRequestUtils.parseStatusLine(splittedHeaderTexts[0]);

return ResponseHeader.of(statusLine, attributeFrom(headerText));
return ResponseHeader.of(statusLine, Attributes.from(headerText));
}

@Override
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/webserver/http/statusline/StatusLine.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package webserver.http.statusline;

import webserver.http.attribute.Attributes;

import java.util.Map;

public abstract class StatusLine {
protected static final String PROTOCOL_VERSION_KEY = "protocolVersion";

private Map<String, String> statusLineAttributes;
private Attributes statusLineAttributes;

public StatusLine(Map<String, String> statusLineAttributes) {
this.statusLineAttributes = statusLineAttributes;
this.statusLineAttributes = Attributes.from(statusLineAttributes);
}

public String getProtocol() {
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/webserver/RequestHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
import model.User;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
Expand Down
Loading

0 comments on commit a4f4cf2

Please sign in to comment.