1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.fosstrak.epcis.utils;
22
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.PrintWriter;
28 import java.net.ServerSocket;
29 import java.net.Socket;
30 import java.net.SocketException;
31 import java.nio.ByteBuffer;
32 import java.nio.CharBuffer;
33 import java.nio.charset.Charset;
34 import java.nio.charset.CharsetDecoder;
35
36
37
38
39
40
41
42
43
44 public final class QueryCallbackListener extends Thread {
45
46 private static final int PORT = 8899;
47
48 private static QueryCallbackListener instance = null;
49
50 private ServerSocket server = null;
51
52 private boolean isRunning = false;
53
54 private String response = null;
55
56
57
58
59
60
61
62
63 private QueryCallbackListener() throws IOException {
64 System.out.println("listening for query callbacks on port " + PORT + " ...");
65 server = new ServerSocket(PORT);
66 }
67
68
69
70
71
72
73 public static QueryCallbackListener getInstance() throws IOException {
74 if (instance == null) {
75 instance = new QueryCallbackListener();
76 }
77 return instance;
78 }
79
80
81
82
83
84
85 public void run() {
86 isRunning = true;
87 while (isRunning) {
88 Socket client = null;
89 try {
90 client = server.accept();
91 handleConnection(client);
92 } catch (SocketException e) {
93
94 } catch (IOException e) {
95 e.printStackTrace();
96 } finally {
97 if (client != null) {
98 try {
99 client.close();
100 } catch (IOException e) {
101 e.printStackTrace();
102 }
103 }
104 }
105 }
106 }
107
108
109
110
111
112
113
114
115
116
117 private void handleConnection(final Socket client) throws IOException {
118 PrintWriter out = new PrintWriter(client.getOutputStream(), true);
119 InputStream is = client.getInputStream();
120 BufferedReader in = new BufferedReader(new InputStreamReader(is));
121
122
123 String prefix = "content-length: ";
124 String inputLine = in.readLine().toLowerCase();
125 while (!inputLine.startsWith(prefix)) {
126
127 inputLine = in.readLine().toLowerCase();
128 }
129
130
131 String length = inputLine.substring(prefix.length());
132 int len = Integer.parseInt(length);
133
134 inputLine = in.readLine();
135 while (!inputLine.equals("")) {
136
137 inputLine = in.readLine();
138 }
139
140
141 byte[] xml = new byte[len];
142 is.read(xml);
143 ByteBuffer buf = ByteBuffer.wrap(xml);
144 Charset charset = Charset.forName("UTF-8");
145 CharsetDecoder decoder = charset.newDecoder();
146 CharBuffer charBuffer = decoder.decode(buf);
147 parseResponse(charBuffer.toString().trim());
148
149
150 out.write("HTTP/1.0 200 OK\n\n");
151 out.flush();
152
153
154 synchronized (this) {
155 this.notifyAll();
156 }
157
158 out.close();
159 in.close();
160 }
161
162
163
164
165
166
167
168 private void parseResponse(final String resp) {
169 if (resp.startsWith("<?xml")) {
170
171 int index = resp.indexOf("?>") + 2;
172 if (index >= 0) {
173 response = resp.substring(index).trim();
174 }
175 }
176 }
177
178
179
180
181 public String fetchResponse() {
182 String resp = this.response;
183 this.response = null;
184 return resp;
185 }
186
187
188
189
190 public boolean isRunning() {
191 return isRunning;
192 }
193
194
195
196
197 public void stopRunning() {
198 isRunning = false;
199 instance = null;
200 try {
201 server.close();
202 } catch (IOException e) {
203 e.printStackTrace();
204 }
205 }
206
207 }