Changeset 368

User picture

Author: klystr

(2009/12/07 10:44) About 2 years ago

Final

Affected files

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

367368
21
		System.out.println("[" + this.getqName() + "] Item was added");
21
		System.out.println("[" + this.getqName() + "] Item was added");
22
		this.queue.add(input);
22
		this.queue.add(input);
23
	}
23
	}
24
	
24
25
	public void deleteFromQueue(String queuedObject) {
25
	public void deleteFromQueue(String queuedObject) {
26
		this.queue.remove(queuedObject);
26
		this.queue.remove(queuedObject);
27
	}
27
	}
...
...
47
	}
47
	}
48
48
49
	/**
49
	/**
50
	 * Geeft Array terug van hele queue
51
	 * 
52
	 * @return Lijst met alle items
53
	 */
54
	public LinkedList<String> peekEntireQueue() {
55
		return new LinkedList<String>(this.queue);
56
	}
57
58
	/**
50
	 * Removes latest item from the Queue and removes it from the Queue.
59
	 * Removes latest item from the Queue and removes it from the Queue.
51
	 * 
60
	 * 
52
	 * @return File contents
61
	 * @return File contents
...
...
56
		return this.queue.poll();
65
		return this.queue.poll();
57
	}
66
	}
58
67
59
	/**
60
	 * Geeft Array terug van hele queue
61
	 * 
62
	 * @return Lijst met alle items
63
	 */
64
	public LinkedList<String> peekEntireQueue() {
65
		return new LinkedList<String>(this.queue);
66
	}
67
68
	public void setqName(String qName) {
68
	public void setqName(String qName) {
69
		this.qName = qName;
69
		this.qName = qName;
70
	}
70
	}

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

367368
5
import java.io.InputStream;
5
import java.io.InputStream;
6
import java.net.SocketException;
6
import java.net.SocketException;
7
7
8
import org.apache.commons.net.ftp.FTPClient;
9
import org.apache.commons.net.ftp.FTPFile;
10
11
public class ESBFTP {
8
public class ESBFTP {
12
9
13
	private String ftpAddress, ftpUserName, ftpPassword;
10
	private String ftpAddress, ftpUserName, ftpPassword;
...
...
34
	}
31
	}
35
32
36
	/**
33
	/**
34
	 * Haalt bestanden uit directory
35
	 * 
36
	 * @param directory
37
	 *            doeldirectory
38
	 * @return Array van FTPFiles
39
	 */
40
	public FTPFile[] getFilesFromDir(String directory) {
41
		this.makeConnection();
42
		FTPFile[] output = null;
43
		try {
44
			output = this.ftpClient.listFiles(directory);
45
		} catch (IOException e) {
46
			e.printStackTrace();
47
		}
48
		this.terminateConnection();
49
		return output;
50
	}
51
52
	/**
37
	 * Maakt verbinding met de FTP-server en slaat deze verbinding op in de
53
	 * Maakt verbinding met de FTP-server en slaat deze verbinding op in de
38
	 * lokale variabele ftpClient
54
	 * lokale variabele ftpClient
39
	 */
55
	 */
...
...
50
	}
66
	}
51
67
52
	/**
68
	/**
53
	 * Sluit connectie met server die nu in lokale variabele ftpClient staat
69
	 * Haalt opgegeven bestand van de server er verwijderd deze.
70
	 * 
71
	 * @param filePath
72
	 *            Pad + bestandsnaam
73
	 * @return Bestandsinhoud
54
	 */
74
	 */
55
	private void terminateConnection() {
75
	public String pollFile(String filePath) {
76
		this.makeConnection();
77
		ByteArrayOutputStream outputStream = null;
56
		try {
78
		try {
57
			this.ftpClient.disconnect();
79
			outputStream = new ByteArrayOutputStream();
80
			this.ftpClient.retrieveFile(filePath, outputStream);
81
			// this.ftpClient.dele(filePath);
58
		} catch (IOException e) {
82
		} catch (IOException e) {
59
			e.printStackTrace();
83
			e.printStackTrace();
60
		}
84
		}
61
		this.ftpClient = null;
85
86
		this.terminateConnection();
87
		return outputStream.toString();
62
	}
88
	}
63
89
64
	/**
90
	/**
...
...
82
	}
108
	}
83
109
84
	/**
110
	/**
85
	 * Haalt bestanden uit directory
111
	 * Sluit connectie met server die nu in lokale variabele ftpClient staat
86
	 * 
87
	 * @param directory
88
	 *            doeldirectory
89
	 * @return Array van FTPFiles
90
	 */
112
	 */
