commit 39af154b5d3d120fb9e38d5871f45ad18a010db5 Author: syukri-radius Date: Mon Nov 4 17:16:40 2024 +0700 init span osb mdb diff --git a/LogProcessorMdb.jpr b/LogProcessorMdb.jpr new file mode 100644 index 0000000..439c4d3 --- /dev/null +++ b/LogProcessorMdb.jpr @@ -0,0 +1,278 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/classes/.data/LogProcessorMdb.cdi b/classes/.data/LogProcessorMdb.cdi new file mode 100644 index 0000000..e07c836 Binary files /dev/null and b/classes/.data/LogProcessorMdb.cdi differ diff --git a/classes/META-INF/ejb-jar.xml b/classes/META-INF/ejb-jar.xml new file mode 100644 index 0000000..587dbdb --- /dev/null +++ b/classes/META-INF/ejb-jar.xml @@ -0,0 +1,24 @@ + + + + + + MessageDrivenEJBBean + org.soakemenkeu.MessageDrivenEJBBean + Container + Auto-acknowledge + + javax.jms.Queue + + + + + + + MessageDrivenEJBBean + onMessage + + NotSupported + + + \ No newline at end of file diff --git a/classes/META-INF/weblogic-ejb-jar.xml b/classes/META-INF/weblogic-ejb-jar.xml new file mode 100644 index 0000000..7c1e4a2 --- /dev/null +++ b/classes/META-INF/weblogic-ejb-jar.xml @@ -0,0 +1,18 @@ + + + + + MessageDrivenEJBBean + + + 20 + 5 + + jms/loggingQueue + jms/logConnFactory + 5 + + + mdb/MessageDrivenEJBBean + + \ No newline at end of file diff --git a/classes/config.properties b/classes/config.properties new file mode 100644 index 0000000..c904e33 --- /dev/null +++ b/classes/config.properties @@ -0,0 +1 @@ +api.url=http://10.216.99.101:8002/jmsLogging/ProxyService/httpLoggingPS diff --git a/classes/org/soakemenkeu/ConfigUtil.class b/classes/org/soakemenkeu/ConfigUtil.class new file mode 100644 index 0000000..165c4b7 Binary files /dev/null and b/classes/org/soakemenkeu/ConfigUtil.class differ diff --git a/classes/org/soakemenkeu/MessageDrivenEJBBean.class b/classes/org/soakemenkeu/MessageDrivenEJBBean.class new file mode 100644 index 0000000..ebb125b Binary files /dev/null and b/classes/org/soakemenkeu/MessageDrivenEJBBean.class differ diff --git a/deploy/OsbLogProcessor_LogProcessorMdb_ejb.jar b/deploy/OsbLogProcessor_LogProcessorMdb_ejb.jar new file mode 100644 index 0000000..e3c57d1 Binary files /dev/null and b/deploy/OsbLogProcessor_LogProcessorMdb_ejb.jar differ diff --git a/src/META-INF/ejb-jar.xml b/src/META-INF/ejb-jar.xml new file mode 100644 index 0000000..587dbdb --- /dev/null +++ b/src/META-INF/ejb-jar.xml @@ -0,0 +1,24 @@ + + + + + + MessageDrivenEJBBean + org.soakemenkeu.MessageDrivenEJBBean + Container + Auto-acknowledge + + javax.jms.Queue + + + + + + + MessageDrivenEJBBean + onMessage + + NotSupported + + + \ No newline at end of file diff --git a/src/META-INF/weblogic-ejb-jar.xml b/src/META-INF/weblogic-ejb-jar.xml new file mode 100644 index 0000000..7c1e4a2 --- /dev/null +++ b/src/META-INF/weblogic-ejb-jar.xml @@ -0,0 +1,18 @@ + + + + + MessageDrivenEJBBean + + + 20 + 5 + + jms/loggingQueue + jms/logConnFactory + 5 + + + mdb/MessageDrivenEJBBean + + \ No newline at end of file diff --git a/src/config.properties b/src/config.properties new file mode 100644 index 0000000..c904e33 --- /dev/null +++ b/src/config.properties @@ -0,0 +1 @@ +api.url=http://10.216.99.101:8002/jmsLogging/ProxyService/httpLoggingPS diff --git a/src/org/soakemenkeu/ConfigUtil.java b/src/org/soakemenkeu/ConfigUtil.java new file mode 100644 index 0000000..fbcd2df --- /dev/null +++ b/src/org/soakemenkeu/ConfigUtil.java @@ -0,0 +1,25 @@ +package org.soakemenkeu; + +import java.io.InputStream; +import java.util.Properties; + +public class ConfigUtil { + private static Properties properties = new Properties(); + + static { + try (InputStream input = ConfigUtil.class.getClassLoader().getResourceAsStream("config.properties")) { + if (input == null) { + System.out.println("Sorry, unable to find config.properties"); +// return; + } + // Load the properties file + properties.load(input); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + public static String getProperty(String key) { + return properties.getProperty(key); + } +} diff --git a/src/org/soakemenkeu/MessageDrivenEJBBean.java b/src/org/soakemenkeu/MessageDrivenEJBBean.java new file mode 100644 index 0000000..ba954ad --- /dev/null +++ b/src/org/soakemenkeu/MessageDrivenEJBBean.java @@ -0,0 +1,230 @@ +package org.soakemenkeu; + + +import java.io.BufferedReader; + + +import javax.jms.TextMessage; +import javax.jms.Message; +import javax.jms.MessageListener; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; + +import java.io.OutputStreamWriter; + +import java.net.HttpURLConnection; +import java.net.URL; + + +import javax.jms.JMSException; + +import javax.ejb.EJBException; +import javax.ejb.CreateException; +import javax.ejb.MessageDrivenBean; +import javax.ejb.MessageDrivenContext; +import java.io.StringReader; +import java.io.StringWriter; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import javax.xml.XMLConstants; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.DriverManager; +import java.sql.PreparedStatement; + + +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public class MessageDrivenEJBBean implements MessageDrivenBean, MessageListener { + private MessageDrivenContext mdbContext; + private void log(String s) { +// if (true) System.out.println(s); + } // container callback methods + + public void ejbRemove() throws EJBException { log("ejbRemove called"); } + + public void setMessageDrivenContext(MessageDrivenContext ctx) { mdbContext = ctx; log("context set"); } + + public void ejbCreate () throws CreateException { log("ejbCreate called"); } // The implementation of the onMessage method + + public void onMessage(Message msg) { + TextMessage tm = (TextMessage) msg; + try { + log("onMessage called"); + String msgPayload = tm.getText(); + msgPayload.replace("",""); +// sendHttpPost(text); +// Document jmsMessage = convertStringToXml(text); +// Node root = jmsMessage.getDocumentElement(); +// int logID = getLogNextSeq(); +// if (root != null){ +// NodeList nodeList = ((Element) root).getElementsByTagName("soa:LogRequest"); +// for (int i = 0; i < nodeList.getLength(); i++) { +// Node nodeXml = nodeList.item(i); +// if (nodeXml.getNodeType() == Node.ELEMENT_NODE) { +// Element element = (Element) nodeXml; +// System.out.println("[myMessageDrivenBean] element: " + element.toString() ); +// } else { +// System.out.println("[myMessageDrivenBean] node: " + nodeXml.toString() ); +// } +// } +// } + + + + String postPayload =""; + String cleanPayload = msgPayload.replace("", ""); + postPayload += cleanPayload2.replace("",""); + postPayload += " "; + + + System.out.println("[myMessageDrivenBean] Received message: " + msgPayload ); + System.out.println("[myMessageDrivenBean] postPayload message: " + postPayload ); + sendHttpPost(postPayload); + + + } catch(JMSException ex) { + System.out.println("Caught JMSException: " + ex ); + } + } + +// private static String convertXmlToString(Document doc) { +// DOMSource domSource = new DOMSource(doc); +// StringWriter writer = new StringWriter(); +// StreamResult result = new StreamResult(writer); +// TransformerFactory tf = TransformerFactory.newInstance(); +// Transformer transformer = null; +// try { +// transformer = tf.newTransformer(); +// transformer.transform(domSource, result); +// } catch (TransformerException e) { +// throw new RuntimeException(e); +// } +// return writer.toString(); +// } +// +// private static Document convertStringToXml(String xmlString) { +// +// DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); +// +// try { +// +// // optional, but recommended +// // process XML securely, avoid attacks like XML External Entities (XXE) +// dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); +// +// DocumentBuilder builder = dbf.newDocumentBuilder(); +// +// Document doc = builder.parse(new InputSource(new StringReader(xmlString))); +// +// return doc; +// +// } catch (ParserConfigurationException | IOException | SAXException e) { +// throw new RuntimeException(e); +// } +// +// } + +// private int getLogNextSeq(){ +// int logId = 0; +// String dbUrl = ConfigUtil.getProperty("db.url"); +// String dbUsername = ConfigUtil.getProperty("db.username"); +// String dbPassword = ConfigUtil.getProperty("db.password"); +// ResultSet rs = null; +// try (Connection connection = DriverManager.getConnection(dbUrl, dbUsername, dbPassword)) { +// String sql = "SELECT PKLOGIDSEQ.nextval FROM dual"; +// PreparedStatement preparedStatement = connection.prepareStatement(sql); +// rs = preparedStatement.executeQuery(); +// +// if (rs.next()) { +// logId = rs.getInt(1); +// } +// +// +// } catch (Exception e) { +// e.printStackTrace(); +// } +// +// return logId; +// } + +// private void storeMessageInDatabase(String messageContent) { +// String dbUrl = ConfigUtil.getProperty("db.url"); +// String dbUsername = ConfigUtil.getProperty("db.username"); +// String dbPassword = ConfigUtil.getProperty("db.password"); +// +// try (Connection connection = DriverManager.getConnection(dbUrl, dbUsername, dbPassword)) { +// String sql = "INSERT INTO jms_messages (message_content) VALUES (?)"; +// try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { +// preparedStatement.setString(1, messageContent); +// preparedStatement.executeUpdate(); +// System.out.println("Message stored in the database successfully."); +// } +// } catch (Exception e) { +// e.printStackTrace(); +// } +// } + + private void sendHttpPost(String messageContent) { + try { + String apiUrl = ConfigUtil.getProperty("api.url"); + URL url = new URL(apiUrl); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + +// URL url = new URL("http://localhost:17080/soa-infra/services/default/SoaLoggerProject/LogOsbInsertService"); +// HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("POST"); + conn.setRequestProperty("Content-Type", "text/xml"); + conn.setRequestProperty("SOAPAction","\"execute\""); + conn.setDoInput(true); + conn.setDoOutput(true); + + String body = messageContent; + OutputStream out = conn.getOutputStream(); + OutputStreamWriter wout = new OutputStreamWriter(out, "UTF-8"); +// System.out.println("Writing message: " + body + " to outputstream."); + wout.write(body); + wout.flush(); + out.close(); + +// OutputStream output = new BufferedOutputStream(conn.getOutputStream()); +// output.write(body.getBytes()); +// output.flush(); + + int responseCode = conn.getResponseCode(); +// System.out.println("POST Response Code :: " + responseCode); + + if (responseCode == HttpURLConnection.HTTP_OK) { // success + try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { + String inputLine; + StringBuilder response = new StringBuilder(); + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + System.out.println("Response :: " + response.toString()); + } + } else { + System.out.println("POST request failed"); + } + conn.disconnect(); + } catch (IOException e) { + e.printStackTrace(); + + } + } +}