2

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?

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
progrrammer
  • 4,475
  • 2
  • 30
  • 38

2 Answers2

1

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.

jesse mcconnell
  • 7,102
  • 1
  • 22
  • 33
  • Ok! the error is not on typecasting, I think error is on setting outerclass variable from innerclass. – progrrammer May 21 '13 at 13:55
  • sure, but it is final, you can not _replace_ the object like you are showing..for that reason _and_ that they are completely different object types. – jesse mcconnell May 21 '13 at 13:59
0

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?

user
  • 3,058
  • 23
  • 45
  • final in the code was suggested by my first error. Error is as:"Cannot refer to a non-final variable response inside an inner class defined in a different method" – progrrammer May 21 '13 at 13:42