cancel
Showing results for 
Search instead for 
Did you mean: 

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

raghav
Champ in-the-making
Champ in-the-making

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 2

krutik_jayswal
Elite Collaborator
Elite Collaborator

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.

You can get by the casting error like this:

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

Explore our Alfresco products with the links below. Use labels to filter content by product module.