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 |
package hanze.ga.wt3.http;
import hanze.ga.wt3.channels.App2Q;
import hanze.ga.wt3.main.ESBServer;
import hanze.ga.wt3.socket.SocketApp2QChannel;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class HTTPPostConnection implements Runnable {
private Socket socket;
private PrintWriter outPutWriter;
private BufferedReader inputReader;
private HTTPApp2QChannel httpObject;
public HTTPPostConnection(Socket accept, HTTPApp2QChannel httpObject) {
this.socket = accept;
this.httpObject = httpObject;
}
private String getHttpQueueName(App2Q object) {
return object.getDestinationQueue().getqName();
}
private String getQueueName(String header) {
// "POST /<<QueueNaam>> HTTP/1.1"
return header.substring(6, header.indexOf(" HTTP/1.1"));
}
private String getReqeustInfo(String inputLine) {
// Queuenaam / Content length
return inputLine.substring(5).trim();
}
private void handleSocketRequest(String inputLine) {
// Pak socket
SocketApp2QChannel inputSocket;
inputSocket = (SocketApp2QChannel) ESBServer.esbChannels.get("LocatorIn");
// Header info
String header = getReqeustInfo(inputLine);
String[] info = header.split("/");
// Queue controleren
if (this.getHttpQueueName(inputSocket).equals(info[0].trim())) {
try {
String readline = inputReader.readLine();
while (readline != null) {
inputSocket.getDestinationQueue().addToQueue(inputLine);
outPutWriter.println("ACK");
break;
}
} catch (Exception e) {
// Do nothing
outPutWriter.println("NACK");
}
} else {
outPutWriter.println("NACK");
}
}
private void handleValidRequest() {
int contentLength = 0;
try {
String inputLine = inputReader.readLine();
while (inputLine != null) {
if (inputLine.startsWith("Content-Length=")) {
// Content length oplezen
try {
contentLength = Integer.parseInt(inputLine.substring(15));
} catch (Exception e) {
outPutWriter.println("HTTP/1.1 500 Bad Request");
break;
}
} else {
this.httpObject.getDestinationQueue().addToQueue(inputLine);
break;
}
inputLine = inputReader.readLine().trim();
}
} catch (Exception e) {
outPutWriter.println("HTTP/1.1 500 Bad Request");
}
}
private void initListen() {
System.out.println("[HTTPApp2QChannel] 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("POST")) { // Naar FTP Queue
if (this.getHttpQueueName(this.httpObject).equals(this.getQueueName(inputLine))) {
this.handleValidRequest();
outPutWriter.println("HTTP/1.1 200 OK");
break;
} else {
outPutWriter.println("HTTP/1.1 404 Not Found");
break;
}
} else if (inputLine.startsWith("SEND")) { // Naar Socket Queue
this.handleSocketRequest(inputLine);
}
// Volgende line
inputLine = inputReader.readLine();
}
// Sluiten
outPutWriter.close();
inputReader.close();
} catch (Exception e) {
// do nothing
System.err.println("Fout in connectie");
}
}
@Override
public void run() {
// Open connection
this.initListen();
// Input lezen
this.listenAndHandle();
}
} |