Compare commits

..

1 Commits
main ... devel

Author SHA1 Message Date
c71a8c6a73 remove some system out 2024-11-04 17:26:42 +07:00
4 changed files with 116 additions and 13 deletions

Binary file not shown.

@ -64,30 +64,130 @@ public class MessageDrivenEJBBean implements MessageDrivenBean, MessageListener
public void onMessage(Message msg) {
TextMessage tm = (TextMessage) msg;
try {
log("onMessage called");
String msgPayload = tm.getText();
log("onMessage called");
String msgPayload = tm.getText();
msgPayload.replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>","");
String postPayload ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soa=\"http://soa-kemenkeu.org\"><soapenv:Header/><soapenv:Body>";
// 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 ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soa=\"http://soa-kemenkeu.org\"><soapenv:Header/><soapenv:Body>";
String cleanPayload = msgPayload.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"", "");
String cleanPayload2 = cleanPayload.replace("?>", "");
postPayload += cleanPayload2.replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>","");
postPayload += " </soapenv:Body></soapenv:Envelope>";
// System.out.println("[myMessageDrivenBean] Received message: " + msgPayload );
// System.out.println("[myMessageDrivenBean] postPayload message: " + postPayload );
sendHttpPost(postPayload);
} catch(JMSException ex) {
System.out.println("[logJmsMessageDrivenBean] Caught JMSException: " + ex.getMessage());
// 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\"");
@ -97,14 +197,17 @@ public class MessageDrivenEJBBean implements MessageDrivenBean, MessageListener
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("[logJmsMessageDrivenBean] POST Response Code :: " + responseCode);
System.out.println("[logJmsMessageDrivenBean]:POST Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { // success
try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
@ -113,14 +216,14 @@ public class MessageDrivenEJBBean implements MessageDrivenBean, MessageListener
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
// System.out.println("Response :: " + response.toString());
}
} else {
System.out.println("[logJmsMessageDrivenBean] POST request failed");
System.out.println("[logJmsMessageDrivenBean]:POST request failed");
}
conn.disconnect();
} catch (IOException e) {
System.out.println("[logJmsMessageDrivenBean] sendHttpPost Exception : " + e.getMessage());
e.printStackTrace();
}
}