Issue reading multiline string as Java Callout property

I am facing an issue with the Java Callout in Apigee. The problem occurs when I'm attempting to read a multiline string property (public_key) from the message context in my Java Callout.
Here's the code snippet in my Java class where I read the variables:
String publicKeyPem = (String) messageContext.getVariable("public_key");
String passphrase = (String) messageContext.getVariable("passphrase");

The policy XML where I set these variables is as follows:
<JavaCallout name='RsaCalloutEncryption'>
<ClassName>com.apihub.callout.RsaCallout</ClassName>
<ResourceURL>java://RsaCallout.jar</ResourceURL>
<Properties>
<Property name='public_key'>
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAr0cT3MFELFPMfSMjCh9T
7nc2lSpfTMSEXiIV+bL7tmP+jBpFdE6R6x0imRFifqT+1okn/OnnNh9ZVHYuAlWX
YKcPHpGe+ZdBtPL8zK1ux8Mspk6FBFfprlE2hr+kWvZeQaDUkQSmFSkLxfoK5DWi
Q5F6MP2h3NmZ13Zd+Z4b3zmg6hHR23TViZr+z+cr9brhAZLrkMSzmMq43bqyuzYc
56OZe/G58PQAIhqEAwFvZ1nlf4INet8dfEERZkNuyrlM4jNms/IeR65WCeZpTTB2
LulbQ7cZRkyV8sxTHoBP1KRXJ8Qs0fC+pPfbFTrj7QPpuqvUN7yjcRYNob8SblZs
UwIDAQAB
-----END PUBLIC KEY-----
</Property>
<Property name='passphrase'>APIGEE123456</Property>
</Properties>
</JavaCallout>

While the passphrase property is being read correctly, the public_key property is coming up as empty when I try to read it in my Java code.
Is there a specific way to handle multiline string properties in Java Callouts? Any help or suggestions would be greatly appreciated.

Solved Solved
1 1 299
1 ACCEPTED SOLUTION

In my experience "it just works" to retrieve a property value that is a multi-line string. 

My Java code is like this: 

  private String getStringSetting(MessageContext msgCtxt) throws IllegalStateException {
    String value = (String) this.properties.get("string-setting");
    if (value == null || value.equals("")) {
      return STRING_SETTING_DEFAULT;
    }
    value = resolveVariableReferences(value, msgCtxt);
    if (value == null || value.equals("")) {
      // Could also return default here, but the thinking is,
      // if someone provided a string and it resolves to empty, that's a bad thing.
      throw new IllegalStateException("value resolves to null or empty.");
    }
    return value;
  }

That resolveVariableReferences method... is just  way to resolve variables specified within curly braces. But it should have no impact on the ability to retrieve a multi-line value. In my experience when I specify the policy config like this :

<JavaCallout name='JavaCallout-Example'>
  <Properties>
    <Property name="integer-setting">176</Property>
    <Property name="string-setting">
      This is a multiline string.
      This is line 2.
      Each line is preceded by spaces.
    </Property>
  </Properties>
  <ClassName>com.google.apigee.callout.MultilinePropertyExampleCallout</ClassName>
  <ResourceURL>java://Apigee-read-multiline-property-20230522.jar</ResourceURL>
</JavaCallout>

...it just works and my proxy gets that multiline string.  If you indent (add whitespace before each line), then you will need to deal with that in your Java code before interpreting the string value. 

 

View solution in original post

1 REPLY 1

In my experience "it just works" to retrieve a property value that is a multi-line string. 

My Java code is like this: 

  private String getStringSetting(MessageContext msgCtxt) throws IllegalStateException {
    String value = (String) this.properties.get("string-setting");
    if (value == null || value.equals("")) {
      return STRING_SETTING_DEFAULT;
    }
    value = resolveVariableReferences(value, msgCtxt);
    if (value == null || value.equals("")) {
      // Could also return default here, but the thinking is,
      // if someone provided a string and it resolves to empty, that's a bad thing.
      throw new IllegalStateException("value resolves to null or empty.");
    }
    return value;
  }

That resolveVariableReferences method... is just  way to resolve variables specified within curly braces. But it should have no impact on the ability to retrieve a multi-line value. In my experience when I specify the policy config like this :

<JavaCallout name='JavaCallout-Example'>
  <Properties>
    <Property name="integer-setting">176</Property>
    <Property name="string-setting">
      This is a multiline string.
      This is line 2.
      Each line is preceded by spaces.
    </Property>
  </Properties>
  <ClassName>com.google.apigee.callout.MultilinePropertyExampleCallout</ClassName>
  <ResourceURL>java://Apigee-read-multiline-property-20230522.jar</ResourceURL>
</JavaCallout>

...it just works and my proxy gets that multiline string.  If you indent (add whitespace before each line), then you will need to deal with that in your Java code before interpreting the string value.