root/Weektaken2/Weektaak3/ESB/src/hanze/ga/wt3/http/HTTPConnection.java

356357
11
	private Socket socket;
11
	private Socket socket;
12
	private PrintWriter outPutWriter;
12
	private PrintWriter outPutWriter;
13
	private BufferedReader inputReader;
13
	private BufferedReader inputReader;
14
	private StringBuilder inputMessage;
14
	private String inputMessage;
15
	private HTTPApp2QChannel httpObject;
15
	private HTTPApp2QChannel httpObject;
16
16
17
	public HTTPConnection(Socket accept, HTTPApp2QChannel httpObject) {
17
	public HTTPConnection(Socket accept, HTTPApp2QChannel httpObject) {
...
...
19
		this.httpObject = httpObject;
19
		this.httpObject = httpObject;
20
	}
20
	}
21
21
22
	private String getHttpQueueName() {
23
		return this.httpObject.getDestinationQueue().getqName();
24
	}
25
26
	private String getQueueName(String header) {
27
		// "POST /<<QueueNaam>> HTTP/1.1"
28
		return header.substring(6, header.indexOf(" HTTP/1.1"));
29
	}
30
31
	private void handleValidRequest() {
32
		int contentLength = 0;
33
		try {
34
			String inputLine = inputReader.readLine().trim();
35
			while (inputLine != null) {
36
				if (inputLine.startsWith("Content-Length=")) {
37
					// Content length oplezen
38
					try {
39
						contentLength = Integer.parseInt(inputLine.substring(15));
40
					} catch (Exception e) {
41
						outPutWriter.println("HTTP/1.1 500 Bad Request");
42
						break;
43
					}
44
				} else {
45
					inputMessage = inputLine;
46
					break;
47
				}
48
				inputLine = inputReader.readLine().trim();
49
			}
50
51
		} catch (Exception e) {
52
			outPutWriter.println("HTTP/1.1 500 Bad Request");
53
		}
54
	}
55
22
	private void initListen() {
56
	private void initListen() {
23
		System.out.println("[HTTPApp2QChannel] Connection received from "
57
		System.out.println("[HTTPApp2QChannel] Connection received from "
24
				+ socket.getInetAddress().getHostName());
58
				+ socket.getInetAddress().getHostName());
...
...
66
100
67
	}
101
	}
68
102
69
	private void handleValidRequest() {
70
		int contentLength = 0;
71
		this.inputMessage = new StringBuilder();
72
		try {
73
			String inputLine = inputReader.readLine().trim();
74
			while (inputLine != null) {
75
				if (inputLine.startsWith("Content-Length=")) {
76
					// Content length oplezen
77
					try {
78
						contentLength = Integer.parseInt(inputLine.substring(15));
79
					} catch (Exception e) {
80
						outPutWriter.println("HTTP/1.1 500 Bad Request");
81
						break;
82
					}
83
				} else {
84
					// Lees input in stringbuilder
85
					char[] tmp = new char[contentLength];
86
					int index = 0;
87
					int read = 0;
88
					while (index < contentLength
89
							&& (read = inputReader.read(tmp, index, contentLength - index)) >= 0) {
90
						index += read;
91
					}
92
93
					inputMessage.append("<message>");
94
					inputMessage.append(tmp);
95
					break;
96
				}
97
				inputLine = inputReader.readLine().trim();
98
			}
99
100
		} catch (Exception e) {
101
			outPutWriter.println("HTTP/1.1 500 Bad Request");
102
		}
103
	}
104
105
	private String getHttpQueueName() {
106
		return this.httpObject.getDestinationQueue().getqName();
107
	}
108
109
	private String getQueueName(String header) {
110
		// "POST /<<QueueNaam>> HTTP/1.1"
111
		return header.substring(6, header.indexOf(" HTTP/1.1"));
112
	}
113
114
	private void writeToQueue() {
115
		this.httpObject.getDestinationQueue().addToQueue(inputMessage.toString());
116
	}
117
118
	@Override
103
	@Override
119
	public void run() {
104
	public void run() {
120
		// Open connection
105
		// Open connection
121
		this.initListen();
106
		this.initListen();
122
		
107
123
		// Input lezen
108
		// Input lezen
124
		this.listenAndHandle();
109
		this.listenAndHandle();
125
		
110
126
		// Schrijf input naar Queue
111
		// Schrijf input naar Queue
127
		this.writeToQueue();
112
		this.writeToQueue();
128
	}
113
	}
129
114
115
	private void writeToQueue() {
116
		this.httpObject.getDestinationQueue().addToQueue(inputMessage);
117
	}
118
130
}
119
}