Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.cooksys.httpserver; import java.io.Serializable; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.http.Header; import org.apache.http.HttpMessage; import org.apache.http.HttpRequest; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.utils.URLEncodedUtils; /** * * @author Tim Davidson * Parser class for Apache's HttpRequest object. Needed this abstraction because * with HttpRequest, you can only access stream parameters once (namely * requestLine and entity). This class allows us to store the http request and * view it later. */ public class ParsedHttpRequest implements Serializable { private String requestLine; private String method; private String uriPath; private List<String> uriParams; private List<String> headers; private String messageBody; public ParsedHttpRequest() { } //static method that returns ParsedHttpRequest object from an HttpRequest public static ParsedHttpRequest parseHttpRequest(HttpRequest request, String messageBody) { ParsedHttpRequest parsedRequest = new ParsedHttpRequest(); parsedRequest.requestLine = request.getRequestLine().toString(); parsedRequest.method = request.getRequestLine().getMethod(); try { //Parse the URI URI uri = new URI(request.getRequestLine().getUri()); parsedRequest.uriPath = uri.getPath(); parsedRequest.uriParams = parseUrlParameters(uri); parsedRequest.headers = (parseHeaders(request)); parsedRequest.messageBody = messageBody; } catch (URISyntaxException | ParseException ex) { Logger.getLogger(ParsedHttpRequest.class.getName()).log(Level.SEVERE, null, ex); } return parsedRequest; } //parses the queryString into a list of string format: [header] [value] private static List<String> parseUrlParameters(URI uri) { List<String> parameterList = new ArrayList<>(); List<NameValuePair> params = URLEncodedUtils.parse(uri, "UTF-8"); for (NameValuePair param : params) { parameterList.add(param.getName() + " " + param.getValue()); } return parameterList; } //parses the headers into a list of string with format: [header] [value] private static List<String> parseHeaders(HttpMessage message) { Header[] headers = message.getAllHeaders(); List<String> headerList = new ArrayList<>(); for (Header header : headers) { headerList.add(header.getName() + " " + header.getValue()); } return headerList; } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } public String getUriPath() { return uriPath; } public void setUriPath(String uriPath) { this.uriPath = uriPath; } public List<String> getUriParams() { return uriParams; } public void setUriParams(List<String> uriParams) { this.uriParams = uriParams; } public List<String> getHeaders() { return headers; } public void setHeaders(List<String> headers) { this.headers = headers; } public String getMessageBody() { return messageBody; } public void setMessageBody(String messageBody) { this.messageBody = messageBody; } public String getRequestLine() { return requestLine; } public void setRequestLine(String requestLine) { this.requestLine = requestLine; } }