Skip to content

Instantly share code, notes, and snippets.

@cr0wst
Created January 21, 2018 01:04
Show Gist options
  • Save cr0wst/f80e7e6bb3952015e738a9939b2fceea to your computer and use it in GitHub Desktop.
Save cr0wst/f80e7e6bb3952015e738a9939b2fceea to your computer and use it in GitHub Desktop.
Nexmo SMS XML -> JSON
package com.nexmo.client.sms;
import java.util.HashMap;
import java.util.Map;
public enum MessageStatus {
STATUS_OK(0),
STATUS_THROTTLED(1),
STATUS_MISSING_PARAMS(2),
STATUS_INVALID_PARAMS(3),
STATUS_INVALID_CREDENTIALS(4),
STATUS_INTERNAL_ERROR(5),
STATUS_INVALID_MESSAGE(6),
STATUS_NUMBER_BARRED(7),
STATUS_PARTNER_ACCOUNT_BARRED(8),
STATUS_PARTNER_QUOTA_EXCEEDED(9),
STATUS_TOO_MANY_BINDS(10),
STATUS_ACCOUNT_NOT_HTTP(11),
STATUS_MESSAGE_TOO_LONG(12),
STATUS_COMMS_FAILURE(13),
STATUS_INVALID_SIGNATURE(14),
STATUS_INVALID_FROM_ADDRESS(15),
STATUS_INVALID_TTL(16),
STATUS_NUMBER_UNREACHABLE(17),
STATUS_TOO_MANY_DESTINATIONS(18),
STATUS_FACILITY_NOT_ALLOWED(19),
STATUS_INVALID_MESSAGE_CLASS(20);
private int messageStatus;
private static Map<Integer, MessageStatus> integerStatusValues = new HashMap<>();
static {
for(MessageStatus messageStatus : MessageStatus.values()) {
integerStatusValues.put(messageStatus.messageStatus, messageStatus);
}
}
/**
* Look up the {@link MessageStatus} based on the int value.
*
* @param messageStatus the int value of the message status.
* @return MessageStatus based on the int value given.
*/
public static MessageStatus fromInt(int messageStatus) {
return integerStatusValues.get(messageStatus);
}
MessageStatus(int messageStatus) {
this.messageStatus = messageStatus;
}
/**
* @return int The message status code for the specified message status.
*/
public int getMessageStatus() {
return this.messageStatus;
}
}
/*
* Copyright (c) 2011-2017 Nexmo Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.nexmo.client.sms;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nexmo.client.NexmoUnexpectedException;
import java.io.IOException;
import java.util.Collection;
@JsonIgnoreProperties(ignoreUnknown = true)
public class SmsSubmissionResponse {
@JsonProperty("message-count")
private int messageCount;
@JsonProperty("messages")
private Collection<SmsSubmissionResponseMessage> messages;
public static SmsSubmissionResponse fromJson(String json) {
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json, SmsSubmissionResponse.class);
} catch (IOException jpe) {
throw new NexmoUnexpectedException("Failed to produce SmsSubmissionResponse from json.", jpe);
}
}
public int getMessageCount() {
return this.messageCount;
}
public Collection<SmsSubmissionResponseMessage> getMessages() {
return this.messages;
}
}
/*
* Copyright (c) 2011-2017 Nexmo Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.nexmo.client.sms;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.math.BigDecimal;
public class SmsSubmissionResponseMessage {
@JsonProperty("to")
private String to;
@JsonProperty("message-id")
private String id;
@JsonProperty("status")
private MessageStatus status;
@JsonProperty("error-text")
private String errorText;
@JsonProperty("client-ref")
private String clientRef;
@JsonProperty("remaining-balance")
private BigDecimal remainingBalance;
@JsonProperty("message-price")
private BigDecimal messagePrice;
@JsonProperty("network")
private String network;
public String getTo() {
return this.to;
}
public String getId() {
return this.id;
}
public MessageStatus getStatus() {
return this.status;
}
public String getErrorText() {
return this.errorText;
}
public String getClientRef() {
return this.clientRef;
}
public BigDecimal getRemainingBalance() {
return this.remainingBalance;
}
public BigDecimal getMessagePrice() {
return this.messagePrice;
}
public String getNetwork() {
return this.network;
}
}
@Test
public void testSteve() {
String json = "{\n" +
" \"message-count\": 1,\n" +
" \"messages\": [\n" +
" {\n" +
" \"to\": \"447700900000\",\n" +
" \"message-id\": \"0A0000000123ABCD1\",\n" +
" \"status\": \"0\",\n" +
" \"remaining-balance\": \"3.14159265\",\n" +
" \"message-price\": \"0.03330000\",\n" +
" \"network\": \"12345\"\n" +
" }\n" +
" ]\n" +
"}";
SmsSubmissionResponse response = SmsSubmissionResponse.fromJson(json);
SmsSubmissionResponseMessage message = response.getMessages().iterator().next();
assertEquals("447700900000", message.getTo());
assertEquals("0A0000000123ABCD1", message.getId());
assertEquals(MessageStatus.STATUS_OK, message.getStatus());
assertEquals(new BigDecimal("3.14159265"), message.getRemainingBalance());
assertEquals(new BigDecimal("0.03330000"), message.getMessagePrice());
assertEquals("12345", message.getNetwork());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment