begi
begi

Reputation: 19

Java Is it possible to send a httpServletRequest to a function?

i got a function that looks like this:

@GET
@Path("/execute/{scriptId}")
public String execute(@Context HttpServletRequest req, @PathParam("scriptId") Long scriptId) {

/* ...  */
        engine.eval(getSrc(req.getServletContext().getRealPath("js/boot.js")));

        if (scriptId == 1L)
                engine.eval(getSrc(req.getServletContext().getRealPath("js/test.js")));
        else
                engine.eval(getSrc(req.getServletContext().getRealPath("js/test2.js")));

/* that above, its the only place i need the req  */

}

i call it from a html page...

<a href="rest/dss/execute/1">execute 1</a>

and it works fine...

now...i made a timer....and in the timer i need to call that function, but i have no idea how to get the httpservletrequest parameter for the function...

here is the code:

@Timeout
public void execute(Timer timer) {
    Long scriptId = Long.parseLong(timer.getInfo().toString());
    execute(/*here i need something*/, scriptId);

    System.out.println("Timer Service : " + scriptId);
    System.out.println("Current Time : " + new Date());
    System.out.println("Next Timeout : " + timer.getNextTimeout());
    System.out.println("Time Remaining : " + timer.getTimeRemaining());
    System.out.println("____________________________________________");

}

so, basically, i need to call that function with the timer...

any ideas?

Upvotes: 1

Views: 565

Answers (2)

Thomas Kl&#228;ger
Thomas Kl&#228;ger

Reputation: 21570

If your function doesn't need the HttpServletRequest (i.e. it doesn't need to call methods on the HttpServletRequest) then you can extract your existing code into an implementation method that does not depend on an HttpServletRequest and in your execute method call that implementation:

@GET
@Path("/execute/{scriptId}")
public String execute(@Context HttpServletRequest req, @PathParam("scriptId") Long scriptId) {
    return executeImpl(scriptId);
}

public String executeImpl(Long scriptId) {
    ...// your current implementation
}

And then your timer can also call that method:

@Timeout
public void execute(Timer timer) {
    Long scriptId = Long.parseLong(timer.getInfo().toString());
    executeImpl(scriptId);

    System.out.println("Timer Service : " + scriptId);
    System.out.println("Current Time : " + new Date());
    System.out.println("Next Timeout : " + timer.getNextTimeout());
    System.out.println("Time Remaining : " + timer.getTimeRemaining());
    System.out.println("____________________________________________");

}

Upvotes: 1

jwenting
jwenting

Reputation: 5663

Sure, it's just an interface which you can implement.

Of course implementing it to do something useful may not be trivial, depending on what you're doing with the request in the other method.

Getting a ready implementation of an HttpServletRequest from some 3rd party library that implements the JEE standard might help, but may well be overkill.

Upvotes: 0

Related Questions