{ Community }
  • Academy
  • Docs
  • Developers
  • Resources
    • Community Articles
    • Apigee on GitHub
    • Code Samples
    • Videos & eBooks
    • Accelerator Methodology
  • Support
  • Ask a Question
  • Spaces
    • Product Announcements
    • General
    • Edge/API Management
    • Developer Portal (Drupal-based)
    • Developer Portal (Integrated)
    • API Design
    • APIM on Istio
    • Extensions
    • Business of APIs
    • Academy/Certification
    • Adapter for Envoy
    • Analytics
    • Events
    • Hybrid
    • Integration (AWS, PCF, Etc.)
    • Microgateway
    • Monetization
    • Private Cloud Deployment
    • 日本語コミュニティ
    • Insights
    • IoT Apigee Link
    • BaaS/Usergrid
    • BaaS Transition/Migration
    • Apigee-127
    • New Customers
    • Topics
    • Questions
    • Articles
    • Ideas
    • Leaderboard
    • Badges
  • Log in
  • Sign up

Get answers, ideas, and support from the Apigee Community

  • Home /
  • Edge/API Management /
avatar image
1
Question by Jacobo Villarreal · May 12, 2017 at 08:08 PM · 467 Views java callouttarget serversjavacallouttarget serverjava-callouttargetserver

Is there a way to pass a target server as a property to a java callout?

I want to be able to pass a target server host and port as properties to a java callout policy. Is there a way to accomplish this?

I'm thinking some usage like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<JavaCallout async="false" continueOnError="false" enabled="true" name="Java-Callout-1">
    <DisplayName>Java Callout-1</DisplayName>
    <Properties>
        <Property name="host" ref="target_server_host"/>
    </Properties>
    <ClassName>com.myorg.callouts.MyCallout</ClassName>
    <ResourceURL>java://my-callouts.jar</ResourceURL>
</JavaCallout>
Comment
Add comment Show 1
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jacobo Villarreal · May 13, 2017 at 02:25 AM 0
Link

I might not have formulated well my question. But what I really want to do is to access an environment target server by its name and maybe pass it to my Java callout as a property (if I cannot access out directly from the Java callout itself)

Close

2 Answers

  • Sort: 
avatar image
0

Answer by sudheendras   · May 12, 2017 at 09:57 PM

Yes.. Any property you pass under <Properties> will be accessible inside your Java class.

public class Callout implements Execution{
   public Callout(Map<String, String> properties){
      //Map containing Host and Port properties configured in the policy.
   }
  ...
}

It's documented here under Samples > Retrieve Property value from Java.

Comment
Add comment Show 2 · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jacobo Villarreal · May 13, 2017 at 02:27 AM 0
Link

Thanks for the response. I might not have formulated well my question. But what I really want to do is to access an environment target server by its name and maybe pass it to my Java callout as a property (if I cannot access it directly from the Java callout itself)

avatar image sudheendras ♦ Jacobo Villarreal   · May 13, 2017 at 02:33 PM 0
Link

Within the Java callout policy you can get your variables directly off the context for instance for e.g.

//target.host, target.ip, target.port etc.
messageContext.getVariable("target.url");

Full list of variables can be found here. I also recommend you read the best practices before investing a lot of time on Java Callout policy. They are discussed here.

avatar image
0

Answer by Dino   · May 12, 2017 at 10:33 PM

yes, you can do it, by implementing in your callout the constructor that accepts a Map<String,String> , as Sudhee stated.

But to reference a variable you cannot use the "ref" attribute. There's no way, in the Java code, to retrieve the attributes present on a Property object. All you get is the string itself. The convention I have adopted is to wrap the value in curly-braces if it is intended to be a variable. And then at runtime, strip the braces and de-reference the variable if appropriate.

You can see a working callout that uses this approach in Edit-Xml-Node. For example, this policy config:

<JavaCallout name='Java-ReplaceXmlNode-1'>
  <Properties>
    <Property name='new-node-type'>text</Property>
    <Property name='new-node-text'>{request.queryparam.texttoinsert}</Property>
    <Property name='xpath'>{request.queryparam.xpath}</Property>
    <Property name='action'>replace</Property>
    <Property name='output-variable'>my_variable</Property>
  </Properties>
  <ClassName>com.dinochiesa.edgecallouts.EditXmlNode</ClassName>
  <ResourceURL>java://edge-custom-edit-xml-node.jar</ResourceURL>
</JavaCallout>

...tells the callout to get the value for "new-node-text" by de-referencing a queryparam called "texttoinsert".

In the Java code, resolving a reference looks like this:

    private static final String variableReferencePatternString = "(.*?)\\{([^\\{\\} ]+?)\\}(.*?)";
    private static final Pattern variableReferencePattern = Pattern.compile(variableReferencePatternString);



    // If the value of a property contains a pair of curlies,
    // eg, {apiproxy.name}, then "resolve" the value by de-referencing
    // the context variable whose name appears between the curlies.
    private String resolvePropertyValue(String spec, MessageContext msgCtxt) {
        Matcher matcher = variableReferencePattern.matcher(spec);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "");
            sb.append(matcher.group(1));
            sb.append((String) msgCtxt.getVariable(matcher.group(2)));
            sb.append(matcher.group(3));
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

It returns an empty string if the variable is not known.

Comment
Add comment Show 1 · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jacobo Villarreal · May 13, 2017 at 02:26 AM 0
Link

Thanks for the response. I might not have formulated well my question. But what I really want to do is to access an environment target server by its name and maybe pass it to my Java callout as a property (if I cannot access it directly from the Java callout itself)

Follow this Question

Answers Answers and Comments

50 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Java Callout Lifecycle 2 Answers

Failed to instantiate the JavaCallout Class com.telstra.authorization.pep.ApigeeCalloutPolicy 3 Answers

Is it possible to have a Java Callout that directly references a jar at the org or env level? 1 Answer

How to handle 301 Moved Permanently (Redirect URL in apigee from target server) 3 Answers

Java Call Out Response 7 Answers

  • Products
    • Edge - APIs
    • Insights - Big Data
    • Plans
  • Developers
    • Overview
    • Documentation
  • Resources
    • Overview
    • Blog
    • Apigee Institute
    • Academy
    • Documentation
  • Company
    • Overview
    • Press
    • Customers
    • Partners
    • Team
    • Events
    • Careers
    • Contact Us
  • Support
    • Support Overview
    • Documentation
    • Status
    • Edge Support Portal
    • Privacy Policy
    • Terms & Conditions
© 2021 Apigee Corp. All rights reserved. - Apigee Community Terms of Use - Powered by AnswerHub
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Create an article
  • Post an idea
  • Spaces
  • Product Announcements
  • General
  • Edge/API Management
  • Developer Portal (Drupal-based)
  • Developer Portal (Integrated)
  • API Design
  • APIM on Istio
  • Extensions
  • Business of APIs
  • Academy/Certification
  • Adapter for Envoy
  • Analytics
  • Events
  • Hybrid
  • Integration (AWS, PCF, Etc.)
  • Microgateway
  • Monetization
  • Private Cloud Deployment
  • 日本語コミュニティ
  • Insights
  • IoT Apigee Link
  • BaaS/Usergrid
  • BaaS Transition/Migration
  • Apigee-127
  • New Customers
  • Explore
  • Topics
  • Questions
  • Articles
  • Ideas
  • Badges