Set attrubute to WebScriptRequest, or cast it to a class which allows setting attribute to it

cancel
Showing results for 
Search instead for 
Did you mean: 
raghav
Member II

Set attrubute to WebScriptRequest, or cast it to a class which allows setting attribute to it

My question:

How to set attributes in the WebScriptRequest, or cast WebScriptRequest to another class which allows setting attributes to it? 

My approach:

The issue is, WebScriptRequest does not have set methods. In order to overcome this, I attempted to cast WebScriptRequest to HttpServletRequest. Next in the HttpServletRequest, I set my attribute.

I am attempting code as the one below (which compiles, but throws the runtime error):

public class MyWebScript extends AAbstractWebScript{

public void execute(WebScriptRequest request, WebScriptResponse response) throws IOException {

      HttpServletRequest httpReq = ((WebScriptServletRequest) request).getHttpServletRequest();
      httpReq.setAttribute("name", "John");

}

The issue is: 

With the approach above, the code compiles, but I get runtime error as below:

org.alfresco.repo.web.scripts.BufferedRequest cannot be cast to org.springframework.extensions.webscripts.servlet.WebScriptServletRequest

2 Replies
krutik_jayswal
Senior Member II

Re: Set attrubute to WebScriptRequest, or cast it to a class which allows setting attribute to it

There are few basic rule/need when we do casting of an object in javaIn your case it does not satisfy those.For more details of it you can check below link.

Casting variables in Java - Stack Overflow 

You just mentioned that you would like to cast an WebScriptServletRequest in to HttpServletRequest , can you tell me the reason behind this.If not what is the exact requirement which you would like to achieve.If possible both.

lancestine
Customer

Re: Set attrubute to WebScriptRequest, or cast it to a class which allows setting attribute to it

You can get by the casting error like this:

 

WebScriptRequest webScriptRequest = ((WrappingWebScriptRequest) request).getNext();
WebScriptServletRequest webScriptServletRequest = (WebScriptServletRequest) webScriptRequest;