diff --git a/sample-proxies/validatemsisdn/README.md b/sample-proxies/validatemsisdn/README.md new file mode 100644 index 00000000..e8b79cd0 --- /dev/null +++ b/sample-proxies/validatemsisdn/README.md @@ -0,0 +1,46 @@ +# Validate MSISDN Sample + +This sample provides a simple implementation of the Validating Mobile Number for a given country . +This uses Google's Utility : i18n/phonenumbers/PhoneNumberUtil +This validates the phone number and country code . + +# Set up + +* The username and password that you use to login to enterprise.apigee.com. +* The name of the organization in which you have an account. Login to + enterprise.apigee.com and check account settings. + +# Configure + +Update `/setup/setenv.sh` with your environment details + +# Import and deploy sample project + +To deploy, run `$ sh deploy.sh` + +To test, run `$ sh invoke.sh` + + + +#3rd Part Library Information +libphonenumber version 5.8 +This is provided by Google and licensed as Apache License V2.0 + + +# Get help + +For assistance, post to the [Apigee Developer Forum](http://support.apigee.com) + +Copyright © 2013 Apigee Corporation + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use +this file except in compliance with the License. You may obtain a copy +of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/sample-proxies/validatemsisdn/apiproxy/policies/AssignParamsToFlowVars.xml b/sample-proxies/validatemsisdn/apiproxy/policies/AssignParamsToFlowVars.xml new file mode 100644 index 00000000..f7c9cbd9 --- /dev/null +++ b/sample-proxies/validatemsisdn/apiproxy/policies/AssignParamsToFlowVars.xml @@ -0,0 +1,9 @@ + + + Extract Variables Policy for URI extraction + + /{countryCode}/{mobileNumber} + + flow + true + \ No newline at end of file diff --git a/sample-proxies/validatemsisdn/apiproxy/policies/RaiseFault-InvalidPhone.xml b/sample-proxies/validatemsisdn/apiproxy/policies/RaiseFault-InvalidPhone.xml new file mode 100644 index 00000000..ce20f89c --- /dev/null +++ b/sample-proxies/validatemsisdn/apiproxy/policies/RaiseFault-InvalidPhone.xml @@ -0,0 +1,17 @@ + + + RaiseFault-InvalidPhone + + + + + + + {flow.error} + + 400 + Bad Request + + + true + \ No newline at end of file diff --git a/sample-proxies/validatemsisdn/apiproxy/policies/SetValidateMessage.xml b/sample-proxies/validatemsisdn/apiproxy/policies/SetValidateMessage.xml new file mode 100644 index 00000000..36709cdd --- /dev/null +++ b/sample-proxies/validatemsisdn/apiproxy/policies/SetValidateMessage.xml @@ -0,0 +1,17 @@ + + + SetValidateMessage + + + + + + {flow.isValidMobileNumber} + {flow.mobileNumberInternational} + {flow.mobileNumberNational} + {flow.mobileNumberE164} + + + + true + diff --git a/sample-proxies/validatemsisdn/apiproxy/policies/validatemsisdn.xml b/sample-proxies/validatemsisdn/apiproxy/policies/validatemsisdn.xml new file mode 100755 index 00000000..0c74cfab --- /dev/null +++ b/sample-proxies/validatemsisdn/apiproxy/policies/validatemsisdn.xml @@ -0,0 +1,5 @@ + + + com.samples.apigee.phone.ValidateNumber + java://ValidateNumber.jar + diff --git a/sample-proxies/validatemsisdn/apiproxy/proxies/default.xml b/sample-proxies/validatemsisdn/apiproxy/proxies/default.xml new file mode 100755 index 00000000..289afd29 --- /dev/null +++ b/sample-proxies/validatemsisdn/apiproxy/proxies/default.xml @@ -0,0 +1,42 @@ + + + Default Proxy to create flows on an API + + + + + + + + + + Country code and Phone + + + AssignParamsToFlowVars + + + validatemsisdn + + + + raisefault-invalidphone + flow.validatenumbersuccess = false + + + SetValidateMessage + + + + (proxy.pathsuffix MatchesPath "/{code}/{phone}") and (request.verb = "GET") + + + + + /validatemsisdn + + default + + + + diff --git a/sample-proxies/validatemsisdn/apiproxy/validatemsisdn.xml b/sample-proxies/validatemsisdn/apiproxy/validatemsisdn.xml new file mode 100755 index 00000000..6f367082 --- /dev/null +++ b/sample-proxies/validatemsisdn/apiproxy/validatemsisdn.xml @@ -0,0 +1,2 @@ + + diff --git a/sample-proxies/validatemsisdn/deploy.sh b/sample-proxies/validatemsisdn/deploy.sh new file mode 100755 index 00000000..26f092d5 --- /dev/null +++ b/sample-proxies/validatemsisdn/deploy.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +source ../../setup/setenv.sh + +echo "Enter your password for the Apigee Enterprise organization $org, followed by [ENTER]:" + +read -s password + +echo Deploying $proxy to $env on $url using $username and $org + +cd ./java +javac -d bin -sourcepath src -classpath ../lib/libphonenumber-5.8.jar:../lib/expressions-1.0.0.jar:../lib/message-flow-1.0.0.jar: src/com/samples/apigee/phone/ValidateNumber.java +cd bin +jar -cvf ValidateNumber.jar ./com +cp ValidateNumber.jar ../../apiproxy/resources/java +cp ../../lib/libphonenumber-5.8.jar ../../apiproxy/resources/java +cd ../.. +rm -fr java/bin/* + +../../tools/deploy.py -n validatemsisdn -u $username:$password -o $org -h $url -e $env -p / -d ../validatemsisdn +rm -fr apiproxy/resources/java/* + +echo "If 'State: deployed', then your API Proxy is ready to be invoked." + +echo "Run 'invoke.sh'" diff --git a/sample-proxies/validatemsisdn/invoke.sh b/sample-proxies/validatemsisdn/invoke.sh new file mode 100755 index 00000000..d3307896 --- /dev/null +++ b/sample-proxies/validatemsisdn/invoke.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +echo Using org and environment configured in /setup/setenv.sh + +source ../../setup/setenv.sh + +echo "Checking Valid Phone Number" + +curl "http://$org-$env.apigee.net/validatemsisdn/+41/+41446681800" + +echo "Checking Invalid Phone Number" +curl "http://$org-$env.apigee.net/validatemsisdn/+91/+91456565779" + diff --git a/sample-proxies/validatemsisdn/java/src/com/samples/apigee/phone/ValidateNumber.java b/sample-proxies/validatemsisdn/java/src/com/samples/apigee/phone/ValidateNumber.java new file mode 100755 index 00000000..06155d07 --- /dev/null +++ b/sample-proxies/validatemsisdn/java/src/com/samples/apigee/phone/ValidateNumber.java @@ -0,0 +1,84 @@ +package com.samples.apigee.phone; +import com.apigee.flow.execution.ExecutionContext; +import com.apigee.flow.execution.ExecutionResult; +import com.apigee.flow.execution.IOIntensive; +import com.apigee.flow.execution.spi.Execution; +import com.apigee.flow.message.MessageContext; + +import com.google.i18n.phonenumbers.NumberParseException; +import com.google.i18n.phonenumbers.PhoneNumberUtil; +import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat; +import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; + +public class ValidateNumber implements Execution{ + + + @Override + public ExecutionResult execute(MessageContext messageContext, ExecutionContext executionContext) + { + try + { + + messageContext.setVariable("flow.error", null); + messageContext.setVariable("flow.validatenumbersuccess", "true"); + + String mobileNumber = messageContext.getVariable("flow.mobileNumber"); + String countryCode = messageContext.getVariable("flow.countryCode"); + + PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); + PhoneNumber mobileNumberProto = phoneUtil.parse(mobileNumber, countryCode); + boolean isValid = phoneUtil.isValidNumber(mobileNumberProto); // returns true + + // Produces "+41 44 668 18 00" + String mobileNumberInternational =phoneUtil.format(mobileNumberProto, PhoneNumberFormat.INTERNATIONAL); + // System.out.println(mobileNumberInternational); + // Produces "044 668 18 00" + String mobileNumberNational = phoneUtil.format(mobileNumberProto, PhoneNumberFormat.NATIONAL); + // System.out.println(mobileNumberNational); + // Produces "+41446681800" + String mobileNumberE164 =phoneUtil.format(mobileNumberProto, PhoneNumberFormat.E164); + //System.out.println(mobileNumberE164); + + messageContext.setVariable("flow.isValidMobileNumber", isValid); + messageContext.setVariable("flow.mobileNumberInternational", mobileNumberInternational); + messageContext.setVariable("flow.mobileNumberNational", mobileNumberNational); + messageContext.setVariable("flow.mobileNumberE164", mobileNumberE164); + + return ExecutionResult.SUCCESS; + } + + catch (NumberParseException e) + { + messageContext.setVariable("flow.validatenumbersuccess", "false"); + messageContext.setVariable("flow.error", e.toString()); + return ExecutionResult.SUCCESS; + } catch (Exception e) { + return ExecutionResult.ABORT; + } + } + + public static void main(String [] args) { + System.out.println("ValidateNumber called"); + + try { + String mobileNumber = "+41446681800"; + String countryCode = "+41"; + + PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); + PhoneNumber mobileNumberProto = phoneUtil.parse(mobileNumber, countryCode); + + boolean isValid = phoneUtil.isValidNumber(mobileNumberProto); // returns true + + String mobileNumberInternational =phoneUtil.format(mobileNumberProto, PhoneNumberFormat.INTERNATIONAL); + System.out.println("Mobile International :" + mobileNumberInternational + " is " + isValid); + String mobileNumberNational = phoneUtil.format(mobileNumberProto, PhoneNumberFormat.NATIONAL); + System.out.println("Mobile National :" + mobileNumberNational + " is " + isValid); + String mobileNumberE164 =phoneUtil.format(mobileNumberProto, PhoneNumberFormat.E164); + System.out.println("Mobile E164 :" + mobileNumberE164 + " is " + isValid); + + } + catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/sample-proxies/validatemsisdn/lib/expressions-1.0.0.jar b/sample-proxies/validatemsisdn/lib/expressions-1.0.0.jar new file mode 100644 index 00000000..b2979541 Binary files /dev/null and b/sample-proxies/validatemsisdn/lib/expressions-1.0.0.jar differ diff --git a/sample-proxies/validatemsisdn/lib/libphonenumber-5.8.jar b/sample-proxies/validatemsisdn/lib/libphonenumber-5.8.jar new file mode 100644 index 00000000..b6f5be5c Binary files /dev/null and b/sample-proxies/validatemsisdn/lib/libphonenumber-5.8.jar differ diff --git a/sample-proxies/validatemsisdn/lib/message-flow-1.0.0.jar b/sample-proxies/validatemsisdn/lib/message-flow-1.0.0.jar new file mode 100644 index 00000000..da516247 Binary files /dev/null and b/sample-proxies/validatemsisdn/lib/message-flow-1.0.0.jar differ diff --git a/sample-proxies/validatemsisdn/runjava.sh b/sample-proxies/validatemsisdn/runjava.sh new file mode 100755 index 00000000..50ab6cee --- /dev/null +++ b/sample-proxies/validatemsisdn/runjava.sh @@ -0,0 +1,6 @@ +cd ./java +javac -d bin -sourcepath src -classpath ../lib/libphonenumber-5.8.jar:../lib/expressions-1.0.0.jar:../lib/message-flow-1.0.0.jar: src/com/samples/apigee/phone/ValidateNumber.java +cd bin +java -classpath ../../lib/libphonenumber-5.8.jar:../../lib/expressions-1.0.0.jar:../../lib/message-flow-1.0.0.jar:. com.samples.apigee.phone.ValidateNumber +cd ../.. +rm -fr java/bin/*