How to Access Custom Attributes using Java

Not applicable

I want to set ID as custom Attributes and access this Attributes using java.

curl -H "Authorization: Bearer wzi34Md0Q2R4GLJrnA72GuysG4Co" 'http://{my-org}-test.apigee.net/v1/checkStatus?ID=5666666665649cb89e4'

But not able to access custom Attributes using java.

Is there any way to achieve my objective using 'Apigee API'?

Any help or hint will be appreciated.

1 15 1,455
15 REPLIES 15

Hi @Rakesh Jain,

I understand from your question that you need a place holder for your ID value so that you can use it later in Java callout policy. Please correct me if I am wrong. Is there a particular reason you are using custom Attributes? I am not sure if you really need custom Attribute to achieve the above. There are other options -

  1. Key - value map - If you need the ID across different transactions.
  2. Simple context variable - Extract the ID query param and save as a context variable. This context variable can be read within JAVA callout policy. This context variable can be used within teh current transaction.

Hi @Abhishek Subramanya, I have multiple api's which access different customers using Id. so we create customer vise API Product and and assign customer Id as custom attribute to api product, and access api product using access_token. so i want to access custom attributes.

OK @Rakesh Jain, then you should have the variable created when Oauth policies are invoked. Just extract them as @Dino mentioned, you should be fine.

It sounds to me that you are storing some custom attributes on an OAuth token, and then you'd like to access those custom attribtues atfer having verified the oauth token. Is that right?

If so, it's very simple. When you call OAuthV2/VerifyAccessToken on a valid token, the policy implicitly sets context variables that contain the values of the custom attributes stored on the token. The name of the variable is "accesstoken.CustomAttributeName where you must replace CustomAttributeName with the name of your particular attribute.

At that point, retrieving the custom attribute within a Java callout is a simple matter of calling

String v = (String) messageContext.getVariable("accesstoken.CustomAttributeName");

You will need appropriate error handling around that.

hi @Dino

Thanks,

Am doing the same thing as you describe

def myMethod = Action.async { implicit request =>
 //  some code
 }

How should I access messageContext from my method using request obj?

@Rakesh Jain

Please explore the Class - MessageContext. There are a number of methods & sub classes which you can use.

Ex: messageContext.getRequestMessage() , messageContext.getResponseMessage()

You can refer to the sample in apigee docs page -

http://apigee.com/docs/api-services/cookbook/use-java-customize-api

@Dino , @Abhishek Subramanya Thanks. It's working.

Great. @Rakesh Jain.

Hi @Rakesh Jain,

You can achieve this in two way.

1. As you are using Oauth and tokens over API's, you can use variable "apiproduct.attributeName".

attributeName can be ID in your case. This variable will contain the customer ID that you stored in each product. You can use this value in JAVA callout as:

messageContext.getVariable("apiproduct.attributeName");

2.You can also access the attribute value using management call:

https://api.enterprise.apigee.com/v1/organizations/{orgname}/apiproducts/{productName}/attributes/{a...

For more operation on product refer this.

Hi @Sonali Thanks

Am doing the same thing as you describe

def myMethod = Action.async { implicit request =>
 //  some code
 }

How should I access messageContext from my method using request obj?

You can do this using message-flow and expressions library of apigee for java.

message-flow library has MessageContext class, using its object you can access variables.

@Sonali Thanks.

Am not getting any Dependencies. If you know any link for jar or dependencies please share. It will be helpful for me.

@Rakesh Jain, Here you will find required dependencies with sample example.

@Sonali Thanks.

Hi @Rakesh Jain,

You can achieve this in two way.

1. As you are using Oauth and tokens over API's, you can use variable "apiproduct.attributeName".

attributeName can be ID in your case. This variable will contain the customer ID that you stored in each product. You can use this value in JAVA callout as:

messageContext.getVariable("apiproduct.attributeName");

2.You can also access the attribute value using management call:

https://api.enterprise.apigee.com/v1/organizations/{orgname}/apiproducts/{productName}/attributes/{a...

For more operation on product refer this.