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.httpserver.codec;
21  
22  import org.apache.mina.common.IdleStatus;
23  import org.apache.mina.common.IoHandler;
24  import org.apache.mina.common.IoHandlerAdapter;
25  import org.apache.mina.common.IoSession;
26  import org.apache.mina.util.SessionLog;
27  
28  /**
29   * An {@link IoHandler} for HTTP.
30   * 
31   * @author The Apache Directory Project (mina-dev@directory.apache.org)
32   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13  7월 2007) $
33   */
34  public class ServerHandler extends IoHandlerAdapter {
35      public void sessionOpened(IoSession session) {
36          // set idle time to 60 seconds
37          session.setIdleTime(IdleStatus.BOTH_IDLE, 60);
38      }
39  
40      public void messageReceived(IoSession session, Object message) {
41          // Check that we can service the request context
42          HttpResponseMessage response = new HttpResponseMessage();
43          response.setContentType("text/plain");
44          response.setResponseCode(HttpResponseMessage.HTTP_STATUS_SUCCESS);
45          response.appendBody("CONNECTED");
46  
47          // msg.setResponseCode(HttpResponseMessage.HTTP_STATUS_SUCCESS);
48          // byte[] b = new byte[ta.buffer.limit()];
49          // ((ByteBuffer)ta.buffer.rewind()).get(b);
50          // msg.appendBody(b);
51          // System.out.println("####################");
52          // System.out.println("  GET_TILE RESPONSE SENT - ATTACHMENT GOOD DIAMOND.SI="+d.si+
53          // ", "+new java.text.SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss.SSS").format(new java.util.Date()));
54          // System.out.println("#################### - status="+ta.state+", index="+message.getIndex());
55  
56          //// Unknown request
57          // response = new HttpResponseMessage();
58          // response.setResponseCode(HttpResponseMessage.HTTP_STATUS_NOT_FOUND);
59          // response.appendBody(String.format(
60          // "<html><body><h1>UNKNOWN REQUEST %d</h1></body></html>",
61          // HttpResponseMessage.HTTP_STATUS_NOT_FOUND));
62  
63          if (response != null)
64              session.write(response).join();
65      }
66  
67      public void sessionIdle(IoSession session, IdleStatus status) {
68          SessionLog.info(session, "Disconnecting the idle.");
69          session.close();
70      }
71  
72      public void exceptionCaught(IoSession session, Throwable cause) {
73          session.close();
74      }
75  }