Total Pageviews

2017/12/11

[Fortify] Fix Path Manipulation

Problem


Before
Original code snippet:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    public static List<String> readFile(String file) throws TdccInitException, IOException {
        List<String> rows = new ArrayList<>();

        if (!StringUtils.isNotEmpty(file)) {
            throw new TdccInitException("Please assign file, must not be empty or null");
        }
        Boolean isFileExisted = Files.exists(Paths.get(file));
        if (!isFileExisted) {
            throw new TdccInitException(MessageFormat.format(wrongFileErr, file));
        } else {
            rows = com.google.common.io.Files.readLines(new File(file), Charsets.UTF_8);
        }
        return rows;
    }


After
Add dependency in your pom.xml
1
2
3
4
5
6
    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.5</version>
    </dependency>


Updated code snippet:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
    public static List<String> readFile(String file) throws TdccInitException, IOException {
        List<String> rows = new ArrayList<>();

        if (!StringUtils.isNotEmpty(file)) {
            throw new TdccInitException("Please assign file, must not be empty or null");
        }
        file = FilenameUtils.normalize(file);
        Boolean isFileExisted = Files.exists(Paths.get(file));
        if (!isFileExisted) {
            throw new TdccInitException(MessageFormat.format(wrongFileErr, file));
        } else {
            rows = com.google.common.io.Files.readLines(new File(file), Charsets.UTF_8);
        }
        return rows;
    }



2017/12/10

[DBCP2] [DB2] java.lang.AbstractMethodError: com.ibm.db2.jcc.t4.b.isValid(I)Z

Problem
I am using DBCP2 as my third party library to do connection pool.


But as I try to get connection it throws exception:
java.lang.AbstractMethodError: com.ibm.db2.jcc.t4.b.isValid(I)Z


How-To

Owing to the isValid method was added in JDBC 4.0. I need to change IBM DB2 class from db2jcc.jar (JDBC 3.0) to db2jcc4.jar (JDBC 4.0), then this problem will be solved.


Reference

[1] https://stackoverflow.com/questions/37193965/java-lang-abstractmethoderror-com-ibm-db2-jcc-t4-b-isvalidiz

2017/12/09

[Maven] How to assign WAR file name in pom.xml

Problem
When I utilize maven to build WAR file, the WAR file name always suffix by version which define in pom.xml (i.e. StockQry-1.0.war).
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>StockQry</groupId>
    <artifactId>StockQry</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <resources>
            <resource>
                <directory>src</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <!-- ignore plugins -->
        </plugins>
    </build>

    <dependencies>
        <!-- ignore dependencies -->
    </dependencies>

</project>
If I would like to have StockQry.war instead of StockQry-1.0.war, how to do it?


How-To
Add finalName in build tag, the updated pom.xml are the following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>StockQry</groupId>
    <artifactId>StockQry</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>
    <build>
        <finalName>StockQry</finalName>
        <sourceDirectory>src</sourceDirectory>
        <resources>
            <resource>
                <directory>src</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <!-- ignore plugins -->
        </plugins>
    </build>

    <dependencies>
        <!-- ignore dependencies -->
    </dependencies>

</project>


2017/12/08

[Java] How to write text message to IBM MQ?

Problem
I am using IBM MQ as my queue service.
How to write text message into IBM MQ by Java?


How-To

Here has sample code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.xxx.mql.service;

import java.io.IOException;
import java.util.Enumeration;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.commons.lang.StringUtils;

