Changeset 356

User picture

Author: klystr

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

Naar IBG Gaat goed

Affected files

Updated Weektaken2/Weektaak3/ESB/.classpath Download diff

355356
3
	<classpathentry kind="src" path="src"/>
3
	<classpathentry kind="src" path="src"/>
4
	<classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
4
	<classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
5
	<classpathentry kind="lib" path="ini4j-0.5.1.jar"/>
5
	<classpathentry kind="lib" path="ini4j-0.5.1.jar"/>
6
	<classpathentry kind="lib" path="C:/Users/Martijn/Downloads/commons-net-2.0/commons-net-2.0/commons-net-ftp-2.0.jar"/>
6
	<classpathentry kind="output" path="bin"/>
7
	<classpathentry kind="output" path="bin"/>
7
</classpath>
8
</classpath>

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

355356
1
package hanze.ga.wt3.channels;
1
package hanze.ga.wt3.channels;
2
2
3
import java.util.HashMap;
3
import java.util.LinkedList;
4
import java.util.Queue;
4
5
5
public class ESBQueue {
6
public class ESBQueue {
6
7
...
...
8
	private Channel qSenderChannel;
9
	private Channel qSenderChannel;
9
	private String qSenderFilter;
10
	private String qSenderFilter;
10
	private Channel qReceiverChannel;
11
	private Channel qReceiverChannel;
11
	private HashMap<Integer, String> queue = new HashMap<Integer, String>();
12
	private Queue<String> queue = new LinkedList<String>();
12
13
13
	public void addToQueue(int queueId, String value) {
14
	public void addToQueue(String input) {
14
		this.queue.put(queueId, value);
15
		System.out.println("[" + this.getqName() + "] Item was added");
15
		System.out.println("[" + this.getqName() + "] Added key " + queueId + " to queue");
16
		this.queue.add(input);
16
	}
17
	}
17
18
18
	public String pollQueue() {
19
	public String pollFromQueue() {
19
		int index = 0;
20
		System.out.println("[" + this.getqName() + "] Item was polled");
20
		int lowestId = 0;
21
		return this.queue.poll();
21
		for (int key : this.queue.keySet()) {
22
			if (index == 0) {
23
				lowestId = key;
24
			}
25
			if (lowestId > key) {
26
				lowestId = key;
27
			}
28
			index++;
29
		}
30
		String output = this.queue.get(lowestId);
31
		this.queue.remove(lowestId);
32
		System.out.println("[" + this.getqName() + "] Polled key " + lowestId + " from queue");
33
		return output;
34
	}
22
	}
35
23
36
	public int getFreeId() {
37
		int max = 0;
38
		for (int key : this.queue.keySet()) {
39
			if (max < key) {
40
				max = key;
41
			}
42
		}
43
		return max + 1;
44
	}
45
46
	public String getqName() {
24
	public String getqName() {
47
		return qName;
25
		return qName;
48
	}
26
	}
...
...
75
		this.qSenderFilter = qSenderFilter;
53
		this.qSenderFilter = qSenderFilter;
76
	}
54
	}
77
55
56
	public int getSize() {
57
		return this.queue.size();
58
	}
59
78
}
60
}

Added Weektaken2/Weektaak3/ESB/src/hanze/ga/wt3/ftp/ESBFTP.java

Show contents

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

