View Javadoc

    1   package org.codehaus.xfire.transport.http;
    2
    3   import javax.servlet.http.HttpServletRequest;
    4   import javax.servlet.http.HttpServletResponse;
    5
    6   import org.apache.commons.logging.Log;
    7   import org.apache.commons.logging.LogFactory;
    8   import org.codehaus.xfire.MessageContext;
    9   import org.codehaus.xfire.handler.AbstractHandler;
    10  import org.codehaus.xfire.handler.Phase;
    11  import org.codehaus.xfire.service.Service;
    12  import org.codehaus.xfire.transport.AbstractWSDLTransport;
    13  import org.codehaus.xfire.transport.Channel;
    14  import org.codehaus.xfire.transport.DefaultEndpoint;
    15
    16  /***
    17   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
    18   */
    19  public class SoapHttpTransport
    20      extends AbstractWSDLTransport
    21  {
    22      private static final Log log = LogFactory.getLog(SoapHttpTransport.class);
    23
    24      public final static String NAME = "Http";
    25
    26      public final static String HTTP_TRANSPORT_NS = "http://schemas.xmlsoap.org/soap/http";
    27
    28      private final static String URI_PREFIX = "urn:xfire:transport:http:";
    29
    30      public SoapHttpTransport()
    31      {
    32          addFaultHandler(new FaultResponseCodeHandler());
    33      }
    34
    35      /***
    36       * @see org.codehaus.xfire.transport.Transport#getName()
    37       */
    38      public String getName()
    39      {
    40          return NAME;
    41      }
    42
    43      protected Channel createNewChannel(String uri)
    44      {
    45          log.debug("Creating new channel for uri: " + uri);
    46
    47          HttpSoapChannel c = new HttpSoapChannel(uri, this);
    48          c.setEndpoint(new DefaultEndpoint());
    49
    50          return c;
    51      }
    52
    53      protected String getUriPrefix()
    54      {
    55          return URI_PREFIX;
    56      }
    57      /***
    58  	 * Get the URL for a particular service.
    59  	 */
    60  	public String getServiceURL( Service service )
    61  	{
    62  		HttpServletRequest req = XFireServletController.getRequest();
    63
    64          if (req == null)
    65          {
    66              return "http://localhost/services/" + service.getName();
    67          }
    68
    69          StringBuffer output = new StringBuffer( 128 );
    70
    71          output.append( req.getScheme() );
    72          output.append( "://" );
    73          output.append( req.getServerName() );
    74
    75          if ( req.getServerPort() != 80 &&
    76               req.getServerPort() != 443 &&
    77               req.getServerPort() != 0 )
    78          {
    79              output.append( ':' );
    80              output.append( req.getServerPort() );
    81          }
    82
    83          output.append( req.getRequestURI() );
    84
    85          return output.toString();
    86  	}
    87
    88      public String getTransportURI( Service service )
    89      {
    90          return HTTP_TRANSPORT_NS;
    91      }
    92
    93      protected String getWebappBase(HttpServletRequest request)
    94  	{
    95  		StringBuffer baseURL = new StringBuffer(128);
    96  		baseURL.append(request.getScheme());
    97  		baseURL.append("://");
    98  		baseURL.append(request.getServerName());
    99  		if (request.getServerPort() != 80)
    100 		{
    101 			baseURL.append(":");
    102 			baseURL.append(request.getServerPort());
    103 		}
    104 		baseURL.append(request.getContextPath());
    105 		return baseURL.toString();
    106 	}
    107
    108     public String[] getKnownUriSchemes()
    109     {
    110         return new String[] { "http://", "https://" };
    111     }
    112
    113     public class FaultResponseCodeHandler
    114          extends AbstractHandler
    115      {
    116         public String getPhase()
    117         {
    118             return Phase.TRANSPORT;
    119         }
    120
    121         /***
    122          * @see org.codehaus.xfire.handler.Handler#invoke(org.codehaus.xfire.MessageContext)
    123          * @param context
    124          * @throws Exception
    125          */
    126         public void invoke(MessageContext context)
    127         {
    128             HttpServletResponse response = XFireServletController.getResponse();
    129             if ( response != null )
    130                 response.setStatus(500);
    131         }
    132     }
    133 }