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      @Override
36      public void sessionOpened(IoSession session) {
37          // set idle time to 60 seconds
38          session.setIdleTime(IdleStatus.BOTH_IDLE, 60);
39      }
40  
41      @Override
42      public void messageReceived(IoSession session, Object message) {
43          // Check that we can service the request context
44          HttpResponseMessage response = new HttpResponseMessage();
45          response.setContentType("text/plain");
46          response.setResponseCode(HttpResponseMessage.HTTP_STATUS_SUCCESS);
47          response.appendBody("CONNECTED");
48  
49          // msg.setResponseCode(HttpResponseMessage.HTTP_STATUS_SUCCESS);
50          // byte[] b = new byte[ta.buffer.limit()];
51          // ((ByteBuffer)ta.buffer.rewind()).get(b);
52          // msg.appendBody(b);
53          // System.out.println("####################");
54          // System.out.println("  GET_TILE RESPONSE SENT - ATTACHMENT GOOD DIAMOND.SI="+d.si+
55          // ", "+new java.text.SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss.SSS").format(new java.util.Date()));
56          // System.out.println("#################### - status="+ta.state+", index="+message.getIndex());
57  
58          //// Unknown request
59          // response = new HttpResponseMessage();
60          // response.setResponseCode(HttpResponseMessage.HTTP_STATUS_NOT_FOUND);
61          // response.appendBody(String.format(
62          // "<html><body><h1>UNKNOWN REQUEST %d</h1></body></html>",
63          // HttpResponseMessage.HTTP_STATUS_NOT_FOUND));
64  
65          if (response != null) {
66              session.write(response).join();
67          }
68      }
69  
70      @Override
71      public void sessionIdle(IoSession session, IdleStatus status) {
72          SessionLog.info(session, "Disconnecting the idle.");
73          session.close();
74      }
75  
76      @Override
77      public void exceptionCaught(IoSession session, Throwable cause) {
78          session.close();
79      }
80  }