Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate MSISDN #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions sample-proxies/validatemsisdn/README.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables name="AssignParamsToFlowVars">
<DisplayName>Extract Variables Policy for URI extraction</DisplayName>
<URIPath>
<Pattern ignoreCase="true">/{countryCode}/{mobileNumber}</Pattern>
</URIPath>
<VariablePrefix>flow</VariablePrefix>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</ExtractVariables>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RaiseFault async="false" continueOnError="false" enabled="true" name="raisefault-invalidphone">
<DisplayName>RaiseFault-InvalidPhone</DisplayName>
<FaultRules/>
<Properties/>
<FaultResponse>
<Set>
<Headers/>
<Payload contentType="text/plain">
<error>{flow.error}</error>
</Payload>
<StatusCode>400</StatusCode>
<ReasonPhrase>Bad Request</ReasonPhrase>
</Set>
</FaultResponse>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</RaiseFault>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="SetValidateMessage">
<DisplayName>SetValidateMessage</DisplayName>
<FaultRules/>
<Properties/>
<Set>
<Payload contentType="text/xml">
<Phone>
<isValid>{flow.isValidMobileNumber}</isValid>
<International>{flow.mobileNumberInternational}</International>
<National>{flow.mobileNumberNational}</National>
<E164>{flow.mobileNumberE164}</E164>
</Phone>
</Payload>
</Set>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<JavaCallout name="validatemsisdn">
<ClassName>com.samples.apigee.phone.ValidateNumber</ClassName>
<ResourceURL>java://ValidateNumber.jar</ResourceURL>
</JavaCallout>
42 changes: 42 additions & 0 deletions sample-proxies/validatemsisdn/apiproxy/proxies/default.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProxyEndpoint name="default">
<Description>Default Proxy to create flows on an API</Description>
<FaultRules>
</FaultRules>
<PreFlow>
<Request>
</Request>
<Response/>
</PreFlow>
<Flows>
<Flow name="Phone">
<Description>Country code and Phone</Description>
<Request>
<Step>
<Name>AssignParamsToFlowVars</Name>
</Step>
<Step>
<Name>validatemsisdn</Name>
</Step>
<Step>
<FaultRules/>
<Name>raisefault-invalidphone</Name>
<Condition>flow.validatenumbersuccess = false</Condition>
</Step>
<Step>
<Name>SetValidateMessage</Name>
</Step>
</Request>
<Response/>
<Condition>(proxy.pathsuffix MatchesPath &quot;/{code}/{phone}&quot;) and (request.verb = &quot;GET&quot;)</Condition>
</Flow>
</Flows>
<PostFlow/>
<HTTPProxyConnection>
<BasePath>/validatemsisdn</BasePath>
<Properties/>
<VirtualHost>default</VirtualHost>
</HTTPProxyConnection>
<RouteRule name="noRoute">
</RouteRule>
</ProxyEndpoint>
2 changes: 2 additions & 0 deletions sample-proxies/validatemsisdn/apiproxy/validatemsisdn.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<APIProxy name="validatemsisdn">
</APIProxy>
25 changes: 25 additions & 0 deletions sample-proxies/validatemsisdn/deploy.sh
Original file line number Diff line number Diff line change
@@ -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'"
13 changes: 13 additions & 0 deletions sample-proxies/validatemsisdn/invoke.sh
Original file line number Diff line number Diff line change
@@ -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"

Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
6 changes: 6 additions & 0 deletions sample-proxies/validatemsisdn/runjava.sh
Original file line number Diff line number Diff line change
@@ -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/*