91
	public FTPFile[] getFilesFromDir(String directory) {
113
	private void terminateConnection() {
92
		this.makeConnection();
93
		FTPFile[] output = null;
94
		try {
114
		try {
95
			output = this.ftpClient.listFiles(directory);
115
			this.ftpClient.disconnect();
96
		} catch (IOException e) {
116
		} catch (IOException e) {
97
			e.printStackTrace();
117
			e.printStackTrace();
98
		}
118
		}
99
		this.terminateConnection();
119
		this.ftpClient = null;
100
		return output;
101
	}
120
	}
102
103
	/**
104
	 * Haalt opgegeven bestand van de server er verwijderd deze.
105
	 * 
106
	 * @param filePath
107
	 *            Pad + bestandsnaam
108
	 * @return Bestandsinhoud
109
	 */
110
	public String pollFile(String filePath) {
111
		this.makeConnection();
112
		ByteArrayOutputStream outputStream = null;
113
		try {
114
			outputStream = new ByteArrayOutputStream();
115
			this.ftpClient.retrieveFile(filePath, outputStream);
116
			// this.ftpClient.dele(filePath);
117
		} catch (IOException e) {
118
			e.printStackTrace();
119
		}
120
121
		this.terminateConnection();
122
		return outputStream.toString();
123
	}
124
}
121
}

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

367368
6
import java.io.StringReader;
6
import java.io.StringReader;
7
import java.io.StringWriter;
7
import java.io.StringWriter;
8
8
9
import org.apache.commons.net.ftp.FTPFile;
10
11
public class FTPApp2QChannel extends App2Q {
9
public class FTPApp2QChannel extends App2Q {
12
10
13
	private String ftpAddress;
11
	private String ftpAddress;
...
...
16
	private String ftpPassword;
14
	private String ftpPassword;
17
	private ESBFTP ftpClient;
15
	private ESBFTP ftpClient;
18
16
19
	@Override
17
	public String getFtpAddress() {
20
	public void run() {
18
		return ftpAddress;
21
		try {
22
			while (true) {
23
				this.readAndProcessFTP();
24
				Thread.sleep(10000);
25
			}
26
		} catch (InterruptedException e) {
27
			e.printStackTrace();
28
		}
29
	}
19
	}
30
20
21
	public String getFtpInitDir() {
22
		return ftpInitDir;
23
	}
24
25
	public String getFtpPassword() {
26
		return ftpPassword;
27
	}
28
29
	public String getFtpUserName() {
30
		return ftpUserName;
31
	}
32
31
	private void readAndProcessFTP() {
33
	private void readAndProcessFTP() {
32
		// Maak FTP-verbinding klaar
34
		// Maak FTP-verbinding klaar
33
		this.ftpClient = new ESBFTP(this.getFtpAddress(), this.getFtpUserName(), this
35
		this.ftpClient = new ESBFTP(this.getFtpAddress(), this.getFtpUserName(), this
...
...
45
		}
47
		}
46
	}
48
	}
47
49
48
	private String undoFilter(String fileContents) {
50
	@Override
49
		StringWriter stringWriter = new StringWriter();
51
	public void run() {
50
		FromIBGFilter fromFilter = new FromIBGFilter();
52
		try {
51
		fromFilter.doFilter(new StringReader(fileContents), stringWriter);
53
			while (true) {
52
54
				this.readAndProcessFTP();
53
		return stringWriter.toString();
55
				Thread.sleep(10000);
56
			}
57
		} catch (InterruptedException e) {
58
			e.printStackTrace();
59
		}
54
	}
60
	}
55
61
56
	public String getFtpAddress() {
57
		return ftpAddress;
58
	}
59
60
	public void setFtpAddress(String ftpAddress) {
62
	public void setFtpAddress(String ftpAddress) {
61
		this.ftpAddress = ftpAddress;
63
		this.ftpAddress = ftpAddress;
62
	}
64
	}
63
65
64
	public String getFtpInitDir() {
65
		return ftpInitDir;
66
	}
67
68
	public void setFtpInitDir(String ftpInitDir) {
66
	public void setFtpInitDir(String ftpInitDir) {
69
		this.ftpInitDir = ftpInitDir;
67
		this.ftpInitDir = ftpInitDir;
70
	}
68
	}
71
69
72
	public String getFtpUserName() {
70
	public void setFtpPassword(String ftpPassword) {
73
		return ftpUserName;
71
		this.ftpPassword = ftpPassword;
74
	}
72
	}
75
73
76
	public void setFtpUserName(String ftpUserName) {
74
	public void setFtpUserName(String ftpUserName) {
77
		this.ftpUserName = ftpUserName;
75
		this.ftpUserName = ftpUserName;
78
	}
76
	}
79
77
80
	public String getFtpPassword() {
78
	private String undoFilter(String fileContents) {
81
		return ftpPassword;
79
		StringWriter stringWriter = new StringWriter();
82
	}
80
		FromIBGFilter fromFilter = new FromIBGFilter();
81
		fromFilter.doFilter(new StringReader(fileContents), stringWriter);
83
82
84
	public void setFtpPassword(String ftpPassword) {
83
		return stringWriter.toString();
85
		this.ftpPassword = ftpPassword;
86
	}
84
	}
87
85
88
}
86
}

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

