5 Replies Latest reply on Jul 31, 2012 11:22 PM by joe_boy12

    Response over HTTP

    Friedrich Sicher Newbie

      How to send the response from a service over http?

       

      I send a request over http with username and password. In the action-Method I
      check username and password and have to send 'true' odr 'false' as String back.

       

      Here is the Action-Class:

       

      package demo;
      
      import java.util.Map;
      import org.jboss.soa.esb.actions.AbstractActionPipelineProcessor;
       import org.jboss.soa.esb.actions.ActionProcessingException;
       import org.jboss.soa.esb.helpers.ConfigTree;
       import org.jboss.soa.esb.http.HttpRequest;
       import org.jboss.soa.esb.message.Message;
       import org.jboss.soa.esb.message.format.MessageFactory;
      
      
      
      public class LoginHandler extends AbstractActionPipelineProcessor{
           private ConfigTree configTree;
      
      
      
          public LoginHandler(ConfigTree configTree) {
               this.configTree = configTree;
           }
      
      
      
          public Message process(Message message) throws ActionProcessingException {
               
               String answer = "login failed";
               
               HttpRequest req = HttpRequest.getRequest(message);
               Map<String, String[]> params = req.getQueryParams();
               
               if (params != null) {
                   String user = params.get("user")[0];
                   String password = params.get("pwd")[0];
      
      
      
                  System.out.println("User: " + user + ", Password: " + password);
                   
                   
                   if (user.equals("joe") && pwd.equals("secret")){
                       answer = "login successfully";
                   }
                   
               } else {
                   System.out.println("ParameterMap is null!");
               }
               
               /* for demo */
               Message responseMessage = MessageFactory.getInstance().getMessage();
               responseMessage.getBody().add(answer);
               
               return responseMessage;
           }
       }
      
      

       

       

      And here is the jboss-esb.xml

       

      <?xml version="1.0"?>
       <jbossesb parameterReloadSecs="5"
        xmlns="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.2.0.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.2.0.xsd http://anonsvn.jboss.org/repos/labs/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.2.0.xsd">
        <services>
         <service category="DemoServices" description=""
          invmScope="GLOBAL" name="Login">
          <listeners>
           <http-gateway name="Http"/>
          </listeners>
          <actions mep="RequestResponse">
           <action
            name="LoginAction" process="process"/>
          </actions>
         </service>
        </services>
       </jbossesb>
      

       

       

      I tried to call the service from browser:

       

      http://localhost:8080/logintest/http/DemoServices/Login?user=joe&pwd=secret
      

       

      I see the output:

       

      15:19:29,037 INFO  [STDOUT] User: joe, Password: secret
      

       

      following with error message:

       

      15:19:29,037 WARN  [ActionProcessingPipeline] No response message for RequestResponse mep! To: InVMEpr [ PortReference < <wsa:Address invm://4461746168616e64656c53657276696365732424242424242424242424244461746152657175657374/false?false#10000/>, <wsa:ReferenceProperties jbossesb:passByValue : false/> > ] ReplyTo: InVMEpr [ PortReference < <wsa:Address invm://thread-137-43/>, <wsa:ReferenceProperties jbossesb:passByValue : false/>, <wsa:ReferenceProperties jbossesb:temporaryEPR : true/> > ] MessageID: 6330ee62-1112-4b77-81c8-0b1eb545449d
      15:19:59,036 INFO  [ServiceInvoker] Unresponsive EPR: InVMEpr [ PortReference < <wsa:Address invm://4461746168616e64656c53657276696365732424242424242424242424244461746152657175657374/false?false#10000/>, <wsa:ReferenceProperties jbossesb:passByValue : false/> > ] for message: header: [  ]
      15:19:59,036 INFO  [ServiceInvoker] Delivering message [header: [  ]] to DLQ.
      15:19:59,121 ERROR [[Http]] Servlet.service() for servlet Http threw exception
      

       

      I tried to call the same service from a simple java program:

      URL url = new URL("http://localhost:8080/logintest/http/DemoServices/Login?user=joe&pwd=secret");
      URLConnection connection = url.openConnection();
      connection.setDoOutput(true);
      
      PrintStream outStream = new PrintStream(connection.getOutputStream());
      outStream.println("string=" + "test");
      outStream.close();
      
      DataInputStream inStream = new DataInputStream(connection.getInputStream());
      
      // ...
      

       

      I see username and password, and short time after that I get the error message:

       

       

      Server returned HTTP response code: 500 for URL: http://localhost:8080/logintest/http/DemoServices/Login?user=joe&pwd=secret
      

       

       

      What have I to do, to get response as a String? How to set configuration in jboss-esb.xml for 'response message'?

        • 1. Re: Response over HTTP
          Scott Dawson Apprentice

          Friedrich,

          Use an HttpResponse object in your action class:

           

          HttpResponse responseInfo = new HttpResponse(HttpServletResponse.SC_OK);
          responseInfo.setContentType("text/xml");
          // Set other response info ...
          // Set the HttpResponse instance on the ESB response Message instance
          responseInfo.setResponse(responseMessage);
          

           

          You can find more information in the Programmer's Guide or the javadoc.

           

          Regards,

          Scott

          • 2. Re: Response over HTTP
            Friedrich Sicher Newbie

            Thanks, Scot. Now it works.

            • 3. Re: Response over HTTP
              Ju Lee Newbie

              Hi Friedrich,

               

              I've changed action class as below but it still does not work. Could you give me your example ?

              public HttpResponse process(Message message) throws ActionProcessingException {
                      
                       String answer = "login failed";
                       ...............


                       /* for demo */
                      

                                  HttpResponse responseInfo = new HttpResponse(HttpServletResponse.SC_OK);

                                  responseInfo.setContentType("text/xml");

                                  responseInfo.setResponse(responseMessage);


                       return responseInfo;
                   }

               

              Thanks,

              DuLee

              • 4. Re: Response over HTTP
                Tom Cunningham Master

                DuLee, did you check out the Programmer's Guide link that Scott posted?    It'd be helpful to see your jboss-esb.xml as well to determine whether you have the listener set up correctly.

                • 5. Re: Response over HTTP
                  joe_boy12 Novice

                  what happens when do you like this in your action class? instead of responseMessage use original message.

                   

                           message.getBody().add(answer);
                          
                           return message;