httpGet java code not working in Apigee java callout policy

Not applicable

Hi Team,

We want to execute below simple java code in Apigee java callout policy:

java-code.txt

We are doing simple httpget call to one website and attaching response code to header of paramMessageContext.

Above code is working fine on eclipse(in main class),but we are getting below error while trying to run this apigee could through java callout policy.
{"fault":{"detail":{"errorcode":"flow.execution.ExecutionReturnedFailure"},"faultstring":"Execution returned an error result"}}

We have also attached jar file sfdcapigee.zip for your reference.

Kindly advise.

Solved Solved
0 2 694
1 ACCEPTED SOLUTION

Hi

YOU PROBABLY DON'T NEED THIS JAVA CODE.

It is possible to perform an HTTP GET from within a Java callout hosted in Apigee Edge. Before I show you how to do that, I want to remind you that there are two other good ways of performing HTTP GET from Edge, and these ways are much more commonly used:

  • ServiceCallout
  • TargetEndpoint

Those are the mainstream ways to connect to a backend from Edge. They're easier than relying on custom-written Java code. They're well tested. They work. I strongly encourage you to examine those possibilities first.

If for some reason neither of those options are suitable, then, read on.

I put together a working example, with Java code, a pom.xml file, and an example API Proxy bundle that shows how to use the Java callout.

This is the interesting Java code:

    public ExecutionResult execute (final MessageContext msgCtxt,
                                    final ExecutionContext execContext) {
        try {
            String targetUri = "https://www.google.com";
            HttpClient httpclient = HttpClientBuilder.create().build();


            HttpGet request = new HttpGet(targetUri);
            request.addHeader("User-Agent", "Apigee Edge callout");


            HttpResponse  response = httpclient.execute(request);
            int statusCode = response.getStatusLine().getStatusCode();
            msgCtxt.setVariable(varName("status"), statusCode);


            if (statusCode >= 200 && statusCode < 300) {
                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuffer result = new StringBuffer();
                String line = "";
                while ((line = rd.readLine()) != null) {
                    result.append(line);
                }
                msgCtxt.setVariable(varName("content"), result.toString());
            }


            return ExecutionResult.SUCCESS;
        }
        catch (java.lang.Exception exc1) {
            msgCtxt.setVariable(varName("error"), exc1.getMessage());
            msgCtxt.setVariable(varName("stacktrace"), ExceptionUtils.getStackTrace(exc1));
            return ExecutionResult.ABORT;
        }
    }

Find the entire working example attached.

apigee-edge-java-callout-httpget-20160930-1721.zip

View solution in original post

2 REPLIES 2

Hi

YOU PROBABLY DON'T NEED THIS JAVA CODE.

It is possible to perform an HTTP GET from within a Java callout hosted in Apigee Edge. Before I show you how to do that, I want to remind you that there are two other good ways of performing HTTP GET from Edge, and these ways are much more commonly used:

  • ServiceCallout
  • TargetEndpoint

Those are the mainstream ways to connect to a backend from Edge. They're easier than relying on custom-written Java code. They're well tested. They work. I strongly encourage you to examine those possibilities first.

If for some reason neither of those options are suitable, then, read on.

I put together a working example, with Java code, a pom.xml file, and an example API Proxy bundle that shows how to use the Java callout.

This is the interesting Java code:

    public ExecutionResult execute (final MessageContext msgCtxt,
                                    final ExecutionContext execContext) {
        try {
            String targetUri = "https://www.google.com";
            HttpClient httpclient = HttpClientBuilder.create().build();


            HttpGet request = new HttpGet(targetUri);
            request.addHeader("User-Agent", "Apigee Edge callout");


            HttpResponse  response = httpclient.execute(request);
            int statusCode = response.getStatusLine().getStatusCode();
            msgCtxt.setVariable(varName("status"), statusCode);


            if (statusCode >= 200 && statusCode < 300) {
                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuffer result = new StringBuffer();
                String line = "";
                while ((line = rd.readLine()) != null) {
                    result.append(line);
                }
                msgCtxt.setVariable(varName("content"), result.toString());
            }


            return ExecutionResult.SUCCESS;
        }
        catch (java.lang.Exception exc1) {
            msgCtxt.setVariable(varName("error"), exc1.getMessage());
            msgCtxt.setVariable(varName("stacktrace"), ExceptionUtils.getStackTrace(exc1));
            return ExecutionResult.ABORT;
        }
    }

Find the entire working example attached.

apigee-edge-java-callout-httpget-20160930-1721.zip

Thanks a lot Dino.
This is very useful. 🙂