Changeset 357

User picture

Author: klystr

(2009/12/03 13:25) About 2 years ago


  

Affected files

Updated Weektaken2/Weektaak3/ESB/src/hanze/ga/wt3/channels/ESBQueue.java Download diff

356357
16
		this.queue.add(input);
16
		this.queue.add(input);
17
	}
17
	}
18
18
19
	public String pollFromQueue() {
20
		System.out.println("[" + this.getqName() + "] Item was polled");
21
		return this.queue.poll();
22
	}
23
24
	public String getqName() {
19
	public String getqName() {
25
		return qName;
20
		return qName;
26
	}
21
	}
...
...
37
		return qSenderFilter;
32
		return qSenderFilter;
38
	}
33
	}
39
34
35
	public int getSize() {
36
		return this.queue.size();
37
	}
38
39
	public String pollFromQueue() {
40
		System.out.println("[" + this.getqName() + "] Item was polled");
41
		return this.queue.poll();
42
	}
43
40
	public void setqName(String qName) {
44
	public void setqName(String qName) {
41
		this.qName = qName;
45
		this.qName = qName;
42
	}
46
	}
...
...
53
		this.qSenderFilter = qSenderFilter;
57
		this.qSenderFilter = qSenderFilter;
54
	}
58
	}
55
59
56
	public int getSize() {
57
		return this.queue.size();
58
	}
59
60
}
60
}

Updated Weektaken2/Weektaak3/ESB/src/hanze/ga/wt3/ftp/ESBFTP.java Download diff

356357
4
import java.io.InputStream;
4
import java.io.InputStream;
5
import java.net.SocketException;
5
import java.net.SocketException;
6
6
7
import org.apache.commons.net.ftp.*;
7
import org.apache.commons.net.ftp.FTPClient;
8
8
9
public class ESBFTP {
9
public class ESBFTP {
10
10
11
	private String ftpAddress, ftpUserName, ftpPassword;
11
	private String ftpAddress, ftpUserName, ftpPassword;
12
	int ftpPort;
12
	int ftpPort;
13
	
13
14
	public ESBFTP(String ftpAddress, String ftpUserName, String ftpPassword, int ftpPort) {
14
	public ESBFTP(String ftpAddress, String ftpUserName, String ftpPassword, int ftpPort) {
15
		this.ftpAddress = ftpAddress;
15
		this.ftpAddress = ftpAddress;
16
		this.ftpUserName = ftpUserName;
16
		this.ftpUserName = ftpUserName;
...
...
32
			e.printStackTrace();
32
			e.printStackTrace();
33
		}
33
		}
34
	}
34
	}
35
	
35
36
}
36
}

Updated Weektaken2/Weektaak3/ESB/src/hanze/ga/wt3/ftp/FTPQ2AppChannel.java Download diff

356357
1
package hanze.ga.wt3.ftp;
1
package hanze.ga.wt3.ftp;
2
2
3
import java.io.*;
4
5
import hanze.ga.wt3.channels.Q2App;
3
import hanze.ga.wt3.channels.Q2App;
6
import hanze.ga.wt3.filters.ToIBGFilter;
4
import hanze.ga.wt3.filters.ToIBGFilter;
7
import hanze.ga.wt3.xml.Xml;
5
import hanze.ga.wt3.xml.Xml;
8
6
7
import java.io.ByteArrayInputStream;
8
import java.io.File;
9
import java.io.FileWriter;
10
import java.io.StringReader;
11
import java.io.StringWriter;
12
9
public class FTPQ2AppChannel extends Q2App {
13
public class FTPQ2AppChannel extends Q2App {
10
14
11
	private String ftpAddress;
15
	private String ftpAddress;
...
...
30
		return ftpUserName;
34
		return ftpUserName;
31
	}
35
	}
32
36
33
	@Override
34
	public void run() {
35
		try {
36
			while (true) {
37
				this.readQueue();
38
				Thread.sleep(1000);
39
			}
40
		} catch (InterruptedException e) {
41
			e.printStackTrace();
42
		}
43
	}
44
45
	private void readQueue() {
46
		while (this.getSourceQueue().getSize() > 0) {
47
			this.processItem(this.getSourceQueue().pollFromQueue());
48
		}
49
	}
