commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From oglu...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server ResponseWriter.java SimpleHttpServerConnection.java GenericResponse.java HttpRequestHandler.java SimpleHttpServer.java HttpRequestHandlerChain.java
Date Mon, 24 Nov 2003 20:41:11 GMT
oglueck     2003/11/24 12:41:11

  Modified:    httpclient/src/test/org/apache/commons/httpclient/server
                        Tag: HTTPCLIENT_2_0_BRANCH ResponseWriter.java
                        SimpleHttpServerConnection.java
                        GenericResponse.java HttpRequestHandler.java
                        SimpleHttpServer.java HttpRequestHandlerChain.java
  Log:
  refactored code to be more OO and flexible enough for our needs
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.1.2.2   +3 -57     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/Attic/ResponseWriter.java
  
  Index: ResponseWriter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/Attic/ResponseWriter.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -r1.1.2.1 -r1.1.2.2
  --- ResponseWriter.java	18 Nov 2003 12:26:39 -0000	1.1.2.1
  +++ ResponseWriter.java	24 Nov 2003 20:41:11 -0000	1.1.2.2
  @@ -78,7 +78,6 @@
   public class ResponseWriter extends FilterWriter {
       public static final String CRLF = "\r\n";
       public static final String ISO_8859_1 = "ISO-8859-1";
  -    private boolean modified = false;
       private OutputStream outStream;
       private String encoding;
   
  @@ -100,7 +99,6 @@
       
       public void close() throws IOException {
           if(out != null) {
  -            modified = true;
               super.close();
               out = null;
           }
  @@ -118,69 +116,17 @@
   
       public void write(byte b) throws IOException {
           super.flush();
  -        modified = true;
           outStream.write((int)b);
       }
       public void write(byte[] b) throws IOException {
           super.flush();
  -        modified = true;
           outStream.write(b);
       }
       public void write(byte[] b, int off, int len) throws IOException {
           super.flush();
  -        modified = true;
           outStream.write(b,off,len);
       }
   
  -    /* (non-Javadoc)
  -     * @see java.io.Writer#write(char[], int, int)
  -     */
  -    public void write(char[] cbuf, int off, int len) throws IOException {
  -        modified = true;
  -        super.write(cbuf, off, len);
  -    }
  -
  -    /* (non-Javadoc)
  -     * @see java.io.Writer#write(int)
  -     */
  -    public void write(int c) throws IOException {
  -        modified = true;
  -        super.write(c);
  -    }
  -
  -    /* (non-Javadoc)
  -     * @see java.io.Writer#write(java.lang.String, int, int)
  -     */
  -    public void write(String str, int off, int len) throws IOException {
  -        modified = true;
  -        super.write(str, off, len);
  -    }
  -
  -    /* (non-Javadoc)
  -     * @see java.io.Writer#write(char[])
  -     */
  -    public void write(char[] cbuf) throws IOException {
  -        modified = true;
  -        super.write(cbuf);
  -    }
  -
  -    /* (non-Javadoc)
  -     * @see java.io.Writer#write(java.lang.String)
  -     */
  -    public void write(String str) throws IOException {
  -        modified = true;
  -        super.write(str);
  -    }
  -
  -    /**
  -     * Checks if this PrintWriter has been called
  -     * 
  -     * @return  true/false
  -     */
  -    public boolean isModified() {
  -        return modified;
  -    }
  -    
       public void print(String s) throws IOException {
           if (s == null) {
               s = "null";
  
  
  
  1.1.2.3   +34 -18    jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/Attic/SimpleHttpServerConnection.java
  
  Index: SimpleHttpServerConnection.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/Attic/SimpleHttpServerConnection.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- SimpleHttpServerConnection.java	19 Nov 2003 00:23:30 -0000	1.1.2.2
  +++ SimpleHttpServerConnection.java	24 Nov 2003 20:41:11 -0000	1.1.2.3
  @@ -65,6 +65,8 @@
   
   import java.io.IOException;
   import java.io.InputStream;
  +import java.io.OutputStream;
  +import java.io.UnsupportedEncodingException;
   import java.net.Socket;
   import java.net.SocketException;
   
  @@ -86,18 +88,21 @@
       private SimpleHttpServer server;
       private Socket socket;
       private InputStream in;
  -    private ResponseWriter out;
  +    private OutputStream out;
   
       private int requestNo = 0;
       
       private boolean keepAlive = false;
   
  +	private RequestLine requestLine;
  +
  +	private Header[] headers;
  +
       public SimpleHttpServerConnection(SimpleHttpServer server, Socket socket) throws IOException
{
           this.server = server;
           this.socket = socket;
           this.in = socket.getInputStream();
  -        this.out =
  -            new ResponseWriter(socket.getOutputStream());
  +        this.out = socket.getOutputStream();
       }
   
       public void destroy() {
  @@ -154,7 +159,11 @@
        * @return  This connection's ResponseWriter
        */
       public ResponseWriter getWriter() {
  -        return out;
  +        try {
  +			return new ResponseWriter(out);
  +		} catch (UnsupportedEncodingException e) {
  +			throw new RuntimeException(e.toString());
  +		}
       }
       
       /**
  @@ -178,25 +187,32 @@
               return;
           }
   
  -        RequestLine requestLine;
  -        Header[] headers;        
           try {
               requestLine = RequestLine.parseLine(line);
               headers = HttpParser.parseHeaders(in);
           } catch(IOException e) {
               connectionClose();
  -            ErrorResponse.getInstance().getResponse(HttpStatus.SC_BAD_REQUEST).processRequest(getWriter(),
this, null, null);
  +            ErrorResponse.getInstance().getResponse(HttpStatus.SC_BAD_REQUEST).processRequest(this);
               return;     
           }
  -        server.processRequest(this, requestLine, headers);
  +        server.processRequest(this);
       }
       
  -    /**
  -     * Checks if we have found a {@link HttpRequestHandler} for this request.
  -     * 
  -     * @return true/false
  -     */
  -    public boolean isClaimed() {
  -        return out.isModified();
  +    public Header[] getHeaders() {
  +    	Header[] copy = new Header[headers.length];
  +    	System.arraycopy(headers, 0, copy, 0, headers.length);
  +    	return copy;
  +    }
  +    
  +    public RequestLine getRequestLine() {
  +    	return requestLine;
       }
  +
  +	public InputStream getInputStream() {
  +		return in;
  +	}
  +
  +	public OutputStream getOutputStream() {
  +		return out;
  +	}
   }
  
  
  
  1.1.2.2   +6 -10     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/Attic/GenericResponse.java
  
  Index: GenericResponse.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/Attic/GenericResponse.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -r1.1.2.1 -r1.1.2.2
  --- GenericResponse.java	18 Nov 2003 12:26:38 -0000	1.1.2.1
  +++ GenericResponse.java	24 Nov 2003 20:41:11 -0000	1.1.2.2
  @@ -174,16 +174,11 @@
           }
       }
   
  -    public void processRequest(
  -        ResponseWriter out, 
  -        SimpleHttpServerConnection conn,
  -        RequestLine requestLine,
  -        Header[] headers)
  -        throws IOException {
  +    public boolean processRequest(SimpleHttpServerConnection conn)  throws IOException
{
   
           boolean haveContentLength = false;
           boolean haveContentType = false;
  -
  +        ResponseWriter out = conn.getWriter();
           out.println(getStatusLine());
           if (responseHeaders != null) {
               for (int i = 0; i < responseHeaders.length; i++) {
  @@ -218,5 +213,6 @@
           out.write(bos.toByteArray());
   
           bos.close();
  +        return true;
       }
   }
  
  
  
  1.1.2.2   +6 -9      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/Attic/HttpRequestHandler.java
  
  Index: HttpRequestHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/Attic/HttpRequestHandler.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -r1.1.2.1 -r1.1.2.2
  --- HttpRequestHandler.java	18 Nov 2003 12:26:38 -0000	1.1.2.1
  +++ HttpRequestHandler.java	24 Nov 2003 20:41:11 -0000	1.1.2.2
  @@ -65,8 +65,6 @@
   
   import java.io.IOException;
   
  -import org.apache.commons.httpclient.Header;
  -
   /**
    * Defines an HTTP request handler for the SimpleHttpServer
    * 
  @@ -86,11 +84,10 @@
        * The handler may also rewrite the request parameters (this is useful in
        * {@link HttpRequestHandlerChain} structures).
        * 
  -     * @param out           The ResponseWriter to be written to (may differ from <code>conn.getResponseWriter()</code>);
        * @param conn          The Connection object to which this request belongs to.
  -     * @param requestLine   The request-line ("GET / HTTP/1.0", for example)
  -     * @param headers       The request-headers
  +     * @return true if this handler handled the request and no other handlers in the 
  +     * chain should be called, false otherwise.
        * @throws IOException
        */
  -    public void processRequest(ResponseWriter out, SimpleHttpServerConnection conn, RequestLine
requestLine, Header[] headers) throws IOException;
  +    public boolean processRequest(SimpleHttpServerConnection conn) throws IOException;
   }
  
  
  
  1.1.2.3   +8 -11     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/Attic/SimpleHttpServer.java
  
  Index: SimpleHttpServer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/Attic/SimpleHttpServer.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- SimpleHttpServer.java	19 Nov 2003 00:23:30 -0000	1.1.2.2
  +++ SimpleHttpServer.java	24 Nov 2003 20:41:11 -0000	1.1.2.3
  @@ -71,7 +71,6 @@
   import java.util.Iterator;
   import java.util.Set;
   
  -import org.apache.commons.httpclient.Header;
   import org.apache.commons.httpclient.HttpStatus;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  @@ -192,18 +191,16 @@
           connections.remove(conn);
       }
   
  -    public void processRequest(
  -        SimpleHttpServerConnection conn,
  -        RequestLine requestLine,
  -        Header[] headers)
  +    public void processRequest(SimpleHttpServerConnection conn)
           throws IOException {
   
  +    	boolean complete = false;
           if (requestHandler != null) {
  -            requestHandler.processRequest(conn.getWriter(), conn, requestLine, headers);
  +            complete = requestHandler.processRequest(conn);
           }
  -        if (!conn.isClaimed()) {
  +        if (!complete) {
               conn.connectionClose();
  -            ErrorResponse.getInstance().getResponse(HttpStatus.SC_SERVICE_UNAVAILABLE).processRequest(conn.getWriter(),
conn, requestLine, headers);
  +            ErrorResponse.getInstance().getResponse(HttpStatus.SC_SERVICE_UNAVAILABLE).processRequest(conn);
           }
   
           conn.getWriter().flush();
  
  
  
  1.1.2.2   +8 -14     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/Attic/HttpRequestHandlerChain.java
  
  Index: HttpRequestHandlerChain.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/Attic/HttpRequestHandlerChain.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -r1.1.2.1 -r1.1.2.2
  --- HttpRequestHandlerChain.java	18 Nov 2003 12:26:38 -0000	1.1.2.1
  +++ HttpRequestHandlerChain.java	24 Nov 2003 20:41:11 -0000	1.1.2.2
  @@ -68,8 +68,6 @@
   import java.util.Iterator;
   import java.util.List;
   
  -import org.apache.commons.httpclient.Header;
  -
   /**
    * Maintains a chain of {@link HttpRequestHandler}s where new request-handlers
    * can be prepended/appended.
  @@ -96,19 +94,15 @@
           subhandlers.add(handler);
       }
   
  -    public void processRequest(
  -        ResponseWriter out, 
  -        SimpleHttpServerConnection conn,
  -        RequestLine requestLine,
  -        Header[] headers)
  -        throws IOException {
  +    public boolean processRequest(SimpleHttpServerConnection conn)  throws IOException
{
           
           for(Iterator it=subhandlers.iterator();it.hasNext();) {
               HttpRequestHandler h = (HttpRequestHandler)it.next();
  -            h.processRequest(out, conn, requestLine, headers);
  -            if(conn.isClaimed()) {
  -                break;
  +            boolean stop = h.processRequest(conn);
  +            if(stop) {
  +                return true;
               }
           }
  +        return false;
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message