Announcement Announcement Module
Collapse
No announcement yet.
Returning HTTP codes from Web Service Outbound Gateway Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Returning HTTP codes from Web Service Outbound Gateway

    Hello,

    I have implemented the Ticket example in chapter 14 of the Pro Spring Integration Book published by Apress (see output below), and wonder how I can return HTTP error codes (200, 401, 500...) when the provided data are invalid. Is there a class you can wrap the response in to return an error code?

    Many thanks.

    Philroc



    ------------------------------------------- Request --------------------------------------------------
    <TestRequest xmlns="http://prospringintegration.com/tk/schemas"><number>12345678</number><email>[email protected]</email></TestRequest>
    ----------------0-------------------------- Response -------------------------------------------------
    <?xml version="1.0" encoding="UTF-8"?><TestResponse xmlns="http://prospringintegration.com/tk/schemas"><Test><number>12345678</number><email>[email protected]</email><issueDateTime>Sun Apr 19 10:29:57 CEST 63121378</issueDateTime></Test></TestResponse>

  • #2
    Sorry, but it's not clear what you are asking. The outbound gateway is an HTTP client, it's the server that returns those codes; the client doesn't generate them.

    The StatusCode returned by the server is provided in a header in the response message...

    Code:
    replyBuilder.setHeader(org.springframework.integration.http.HttpHeaders.STATUS_CODE, httpResponse.getStatusCode());
    where the header name is "http_statusCode".

    Comment


    • #3
      I meant an inbound gateway.

      Let me explain my problem better.

      I have set up a Spring Integration with an inbound gateway, a channel, and a service activator:

      <ws:inbound-gateway id="wsInboundGateway"
      request-channel="inboundDOMTicketRequest" />

      <int:channel id="inboundDOMTicketRequest" />

      <int:service-activator input-channel="inboundDOMTicketRequest"
      ref="ticketIssuerEndpoint" />
      When a user sends a ticket using an outbound gateway,

      <TicketRequest xmlns="http://prospringintegration.com/tk/schemas"><number>12345678</number><email>[email protected]</email></TicketRequest>
      the ticket goes through the server's inbound-gateway, the channel, and is processed by the ticketIssuerEndpoint which returns this

      <?xml version="1.0" encoding="UTF-8"?><TicketResponse xmlns="http://prospringintegration.com/tk/schemas"><Ticket><number>12345678</number><email>[email protected]</email><issueDateTime>Sun Apr 19 10:29:57 CEST 63121378</issueDateTime></Ticket></TicketResponse>

      Here's the ticketIssuerEndpoint's code:

      @Component
      public class TicketIssuerEndpoint {

      private String replyTemplate =
      "<TicketResponse xmlns=\""
      + CommonConfiguration.NAMESPACE + "\">"
      + "<Ticket>"
      + "<number>%s</number>"
      + "<email>%s</email>"
      + "<issueDateTime>%tc</issueDateTime>"
      + "</Ticket>"
      + "</TicketResponse>";

      @ServiceActivator
      public Source handleRequest(DOMSource source) throws Exception {

      NodeList nodeList = source.getNode().getChildNodes();
      String number = "";
      String email = "";

      for (int i = 0; i < nodeList.getLength(); ++i) {
      Node node = nodeList.item(i);
      if (node.getNodeName().equals("number")) {
      number = node.getFirstChild().getNodeValue();
      } else if (node.getNodeName().equals("email")) {
      email = node.getFirstChild().getNodeValue();
      }
      } // end for

      String xml = String.format(replyTemplate, number, email,
      new Random().nextLong() * 1000, new Date());

      return new DomSourceFactory().createSource(xml);

      } // end handleRequest

      } // end class

      My boss want the HTTP server to return one of several codes based on the incoming ticket's values:

      - 200 if the ticket number is valid
      - 500 if an error occurred on the server
      - 401 if the email was not found (in a database)
      - etc.

      How do I add one of the above codes to the message returned by the server?

      Many thanks.

      Philroc

      Comment


      • #4
        When using the ws inbound gateway, all the heavy SOAP lifting is delegated to the Spring-WS components; when you throw an exception, a SOAP fault is returned to the client with status code 500 (internal server error); otherwise you get a 200 OK.

        You can manipulate the SOAP fault using an exception resolver ... http://static.springsource.org/sprin...ption-resolver

        You could also add a servlet filter to interrogate the fault and change the response status code.

        If you don't really need the sophistication of the full Spring Web Services stack, you could use an http inbound adapter instead (and an unmarshalling transformer to get the XML into a DOM - and a marshalling transformer to get back to XML for your response.

        That way, you can simply set any status code you want by setting the status code header in the reply message.

        Comment


        • #5
          Hi,

          I have create an http:inbound-gateway whose GatewayHandler code is as follows:

          @Component
          public class GatewayHandler {

          @ServiceActivator
          public Message<?> handleMessage(Message<?> message) {
          System.out.println("Http Gateway message: " + message);

          Message<String> helloMessage =
          MessageBuilder.withPayload("xxx").setHeader("Statu s-Code", "666").build();
          return helloMessage;

          }

          } // end class
          When I watch my network with TCPDUMP, the server returns the following:

          HTTP/1.1 200 OK
          Content-Type: text/html
          Content-Length: 3
          ....
          xxx
          The Status-Code wasn't changed to 666

          Comment


          • #6
            Try replacing "Status-Code" with HttpHeaders.STATUS_CODE (from org.springframework.integration.http.HttpHeaders).

            Comment


            • #7
              Message<String> message =
              MessageBuilder.withPayload("xxx").setHeader(HttpHe aders.STATUS_CODE, 403).build();
              return message;
              It worked!

              Many thanks.

              Comment

              Working...
              X