50
51
	private void processItem(String pollItem) {
37
	private void processItem(String pollItem) {
52
38
53
		StringWriter stringWriter = new StringWriter();
39
		StringWriter stringWriter = new StringWriter();
...
...
62
			Xml xmlParser = new Xml("C:/esb.tmp", "message");
48
			Xml xmlParser = new Xml("C:/esb.tmp", "message");
63
			String reqId = xmlParser.child("id").content();
49
			String reqId = xmlParser.child("id").content();
64
			tmpFile.delete();
50
			tmpFile.delete();
65
			
51
66
			// Filter item
52
			// Filter item
67
			ToIBGFilter toFilter = new ToIBGFilter();
53
			ToIBGFilter toFilter = new ToIBGFilter();
68
			toFilter.doFilter(new StringReader(pollItem), stringWriter);
54
			toFilter.doFilter(new StringReader(pollItem), stringWriter);
69
			String fileContents = stringWriter.toString();
55
			String fileContents = stringWriter.toString();
70
			
56
71
			// Maak FTP-verbinding klaar
57
			// Maak FTP-verbinding klaar
72
			ESBFTP ftpClient = new ESBFTP(this.getFtpAddress(), this.getFtpUserName(), this
58
			ESBFTP ftpClient = new ESBFTP(this.getFtpAddress(), this.getFtpUserName(), this
73
					.getFtpPassword(), this.getChannelPort());
59
					.getFtpPassword(), this.getChannelPort());
...
...
88
74
89
	}
75
	}
90
76
77
	private void readQueue() {
78
		while (this.getSourceQueue().getSize() > 0) {
79
			this.processItem(this.getSourceQueue().pollFromQueue());
80
		}
81
	}
82
83
	@Override
84
	public void run() {
85
		try {
86
			while (true) {
87
				this.readQueue();
88
				Thread.sleep(1000);
89
			}
90
		} catch (InterruptedException e) {
91
			e.printStackTrace();
92
		}
93
	}
94
91
	public void setFtpAddress(String ftpAddress) {
95
	public void setFtpAddress(String ftpAddress) {
92
		this.ftpAddress = ftpAddress;
96
		this.ftpAddress = ftpAddress;
93
		super.setChannelIp(ftpAddress);
97
		super.setChannelIp(ftpAddress);

Updated Weektaken2/Weektaak3/ESB/src/hanze/ga/wt3/http/HTTPApp2QChannel.java Download diff

356357
1
package hanze.ga.wt3.http;
1
package hanze.ga.wt3.http;
2
2
3
import java.io.*;
4
import java.net.*;
5
6
import hanze.ga.wt3.channels.App2Q;
3
import hanze.ga.wt3.channels.App2Q;
7
import hanze.ga.wt3.helper.Security;
4
import hanze.ga.wt3.helper.Security;
8
5
6
import java.io.IOException;
7
import java.net.ServerSocket;
8
9
public class HTTPApp2QChannel extends App2Q {
9
public class HTTPApp2QChannel extends App2Q {
10
10
11
	private Security httpSecurity;
11
	private Security httpSecurity;

Updated Weektaken2/Weektaak3/ESB/src/hanze/ga/wt3/http/HTTPConnection.java Download diff

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
}

Updated Weektaken2/Weektaak3/ESB/src/hanze/ga/wt3/main/ESBServer.java Download diff

356357
1
package hanze.ga.wt3.main;
1
package hanze.ga.wt3.main;
2
2
3
import hanze.ga.wt3.channels.*;
3
import hanze.ga.wt3.channels.App2Q;
4
import hanze.ga.wt3.channels.Channel;
5
import hanze.ga.wt3.channels.ESBQueue;
6
import hanze.ga.wt3.channels.Q2App;
4
import hanze.ga.wt3.xml.ESBDOMParser;
7
import hanze.ga.wt3.xml.ESBDOMParser;
5
8
6
import java.util.HashMap;
9
import java.util.HashMap;
...
...
39
	}
42
	}
40
43
41
	/**
44
	/**
45
	 * Start elke channel in een nieuwe Thread
46
	 */
47
	private void initChannels() {
48
		for (Channel channel : this.esbChannels.values()) {
49
			new Thread(channel).start();
50
		}
51
	}
52
53
	/**
42
	 * Zorgt ervoor dat een Channel de juiste bron- en bestemmingsQueue heeft.
54
	 * Zorgt ervoor dat een Channel de juiste bron- en bestemmingsQueue heeft.
43
	 */
55
	 */
44
	private void linkQueueToChannel() {
56
	private void linkQueueToChannel() {
...
...
61
			}
73
			}
62
		}
74
		}
63
	}
75
	}
64
65
	/**
66
	 * Start elke channel in een nieuwe Thread
67
	 */
68
	private void initChannels() {
69
		for (Channel channel : this.esbChannels.values()) {
70
			new Thread(channel).start();
71
		}
72
	}
73
}
76
}