367368
23
		this.httpObject = httpq2AppChannel;
23
		this.httpObject = httpq2AppChannel;
24
	}
24
	}
25
25
26
	private String findId(String inQueue) {
27
		File tmpFile = new File("C:/queuetemp.tmp");
28
		try {
29
			FileWriter fw = new FileWriter("C:/queuetemp.tmp");
30
			fw.write(inQueue);
31
			fw.close();
32
		} catch (Exception e) {
33
			// Doe niets
34
		}
35
		Xml xmlParser = new Xml("C:/queuetemp.tmp", "message");
36
		String reqId = xmlParser.child("id").content();
37
		tmpFile.delete();
38
		return reqId;
39
	}
40
41
	private String getHttpQueueName() {
42
		return this.httpObject.getSourceQueue().getqName();
43
	}
44
26
	private String getId(String queryType) {
45
	private String getId(String queryType) {
27
		int start = queryType.indexOf("?ID=") + 4;
46
		int start = queryType.indexOf("?ID=") + 4;
28
		queryType = queryType.replace(" HTTP/1.1", "");
47
		queryType = queryType.replace(" HTTP/1.1", "");
...
...
38
		return header.substring(5, header.indexOf(" HTTP/1.1"));
57
		return header.substring(5, header.indexOf(" HTTP/1.1"));
39
	}
58
	}
40
59
41
	private void reply(String queryType) {
42
		if (queryType.indexOf("?ID=") > 0) { // Specifiek ID
43
			this.replyId(this.getId(queryType));
44
		} else { // Overzicht
45
			this.replyList();
46
		}
47
	}
48
49
	private String getHttpQueueName() {
50
		return this.httpObject.getSourceQueue().getqName();
51
	}
52
53
	private void initListen() {
60
	private void initListen() {
54
		System.out.println("[HTTPQ2AppChannel] Connection received from "
61
		System.out.println("[HTTPQ2AppChannel] Connection received from "
55
				+ socket.getInetAddress().getHostName());
62
				+ socket.getInetAddress().getHostName());
...
...
97
104
98
	}
105
	}
99
106
100
	private void replyList() {
107
	private void reply(String queryType) {
101
		LinkedList<String> queued = this.httpObject.getSourceQueue().peekEntireQueue();
108
		if (queryType.indexOf("?ID=") > 0) { // Specifiek ID
102
		if (queued.size() > 0) {
109
			this.replyId(this.getId(queryType));
103
			LinkedList<String> idOutput = new LinkedList<String>();
110
		} else { // Overzicht
104
			int contentLength = 0;
111
			this.replyList();
105
			for (String inQueue : queued) {
106
				String inputId = "ID=" + this.findId(inQueue) + "\n";
107
				idOutput.add(inputId);
108
				contentLength += inputId.length();
109
			}
110
			outPutWriter.write("HTTP/1.1 200 OK\n");
111
			outPutWriter.write("Content-Length=" + contentLength + "\n");
112
			for (String id : idOutput) {
113
				System.out.println(id);
114
				outPutWriter.write(id.trim());
115
			}
116
		}
112
		}
117
	}
113
	}
118
114
119
	private String findId(String inQueue) {
120
		File tmpFile = new File("C:/queuetemp.tmp");
121
		try {
122
			FileWriter fw = new FileWriter("C:/queuetemp.tmp");
123
			fw.write(inQueue);
124
			fw.close();
125
		} catch (Exception e) {
126
			// Doe niets
127
		}
128
		Xml xmlParser = new Xml("C:/queuetemp.tmp", "message");
129
		String reqId = xmlParser.child("id").content();
130
		tmpFile.delete();
131
		return reqId;
132
	}
