1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 |
package hanze.ga.wt3.http;
import hanze.ga.wt3.xml.Xml;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.LinkedList;
public class HTTPGetConnection implements Runnable {
private Socket socket;
private PrintWriter outPutWriter;
private BufferedReader inputReader;
private HTTPQ2AppChannel httpObject;
public HTTPGetConnection(Socket accept, HTTPQ2AppChannel httpq2AppChannel) {
this.socket = accept;
this.httpObject = httpq2AppChannel;
}
private String findId(String inQueue) {
File tmpFile = new File("C:/queuetemp.tmp");
try {
FileWriter fw = new FileWriter("C:/queuetemp.tmp");
fw.write(inQueue);
fw.close();
} catch (Exception e) {
// Doe niets
}
Xml xmlParser = new Xml("C:/queuetemp.tmp", "message");
String reqId = xmlParser.child("id").content();
tmpFile.delete();
return reqId;
}
private String getHttpQueueName() {
return this.httpObject.getSourceQueue().getqName();
}
private String getId(String queryType) {
int start = queryType.indexOf("?ID=") + 4;
queryType = queryType.replace(" HTTP/1.1", "");
return queryType.substring(start).trim();
}
private String getQueueName(String header) {
// "GET /<<QueueNaam>> HTTP/1.1"
if (header.indexOf("?") > 0) { // Has ID
return header.substring(5, header.indexOf("?ID="));
}
// "GET /<<QueueNaam>> HTTP/1.1"
return header.substring(5, header.indexOf(" HTTP/1.1"));
}
private void initListen() {
System.out.println("[HTTPQ2AppChannel] Connection received from "
+ socket.getInetAddress().getHostName());
try {
inputReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
outPutWriter = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}
}
private void listenAndHandle() {
// Luister naar input van gebruiker
try {
String inputLine = inputReader.readLine();
while (inputLine != null) {
if (inputLine.startsWith("GET /")) {
String queryType = this.getHttpQueueName();
String qName = this.getQueueName(inputLine);
if (queryType.equals(qName)) {
this.reply(inputLine);
break;
} else {
System.err.println("Queue " + qName + " niet gevonden");
outPutWriter.println("HTTP/1.1 404 Not Found");
break;
}
}
// Volgende line
inputLine = inputReader.readLine();
}
// Sluiten
outPutWriter.close();
inputReader.close();
} catch (Exception e) {
// do nothing
System.err.println("Fout in connectie");
e.printStackTrace();
}
}
private void reply(String queryType) {
if (queryType.indexOf("?ID=") > 0) { // Specifiek ID
this.replyId(this.getId(queryType));
} else { // Overzicht
this.replyList();
}
}
private void replyId(String queryId) {
LinkedList<String> queued = this.httpObject.getSourceQueue().peekEntireQueue();
if (queued.size() > 0) {
for (String inQueue : queued) {
if (queryId.equals(this.findId(inQueue))) {
outPutWriter.write("HTTP/1.1 200 OK\n");
outPutWriter.write("Content-Length=" + inQueue.length() + "\n");
outPutWriter.write(inQueue);
// Verwijder
this.httpObject.getSourceQueue().deleteFromQueue(inQueue);
return;
}
}
}
outPutWriter.write("HTTP/1.1 404 Not Found");
}
private void replyList() {
LinkedList<String> queued = this.httpObject.getSourceQueue().peekEntireQueue();
if (queued.size() > 0) {
LinkedList<String> idOutput = new LinkedList<String>();
int contentLength = 0;
for (String inQueue : queued) {
String inputId = "ID=" + this.findId(inQueue) + "\n";
idOutput.add(inputId);
contentLength += inputId.length();
}
outPutWriter.write("HTTP/1.1 200 OK\n");
outPutWriter.write("Content-Length=" + contentLength + "\n");
for (String id : idOutput) {
System.out.println(id);
outPutWriter.write(id.trim());
}
}
}
@Override
public void run() {
// Open connection
this.initListen();
// Input lezen
this.listenAndHandle();
}
} |