MessageDecoder.java

1
package fr.sii.ogham.testing.sms.simulator.decode;
2
3
import java.util.Arrays;
4
5
import ogham.testing.org.jsmpp.bean.Alphabet;
6
import org.slf4j.Logger;
7
import org.slf4j.LoggerFactory;
8
9
import ogham.testing.com.cloudhopper.commons.charset.Charset;
10
import ogham.testing.com.cloudhopper.commons.charset.CharsetUtil;
11
12
import fr.sii.ogham.testing.sms.simulator.bean.OptionalParameter;
13
import fr.sii.ogham.testing.sms.simulator.bean.SubmitSm;
14
import fr.sii.ogham.testing.sms.simulator.bean.Tag;
15
16
/**
17
 * Utility class that is smart enough to find which field was used to send the
18
 * message. It is also able to determine if a User Data Header was used and
19
 * extract real payload. It uses the right decoder to decode the message into a
20
 * string.
21
 * 
22
 * @author Aurélien Baudet
23
 *
24
 */
25
public final class MessageDecoder {
26
	private static final Logger LOG = LoggerFactory.getLogger(MessageDecoder.class);
27
28
	/**
29
	 * Decode the received message to extract the text message. The
30
	 * alphabet/encoding is automatically determined from the message.
31
	 * 
32
	 * @param submitSm
33
	 *            the message to decode
34
	 * @return the text message
35
	 */
36
	public static String decode(SubmitSm submitSm) {
37
		Alphabet alphabet = Alphabet.parseDataCoding(submitSm.getDataCoding());
38
		Charset charset = getCharset(alphabet);
39 1 1. decode : replaced return value with "" for fr/sii/ogham/testing/sms/simulator/decode/MessageDecoder::decode → NO_COVERAGE
		return decode(submitSm, new CloudhopperCharsetAdapter(charset));
40
	}
41
42
	/**
43
	 * Decode the received message to extract the text message. The
44
	 * alphabet/encoding is explicitly defined.
45
	 * 
46
	 * @param submitSm
47
	 *            the message to decode
48
	 * @param charset
49
	 *            the charset used to decode the message
50
	 * @return the text message
51
	 */
52
	public static String decode(SubmitSm submitSm, fr.sii.ogham.testing.sms.simulator.decode.Charset charset) {
53
		byte[] shortMessage = getMessageBytes(submitSm);
54 1 1. decode : negated conditional → NO_COVERAGE
		if (shortMessage == null) {
55 1 1. decode : replaced return value with "" for fr/sii/ogham/testing/sms/simulator/decode/MessageDecoder::decode → NO_COVERAGE
			return null;
56
		}
57 1 1. decode : negated conditional → NO_COVERAGE
		if (submitSm.isUdhi()) {
58 1 1. decode : Replaced integer addition with subtraction → NO_COVERAGE
			int headerLength = shortMessage[0] + 1;
59
			shortMessage = Arrays.copyOfRange(shortMessage, headerLength, shortMessage.length);
60
		}
61
		Alphabet alphabet = Alphabet.parseDataCoding(submitSm.getDataCoding());
62
		LOG.trace("alphabet={}, charset={}, isUdhi={}, header={}", alphabet, charset, submitSm.isUdhi(), shortMessage);
63 1 1. decode : replaced return value with "" for fr/sii/ogham/testing/sms/simulator/decode/MessageDecoder::decode → NO_COVERAGE
		return charset.decode(shortMessage);
64
	}
65
66
	@SuppressWarnings("squid:S1168")
67
	private static byte[] getMessageBytes(SubmitSm submitSm) {
68
		byte[] shortMessage = submitSm.getShortMessage();
69 3 1. getMessageBytes : negated conditional → NO_COVERAGE
2. getMessageBytes : changed conditional boundary → NO_COVERAGE
3. getMessageBytes : negated conditional → NO_COVERAGE
		if (shortMessage != null && shortMessage.length > 0) {
70 1 1. getMessageBytes : replaced return value with null for fr/sii/ogham/testing/sms/simulator/decode/MessageDecoder::getMessageBytes → NO_COVERAGE
			return shortMessage;
71
		}
72
		OptionalParameter parameter = submitSm.getOptionalParameter(Tag.MESSAGE_PAYLOAD);
73 1 1. getMessageBytes : negated conditional → NO_COVERAGE
		if (parameter != null) {
74 1 1. getMessageBytes : replaced return value with null for fr/sii/ogham/testing/sms/simulator/decode/MessageDecoder::getMessageBytes → NO_COVERAGE
			return parameter.getValue();
75
		}
76
		return null;
77
	}
78
79
	private static Charset getCharset(Alphabet alphabet) {
80
		// @formatter:off
81
		switch (alphabet) {
82 1 1. getCharset : replaced return value with null for fr/sii/ogham/testing/sms/simulator/decode/MessageDecoder::getCharset → NO_COVERAGE
			case ALPHA_DEFAULT:						return CharsetUtil.CHARSET_GSM7;
83 1 1. getCharset : replaced return value with null for fr/sii/ogham/testing/sms/simulator/decode/MessageDecoder::getCharset → NO_COVERAGE
			case ALPHA_8_BIT:						return CharsetUtil.CHARSET_GSM8;
84 1 1. getCharset : replaced return value with null for fr/sii/ogham/testing/sms/simulator/decode/MessageDecoder::getCharset → NO_COVERAGE
			case ALPHA_UCS2:						return CharsetUtil.CHARSET_UCS_2;
85 1 1. getCharset : replaced return value with null for fr/sii/ogham/testing/sms/simulator/decode/MessageDecoder::getCharset → NO_COVERAGE
			case ALPHA_LATIN1:						return CharsetUtil.CHARSET_ISO_8859_1;
86
//			case ALPHA_IA5:							return CharsetUtil.;
87
//			case ALPHA_CYRILLIC:					return CharsetUtil.;
88
//			case ALPHA_ISO_2022_JP_MUSIC_CODES:		return CharsetUtil.;
89
//			case ALPHA_JIS:							return CharsetUtil.;
90
//			case ALPHA_JIS_X_0212_1990:				return CharsetUtil.;
91
//			case ALPHA_KS_C_5601:					return CharsetUtil.;
92
//			case ALPHA_LATIN_HEBREW:				return CharsetUtil.;
93
//			case ALPHA_PICTOGRAM_ENCODING:			return CharsetUtil.;
94
			default:
95
				throw new IllegalStateException("The alphabet " + alphabet.name() + " can't be decoded because no charset implementation can handle it");
96
		}
97
		// @formatter:on
98
	}
99
100
	private MessageDecoder() {
101
		super();
102
	}
103
}

