How to make JAX-WS webservice respond with specific http code
Asked Answered
L

2

5

Just like the title says.

    @WebService(
        targetNamespace = "http://com.lalaland.TestWs",
        portName = "TestWs",
        serviceName = "TestWs")
public class TestWs implements TestWsInterface {

    @EJB(name="validator")
    private ValidatorLocal validator;

    @WebMethod(operationName = "getStuff")
    public List<StuffItem> getStuff(@WebParam(name = "aaa")String aaa, 
                        @WebParam(name = "bbb")int bbb )  {

          if ( ! validator.check1(...) ) 
               return HTTP code 403        <------------ Here
          if ( ! validator.check2(...) )
               return HTTP code 404        <------------ Here
          if ( ! validator.check3(...) ) 
               return HTTP code 499        <------------ Here

          return good list of Stuff Items

    }

Is there anyway I can make a method return a specific HTTP code on demand? I know that some of the stuff, like authentication, internal server errors , etc make the the WS method return 500 and auth errors , but I would like to be able to send these in accordance with by business logic.

Anyone done this before? Been using jax-WS for some time and this was the first time I had this need, tried searching for it and couldn't find an answer anywhere.

Thanks

Larondalarosa answered 10/10, 2013 at 13:54 Comment(3)
Just for completeness sake , I already tried : throw new javax.xml.ws.http.HTTPException(123) and it didn't workLarondalarosa
which one are you using: metro or cxf?Abram
@Chechus this is being deployed on Glassfish 3 , so metro, forgot to mention this and just placed the glassfish-3 tagLarondalarosa
E
12

Only get the current instance of javax.servlet.http.HttpServletResponse and sends the error.

@WebService
public class Test {

    private static final Logger LOG = Logger.getLogger(Test.class.getName());

    @Resource
    private WebServiceContext context;

    @WebMethod(operationName = "testCode")
    public String testCode(@WebParam(name = "code") int code) {
        if (code < 200 || code > 299) {
            try {
                MessageContext ctx = context.getMessageContext();
                HttpServletResponse response = (HttpServletResponse) 
                        ctx.get(MessageContext.SERVLET_RESPONSE);
                response.sendError(code, code + " You want it!");
            } catch (IOException e) {
                LOG.severe("Never happens, or yes?");
            }
        }
        return code + " Everything is fine!";
    }

}

See also List of HTTP status codes - Wikipedia, the free encyclopedia

Egidio answered 10/10, 2013 at 16:29 Comment(0)
A
2

Try this:

Create a SoapHandler like this: http://www.mkyong.com/webservices/jax-ws/jax-ws-soap-handler-in-server-side/ implementing the interface: Handler.handleResponse();

then, inside the handler you are avalaible to modify as you like the http headers, so you can add something like: http://download.java.net/jdk7/archive/b123/docs/api/javax/xml/ws/handler/MessageContext.html

Where you can use the: HTTP_RESPONSE_CODE as you want.

Other resource: http://docs.oracle.com/cd/E14571_01/web.1111/e13735/handlers.htm

Tip: think on soaphandlers as interceptors for soap messages

Abram answered 10/10, 2013 at 16:13 Comment(3)
Testing it now. Will let you know in a minute or twoLarondalarosa
by Handler.handleResponse(); you mean handleMessage() ? Can't find the signature for handleResponse.Larondalarosa
you will find how to do it in the oracle tutorial, but in fact the Paul response is pretty cleaner, try it if you just need change http response codes.Abram

© 2022 - 2024 — McMap. All rights reserved.