import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueReceiver;
import com.ibm.mq.jms.MQQueueSender;
import com.ibm.mq.jms.MQQueueSession;
import com.tdcc.mql.enumeration.MQParamKeyType;
import com.tdcc.mql.utils.MqlUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestMQService {

    private static TestMQService mqServuce;
    
    private static String mqHost = MQParamKeyType.MQ_HOST.getSystemValue();
    private static String mqChannel = MQParamKeyType.MQ_CHANNEL.getSystemValue();
    private static String mqUser = MQParamKeyType.MQ_USER.getSystemValue();
    private static String mqPwd = MQParamKeyType.MQ_PASSWORD.getSystemValue();
    private static String mqQueueMgr = MQParamKeyType.MQ_QUEUEMGR.getSystemValue();
    private static String mqPort = MQParamKeyType.MQ_PORT.getSystemValue();

    public static TestMQService getInstance() {
        if (mqServuce == null) {
            mqServuce = new TestMQService();
            return mqServuce;
        } else {
            return mqServuce;
        }
    }
    
    /**
     * Send Message to MQ.
     * 
     * @param queueName
     * @param message
     * @throws JMSException
     */
    public void writeMessageToMQ(String queueName, String message) throws JMSException {
        MQQueueConnectionFactory connFactory = new MQQueueConnectionFactory();
        MQQueueSender sender = null;
        MQQueueSession session = null;
        MQQueueConnection connection = null;
        
        try {
            connFactory.setHostName(mqHost);
            connFactory.setPort(Integer.parseInt(mqPort));
            connFactory.setTransportType(1);
            connFactory.setQueueManager(mqQueueMgr);
            connFactory.setChannel(mqChannel);

            connection = (MQQueueConnection) connFactory.createQueueConnection();
            session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue(queueName);
            
            sender = (MQQueueSender) session.createSender(queue);
            
            connection.start();
            
            TextMessage textMsg = session.createTextMessage();
            textMsg.setText(message);
            
            sender.send(textMsg);
            
        } catch (JMSException e) {
            log.error(MqlUtils.toStackTrace(e));
        } finally {
            if (sender != null) {
                sender.close();
            }
            if (session != null) {
                session.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
}    




2017/12/07

[Java] ClassCastException : cannot cast javax.jms.Message to javax.jms.TextMessage

Problem
I get ClassCastException when I read IBM MQ queue message by Java.
The code snippet looks like:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
package com.xxx.mql.service;

import java.io.IOException;
import java.util.Enumeration;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.commons.lang.StringUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Strings;
import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueReceiver;
import com.ibm.mq.jms.MQQueueSession;
import com.tdcc.common.type.MQInfoEnum;
import com.tdcc.mql.utils.MqlUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestMQService {

    private static TestMQService mqServuce;

    private static String mqHost = MQInfoEnum.MQ_HOST.getValue();
    private static String mqChannel = MQInfoEnum.MQ_CHANNEL.getValue();
    private static String mqUser = MQInfoEnum.MQ_USER.getValue();
    private static String mqPwd = "";
    private static String mqQueueMgr = MQInfoEnum.MQ_QUEUEMGR.getValue();

    public static TestMQService getInstance() {
        if (mqServuce == null) {
            mqServuce = new TestMQService();
            return mqServuce;
        } else {
            return mqServuce;
        }
    }

    /**
     * 透過 JMS 去取得 queue message.
     * 
     * @param queueName
     * @return
     * @throws JMSException
     */
    public String readMessageByJMS(String queueName) throws JMSException {
        MQQueueConnectionFactory connFactory = new MQQueueConnectionFactory();
        MQQueueReceiver receiver = null;
        MQQueueSession session = null;
        MQQueueConnection connection = null;
        String message = "";
        try {
            // Config
            connFactory.setHostName(mqHost);
            connFactory.setPort(1414);
            connFactory.setTransportType(1);
            connFactory.setQueueManager(mqQueueMgr);
            connFactory.setChannel(mqChannel);

            connection = (MQQueueConnection) connFactory.createQueueConnection();
            session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue(queueName);
            receiver = (MQQueueReceiver) session.createReceiver(queue);

            connection.start();

            if (receiver != null) {
                Message msg = receiver.receiveNoWait();
                if (msg == null) {
                    message = "";
                } else {
                    TextMessage textMsg = (TextMessage) msg;
                    message = StringUtils.defaultIfBlank(textMsg.getText(), "");
                }
            }

        } catch (JMSException e) {
            log.error(MqlUtils.toStackTrace(e));
        } finally {
            if (receiver != null) {
                receiver.close();
            }
            if (session != null) {
                session.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
        log.info("[readMessageByJMS] message = " + message);
        return message;
    }
}


How-to
According to original requirement, the queue message should be text format. 

Different content format will use different interface:



But actually it may has non-text format in IBM MQ, so I need to check its class type before do cast. 


The updated code snippet are as bellows:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
package com.xxx.mql.service;

import java.io.IOException;
import java.util.Enumeration;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.commons.lang.StringUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Strings;
import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueReceiver;
import com.ibm.mq.jms.MQQueueSession;
import com.tdcc.common.type.MQInfoEnum;
import com.tdcc.mql.utils.MqlUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestMQService {

    private static TestMQService mqServuce;

    private static String mqHost = MQInfoEnum.MQ_HOST.getValue();
    private static String mqChannel = MQInfoEnum.MQ_CHANNEL.getValue();
    private static String mqUser = MQInfoEnum.MQ_USER.getValue();
    private static String mqPwd = "";
    private static String mqQueueMgr = MQInfoEnum.MQ_QUEUEMGR.getValue();

    public static TestMQService getInstance() {
        if (mqServuce == null) {
            mqServuce = new TestMQService();
            return mqServuce;
        } else {
            return mqServuce;
        }
    }

    /**
     * 透過 JMS 去取得 queue message.
     * 
     * @param queueName
     * @return
     * @throws JMSException
     */
    public String readMessageByJMS(String queueName) throws JMSException {
        MQQueueConnectionFactory connFactory = new MQQueueConnectionFactory();
        MQQueueReceiver receiver = null;
        MQQueueSession session = null;
        MQQueueConnection connection = null;
        String message = "";
        try {
            // Config
            connFactory.setHostName(mqHost);
            connFactory.setPort(1414);
            connFactory.setTransportType(1);
            connFactory.setQueueManager(mqQueueMgr);
            connFactory.setChannel(mqChannel);

            connection = (MQQueueConnection) connFactory.createQueueConnection();
            session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue(queueName);
            receiver = (MQQueueReceiver) session.createReceiver(queue);

            connection.start();

            if (receiver != null) {
                Message msg = receiver.receiveNoWait();
                if (msg == null) {
                    message = "";
                } else {
                    if (msg instanceof TextMessage) {
                        TextMessage textMsg = (TextMessage) msg;
                        message = StringUtils.defaultIfBlank(textMsg.getText(), "");
                    } else {
                        String error = "*****不是文字訊息,跳過此筆訊息,class type = " + msg.getClass().toString();
                        log.error(error);
                    }
                }
            }

        } catch (JMSException e) {
            log.error(MqlUtils.toStackTrace(e));
        } finally {
            if (receiver != null) {
                receiver.close();
            }
            if (session != null) {
                session.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
        log.info("[readMessageByJMS] message = " + message);
        return message;
    }
}


Reference
[1] https://docs.oracle.com/javaee/6/api/javax/jms/Message.html

2017/12/06

[Java] How to read message from IBM MQ?

Problem
I am using IBM MQ as my queue service.
How to read message (text format) from IBM MQ by Java?


How-To

Here has the sample code:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
package com.xxx.mql.service;

import java.io.IOException;
import java.util.Enumeration;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.commons.lang.StringUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Strings;
import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueReceiver;
import com.ibm.mq.jms.MQQueueSession;
import com.tdcc.common.type.MQInfoEnum;
import com.tdcc.mql.utils.MqlUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestMQService {

    private static TestMQService mqServuce;

    private static String mqHost = MQInfoEnum.MQ_HOST.getValue();
    private static String mqChannel = MQInfoEnum.MQ_CHANNEL.getValue();
    private static String mqUser = MQInfoEnum.MQ_USER.getValue();
    private static String mqPwd = "";
    private static String mqQueueMgr = MQInfoEnum.MQ_QUEUEMGR.getValue();

    public static TestMQService getInstance() {
        if (mqServuce == null) {
            mqServuce = new TestMQService();
            return mqServuce;
        } else {
            return mqServuce;
        }
    }

    /**
     * 取得 queue message.
     * 
     * @param queueName
     * @return
     * @throws JMSException
     */
    public String readMessage(String queueName) throws JMSException {
        MQQueueConnectionFactory connFactory = new MQQueueConnectionFactory();
        MQQueueReceiver receiver = null;
        MQQueueSession session = null;
        MQQueueConnection connection = null;
        String message = "";
        try {
            // Config
            connFactory.setHostName(mqHost);
            connFactory.setPort(1414);
            connFactory.setTransportType(1);
            connFactory.setQueueManager(mqQueueMgr);
            connFactory.setChannel(mqChannel);

            connection = (MQQueueConnection) connFactory.createQueueConnection();
            session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue(queueName);
            receiver = (MQQueueReceiver) session.createReceiver(queue);

            connection.start();

            if (receiver != null) {
                Message msg = receiver.receiveNoWait();
                if (msg == null) {
                    message = "";
                } else {
                    if (msg instanceof TextMessage) {
                        TextMessage textMsg = (TextMessage) msg;
                        message = StringUtils.defaultIfBlank(textMsg.getText(), "");
                    } else {
                        String error = "*****不是文字訊息,跳過此筆訊息,class type = " + msg.getClass().toString();
                        log.error(error);
                    }
                }
            }

        } catch (JMSException e) {
            log.error(MqlUtils.toStackTrace(e));
        } finally {
            if (receiver != null) {
                receiver.close();
            }
            if (session != null) {
                session.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
        log.info("[readMessageByJMS] message = " + message);
        return message;
    }
}