0

I want to see the request to the web page in an Interceptor I wrote. I will change my response value according to some values in the incoming request.I'm gonna use something like that ;

String ex = request.getHeader("GET");
if(ex.contains("addHeader("a","example")"));
     response.setHeader("a","null");

Here is my index.ft:

Your name: <@s.url value="${name}"/>
Enter your name here:<br/>
<form action="" method="get">
<input type="text" name="name" value="" />
<input type="submit" value="Submit" />
</form>

Here is part of my TestInterceptor.java class;

public class TestInterceptor implements Interceptor {
....
@Override
public String intercept(ActionInvocation ai) throws Exception {
    System.out.println("before");

    //the area where I want to write the codes I want to use above
    // I can't to reach request.getHeader(...) function in here
    String result = ai.invoke();
    System.out.println("after");
    return result;
}

What's the solituon or another way to use that functions. Thanks for helping. Note : I'm using Struts framework

kadir
  • 23
  • 4
  • FWIW: this kind of question is almost *always* self-answerable with a tiny bit of research. Spending a few minutes with the Javadocs would lead down the path to the answer. – Dave Newton Sep 06 '19 at 12:52
  • I'm equally tired of people that don't follow the Javadocs (which lead to the answer in 2-4 clicks) or cracking open an existing interceptor to just *look* at it. Here's the thing: you *can* solve it by following the trail of the docs and the code. This is pretty much one of like three skills every developer needs: the ability to follow trails. (The other two being regex and recursion.) – Dave Newton Sep 06 '19 at 13:17
  • Also, in addition to neglecting due diligence in reading Javadocs and/or code, searching the web for "struts 2 interceptor access request headers" leads to multiple answers, including on SO, which makes the question a dupe anyway. So four skills every developer needs, adding "searching the web". – Dave Newton Sep 06 '19 at 13:21
  • you are exactly right :)) walk to work – kadir Sep 06 '19 at 13:26
  • Bike for me ;) Please don't interpret my comments negatively--they're intended to help, not hurt. I'll also update some of the S2 docs to include this information more directly and clearly. – Dave Newton Sep 06 '19 at 13:42

2 Answers2

1

You can get it from ActionContext

ActionContext context = ai.getInvocationContext();
HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
TruckDriver
  • 1,383
  • 13
  • 28
0

You need to modifty your request before the request HTTP is triggerd (e.g before the Action has executed and before the Result is executed).

PreResultListener allows us to do just that. Your TestInterceptor should implement PreResultListener and provides implementation of the beforeResult() method. In this method, we get the HttpServletResponse object from the ActionContext and add custom logic to it.

For your case : modifying header value

The TestInterceptor registers itself with the ActionInvocation in the before method, which gets a callback before the result is executed.

public class TestInterceptor extends AbstractInterceptor implements  PreResultListener {

@Override
public String intercept(ActionInvocation invocation) throws Exception {
  before(invocation);
    return invocation.invoke();
}

private void before(ActionInvocation invocation) {
  invocation.addPreResultListener(this);
}

private void modifyHeader(Object action, HttpServletResponse response) {
  response.addHeader("myHeader", "myValue");
}

public void beforeResult(ActionInvocation invocation, String resultCode) {
  ActionContext ac = invocation.getInvocationContext();
  HttpServletResponse response = (HttpServletResponse) ac.get(StrutsStatics.HTTP_RESPONSE);
  modifyHeader(invocation.getAction(), response);  
}
}
Hassam Abdelillah
  • 2,246
  • 3
  • 16
  • 37