3 Replies Latest reply on Feb 25, 2008 2:17 PM by Daniel Bevenius

    jbr-provider and http response codes

    Mikael Larsson Newbie

      Hi!

      I'm using jbr-provider and I would like to control the HTTP response code.
      Is there something I can set on the message that will result in an HTTP 500 response?

      I've found that I can control the returned content type be doing something like:

      message.getProperties().setProperty("Content-Type", new ResponseHeader("Content-Type", "text/xml"));
      


      But how can I control the returned http response code?

      I've configured the provider with something like:
      <jbr-provider name="JBR-Http" protocol="http" host="localhost">
       <jbr-bus busid="myServiceHttpGW" port="8372" />
       </jbr-provider>
      


      Regards,
      /Mikael

        • 1. Re: jbr-provider and http response codes
          Daniel Bevenius Master

          Hi Mikael,

          could you try this and see if it works for you:

          message.getProperties().setProperty("ResponseCode", new Integer(500));
          


          Regards,

          Daniel


          • 2. Re: jbr-provider and http response codes
            Mikael Larsson Newbie

            Hi!
            Thank you for the tip, I've tried with both:

            message.getProperties().setProperty("ResponseCode", new Integer(500));
             message.getProperties().setProperty("ResponseCodeMessage", new String("Indicating error... JustDoinSomeTestin"));
            


            Still gives a 200 OK response.

            I've also tried with:
            message.getProperties().setProperty("ResponseCode", new ResponseHeader("ResponseCode","500"));
            message.getProperties().setProperty("ResponseCodeMessage", new ResponseHeader("ResponseCodeMessage", "JustDoinSomeTestin"));
            

            But that just resulted in two http headers named "ResponseCode"/"ResponseCodeMessage" not exactly what I was hoping for...

            If you look at
            org/jboss/soa/esb/listeners/gateway/JBossRemotingGatewayListener.java
            You will see that the decompose method seems to do:
            if(value instanceof ResponseHeader) {
             ResponseHeader header = (ResponseHeader) value;
             responseMap.put(header.getName(), header.getValue());
            }
            


            So, how to get the status code down to the remoting layer?
            /Mikael

            • 3. Re: jbr-provider and http response codes
              Daniel Bevenius Master

              You might be able to do the following as a workaround...

              If you set the ResponseCode like this:

              message.getProperties().setProperty("ResponseCode", new Integer(500));
              

              And specify a message composer in jboss-esb.xml like this:
              <jbr-listener name="Http-Gateway" busidref="Http-2" is-gateway="true" maxThreads="1">
               <property name="composer-class" value="some.package.name.t.JBRComposer"/>
              </jbr-listener>

              And then implement a MessageComposer like this:
              import java.io.Serializable;
              import java.util.LinkedHashMap;
              import java.util.Map;
              import java.util.Set;
              import org.apache.log4j.Logger;
              
              import org.jboss.remoting.InvocationRequest;
              import org.jboss.soa.esb.actions.AbstractActionLifecycle;
              import org.jboss.soa.esb.actions.ActionUtils;
              import org.jboss.soa.esb.helpers.ConfigTree;
              import org.jboss.soa.esb.listeners.message.AbstractMessageComposer;
              import org.jboss.soa.esb.listeners.message.MessageDeliverException;
              import org.jboss.soa.esb.message.Body;
              import org.jboss.soa.esb.message.Message;
              import org.jboss.soa.esb.message.MessagePayloadProxy;
              import org.jboss.soa.esb.message.Properties;
              import org.jboss.soa.esb.message.ResponseHeader;
              import org.jboss.soa.esb.message.MessagePayloadProxy.NullPayloadHandling;
              import org.jboss.soa.esb.message.body.content.BytesBody;
              
              
              public class JBRComposer<T extends InvocationRequest> extends AbstractMessageComposer<T>
              {
               private Logger logger = Logger.getLogger( JBRComposer.class );
              
               private MessagePayloadProxy payloadProxy;
              
               public void setConfiguration(ConfigTree config) {
               super.setConfiguration(config);
               payloadProxy = new MessagePayloadProxy(config,
               new String[] {ActionUtils.POST_ACTION_DATA, Body.DEFAULT_LOCATION, BytesBody.BYTES_LOCATION},
               new String[] {ActionUtils.POST_ACTION_DATA});
               // Allow null to be set on as the message payload...
               payloadProxy.setNullSetPayloadHandling(NullPayloadHandling.LOG);
               }
              
               protected MessagePayloadProxy getPayloadProxy() {
               return payloadProxy;
               }
              
               @SuppressWarnings("unchecked")
               protected void populateMessage(Message message, T invocationRequest) throws MessageDeliverException {
              
               // Set the payload from the JBR invocation...
               payloadProxy.setPayload(message, invocationRequest.getParameter());
              
               // Copy the request properties onto the message...
               Map properties = invocationRequest.getRequestPayload();
               if (properties != null) {
               // Purposely not iterating over the Map.Entry Set because there's
               // a bug in the Map impl used by JBossRemoting. Not all the
               // "values" are actually in the Map.Entry set. Some of them are handled
               // from within an overridden impl of the Map.get(Object) method.
               Set names = properties.keySet();
               for (Object name : names) {
               Object value = properties.get(name);
               if(value != null) {
               message.getProperties().setProperty(name.toString(), value);
               }
               }
               }
               }
              
               public Object decompose(Message message, T invocationRequest) throws MessageDeliverException {
               Properties properties = message.getProperties();
               String propertyNames[] = properties.getNames();
               Map responseMap = invocationRequest.getReturnPayload();
              
               if(responseMap == null) {
               responseMap = new LinkedHashMap();
               invocationRequest.setReturnPayload(responseMap);
               }
              
               for(String name : propertyNames) {
               Object value = properties.getProperty(name);
              
               if(value instanceof ResponseHeader) {
               ResponseHeader header = (ResponseHeader) value;
               responseMap.put(header.getName(), header.getValue());
               }
               else if ( name.equals("ResponseCode") ) {
               responseMap.put(name, value );
               }
               }
              
               return super.decompose(message, invocationRequest);
               }
              }

              I've tried this by modifying the webservice_producer quickstart.

              Regards,

              Daniel