Error on getting response from HttpClient using Jetty 9

924 Views Asked by At

I want to get content from an external server using Jetty and HttpClient class. Now I want to copy the obtained response as HttpServletResponse and return to client (Browser). I've tried something but cannot succeed. I think I am not getting through with inner class.

Here is my code:

public void getContentsTo(String uri,HttpServletRequest request,final HttpServletResponse response){
        HttpClient httpClient=new HttpClient();
        httpClient.newRequest(uri).send( new org.eclipse.jetty.client.api.Response.CompleteListener() {
            @Override
            public void onComplete(Result result) {
                response=(HttpServletResponse)result.getResponse();
            };
        });

The error is in assigning response as:

The final local variable response cannot be assigned, since it is defined in an enclosing type

If I dont use final for the parameter there is also a error as ,

Cannot refer to a non-final variable response inside an inner class defined in a different method

What is the cause?

2

There are 2 best solutions below

1
user On

You have a final (!) variable in the params and you try to change it in the code. That could be the cause of the error.

Why is it final? Remove this identifier and try again.

Ok, I would try to do like that.

If you don't use the response parameter in the code, as I can see, why don't you declare it inside the inner class as an instance variable?

2
jesse mcconnell On

The result.getResponse() response is a org.eclipse.jetty.client.HttpResponse which does not have HttpServletResponse anywhere in its lineage. If I follow what you are trying to do you will need to copy out the information in the Result you getting and put it into your HttpServletResponse object.