From 3ba99f41336f470d5ca57725cb1bfc4d92a69d17 Mon Sep 17 00:00:00 2001 From: Sam Wierema Date: Wed, 13 Jan 2016 11:48:48 +0100 Subject: [PATCH 1/2] Add support for the Lookup API Every method is added twice, one with the required object, and one with only required parameters. Version is bumped to 1.2.0 for new functionality. Version is also bumped for the examples. Bumped to 1.2.0 (instead of 1.1.0) as it should've been bumped when the Verify API was added in a previous pull request. --- README.md | 4 +- api/pom.xml | 2 +- .../com/messagebird/MessageBirdClient.java | 122 ++++++++++++++++++ .../messagebird/MessageBirdServiceImpl.java | 4 +- .../java/com/messagebird/objects/Hlr.java | 4 +- .../java/com/messagebird/objects/Lookup.java | 108 ++++++++++++++++ .../com/messagebird/objects/LookupHlr.java | 27 ++++ examples/pom.xml | 4 +- .../main/java/ExampleRequestLookupHlr.java | 50 +++++++ examples/src/main/java/ExampleViewLookup.java | 53 ++++++++ .../src/main/java/ExampleViewLookupHlr.java | 54 ++++++++ 11 files changed, 423 insertions(+), 9 deletions(-) create mode 100644 api/src/main/java/com/messagebird/objects/Lookup.java create mode 100644 api/src/main/java/com/messagebird/objects/LookupHlr.java create mode 100644 examples/src/main/java/ExampleRequestLookupHlr.java create mode 100644 examples/src/main/java/ExampleViewLookup.java create mode 100644 examples/src/main/java/ExampleViewLookupHlr.java diff --git a/README.md b/README.md index c653441b..d93efa1e 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.1.0 + 1.2.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.1.0.jar +messagebird-api-1.2.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 76af6d07..0337f831 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -6,7 +6,7 @@ com.messagebird messagebird-api - 1.1.0 + 1.2.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 07ecef72..47092cb0 100644 --- a/api/src/main/java/com/messagebird/MessageBirdClient.java +++ b/api/src/main/java/com/messagebird/MessageBirdClient.java @@ -32,6 +32,8 @@ public class MessageBirdClient { private static final String MESSAGESPATH = "/messages"; private static final String VOICEMESSAGESPATH = "/voicemessages"; private static final String VERIFYPATH = "/verify"; + private static final String LOOKUPPATH = "/lookup"; + private static final String LOOKUPHLRPATH = "/lookup/%s/hlr"; private MessageBirdService messageBirdService; public MessageBirdClient(final MessageBirdService messageBirdService) { @@ -385,4 +387,124 @@ public void deleteVerifyObject(String id) throws NotFoundException, GeneralExcep } messageBirdService.deleteByID(VERIFYPATH, id); } + + /** + * Send a Lookup request + * + * @param Lookup + * @return Lookup + * @throws UnauthorizedException + * @throws GeneralException + * @throws NotFoundException + */ + public Lookup viewLookup(final Lookup lookup) throws UnauthorizedException, GeneralException, NotFoundException { + if (lookup.getPhoneNumber() == null) { + throw new IllegalArgumentException("Phonenumber must be specified."); + } + final Map params = new LinkedHashMap(); + if (lookup.getCountryCode() != null) { + params.put("countryCode", lookup.getCountryCode()); + } + return messageBirdService.requestByID(LOOKUPPATH, String.valueOf(lookup.getPhoneNumber()), params, Lookup.class); + } + + /** + * Send a Lookup request + * + * @param phonenumber + * @return Lookup + * @throws UnauthorizedException + * @throws GeneralException + */ + public Lookup viewLookup(final BigInteger phonenumber) throws UnauthorizedException, GeneralException, NotFoundException { + if (phonenumber == null) { + throw new IllegalArgumentException("Phonenumber must be specified."); + } + final Lookup lookup = new Lookup(phonenumber); + return this.viewLookup(lookup); + } + + /** + * Request a Lookup HLR (lookup) + * + * @param LookupHlr + * @return lookupHlr + * @throws UnauthorizedException + * @throws GeneralException + */ + public LookupHlr requestLookupHlr(final LookupHlr lookupHlr) throws UnauthorizedException, GeneralException { + if (lookupHlr.getPhoneNumber() == null) { + throw new IllegalArgumentException("Phonenumber must be specified."); + } + if (lookupHlr.getReference() == null) { + throw new IllegalArgumentException("Reference must be specified."); + } + final Map payload = new LinkedHashMap(); + payload.put("phoneNumber", lookupHlr.getPhoneNumber()); + payload.put("reference", lookupHlr.getReference()); + if (lookupHlr.getCountryCode() != null) { + payload.put("countryCode", lookupHlr.getCountryCode()); + } + return messageBirdService.sendPayLoad(String.format(LOOKUPHLRPATH, lookupHlr.getPhoneNumber()), payload, LookupHlr.class); + } + + /** + * Request a Lookup HLR (lookup) + * + * @param phonenumber + * @param reference + * @return lookupHlr + * @throws UnauthorizedException + * @throws GeneralException + */ + public LookupHlr requestLookupHlr(final BigInteger phonenumber, final String reference) throws UnauthorizedException, GeneralException { + if (phonenumber == null) { + throw new IllegalArgumentException("Phonenumber must be specified."); + } + if (reference == null) { + throw new IllegalArgumentException("Reference must be specified."); + } + final LookupHlr lookupHlr = new LookupHlr(); + lookupHlr.setPhoneNumber(phonenumber); + lookupHlr.setReference(reference); + return this.requestLookupHlr(lookupHlr); + } + + /** + * View a Lookup HLR (lookup) + * + * @param LookupHlr + * @return LookupHlr + * @throws UnauthorizedException + * @throws GeneralException + * @throws NotFoundException + */ + public LookupHlr viewLookupHlr(final LookupHlr lookupHlr) throws UnauthorizedException, GeneralException, NotFoundException { + if (lookupHlr.getPhoneNumber() == null) { + throw new IllegalArgumentException("Phonenumber must be specified"); + } + final Map params = new LinkedHashMap(); + if (lookupHlr.getCountryCode() != null) { + params.put("countryCode", lookupHlr.getCountryCode()); + } + return messageBirdService.requestByID(String.format(LOOKUPHLRPATH, lookupHlr.getPhoneNumber()), "", params, LookupHlr.class); + } + + /** + * View a Lookup HLR (lookup) + * + * @param phonenumber + * @return LookupHlr + * @throws UnauthorizedException + * @throws GeneralException + * @throws NotFoundException + */ + public LookupHlr viewLookupHlr(final BigInteger phonenumber) throws UnauthorizedException, GeneralException, NotFoundException { + if (phonenumber == null) { + throw new IllegalArgumentException("Phonenumber must be specified"); + } + final LookupHlr lookupHlr = new LookupHlr(); + lookupHlr.setPhoneNumber(phonenumber); + return this.viewLookupHlr(lookupHlr); + } } diff --git a/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java b/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java index d1cfa54b..2750d710 100644 --- a/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java +++ b/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java @@ -32,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.1.0"; + private final String clientVersion = "1.2.0"; private final String userAgentString = "MessageBird/Java ApiClient/" + clientVersion; private Proxy proxy = null; @@ -65,7 +65,7 @@ public R requestByID(String request, String id, Map params, if (id != null) { path = "/" + id; } - // Make rest of get request + // Make rest of GET request String queryParams = ""; if (!params.isEmpty()) { queryParams = "?" + getPathVariables(params); diff --git a/api/src/main/java/com/messagebird/objects/Hlr.java b/api/src/main/java/com/messagebird/objects/Hlr.java index 589c867d..151bd18b 100644 --- a/api/src/main/java/com/messagebird/objects/Hlr.java +++ b/api/src/main/java/com/messagebird/objects/Hlr.java @@ -14,9 +14,9 @@ public class Hlr { private String id; private String href; - private BigInteger msisdn; + protected BigInteger msisdn; private String network; - private String reference; + protected String reference; private String status; private Date createdDatetime; private Date statusDatetime; diff --git a/api/src/main/java/com/messagebird/objects/Lookup.java b/api/src/main/java/com/messagebird/objects/Lookup.java new file mode 100644 index 00000000..2a7780eb --- /dev/null +++ b/api/src/main/java/com/messagebird/objects/Lookup.java @@ -0,0 +1,108 @@ +package com.messagebird.objects; + +import java.io.Serializable; +import java.math.BigInteger; + +public class Lookup implements Serializable { + + private static final long serialVersionUID = 8927014359452296030L; + + private String href; + private String countryCode; + private Integer countryPrefix; + private BigInteger phoneNumber; + private String type; + private Formats formats; + private LookupHlr hlr; + + @Override + public String toString() { + return "Lookup{" + + "href=" + href + + ", countryCode=" + countryCode + + ", countryPrefix=" + countryPrefix + + ", phoneNumber=" + phoneNumber + + ", type=" + type + + ", formats=" + formats + + ", hlr=" + hlr + + "}"; + } + + public Lookup() { + } + + public Lookup(BigInteger phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public String getHref() { + return href; + } + + public String getCountryCode() { + return countryCode; + } + + public void setCountryCode(String countryCode) { + this.countryCode = countryCode; + } + + public Integer getCountryPrefix() { + return countryPrefix; + } + + public BigInteger getPhoneNumber() { + return phoneNumber; + } + + public String getType() { + return type; + } + + public Formats getFormats() { + return formats; + } + + public Hlr getHlr() { + return hlr; + } + + static public class Formats implements Serializable { + + private static final long serialVersionUID = 2165916336570704972L; + + private String e164; + private String international; + private String national; + private String rfc3966; + + public Formats() { + } + + @Override + public String toString() { + return "Formats{" + + "e164=" + e164 + + ", international=" + international + + ", national=" + national + + ", rfc3966=" + rfc3966 + + '}'; + } + + public String getE164() { + return e164; + } + + public String getInternational() { + return international; + } + + public String getNational() { + return national; + } + + public String getRfc3966() { + return rfc3966; + } + } +} diff --git a/api/src/main/java/com/messagebird/objects/LookupHlr.java b/api/src/main/java/com/messagebird/objects/LookupHlr.java new file mode 100644 index 00000000..98e568ae --- /dev/null +++ b/api/src/main/java/com/messagebird/objects/LookupHlr.java @@ -0,0 +1,27 @@ +package com.messagebird.objects; + +import java.math.BigInteger; + +public class LookupHlr extends Hlr { + private String countryCode; + + public String getCountryCode() { + return countryCode; + } + + public void setCountryCode(String countryCode) { + this.countryCode = countryCode; + } + + public BigInteger getPhoneNumber() { + return msisdn; + } + + public void setPhoneNumber(BigInteger phoneNumber) { + this.msisdn = phoneNumber; + } + + public void setReference(String reference) { + this.reference = reference; + } +} diff --git a/examples/pom.xml b/examples/pom.xml index 7f476f7c..b1c725b2 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -6,7 +6,7 @@ com.messagebird examples - 1.0.1 + 1.2.0 @@ -20,7 +20,7 @@ com.messagebird messagebird-api - 1.1.0 + 1.2.0 diff --git a/examples/src/main/java/ExampleRequestLookupHlr.java b/examples/src/main/java/ExampleRequestLookupHlr.java new file mode 100644 index 00000000..730709be --- /dev/null +++ b/examples/src/main/java/ExampleRequestLookupHlr.java @@ -0,0 +1,50 @@ +import com.messagebird.MessageBirdClient; +import com.messagebird.MessageBirdService; +import com.messagebird.MessageBirdServiceImpl; +import com.messagebird.exceptions.NotFoundException; +import com.messagebird.exceptions.GeneralException; +import com.messagebird.exceptions.UnauthorizedException; +import com.messagebird.objects.LookupHlr; + +import java.math.BigInteger; + +public class ExampleRequestLookupHlr { + + public static void main(String[] args) { + + if (args.length < 3) { + System.out.println("Please specify your access key, phone and reference in that order example : java -jar test_accesskey 31612345678 reference"); + 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 { + // Request Lookup HLR + System.out.println("Requesting Lookup HLR:"); + final LookupHlr lookupHlrRequest = new LookupHlr(); + lookupHlrRequest.setPhoneNumber(new BigInteger(args[1])); + lookupHlrRequest.setReference(args[2]); + // Optionally set a country code (in the case of national numbers) + if (args.length > 3 && args[3] != null && !args[3].isEmpty()) { + lookupHlrRequest.setCountryCode(args[3]); + } + final LookupHlr lookupHlr = messageBirdClient.requestLookupHlr(lookupHlrRequest); + System.out.println(lookupHlr.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/ExampleViewLookup.java b/examples/src/main/java/ExampleViewLookup.java new file mode 100644 index 00000000..ee4a14ba --- /dev/null +++ b/examples/src/main/java/ExampleViewLookup.java @@ -0,0 +1,53 @@ +import com.messagebird.MessageBirdClient; +import com.messagebird.MessageBirdService; +import com.messagebird.MessageBirdServiceImpl; +import com.messagebird.exceptions.NotFoundException; +import com.messagebird.exceptions.GeneralException; +import com.messagebird.exceptions.UnauthorizedException; +import com.messagebird.objects.Lookup; + +import java.math.BigInteger; + +public class ExampleViewLookup { + + public static void main(String[] args) { + + if (args.length < 2) { + System.out.println("Please specify your access key and phone in that order example : java -jar test_accesskey 31612345678"); + 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 { + // View Lookup + System.out.println("Viewing Lookup:"); + final Lookup lookupRequest = new Lookup(new BigInteger(args[1])); + // Optionally set a country code (in the case of national numbers) + if (args.length > 2 && args[2] != null && !args[2].isEmpty()) { + lookupRequest.setCountryCode(args[2]); + } + final Lookup lookup = messageBirdClient.viewLookup(lookupRequest); + System.out.println(lookup.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 notFoundException) { + if (notFoundException.getErrors() !=null) { + System.out.println(notFoundException.getErrors().toString()); + } + notFoundException.printStackTrace(); + } + } +} diff --git a/examples/src/main/java/ExampleViewLookupHlr.java b/examples/src/main/java/ExampleViewLookupHlr.java new file mode 100644 index 00000000..9c638cab --- /dev/null +++ b/examples/src/main/java/ExampleViewLookupHlr.java @@ -0,0 +1,54 @@ +import com.messagebird.MessageBirdClient; +import com.messagebird.MessageBirdService; +import com.messagebird.MessageBirdServiceImpl; +import com.messagebird.exceptions.NotFoundException; +import com.messagebird.exceptions.GeneralException; +import com.messagebird.exceptions.UnauthorizedException; +import com.messagebird.objects.LookupHlr; + +import java.math.BigInteger; + +public class ExampleViewLookupHlr { + + public static void main(String[] args) { + + if (args.length < 2) { + System.out.println("Please specify your access key and phone in that order example : java -jar test_accesskey 31612345678"); + 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 { + // View Lookup HLR + System.out.println("Viewing Lookup HLR:"); + final LookupHlr lookupHlrRequest = new LookupHlr(); + lookupHlrRequest.setPhoneNumber(new BigInteger(args[1])); + // Optionally set a country code (in the case of national numbers) + if (args.length > 2 && args[2] != null && !args[2].isEmpty()) { + lookupHlrRequest.setCountryCode(args[2]); + } + final LookupHlr lookupHlr = messageBirdClient.viewLookupHlr(lookupHlrRequest); + System.out.println(lookupHlr.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 notFoundException) { + if (notFoundException.getErrors() !=null) { + System.out.println(notFoundException.getErrors().toString()); + } + notFoundException.printStackTrace(); + } + } +} From 50cae36f54e01fea59df428b8303e5b81869c8ef Mon Sep 17 00:00:00 2001 From: Sam Wierema Date: Wed, 3 Feb 2016 09:19:31 +0100 Subject: [PATCH 2/2] Change the version of the examples back to 1.2.0 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5e1947a4..1711424c 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,7 @@ To try out the command line examples follow the above build instructions. When everything did build successful you can try out the API like this: ```shell cd examples/target -java -cp examples-2.0.0-jar-with-dependencies.jar ExampleSendMessage test_gshuPaZoeEG6ovbc8M79w0QyM 31612345678 "This is a test message" +java -cp examples-1.2.0-jar-with-dependencies.jar ExampleSendMessage test_gshuPaZoeEG6ovbc8M79w0QyM 31612345678 "This is a test message" ``` Please see the other examples for a complete overview of all the available API calls.