diff --git a/.gitignore b/.gitignore index c5a30075..42f72dc9 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,5 @@ hs_err_pid* # The various target folders *target/ +*.iml +.idea diff --git a/README.md b/README.md index 30aa3a82..c653441b 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ If you are using maven simply add the messagebird API to your dependencies like com.messagebird messagebird-api - 1.0.5 + 1.1.0 ``` @@ -36,7 +36,7 @@ In case you are building without maven you still need maven to build the librari then simply copy the following jar's over to your project ``` -messagebird-api-1.0.5.jar +messagebird-api-1.1.0.jar jackson-core-2.1.1.jar jackson-databind-2.1.1.jar jackson-mapper-asl-1.9.13.jar diff --git a/api/pom.xml b/api/pom.xml index 4369ded1..76af6d07 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -6,7 +6,7 @@ com.messagebird messagebird-api - 1.0.5 + 1.1.0 jar ${project.groupId}:${project.artifactId} diff --git a/api/src/main/java/com/messagebird/MessageBirdClient.java b/api/src/main/java/com/messagebird/MessageBirdClient.java index 52f3c694..07ecef72 100644 --- a/api/src/main/java/com/messagebird/MessageBirdClient.java +++ b/api/src/main/java/com/messagebird/MessageBirdClient.java @@ -6,14 +6,13 @@ import com.messagebird.objects.*; import java.math.BigInteger; -import java.text.SimpleDateFormat; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; /** * Message bird general client - *

+ *

*

  * Initialise this class with a MessageBirdService
  *  // First create your service object
@@ -24,7 +23,7 @@
  *  Then read your balance like this:
  *  final Balance balance = messageBirdClient.getBalance();
  * 
- *

+ *