Mutations

39

1.1
Location : decode
Killed by :
replaced return value with "" for fr/sii/ogham/testing/sms/simulator/decode/MessageDecoder::decode → NO_COVERAGE

54

1.1
Location : decode
Killed by :
negated conditional → NO_COVERAGE

55

1.1
Location : decode
Killed by :
replaced return value with "" for fr/sii/ogham/testing/sms/simulator/decode/MessageDecoder::decode → NO_COVERAGE

57

1.1
Location : decode
Killed by :
negated conditional → NO_COVERAGE

58

1.1
Location : decode
Killed by :
Replaced integer addition with subtraction → NO_COVERAGE

63

1.1
Location : decode
Killed by :
replaced return value with "" for fr/sii/ogham/testing/sms/simulator/decode/MessageDecoder::decode → NO_COVERAGE

69

1.1
Location : getMessageBytes
Killed by :
negated conditional → NO_COVERAGE

2.2
Location : getMessageBytes
Killed by :
changed conditional boundary → NO_COVERAGE

3.3
Location : getMessageBytes
Killed by :
negated conditional → NO_COVERAGE

70

1.1
Location : getMessageBytes
Killed by :
replaced return value with null for fr/sii/ogham/testing/sms/simulator/decode/MessageDecoder::getMessageBytes → NO_COVERAGE

73

1.1
Location : getMessageBytes
Killed by :
negated conditional → NO_COVERAGE

74

1.1
Location : getMessageBytes
Killed by :
replaced return value with null for fr/sii/ogham/testing/sms/simulator/decode/MessageDecoder::getMessageBytes → NO_COVERAGE

82

1.1
Location : getCharset
Killed by :
replaced return value with null for fr/sii/ogham/testing/sms/simulator/decode/MessageDecoder::getCharset → NO_COVERAGE

83

1.1
Location : getCharset
Killed by :
replaced return value with null for fr/sii/ogham/testing/sms/simulator/decode/MessageDecoder::getCharset → NO_COVERAGE

84

1.1
Location : getCharset
Killed by :
replaced return value with null for fr/sii/ogham/testing/sms/simulator/decode/MessageDecoder::getCharset → NO_COVERAGE

85

1.1
Location : getCharset
Killed by :
replaced return value with null for fr/sii/ogham/testing/sms/simulator/decode/MessageDecoder::getCharset → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.13.1