1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/server/SimpleProxy.java $
3    * $Revision: 1425331 $
4    * $Date: 2012-12-22 18:29:41 +0000 (Sat, 22 Dec 2012) $
5    *
6    * ====================================================================
7    *
8    *  Licensed to the Apache Software Foundation (ASF) under one or more
9    *  contributor license agreements.  See the NOTICE file distributed with
10   *  this work for additional information regarding copyright ownership.
11   *  The ASF licenses this file to You under the Apache License, Version 2.0
12   *  (the "License"); you may not use this file except in compliance with
13   *  the License.  You may obtain a copy of the License at
14   *
15   *      http://www.apache.org/licenses/LICENSE-2.0
16   *
17   *  Unless required by applicable law or agreed to in writing, software
18   *  distributed under the License is distributed on an "AS IS" BASIS,
19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   *  See the License for the specific language governing permissions and
21   *  limitations under the License.
22   * ====================================================================
23   *
24   * This software consists of voluntary contributions made by many
25   * individuals on behalf of the Apache Software Foundation.  For more
26   * information on the Apache Software Foundation, please see
27   * <http://www.apache.org/>.
28   *
29   */
30  
31  package org.apache.commons.httpclient.server;
32  
33  import java.io.IOException;
34  
35  import org.apache.commons.httpclient.Credentials;
36  
37  /***
38   * Simple server that registers default request handlers to act as a proxy.
39   * 
40   * @author Ortwin Glueck
41   * @author Oleg Kalnichevski
42   */
43  public class SimpleProxy extends SimpleHttpServer {
44      
45      private SimpleConnManager connmanager = null; 
46      private HttpRequestHandlerChain stdchain = null;
47  
48      public SimpleProxy(int port) throws IOException {
49          super(port);
50          this.connmanager = new SimpleConnManager(); 
51          this.stdchain = new HttpRequestHandlerChain();
52          this.stdchain.appendHandler(new TransparentProxyRequestHandler());
53          this.stdchain.appendHandler(new ProxyRequestHandler(this.connmanager));
54          setRequestHandler(this.stdchain);
55      }
56  
57      public SimpleProxy() throws IOException {
58          this(0);
59      }
60  
61      public void requireAuthentication(final Credentials creds, final String realm, boolean keepalive) {
62          HttpRequestHandlerChain chain = new HttpRequestHandlerChain(this.stdchain); 
63          chain.prependHandler(new ProxyAuthRequestHandler(creds ,realm, keepalive));
64          setRequestHandler(chain);
65      }
66  
67      public void requireAuthentication(final Credentials creds) {
68          HttpRequestHandlerChain chain = new HttpRequestHandlerChain(this.stdchain); 
69          chain.prependHandler(new ProxyAuthRequestHandler(creds));
70          setRequestHandler(chain);
71      }
72  
73      public void destroy() {
74          super.destroy();
75          this.connmanager.shutdown();
76      }
77      
78      public void addHandler(final HttpRequestHandler handler) {
79          this.stdchain.prependHandler(handler);
80      }
81      
82  }