View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.example.proxy;
21  
22  import java.io.File;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.nio.channels.FileChannel;
26  
27  import org.apache.mina.core.buffer.IoBuffer;
28  import org.apache.mina.core.session.IdleStatus;
29  import org.apache.mina.core.session.IoSession;
30  import org.apache.mina.proxy.AbstractProxyIoHandler;
31  import org.apache.mina.proxy.handlers.ProxyRequest;
32  import org.apache.mina.proxy.handlers.http.HttpProxyConstants;
33  import org.apache.mina.proxy.handlers.http.HttpProxyRequest;
34  import org.apache.mina.proxy.handlers.socks.SocksProxyRequest;
35  import org.apache.mina.proxy.session.ProxyIoSession;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  /**
40   * ClientSessionHandler.java - Client session handler for the mina proxy test class.
41   * 
42   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
43   * @since MINA 2.0.0-M3
44   */
45  public class ClientSessionHandler extends AbstractProxyIoHandler {
46      private final static Logger logger = LoggerFactory
47              .getLogger(ClientSessionHandler.class);
48  
49      /**
50       * The temporary file were the request result will be written.
51       */
52      private File file;
53  
54      /**
55       * NIO channel of the temporary file.
56       */
57      private FileChannel wChannel;
58  
59      /**
60       * The command to issue to the proxy.
61       */
62      private String cmd;
63  
64      public ClientSessionHandler(String cmd) {
65          this.cmd = cmd;
66      }
67  
68      /**
69       * {@inheritDoc} 
70       */
71      @Override
72      public void sessionCreated(IoSession session) throws Exception {
73          logger.debug("CLIENT - Session created: " + session);
74      }
75  
76      /**
77       * Sends the request to the proxy server when session is opened with
78       * the proxy. 
79       */
80      @Override
81      public void proxySessionOpened(IoSession session) throws Exception {
82          logger.debug("CLIENT - Session opened: " + session);
83          ProxyIoSession proxyIoSession = (ProxyIoSession) session
84                  .getAttribute(ProxyIoSession.PROXY_SESSION);
85          if (proxyIoSession != null) {
86              ProxyRequest req = proxyIoSession.getRequest();
87  
88              byte[] c = null;
89              if (req instanceof SocksProxyRequest && cmd != null) {
90                  logger.debug("Sending request to a SOCKS SESSION ...");
91                  c = cmd.getBytes(proxyIoSession.getCharsetName());
92              } else if (req instanceof HttpProxyRequest
93                      && ((HttpProxyRequest) req).getHttpVerb() == HttpProxyConstants.CONNECT) {
94                  logger.debug("Sending request to a HTTP CONNECTED SESSION ...");
95                  c = (((HttpProxyRequest) req).toHttpString())
96                          .getBytes(proxyIoSession.getCharsetName());
97              }
98  
99              if (c != null) {
100                 IoBuffer buf = IoBuffer.allocate(c.length);
101                 buf.put(c);
102                 buf.flip();
103                 session.write(buf);
104             }
105         }
106     }
107 
108     /**
109      * Writes the request result to a temporary file.
110      */
111     @Override
112     public void messageReceived(IoSession session, Object message) {
113         logger.debug("CLIENT - Message received: " + session);
114         IoBuffer buf = (IoBuffer) message;
115 
116         try {
117             if (file == null) {
118                 file = File.createTempFile("http", ".html");
119                 logger.info("Writing request result to "
120                         + file.getAbsolutePath());
121                 wChannel = new FileOutputStream(file, false).getChannel();
122             }
123 
124             // Write the ByteBuffer contents; the bytes between the ByteBuffer's
125             // position and the limit is written to the file
126             wChannel.write(buf.buf());
127         } catch (IOException e) {
128             //e.printStackTrace();
129         }
130     }
131 
132     /**
133      * Closes the temporary file if it was opened. 
134      */
135     @Override
136     public void sessionClosed(IoSession session) throws Exception {
137         logger.debug("CLIENT - Session closed - closing result file if open.");
138         // Close the file
139         if (wChannel != null) {
140             wChannel.close();
141         }
142     }
143 
144     /**
145      * {@inheritDoc} 
146      */
147     @Override
148     public void sessionIdle(IoSession session, IdleStatus status)
149             throws Exception {
150         if (session.isClosing()) {
151             return;
152         }
153 
154         logger.debug("CLIENT - Session idle");
155     }
156 
157     /**
158      * {@inheritDoc} 
159      */
160     @Override
161     public void exceptionCaught(IoSession session, Throwable cause) {
162         logger.debug("CLIENT - Exception caught");
163         //cause.printStackTrace();
164         session.closeNow();
165     }
166 }