133
134
	private void replyId(String queryId) {
115
	private void replyId(String queryId) {
135
		LinkedList<String> queued = this.httpObject.getSourceQueue().peekEntireQueue();
116
		LinkedList<String> queued = this.httpObject.getSourceQueue().peekEntireQueue();
136
		if (queued.size() > 0) {
117
		if (queued.size() > 0) {
...
...
150
		outPutWriter.write("HTTP/1.1 404 Not Found");
131
		outPutWriter.write("HTTP/1.1 404 Not Found");
151
	}
132
	}
152
133
134
	private void replyList() {
135
		LinkedList<String> queued = this.httpObject.getSourceQueue().peekEntireQueue();
136
		if (queued.size() > 0) {
137
			LinkedList<String> idOutput = new LinkedList<String>();
138
			int contentLength = 0;
139
			for (String inQueue : queued) {
140
				String inputId = "ID=" + this.findId(inQueue) + "\n";
141
				idOutput.add(inputId);
142
				contentLength += inputId.length();
143
			}
144
			outPutWriter.write("HTTP/1.1 200 OK\n");
145
			outPutWriter.write("Content-Length=" + contentLength + "\n");
146
			for (String id : idOutput) {
147
				System.out.println(id);
148
				outPutWriter.write(id.trim());
149
			}
150
		}
151
	}
152
153
	@Override
153
	@Override
154
	public void run() {
154
	public void run() {
155
		// Open connection
155
		// Open connection

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

367368
32
		return header.substring(6, header.indexOf(" HTTP/1.1"));
32
		return header.substring(6, header.indexOf(" HTTP/1.1"));
33
	}
33
	}
34
34
35
	private String getReqeustInfo(String inputLine) {
36
		// Queuenaam / Content length
37
		return inputLine.substring(5).trim();
38
	}
39
40
	private void handleSocketRequest(String inputLine) {
41
42
		// Pak socket
43
		SocketApp2QChannel inputSocket;
44
		inputSocket = (SocketApp2QChannel) ESBServer.esbChannels.get("LocatorIn");
45
46
		// Header info
47
		String header = getReqeustInfo(inputLine);
48
		String[] info = header.split("/");
49
50
		// Queue controleren
51
		if (this.getHttpQueueName(inputSocket).equals(info[0].trim())) {
52
			try {
53
				String readline = inputReader.readLine();
54
				while (readline != null) {
55
					inputSocket.getDestinationQueue().addToQueue(inputLine);
56
					outPutWriter.println("ACK");
57
					break;
58
				}
59
			} catch (Exception e) {
60
				// Do nothing
61
				outPutWriter.println("NACK");
62
			}
63
		} else {
64
			outPutWriter.println("NACK");
65
		}
66
	}
67
35
	private void handleValidRequest() {
68
	private void handleValidRequest() {
36
		int contentLength = 0;
69
		int contentLength = 0;
37
		try {
70
		try {
...
...
103
136
104
	}
137
	}
105
138
106
	private void handleSocketRequest(String inputLine) {
107
108
		// Pak socket
109
		SocketApp2QChannel inputSocket;
110
		inputSocket = (SocketApp2QChannel) ESBServer.esbChannels.get("LocatorIn");
111
112
		// Header info
113
		String header = getReqeustInfo(inputLine);
114
		String[] info = header.split("/");
115
116
		// Queue controleren
117
		if (this.getHttpQueueName(inputSocket).equals(info[0].trim())) {
118
			try {
119
				String readline = inputReader.readLine();
120
				while (readline != null) {
121
					inputSocket.getDestinationQueue().addToQueue(inputLine);
122
					outPutWriter.println("ACK");
123
					break;
124
				}
125
			} catch (Exception e) {
126
				// Do nothing
127
				outPutWriter.println("NACK");
128
			}
129
		} else {
130
			outPutWriter.println("NACK");
131
		}
132
	}
133
134
	private String getReqeustInfo(String inputLine) {
135
		// Queuenaam / Content length
136
		return inputLine.substring(5).trim();
137
	}
138
139
	@Override
139
	@Override
140
	public void run() {
140
	public void run() {
141
		// Open connection
141
		// Open connection

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

367368
8
8
9
public class ESBServer {
9
public class ESBServer {
10
10
11
	public static HashMap<String, Channel> esbChannels;
12
11
	public static void main(String[] args) {
13
	public static void main(String[] args) {
12
		new ESBServer("C:/Users/Martijn/workspace/ESB/src/hanze/ga/wt3/xml/configuration.xml");
14
		new ESBServer("C:/Users/Martijn/workspace/ESB/src/hanze/ga/wt3/xml/configuration.xml");
13
	}
15
	}
14
16
15
	public static HashMap<String, Channel> esbChannels;
16
17
	private HashMap<String, ESBQueue> esbQueues;
17
	private HashMap<String, ESBQueue> esbQueues;
18
18
19
	@SuppressWarnings("static-access")
19
	@SuppressWarnings("static-access")

Updated Weektaken2/Weektaak3/ESB/src/hanze/ga/wt3/xml/ESBDOMParser.java Download diff

367368
39
			this.buildFTPApp2Q(channelDef);
39
			this.buildFTPApp2Q(channelDef);
40
		}
40
		}
41
	}
41
	}
42
	
43
	/**
44
	 * Zorgt ervoor dat een Channel de juiste bron- en bestemmingsQueue heeft.
45
	 */
46
	private void linkQueueToChannel() {
47
		for (ESBQueue esbQueue : this.queues.values()) {
48
42
49
			// Link receiver
50
			Channel receiver = esbQueue.getqReceiverChannel();
51
			if (receiver instanceof App2Q) {
52
				((App2Q) receiver).setDestinationQueue(esbQueue);
53
			} else if (receiver instanceof Q2App) {
54
				((Q2App) receiver).setSourceQueue(esbQueue);
55
			}
56
57
			// Link sender
58
			Channel sender = esbQueue.getqSenderChannel();
59
			if (sender instanceof App2Q) {
60
				((App2Q) sender).setDestinationQueue(esbQueue);
61
			} else if (sender instanceof Q2App) {
62
				((Q2App) sender).setSourceQueue(esbQueue);
63
			}
64
		}
65
	}
66
67
	private void buildFTPApp2Q(Xml channel) {
68
		FTPApp2QChannel ftp = new FTPApp2QChannel();
69
70
		// Globale settings
71
		ftp.setChannelName(channel.child("name").content());
72
73
		// FTP Adapter waarden
74
		Xml adapterNode = channel.child("adapter");
75
		ftp.setFtpAddress(adapterNode.child("address").content());
76
		ftp.setFtpInitDir(adapterNode.child("init-dir").content());
77
		ftp.setFtpPassword(adapterNode.child("password").content());
78
		ftp.setFtpUserName(adapterNode.child("username").content());
79
		ftp.setChannelPort(adapterNode.child("port").content());
80
81
		// Zet in lijst
82
		this.channels.put(channel.child("name").content(), ftp);
83
	}
84
85
	/**
43
	/**
86
	 * Bouwen van ESB aan de hand van XML-bestand.
44
	 * Bouwen van ESB aan de hand van XML-bestand.
87
	 * 
45
	 * 
...
...
118
		}
76
		}
119
	}
77
	}
120
78
79
	private void buildFTPApp2Q(Xml channel) {
80
		FTPApp2QChannel ftp = new FTPApp2QChannel();
81
82
		// Globale settings
83
		ftp.setChannelName(channel.child("name").content());
84
85
		// FTP Adapter waarden
86
		Xml adapterNode = channel.child("adapter");
87
		ftp.setFtpAddress(adapterNode.child("address").content());
88
		ftp.setFtpInitDir(adapterNode.child("init-dir").content());
89
		ftp.setFtpPassword(adapterNode.child("password").content());
90
		ftp.setFtpUserName(adapterNode.child("username").content());
91
		ftp.setChannelPort(adapterNode.child("port").content());
92
93
		// Zet in lijst
94
		this.channels.put(channel.child("name").content(), ftp);
95
	}
96
121
	private void buildFTPQ2App(Xml channel) {
97
	private void buildFTPQ2App(Xml channel) {
122
		FTPQ2AppChannel ftp = new FTPQ2AppChannel();
98
		FTPQ2AppChannel ftp = new FTPQ2AppChannel();
123
99
...
...
271
		return queues;
247
		return queues;
272
	}
248
	}
273
249
250
	/**
251
	 * Zorgt ervoor dat een Channel de juiste bron- en bestemmingsQueue heeft.
252
	 */
253
	private void linkQueueToChannel() {
254
		for (ESBQueue esbQueue : this.queues.values()) {
255
256
			// Link receiver
257
			Channel receiver = esbQueue.getqReceiverChannel();
258
			if (receiver instanceof App2Q) {
259
				((App2Q) receiver).setDestinationQueue(esbQueue);
260
			} else if (receiver instanceof Q2App) {
261
				((Q2App) receiver).setSourceQueue(esbQueue);
262
			}
263
264
			// Link sender
265
			Channel sender = esbQueue.getqSenderChannel();
266
			if (sender instanceof App2Q) {
267
				((App2Q) sender).setDestinationQueue(esbQueue);
268
			} else if (sender instanceof Q2App) {
269
				((Q2App) sender).setSourceQueue(esbQueue);
270
			}
271
		}
272
	}
273
274
}
274
}