355356
1
package hanze.ga.wt3.ftp;
1
package hanze.ga.wt3.ftp;
2
2
3
import java.io.*;
4
3
import hanze.ga.wt3.channels.Q2App;
5
import hanze.ga.wt3.channels.Q2App;
6
import hanze.ga.wt3.filters.ToIBGFilter;
7
import hanze.ga.wt3.xml.Xml;
4
8
5
public class FTPQ2AppChannel extends Q2App {
9
public class FTPQ2AppChannel extends Q2App {
6
10
...
...
28
32
29
	@Override
33
	@Override
30
	public void run() {
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
	}
31
44
45
	private void readQueue() {
46
		while (this.getSourceQueue().getSize() > 0) {
47
			this.processItem(this.getSourceQueue().pollFromQueue());
48
		}
32
	}
49
	}
33
50
51
	private void processItem(String pollItem) {
52
53
		StringWriter stringWriter = new StringWriter();
54
55
		try {
56
			// Haal ID van request
57
			File tmpFile = new File("C:/esb.tmp");
58
			FileWriter fw = new FileWriter("C:/esb.tmp");
59
			fw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?> ");
60
			fw.write(pollItem);
61
			fw.close();
62
			Xml xmlParser = new Xml("C:/esb.tmp", "message");
63
			String reqId = xmlParser.child("id").content();
64
			tmpFile.delete();
65
			
66
			// Filter item
67
			ToIBGFilter toFilter = new ToIBGFilter();
68
			toFilter.doFilter(new StringReader(pollItem), stringWriter);
69
			String fileContents = stringWriter.toString();
70
			
71
			// Maak FTP-verbinding klaar
72
			ESBFTP ftpClient = new ESBFTP(this.getFtpAddress(), this.getFtpUserName(), this
73
					.getFtpPassword(), this.getChannelPort());
74
75
			// Verzend bestand naar FTP
76
			ByteArrayInputStream file = new ByteArrayInputStream(fileContents.getBytes());
77
			String destination = this.getFtpInitDir() + "/" + reqId + ".mmesb";
78
			ftpClient.store(file, destination);
79
80
			// Melding
81
			System.out.println("[" + this.getChannelName() + "] Item from "
82
					+ this.getSourceQueue().getqName() + " sent to ftp://" + this.getFtpAddress()
83
					+ ":" + this.getChannelPort() + destination);
84
85
		} catch (Exception e) {
86
			System.err.println("Fout in het parsen van XML-bestand");
87
		}
88
89
	}
90
34
	public void setFtpAddress(String ftpAddress) {
91
	public void setFtpAddress(String ftpAddress) {
35
		this.ftpAddress = ftpAddress;
92
		this.ftpAddress = ftpAddress;
36
		super.setChannelIp(ftpAddress);
93
		super.setChannelIp(ftpAddress);

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

355356
1
package hanze.ga.wt3.http;
1
package hanze.ga.wt3.http;
2
2
3
import hanze.ga.wt3.channels.ESBQueue;
4
5
import java.io.BufferedReader;
3
import java.io.BufferedReader;
6
import java.io.IOException;
4
import java.io.IOException;
7
import java.io.InputStreamReader;
5
import java.io.InputStreamReader;
...
...
39
		try {
37
		try {
40
			String inputLine = inputReader.readLine().trim();
38
			String inputLine = inputReader.readLine().trim();
41
			while (inputLine != null) {
39
			while (inputLine != null) {
42
43
				if (inputLine.startsWith("POST /")) {
40
				if (inputLine.startsWith("POST /")) {
44
					if (this.getHttpQueueName().equals(this.getQueueName(inputLine))) {
41
					if (this.getHttpQueueName().equals(this.getQueueName(inputLine))) {
45
						this.handleValidRequest();
42
						this.handleValidRequest();
...
...
63
			// System.out.println();
60
			// System.out.println();
64
61
65
		} catch (Exception e) {
62
		} catch (Exception e) {
66
			e.printStackTrace();
63
			// do nothing
64
			System.err.println("Fout in connectie");
67
		}
65
		}
68
66
69
	}
67
	}
...
...
82
						outPutWriter.println("HTTP/1.1 500 Bad Request");
80
						outPutWriter.println("HTTP/1.1 500 Bad Request");
83
						break;
81
						break;
84
					}
82
					}
85
				} else if (contentLength > 0) {
83
				} else {
86
					// Lees input in stringbuilder
84
					// Lees input in stringbuilder
87
					char[] tmp = new char[contentLength];
85
					char[] tmp = new char[contentLength];
88
					int index = 0;
86
					int index = 0;
...
...
92
						index += read;
90
						index += read;
93
					}
91
					}
94
92
93
					inputMessage.append("<message>");
95
					inputMessage.append(tmp);
94
					inputMessage.append(tmp);
96
					break;
95
					break;
97
				}
96
				}
98
				inputLine = inputReader.readLine();
97
				inputLine = inputReader.readLine().trim();
99
			}
98
			}
100
99
101
		} catch (Exception e) {
100
		} catch (Exception e) {
102
			e.printStackTrace();
101
			outPutWriter.println("HTTP/1.1 500 Bad Request");
103
		}
102
		}
104
	}
103
	}
105
104
...
...
113
	}
112
	}
114
113
115
	private void writeToQueue() {
114
	private void writeToQueue() {
116
		ESBQueue q = this.httpObject.getDestinationQueue();
115
		this.httpObject.getDestinationQueue().addToQueue(inputMessage.toString());
117
		q.addToQueue(q.getFreeId(), inputMessage.toString());
118
	}
116
	}
119
117
120
	@Override
118
	@Override
121
	public void run() {
119
	public void run() {
120
		// Open connection
122
		this.initListen();
121
		this.initListen();
122
		
123
		// Input lezen
123
		this.listenAndHandle();
124
		this.listenAndHandle();
125
		
126
		// Schrijf input naar Queue
124
		this.writeToQueue();
127
		this.writeToQueue();
125
	}
128
	}
126
129

Updated Weektaken2/Weektaak3/ESB/src/hanze/ga/wt3/http/post-example.txt Download diff

355356
1
POST /StudentPortalToIBGQueue HTTP/1.1
1
POST /StudentPortalToIBGQueue HTTP/1.1
2
Content-Length=80
2
Content-Length=121
3
<message>
3
<message>
4
	<location>Groningen</location>
4
<queue-name>StudentPortalToIBGQueue</queue-name>
5
	<education>Informatica</education>
5
<id>2</id>
6
	<payload>
7
		Hier staat dan tekst
8
	</payload>
6
</message>
9
</message>