getting HttpServletRequest and HttpServletResponse from within an advice

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

getting HttpServletRequest and HttpServletResponse from within an advice

Floyd Shackelford

Greetings.

Here's my endpoint:

@Path("/foo")
@Produces(MediaType.TEXT_PLAIN)
public class Foo {

@GET
@Path("/bar")
  public String bar() {
    return "hello world"
  }
}

How do I get HttpServletRequest, HttpServletResponse, etc. from within my advice without changing bar's signature?

Regards,
Shackman
_______________________________________________
aspectj-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/aspectj-users
Reply | Threaded
Open this post in threaded view
|

Re: getting HttpServletRequest and HttpServletResponse from within an advice

Frank Pavageau-2
Hi.

2015-12-11 13:13 GMT+01:00 Floyd Shackelford <[hidden email]>
@Path("/foo")
@Produces(MediaType.TEXT_PLAIN)
public class Foo {

@GET
@Path("/bar")
  public String bar() {
    return "hello world"
  }
}

How do I get HttpServletRequest, HttpServletResponse, etc. from within my advice without changing bar's signature?

I think that by using an ITD, you can introduce fields of those types annotated with @Context in Foo, which would then get injected by JAX-RS (as long as everything in your "etc." is part of the context). It's been a while since I last used ITDs, so I'm not sure if the fields are actually added to Foo directly; if they aren't, it won't work, of course.

Regards,
Frank

_______________________________________________
aspectj-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/aspectj-users
Reply | Threaded
Open this post in threaded view
|

Re: getting HttpServletRequest and HttpServletResponse from within an advice

Floyd Shackelford

 

December 11, 2015 2:25 PM from Frank Pavageau

    |

    |  I think that by using an ITD …

    |

 

ITD works. For the benefit of others, here’s a sample of how to get HttpServlet*’s for the active servlet:

 

public interface IServletContext {

     HttpServletRequest  _getRequest();

     HttpServletResponse _getResponse();

}

 

public aspect Foo perthis(endPointsNoArgs(IServletContext)){

 

  declare parents: (@javax.ws.rs.Path *) implements IServletContext;

 

  @Context private HttpServletRequest IServletContext._request;

    

  public HttpServletRequest IServletContext._getRequest() {

    return _request;

  };

 

  @Context private HttpServletResponse IServletContext._response;

 

  public HttpServletResponse IServletContext._getResponse() {

    return _response;

  };

 

  pointcut endPointsNoArgs(IServletContext servletContext):

    ( execution (@javax.ws.rs.GET * *.*()) ||

      execution (@javax.ws.rs.PUT * *.*()) ||

      execution (@javax.ws.rs.POST * *.*()) ||

      execution (@javax.ws.rs.DELETE * *.*()) ) &&

    target(servletContext);

 

  Object around(IServletContext servletContext): endPointsNoArgs(servletContext) {

    HttpServletRequest request = servletContext._getRequest();

    HttpServletResponse response = servletContext._getResponse();

    // you can use request and response here

    Object result = proceed(servletContext);

    // and here

    return result;

  }

}

 

All Izz Well

Regards,
Shackman


_______________________________________________
aspectj-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/aspectj-users