vmassol     2002/08/04 03:34:18

  Added:       framework/src/java/share/org/apache/cactus/client
                        ResponseObjectFactory.java
                        WebResponseObjectFactory.java
  Log:
  I thought I had committed these yesterday ... strange. Anyway they are some 
refatoring to provide ability to create response object for endXXX method parameters 
in an extensible way (at the moment we use it create Cactus WebResponse or HttpUnit 
WebResponse but later on it will be extended with JMS response for example).
  
  Revision  Changes    Path
  1.1                  
jakarta-cactus/framework/src/java/share/org/apache/cactus/client/ResponseObjectFactory.java
  
  Index: ResponseObjectFactory.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Cactus" and "Apache Software
   *    Foundation" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.cactus.client;
  
  import java.net.HttpURLConnection;
  
  import org.apache.cactus.Request;
  
  /**
   * Constructs response object that are passed as parameter to
   * <code>endXXX()</code> and global end methods.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Massol</a>
   *
   * @version $Id: ResponseObjectFactory.java,v 1.1 2002/08/04 10:34:18 vmassol Exp $
   */
  public interface ResponseObjectFactory
  {
      /**
       * Constructs response object that are passed as parameter to
       * <code>endXXX()</code> and global end methods.
       *
       * @param theClassName the class name of the object to construct
       * @param theRequest the request that was sent by Cactus to the server side
       * @param theConnection the HTTP connection object that was used by Cactus
       *        to connect to the server side
       * @return the response object that will be passed to <code>endXXX()</code>
       *         methods and to the global end method
       * @throws ClientException if it fails to construct the response object
       */
      Object getResponseObject(String theClassName, Request theRequest,
          HttpURLConnection theConnection) throws ClientException;
  }
  
  
  
  1.1                  
jakarta-cactus/framework/src/java/share/org/apache/cactus/client/WebResponseObjectFactory.java
  
  Index: WebResponseObjectFactory.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Cactus" and "Apache Software
   *    Foundation" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.cactus.client;
  
  import java.net.HttpURLConnection;
  import java.net.URLConnection;
  import java.lang.reflect.Method;
  
  import org.apache.cactus.Request;
  import org.apache.cactus.WebResponse;
  import org.apache.cactus.WebRequest;
  
  /**
   * Constructs Web response object that are passed as parameter to
   * <code>endXXX()</code> and global end methods.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Massol</a>
   *
   * @version $Id: WebResponseObjectFactory.java,v 1.1 2002/08/04 10:34:18 vmassol Exp 
$
   */
  public class WebResponseObjectFactory implements ResponseObjectFactory
  {
      /**
       * @see ResponseObjectFactory#getResponseObject
       */
      public Object getResponseObject(String theClassName, Request theRequest,
          HttpURLConnection theConnection) throws ClientException
      {
          Object responseObject;
  
          // Is it a Http Unit WebResponse ?
          if (theClassName.equals("com.meterware.httpunit.WebResponse")) {
              responseObject = createHttpUnitWebResponse(theConnection);
  
          // Is it a Cactus WebResponse ?
          } else if (theClassName.equals("org.apache.cactus.WebResponse")) {
              responseObject = new WebResponse((WebRequest) theRequest,
                  theConnection);
  
          // Is it an old HttpURLConnection (deprecated) ?
          } else if (theClassName.equals("java.net.HttpURLConnection")) {
              responseObject = theConnection;
  
          // Else it is an error ...
          } else {
              throw new ClientException("Invalid parameter type [" + theClassName
                  + "]");
          }
  
          return responseObject;
      }
  
      /**
       * Create a HttpUnit <code>WebResponse</code> object by reflection (so
       * that we don't need the HttpUnit jar for users who are not using
       * the HttpUnit endXXX() signature).
       *
       * @param theConnection the HTTP connection that was used when connecting
       *        to the server side and which now contains the returned HTTP
       *        response that we will pass to HttpUnit so that it can construt
       *        a <code>com.meterware.httpunit.WebResponse</code> object.
       * @return a HttpUnit <code>WebResponse</code> object
       * @exception ClientException if it failes to create a HttpClient
       *            WebResponse object for any reason
       */
      private Object createHttpUnitWebResponse(HttpURLConnection theConnection)
          throws ClientException
      {
          Object webResponse;
  
          try {
              Class responseClass =
                  Class.forName("com.meterware.httpunit.WebResponse");
              Method method = responseClass.getMethod("newResponse",
                  new Class[]{URLConnection.class});
              webResponse = method.invoke(null, new Object[]{theConnection});
          } catch (Exception e) {
              throw new ClientException("Error calling "
                  + "[public static com.meterware.httpunit.WebResponse "
                  + "com.meterware.httpunit.WebResponse.newResponse("
                  + "java.net.URLConnection) throws java.io.IOException]", e);
          }
  
          return webResponse;
      }
  
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to