Java SDK Quick Links

Java Server-side SDK

VerifyAndReadJWT(jwt, secret)

Validates the signature of a JSON Web Token. If successful, it returns the instance id. You will use this for validating and reading the JWT that was provided in the query string of the app component iframe. If you use the Client SDK to do the call to your server, the render JWT will be sent in the request headers.

Parameters:

Name Type Description
jwt String JSON Web Token you want to validate and read.
secret String The render secret, which can be found in the Developer Portal

Example:

ValidateAndReadJwt() example

String renderSecret = "exampleRenderSecret";

// We get the JWT from the Request Headers
String instanceId = AppMarketSDK.VerifyAndReadJWT(instanceJwt, renderSecret);

GenerateJwt(instanceId, secret)

Generates a JSON Web Token with the instance id in the payload and a lifetime of one hour.

Parameters:

Name Type Description
instanceId String The instance id that was read from the render JWT.
secret String The API secret, which can be found in the Developer Portal.

Example:

GenerateJwt() example

String apiSecret = "exampleApiKey";

// We use the instance id from the ValidateAndReadJwt() example
String apiJwt = AppMarketSDK.GenerateJwt(instanceId, apiSecret);

GenerateJwt(secret, payLoad, sAlgothm, issued, expiry)

Generates a JWT outside of AppMarket SDK & using Java’s jsonwebtoken library, based on the provided duration and instance id as specified in the payload. Included below is an example in Groovy. More examples can be found at JWT.IO

Parameters:

Name Type Description
secret String The API secret, which can be found in the Developer Portal.
payLoad Map The payLoad Map having ‘instanceID’ key with value of instance id that was read from the render JWT.
sAlgothm SignatureAlgorithm One of the possible values from io.jsonwebtoken.SignatureAlgorithm. E.g. HS256
issued Date The issue date for the token.
expiry Date The expiry date for the token. Usually one hour from date of issue.

Example:

GenerateJwt() example

import io.jsonwebtoken.Jwts
import io.jsonwebtoken.SignatureAlgorithm
import java.nio.charset.Charset

String secret = "exampleApiKey"
Map payload = [instanceId: "exampleInstanceId"]
def issued = new Date()
def expiry = use(TimeCategory) { issued + 3600.seconds }
def sAlgothm = SignatureAlgorithm.HS256

def token = generateJwt(secret, payload, sAlgothm, issued, expiry)

def generateJwt(String secret, Map payload, SignatureAlgorithm sAlgothm,
                Date issued, Date expiry) {
    byte[] bytes = secret.getBytes(Charset.forName("UTF-8"))
    Jwts.builder()
    .setClaims(payload)
    .setIssuedAt(issued)
    .setExpiration(expiry)
    .setHeaderParam("typ", "JWT")
    .signWith(sAlgothm, bytes)
    .compact()
}

PostContact(jwt, contact)

Post contact.

Parameters:

Name Type Description
jwt String JSON Web Token with instanceId as payload.
contact String JSON serialized contact object.

Example:

PostContactAsync() example

// We use the apiJwt from the GenerateJwt() example. We get contact from the Request Body
AppMarketSDK.PostContact(apiJwt, contact);

GetContacts(jwt, queryString)

Get contacts.

Parameters:

Name Type Description
jwt String The API JWT.
queryString String A String containing all the query parameters. (see example)

Example:

GetContactsAsync()

// Here we insert the Request Params we got through our GET method in a String
String parameters = "scope=" + scope;
parameters += "&offset=" + offset;
parameters += "&limit=" + limit;

// We use the apiJwt from the GenerateJwt() example
String contacts = AppMarketSDK.GetContacts(apiJwt, parameters);

Java Spring: REST example using SDK

import java.io.IOException;
import org.springframework.web.bind.annotation.*;
import com.endurance.appmarket.AppMarketSDK;

@RestController
public class APIController {

    private String apiSecret = "apiSecret";
    private String renderSecret = "renderSecret";

    @RequestMapping(value="/getContacts", method = RequestMethod.GET)
    public String getContacts(
            @RequestHeader(value = "instance-jwt") String instanceJwt,
            @RequestParam("scope") String scope,
            @RequestParam("offset") String offset,
            @RequestParam("limit") int limit
    ) throws IOException {
        String parameters = "scope=" + scope;
        parameters += "&offset=" + offset;
        parameters += "&limit=" + limit;

        String instance = AppMarketSDK.VerifyAndReadJWT(instanceJwt, renderSecret);
        String newJwt = AppMarketSDK.GenerateJwt(instance, apiSecret);

        return AppMarketSDK.GetContacts(newJwt, parameters);
    }

    @RequestMapping(value="/postContact", method = RequestMethod.POST)
    public String postContact(
            @RequestHeader(value = "instance-jwt") String instanceJwt, String contact
    ) throws IOException {
        String instance = AppMarketSDK.VerifyAndReadJWT(instanceJwt, renderSecret);
        String newJwt = AppMarketSDK.GenerateJwt(instance, apiSecret);

        AppMarketSDK.PostContact(newJwt, contact);

        return "Done";
    }
}