Author: klystr
(2009/12/03 13:05) About 2 years ago
Naar IBG Gaat goed
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"/>
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"/>
<classpathentry kind="output" path="bin"/>
7
</classpath>
8
1
package hanze.ga.wt3.channels;
2
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
public class ESBQueue {
...
private Channel qSenderChannel;
9
private String qSenderFilter;
10
private Channel qReceiverChannel;
11
private HashMap<Integer, String> queue = new HashMap<Integer, String>();
12
private Queue<String> queue = new LinkedList<String>();
13
public void addToQueue(int queueId, String value) {
14
public void addToQueue(String input) {
this.queue.put(queueId, value);
15
System.out.println("[" + this.getqName() + "] Item was added");
System.out.println("[" + this.getqName() + "] Added key " + queueId + " to queue");
16
this.queue.add(input);
}
17
18
public String pollQueue() {
19
public String pollFromQueue() {
int index = 0;
20
System.out.println("[" + this.getqName() + "] Item was polled");
int lowestId = 0;
21
return this.queue.poll();
for (int key : this.queue.keySet()) {
22
if (index == 0) {
23
lowestId = key;
24
25
if (lowestId > key) {
26
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
35
36
public int getFreeId() {
37
int max = 0;
38
39
if (max < key) {
40
max = key;
41
42
43
return max + 1;
44
45
46
public String getqName() {
47
return qName;
48
75
this.qSenderFilter = qSenderFilter;
53
76
54
77
55
56
public int getSize() {
57
return this.queue.size();
58
59
78
60
package hanze.ga.wt3.ftp;
import java.io.*;
import hanze.ga.wt3.channels.Q2App;
import hanze.ga.wt3.filters.ToIBGFilter;
import hanze.ga.wt3.xml.Xml;
public class FTPQ2AppChannel extends Q2App {
@Override
public void run() {
try {
while (true) {
this.readQueue();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
private void readQueue() {
while (this.getSourceQueue().getSize() > 0) {
this.processItem(this.getSourceQueue().pollFromQueue());
49
50
51
private void processItem(String pollItem) {
52
StringWriter stringWriter = new StringWriter();
// Haal ID van request
File tmpFile = new File("C:/esb.tmp");
FileWriter fw = new FileWriter("C:/esb.tmp");
fw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?> ");
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
// Verzend bestand naar FTP
ByteArrayInputStream file = new ByteArrayInputStream(fileContents.getBytes());
String destination = this.getFtpInitDir() + "/" + reqId + ".mmesb";
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
public void setFtpAddress(String ftpAddress) {
91
this.ftpAddress = ftpAddress;
92
super.setChannelIp(ftpAddress);
93
package hanze.ga.wt3.http;
import hanze.ga.wt3.channels.ESBQueue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
String inputLine = inputReader.readLine().trim();
while (inputLine != null) {
if (inputLine.startsWith("POST /")) {
if (this.getHttpQueueName().equals(this.getQueueName(inputLine))) {
this.handleValidRequest();
// System.out.println();
// do nothing
System.err.println("Fout in connectie");
outPutWriter.println("HTTP/1.1 500 Bad Request");
break;
} else if (contentLength > 0) {
} else {
// Lees input in stringbuilder
char[] tmp = new char[contentLength];
index += read;
94
inputMessage.append("<message>");
95
inputMessage.append(tmp);
96
97
98
inputLine = inputReader.readLine();
inputLine = inputReader.readLine().trim();
99
100
101
102
103
104
105
113
112
114
115
private void writeToQueue() {
116
ESBQueue q = this.httpObject.getDestinationQueue();
this.httpObject.getDestinationQueue().addToQueue(inputMessage.toString());
117
q.addToQueue(q.getFreeId(), inputMessage.toString());
118
119
120
121
// Open connection
122
this.initListen();
123
// Input lezen
this.listenAndHandle();
124
125
126
// Schrijf input naar Queue
this.writeToQueue();
127
128
129
POST /StudentPortalToIBGQueue HTTP/1.1
Content-Length=80
Content-Length=121
<message>
<location>Groningen</location>
<queue-name>StudentPortalToIBGQueue</queue-name>
<education>Informatica</education>
<id>2</id>
<payload>
Hier staat dan tekst
</payload>
</message>
import java.util.HashMap;import java.util.LinkedList;import java.util.Queue;public class ESBQueue {public class ESBQueue {private HashMap<Integer, String> queue = new HashMap<Integer, String>();private Queue<String> queue = new LinkedList<String>();public void addToQueue(int queueId, String value) {public void addToQueue(String input) {this.queue.put(queueId, value);System.out.println("[" + this.getqName() + "] Item was added");System.out.println("[" + this.getqName() + "] Added key " + queueId + " to queue");this.queue.add(input);public String pollQueue() {public String pollFromQueue() {int index = 0;System.out.println("[" + this.getqName() + "] Item was polled");int lowestId = 0;return this.queue.poll();for (int key : this.queue.keySet()) {if (index == 0) {lowestId = key;}if (lowestId > key) {lowestId = key;}index++;}String output = this.queue.get(lowestId);this.queue.remove(lowestId);System.out.println("[" + this.getqName() + "] Polled key " + lowestId + " from queue");return output;public int getFreeId() {int max = 0;for (int key : this.queue.keySet()) {if (max < key) {max = key;}}return max + 1;}public String getqName() {public String getqName() {public int getSize() {public class FTPQ2AppChannel extends Q2App {public class FTPQ2AppChannel extends Q2App {public void run() {public void run() {try {while (true) {} catch (InterruptedException e) {private void readQueue() {while (this.getSourceQueue().getSize() > 0) {private void processItem(String pollItem) {try {File tmpFile = new File("C:/esb.tmp");FileWriter fw = new FileWriter("C:/esb.tmp");fw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?> ");Xml xmlParser = new Xml("C:/esb.tmp", "message");String reqId = xmlParser.child("id").content();System.out.println("[" + this.getChannelName() + "] Item from "} catch (Exception e) {System.err.println("Fout in het parsen van XML-bestand");public void setFtpAddress(String ftpAddress) {public void setFtpAddress(String ftpAddress) {import hanze.ga.wt3.channels.ESBQueue;try {try {while (inputLine != null) {while (inputLine != null) {if (inputLine.startsWith("POST /")) {if (inputLine.startsWith("POST /")) {if (this.getHttpQueueName().equals(this.getQueueName(inputLine))) {if (this.getHttpQueueName().equals(this.getQueueName(inputLine))) {} catch (Exception e) {} catch (Exception e) {e.printStackTrace();// do nothingSystem.err.println("Fout in connectie");outPutWriter.println("HTTP/1.1 500 Bad Request");outPutWriter.println("HTTP/1.1 500 Bad Request");} else if (contentLength > 0) {} else {inputMessage.append("<message>");inputLine = inputReader.readLine().trim();} catch (Exception e) {} catch (Exception e) {e.printStackTrace();outPutWriter.println("HTTP/1.1 500 Bad Request");private void writeToQueue() {private void writeToQueue() {ESBQueue q = this.httpObject.getDestinationQueue();this.httpObject.getDestinationQueue().addToQueue(inputMessage.toString());q.addToQueue(q.getFreeId(), inputMessage.toString());public void run() {public void run() {Content-Length=80Content-Length=121<location>Groningen</location><queue-name>StudentPortalToIBGQueue</queue-name><education>Informatica</education><id>2</id><payload>Hier staat dan tekst</payload>