Created
April 16, 2015 16:47
-
-
Save pumpkincouture/dcd153eb70bbe0ecd9ae to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class RequestParser { | |
static final int FIRST_ELEMENT = 0; | |
static final int SECOND_ELEMENT = 1; | |
static final int ELEMENTS_IN_REQUEST_LINE = 3; | |
static final String LINE_BREAK = "\\n"; | |
static final String EMPTY_SPACE = " "; | |
static final String COLON = ": "; | |
static final String AFTER_COLON = ":\\s"; | |
static final String EQUAL_SIGN = "="; | |
String request; | |
String[] splitFirstLine; | |
String[] splitRequest; | |
public RequestParser(String request) { | |
this.request = request; | |
splitFirstLine = request.split(EMPTY_SPACE , ELEMENTS_IN_REQUEST_LINE); | |
splitRequest = request.split((blankLine())); | |
} | |
public String getMethod() { | |
return findMethod(); | |
} | |
public String getPath() { | |
return findPath(); | |
} | |
public HashMap<String, String> getHeaders() { | |
return createTable(getMatchingStrings(COLON), AFTER_COLON); | |
} | |
public HashMap<String, String> getData() { | |
return createTable(getMatchingStrings(EQUAL_SIGN), EQUAL_SIGN); | |
} | |
private String findMethod() { | |
return splitFirstLine[FIRST_ELEMENT]; | |
} | |
private String findPath() { | |
return splitFirstLine[SECOND_ELEMENT]; | |
} | |
private List<String> getMatchingStrings(String stringToMatch) { | |
List<String> stringsList = new ArrayList<>(); | |
for (String string : splitRequest) { | |
if (string.contains(stringToMatch)) { | |
stringsList.add(string); | |
} | |
} | |
return stringsList; | |
} | |
private HashMap<String, String> createTable(List<String> listOfStrings, String stringToSplitOn) { | |
HashMap<String, String> table = new LinkedHashMap<>(); | |
for (String string : listOfStrings) { | |
String[] splitHeaders = string.split(stringToSplitOn); | |
table.put(splitHeaders[FIRST_ELEMENT], splitHeaders[SECOND_ELEMENT]); | |
} | |
return table; | |
} | |
private String blankLine() { | |
return LINE_BREAK; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment