A Java callout that uses java.security.SecureRandom to generate a pseudo random number within an Apigee Edge policy
This directory contains the Java source code and pom.xml file required
to compile a simple Java callout for Apigee. The callout uses
java.security.SecureRandom to generate random numbers (ints, UUIDs, or
Gaussian values) within a policy Apigee proxy, and sets a context
variable with that information.
This example is not an official Google product, nor is it part of an official Google product.
You do not need to compile the callout to use it.
It is usable as is, in pre-built form. Just grab the jar from this repository and use it.
Configure the policy like this:
<JavaCallout name='Java-PRNG-gaussian'>
<ClassName>com.google.apigee.edgecallouts.prng.SecureRandomCallout</ClassName>
<Properties>
<Property name='algorithm'>SHA1PRNG</Property>
<Property name='output-type'>int</Property>
</Properties>
<ResourceURL>java://edge-java-callout-prng-1.0.2.jar</ResourceURL>
</JavaCallout>
The callout sets the context variable prng_random
to a randomly-generated value.
The properties:
Property | Required? | description |
---|---|---|
algorithm | no | one of the algorithms returned by Java’s java.security.Security.getProviders()). Typically you will use one of these:
|
output-type | no | uuid , gaussian , or int . Defaults to int. A Gaussian output will return the next pseudorandom, Gaussian (“normally”) distributed double value with mean 0.0 and standard deviation 1.0, as returned by java.util.Random.nextGaussian()) |
decimal-digits | no | The number of decimal digits with which to render the value generated for the Gaussian distribution. The default is 12. Used only when output-type is gaussian . |
range | no | The min,max range for integer values, inclusive. Used only when output-type is int . |
The policy caches the java.security.SecureRandom and re-uses it for multiple threads. This means it should perform well at high load and concurrency.
Example successive values of prng_random
for int:
Example successive values of prng_random
for gaussian:
Example successive values of prng_random
for uuid:
Presumably you will use this value in a subsequent policy. See the example apiproxy for suggestions.
You do not need to compile the callout to use it. It is usable as is, in pre-built
form. Just grab the jar from this
repository and use it. But
if YOU DO wish to build it, here’s how you can do so:
clone this repo
git clone
configure the build on your machine by loading the Apigee jars into
your local cache. You need to do this once, ever, on the machine doing
the compilation.
./buildsetup.sh
Build with maven.
mvn clean package
if you edit proxy bundles offline, copy the resulting jar file, available in
target/apigee-java-callout-prng-20220802.jar to your apiproxy/resources/java directory. If you
don’t edit proxy bundles offline, upload the jar file into the API Proxy via the Apigee
API Proxy Editor .
include an XML file for the Java callout policy in your
apiproxy/resources/policies directory. It should look as shown above.
use the APigee UI, or a command-line tool like apigeecli or importAndDeploy or similar to
import the proxy into an Apigee organization, and then deploy the proxy .
Eg,
node importAndDeploy.js -v -o ORGNAME -e test -n prng -d bundle
Use a client to generate and send http requests to the example proxy. Eg,
endpoint=https://whatever.api.com
curl -i $endpoint/prng/int
or
curl -i $endpoint/prng/gaussian
or
curl -i $endpoint/prng/uuid
The first two jars must be available on the classpath for the compile to succeed. The
buildsetup.sh script will download these files for you automatically, and will insert
them into your maven cache.
If you want to download them manually:
These jars are produced by Apigee; contact Apigee support to obtain these jars to allow
the compile, or get them here:
https://github.com/apigee/api-platform-samples/tree/master/doc-samples/java-cookbook/lib
There is one callout class, com.google.apigee.callouts.prng.SecureRandomCallout ,
which uses java.security.SecureRandom to generate random values.
This callout is open-source software, and is not a supported part of Apigee Edge.
If you need assistance, you can try inquiring on
the Apigee Community Site. There is no service-level
guarantee for responses to inquiries regarding this callout.
This material is Copyright © 2015-2022 Google LLC.
and is licensed under the Apache 2.0 license. See the LICENSE file.