* Created by rvt on 1/5/15. */ public class MessageBirdClient { @@ -32,6 +31,7 @@ public class MessageBirdClient { private static final String HLRPATH = "/hlr"; private static final String MESSAGESPATH = "/messages"; private static final String VOICEMESSAGESPATH = "/voicemessages"; + private static final String VERIFYPATH = "/verify"; private MessageBirdService messageBirdService; public MessageBirdClient(final MessageBirdService messageBirdService) { @@ -308,4 +308,81 @@ public VoiceMessageList listVoiceMessages(final Integer offset, final Integer li return messageBirdService.requestList(VOICEMESSAGESPATH, offset, limit, VoiceMessageList.class); } + /** + * @param verifyRequest + * @return Verify object + * @throws UnauthorizedException + * @throws GeneralException + */ + public Verify sendVerifyToken(VerifyRequest verifyRequest) throws UnauthorizedException, GeneralException { + if (verifyRequest == null) { + throw new IllegalArgumentException("Verify request cannot be null"); + } else if (verifyRequest.getRecipient() == null || verifyRequest.getRecipient().isEmpty()) { + throw new IllegalArgumentException("Recipient cannot be empty for verify"); + } + return messageBirdService.sendPayLoad(VERIFYPATH, verifyRequest, Verify.class); + } + + /** + * @param recipient + * @return Verify object + * @throws UnauthorizedException + * @throws GeneralException + */ + public Verify sendVerifyToken(String recipient) throws UnauthorizedException, GeneralException { + if (recipient == null || recipient.isEmpty()) { + throw new IllegalArgumentException("Recipient cannot be empty for verify"); + } + VerifyRequest verifyRequest = new VerifyRequest(recipient); + return this.sendVerifyToken(verifyRequest); + } + + /** + * + * @param id + * @param token + * @return Verify object + * @throws NotFoundException + * @throws GeneralException + * @throws UnauthorizedException + */ + public Verify verifyToken(String id, String token) throws NotFoundException, GeneralException, UnauthorizedException { + if (id == null || id.isEmpty()) { + throw new IllegalArgumentException("ID cannot be empty for verify"); + } else if (token == null || token.isEmpty()) { + throw new IllegalArgumentException("ID cannot be empty for verify"); + } + final Map params = new LinkedHashMap(); + params.put("token", token); + return messageBirdService.requestByID(VERIFYPATH, id, params, Verify.class); + } + + /** + * + * @param id + * @return Verify object + * @throws NotFoundException + * @throws GeneralException + * @throws UnauthorizedException + */ + public Verify getVerifyObject(String id) throws NotFoundException, GeneralException, UnauthorizedException { + if (id == null || id.isEmpty()) { + throw new IllegalArgumentException("ID cannot be empty for verify"); + } + return messageBirdService.requestByID(VERIFYPATH, id, Verify.class); + } + + /** + * + * @param id + * @throws NotFoundException + * @throws GeneralException + * @throws UnauthorizedException + */ + public void deleteVerifyObject(String id) throws NotFoundException, GeneralException, UnauthorizedException { + if (id == null || id.isEmpty()) { + throw new IllegalArgumentException("ID cannot be empty for verify"); + } + messageBirdService.deleteByID(VERIFYPATH, id); + } } diff --git a/api/src/main/java/com/messagebird/MessageBirdService.java b/api/src/main/java/com/messagebird/MessageBirdService.java index 525e6d50..410fa8db 100644 --- a/api/src/main/java/com/messagebird/MessageBirdService.java +++ b/api/src/main/java/com/messagebird/MessageBirdService.java @@ -4,6 +4,7 @@ import com.messagebird.exceptions.NotFoundException; import com.messagebird.exceptions.UnauthorizedException; +import java.io.UnsupportedEncodingException; import java.util.Map; /** @@ -22,6 +23,8 @@ public interface MessageBirdService { */ R requestByID(String request, String id, Class clazz) throws UnauthorizedException, GeneralException, NotFoundException; + R requestByID(String request, String id, Map params, Class clazz) throws UnauthorizedException, GeneralException, NotFoundException; + /** * Delete a object by ID. * diff --git a/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java b/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java index c69d9cfa..d1cfa54b 100644 --- a/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java +++ b/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java @@ -14,10 +14,7 @@ import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.*; -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * Implementation of MessageBirdService @@ -35,7 +32,7 @@ public class MessageBirdServiceImpl implements MessageBirdService { private static final List REQUESTMETHODS = Arrays.asList(new String[]{"GET", "POST", "DELETE"}); private final String accessKey; private final String serviceUrl = "https://rest.messagebird.com"; - private final String clientVersion = "1.0.5"; + private final String clientVersion = "1.1.0"; private final String userAgentString = "MessageBird/Java ApiClient/" + clientVersion; private Proxy proxy = null; @@ -62,6 +59,20 @@ public R requestByID(String request, String id, Class clazz) throws Unaut return getJsonData(request + path, null, "GET", clazz); } + @Override + public R requestByID(String request, String id, Map params, Class clazz) throws UnauthorizedException, GeneralException, NotFoundException { + String path = ""; + if (id != null) { + path = "/" + id; + } + // Make rest of get request + String queryParams = ""; + if (!params.isEmpty()) { + queryParams = "?" + getPathVariables(params); + } + return getJsonData(request + path + queryParams, null, "GET", clazz); + } + @Override public void deleteByID(String request, String id) throws UnauthorizedException, GeneralException, NotFoundException { getJsonData(request + "/" + id, null, "DELETE", null); @@ -105,6 +116,8 @@ public T getJsonData(final String request, final P payload, final String // If we as new properties, we don't want the system to fail, we rather want to ignore them mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); return mapper.readValue(inputStream, clazz); + } else if (responseCode == HttpURLConnection.HTTP_NO_CONTENT) { + return null; // no content doesn't mean an error } else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) { final List errorReport = getErrorReport(connection.getErrorStream()); throw new UnauthorizedException(NOT_AUTHORISED_MSG, errorReport); @@ -169,10 +182,15 @@ public

HttpURLConnection getConnection(final String serviceUrl, final P post final String json = mapper.writeValueAsString(postData); connection.getOutputStream().write(json.getBytes("UTF-8")); + } else if ("DELETE".equals(requestType)) { + // could have just used rquestType as it is + connection.setDoOutput(false); + connection.setRequestMethod("DELETE"); + connection.setRequestProperty("Content-Type", "application/text"); } else { connection.setDoOutput(false); connection.setRequestMethod("GET"); - connection.setRequestProperty("Content-Type:", "application/text"); + connection.setRequestProperty("Content-Type", "application/text"); } return connection; diff --git a/api/src/main/java/com/messagebird/objects/Gender.java b/api/src/main/java/com/messagebird/objects/Gender.java new file mode 100644 index 00000000..21df1830 --- /dev/null +++ b/api/src/main/java/com/messagebird/objects/Gender.java @@ -0,0 +1,21 @@ +package com.messagebird.objects; + +/** + * Created by faizan on 09/12/15. + */ +public enum Gender { + + MALE("male"), + FEMALE("female"); + + private String code; + + Gender(String code) { + this.code = code; + } + + public String toString() { + return this.code; + } +} + diff --git a/api/src/main/java/com/messagebird/objects/Language.java b/api/src/main/java/com/messagebird/objects/Language.java new file mode 100644 index 00000000..6ce76027 --- /dev/null +++ b/api/src/main/java/com/messagebird/objects/Language.java @@ -0,0 +1,37 @@ +package com.messagebird.objects; + +/** + * Created by faizan on 09/12/15. + */ +public enum Language { + + NL_NL("nl-nl"), + DE_DE("de-de"), + EN_GB("en-gb"), + EN_US("en-us"), + ES_ES("es-es"), + FR_FR("fr-fr"), + RU_RU("ru_ru"), + ZH_CN("zh-cn"), + EN_AU("en-au"), + ES_MX("es-mx"), + ES_US("es-us"), + FR_CA("fr-ca"), + IS_IS("is-is"), + IT_IT("it-it"), + JA_JP("ja-jp"), + KO_KR("ko-kr"), + PL_PL("pl-pl"), + PT_BR("pt-br"), + RO_RO("ro-ro"); + + private String code; + + Language(String code) { + this.code = code; + } + + public String toString() { + return this.code; + } +} diff --git a/api/src/main/java/com/messagebird/objects/Messages.java b/api/src/main/java/com/messagebird/objects/Messages.java new file mode 100644 index 00000000..3319d81d --- /dev/null +++ b/api/src/main/java/com/messagebird/objects/Messages.java @@ -0,0 +1,24 @@ +package com.messagebird.objects; + +import java.io.Serializable; + +/** + * Created by faizan on 09/12/15. + */ +public class Messages implements Serializable { + private String href; + + public String getHref() { + return href; + } + + public void setHref(String href) { + this.href = href; + } + + public String toString() { + return "Messages{" + + "href=" + this.href + + "}"; + } +} diff --git a/api/src/main/java/com/messagebird/objects/Verify.java b/api/src/main/java/com/messagebird/objects/Verify.java new file mode 100644 index 00000000..346847c2 --- /dev/null +++ b/api/src/main/java/com/messagebird/objects/Verify.java @@ -0,0 +1,86 @@ +package com.messagebird.objects; + +import java.io.Serializable; +import java.util.Date; + +/** + * Created by faizan on 09/12/15. + */ +public class Verify implements Serializable { + + private String id; + private String href; + private String recipient; + private Messages messages; + private String status; + private Date createdDatetime; + private Date validUntilDatetime; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getHref() { + return href; + } + + public void setHref(String href) { + this.href = href; + } + + public String getRecipient() { + return recipient; + } + + public void setRecipient(String recipient) { + this.recipient = recipient; + } + + public Messages getMessages() { + return messages; + } + + public void setMessages(Messages messages) { + this.messages = messages; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public Date getCreatedDatetime() { + return createdDatetime; + } + + public void setCreatedDatetime(Date createdDatetime) { + this.createdDatetime = createdDatetime; + } + + public Date getValidUntilDatetime() { + return validUntilDatetime; + } + + public void setValidUntilDatetime(Date validUntilDatetime) { + this.validUntilDatetime = validUntilDatetime; + } + + public String toString() { + return "Verify {" + " " + + "id=" + this.id + " " + + "href=" + this.href + " " + + "recipient=" + this.recipient + " " + + "messages=" + this.messages + " " + + "status=" + this.status + " " + + "createdDatetime=" + this.createdDatetime + " " + + "validUntilDatetime=" + this.validUntilDatetime+ " " + + "}"; + } +} diff --git a/api/src/main/java/com/messagebird/objects/VerifyRequest.java b/api/src/main/java/com/messagebird/objects/VerifyRequest.java new file mode 100644 index 00000000..80db049a --- /dev/null +++ b/api/src/main/java/com/messagebird/objects/VerifyRequest.java @@ -0,0 +1,96 @@ +package com.messagebird.objects; + +import java.io.Serializable; + +/** + * Created by faizan on 09/12/15. + */ +public class VerifyRequest implements Serializable { + + private String recipient; + private String originator; + private String reference; + private MsgType type; + private String template; + private Integer timeout; + private Integer tokenLength; + private Gender voice; + private Language language; + + + public VerifyRequest(String recipient) { + this.recipient = recipient; + } + + public String getRecipient() { + return recipient; + } + + public void setRecipient(String recipient) { + this.recipient = recipient; + } + + public String getOriginator() { + return originator; + } + + public void setOriginator(String originator) { + this.originator = originator; + } + + public String getReference() { + return reference; + } + + public void setReference(String reference) { + this.reference = reference; + } + + public MsgType getType() { + return type; + } + + public void setType(MsgType type) { + this.type = type; + } + + public String getTemplate() { + return template; + } + + public void setTemplate(String template) { + this.template = template; + } + + public Integer getTimeout() { + return timeout; + } + + public void setTimeout(Integer timeout) { + this.timeout = timeout; + } + + public Integer getTokenLength() { + return tokenLength; + } + + public void setTokenLength(Integer tokenLength) { + this.tokenLength = tokenLength; + } + + public Gender getVoice() { + return voice; + } + + public void setVoice(Gender voice) { + this.voice = voice; + } + + public Language getLanguage() { + return language; + } + + public void setLanguage(Language language) { + this.language = language; + } +} diff --git a/api/src/test/java/com/messagebird/MessageBirdClientTest.java b/api/src/test/java/com/messagebird/MessageBirdClientTest.java index 2d42a91a..75e6f50f 100644 --- a/api/src/test/java/com/messagebird/MessageBirdClientTest.java +++ b/api/src/test/java/com/messagebird/MessageBirdClientTest.java @@ -8,11 +8,13 @@ import org.junit.BeforeClass; import org.junit.Test; +import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.util.Arrays; import java.util.List; -import java.util.Map; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; /** @@ -327,4 +329,51 @@ public void testDeleteVoiceMessage() throws Exception { messageBirdClient.deleteVoiceMessage("Foo"); } + @Test + public void testSendVerifyToken1() throws UnauthorizedException, GeneralException { + final String reference = "5551234"; + VerifyRequest verifyRequest = new VerifyRequest(this.messageBirdMSISDN.toString()); + verifyRequest.setOriginator("Code"); + verifyRequest.setReference(reference); + verifyRequest.setLanguage(Language.NL_NL); + verifyRequest.setType(MsgType.sms); + verifyRequest.setTimeout(30); + verifyRequest.setTokenLength(6); + verifyRequest.setVoice(Gender.FEMALE); + Verify verify = messageBirdClient.sendVerifyToken(verifyRequest); + assertFalse("href is empty", verify.getHref().isEmpty()); + } + + @Test + public void testSendVerifyTokenAndGetVerifyObject() throws UnauthorizedException, GeneralException, NotFoundException { + Verify verify = messageBirdClient.sendVerifyToken(this.messageBirdMSISDN.toString()); + assertFalse("href is empty", verify.getHref().isEmpty()); + assertFalse("id is empty", verify.getId().isEmpty()); + + verify = messageBirdClient.getVerifyObject(verify.getId()); + assertFalse("href is empty", verify.getHref().isEmpty()); + assertFalse("id is empty", verify.getId().isEmpty()); + } + + @Test + public void testVerifyToken() throws UnauthorizedException, GeneralException, NotFoundException, UnsupportedEncodingException { + Verify verify = messageBirdClient.sendVerifyToken(this.messageBirdMSISDN.toString()); + assertFalse("href is empty", verify.getHref().isEmpty()); + + try { + messageBirdClient.verifyToken(verify.getId(), "123456"); + } catch (GeneralException e) { + // we expect only one error about token and nothing else + assertEquals("token", e.getErrors().get(0).getParameter()); + assertTrue(e.getErrors().size() == 1); + } + } + + @Test + public void testDeleteVerifyToken() throws UnauthorizedException, GeneralException, NotFoundException, UnsupportedEncodingException { + Verify verify = messageBirdClient.sendVerifyToken(this.messageBirdMSISDN.toString()); + assertFalse("href is empty", verify.getHref().isEmpty()); + + messageBirdClient.deleteVerifyObject(verify.getId()); + } } diff --git a/examples/pom.xml b/examples/pom.xml index e9bfaee1..7f476f7c 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -20,7 +20,7 @@ com.messagebird messagebird-api - 1.0.5 + 1.1.0 diff --git a/examples/src/main/java/ExampleSendVerifyToken.java b/examples/src/main/java/ExampleSendVerifyToken.java new file mode 100644 index 00000000..fbb85f5d --- /dev/null +++ b/examples/src/main/java/ExampleSendVerifyToken.java @@ -0,0 +1,48 @@ +import com.messagebird.MessageBirdClient; +import com.messagebird.MessageBirdService; +import com.messagebird.MessageBirdServiceImpl; +import com.messagebird.exceptions.GeneralException; +import com.messagebird.exceptions.NotFoundException; +import com.messagebird.exceptions.UnauthorizedException; +import com.messagebird.objects.MessageResponse; +import com.messagebird.objects.Verify; +import com.messagebird.objects.VerifyRequest; + +/** + * Created by faizan on 10/12/15. + */ +public class ExampleSendVerifyToken { + + public static void main(String[] args) { + if (args.length < 2) { + System.out.println("Please specify your access key and a recipient : java -jar test_accesskey recipientNumber"); + return; + } + + // First create your service object + final MessageBirdService wsr = new MessageBirdServiceImpl(args[0]); + + // Add the service to the client + final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr); + + + try { + // Send verify token + System.out.println("sending verify token request:"); + VerifyRequest verifyRequest = new VerifyRequest(args[1]); + verifyRequest.setTimeout(120); + final Verify verify = messageBirdClient.sendVerifyToken(verifyRequest); + System.out.println(verify.toString()); + } catch (UnauthorizedException unauthorized) { + if (unauthorized.getErrors() != null) { + System.out.println(unauthorized.getErrors().toString()); + } + unauthorized.printStackTrace(); + } catch (GeneralException generalException) { + if (generalException.getErrors() != null) { + System.out.println(generalException.getErrors().toString()); + } + generalException.printStackTrace(); + } + } +} diff --git a/examples/src/main/java/ExampleVerifyToken.java b/examples/src/main/java/ExampleVerifyToken.java new file mode 100644 index 00000000..465f36af --- /dev/null +++ b/examples/src/main/java/ExampleVerifyToken.java @@ -0,0 +1,46 @@ +import com.messagebird.MessageBirdClient; +import com.messagebird.MessageBirdService; +import com.messagebird.MessageBirdServiceImpl; +import com.messagebird.exceptions.GeneralException; +import com.messagebird.exceptions.NotFoundException; +import com.messagebird.exceptions.UnauthorizedException; +import com.messagebird.objects.Verify; + +/** + * Created by faizan on 10/12/15. + */ +public class ExampleVerifyToken { + + public static void main(String[] args) { + if (args.length < 3) { + System.out.println("Please specify your access key, verifyId and a token : java -jar test_accessKey verifyId token"); + return; + } + + // First create your service object + final MessageBirdService wsr = new MessageBirdServiceImpl(args[0]); + + // Add the service to the client + final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr); + + + try { + // Send verify token + System.out.println("verifying token request: " + args[2]); + final Verify verify = messageBirdClient.verifyToken(args[1], args[2]); + System.out.println(verify.toString()); + } catch (UnauthorizedException unauthorized) { + if (unauthorized.getErrors() != null) { + System.out.println(unauthorized.getErrors().toString()); + } + unauthorized.printStackTrace(); + } catch (GeneralException generalException) { + if (generalException.getErrors() != null) { + System.out.println(generalException.getErrors().toString()); + } + generalException.printStackTrace(); + } catch (NotFoundException e) { + e.printStackTrace(); + } + } +}