title: Apache Mina: How to write a Http Server?
body: <p>I'm trying to write an Http Server using Apache Mina.</p>
  
  <p>According to Mina's architecture, there should be 2 filters for this task, one for Http Request Passing and another for processing the request and generating the response. So using the Mina example codes, I came up with the following code, that has an acceptor, logging filter, Http filter, and a filter for processing request.</p>
  
  <p>Initiation of the server runs correctly, but the request does not come to DummyHttpSever filter. I tried to debug, but could not find the issue. What is going wrong here?</p>
  
  <pre><code>import java.net.InetSocketAddress;
  import java.nio.ByteBuffer;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import org.apache.mina.filter.logging.LoggingFilter;
  import org.apache.mina.api.AbstractIoFilter;
  import org.apache.mina.api.IoSession;
  import org.apache.mina.filter.codec.ProtocolCodecFilter;
  import org.apache.mina.filterchain.ReadFilterChainController;
  import org.apache.mina.http.DateUtil;
  import org.apache.mina.http.HttpDecoderState;
  import org.apache.mina.http.HttpServerDecoder;
  import org.apache.mina.http.HttpServerEncoder;
  import org.apache.mina.http.api.DefaultHttpResponse;
  import org.apache.mina.http.api.HttpContentChunk;
  import org.apache.mina.http.api.HttpEndOfContent;
  import org.apache.mina.http.api.HttpMethod;
  import org.apache.mina.http.api.HttpPdu;
  import org.apache.mina.http.api.HttpRequest;
  import org.apache.mina.http.api.HttpStatus;
  import org.apache.mina.http.api.HttpVersion;
  import org.apache.mina.transport.nio.NioTcpServer;
  
  public class HttpTest {
  
      public static void main(String[] args) throws Exception {
  
          NioTcpServer httpServer = new NioTcpServer();
          httpServer.setReuseAddress(true);
          httpServer.setFilters(new ProtocolCodecFilter&lt;HttpPdu, ByteBuffer, Void, HttpDecoderState&gt;(new HttpServerEncoder(),
                          new HttpServerDecoder()), new LoggingFilter("DECODED"), new DummyHttpSever());
  
  
          httpServer.getSessionConfig().setTcpNoDelay(true);
  
          httpServer.bind(new InetSocketAddress(8080));
  
          // run for 20 seconds
          Thread.sleep(2000000000);
          httpServer.unbind();
  
      }
  
      private static class DummyHttpSever extends AbstractIoFilter {
  
          private HttpRequest incomingRequest;
  
          private List&lt;ByteBuffer&gt; body;
  
          @Override
          public void messageReceived(IoSession session, Object message, ReadFilterChainController controller) {
              if (message instanceof HttpRequest) {
                  System.out.println("This shit is working");
  
  
                  incomingRequest = (HttpRequest) message;
                  body = new ArrayList&lt;ByteBuffer&gt;();
  
                  // check if this request is going to be followed by and HTTP body or not
                  if (incomingRequest.getMethod() != HttpMethod.POST &amp;&amp; incomingRequest.getMethod() != HttpMethod.PUT) {
                      sendResponse(session, incomingRequest);
                  } else {
  
                  }
              } else if (message instanceof ByteBuffer) {
                  body.add((ByteBuffer) message);
              } else if (message instanceof HttpEndOfContent) {
                  // we received all the post content, send the crap back
                  sendResponse(session, incomingRequest);
              }
  
          }
  
          public void sendResponse(IoSession session, HttpRequest request) {
              Map&lt;String, String&gt; headers = new HashMap&lt;String, String&gt;();
              headers.put("Server", "Apache MINA Dummy test server/0.0.");
              headers.put("Date", DateUtil.getCurrentAsString());
              headers.put("Connection", "Close");
              String strContent = "Hello ! we reply to request !";
              ByteBuffer content = ByteBuffer.wrap(strContent.getBytes());
  
              // compute content len
              headers.put("Content-Length", String.valueOf(content.remaining()));
              session.write(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SUCCESS_OK, headers));
              session.write(new HttpContentChunk(content));
              session.write(new HttpEndOfContent());
              session.close(false);
  
          }
      }
  }
  </code></pre>
  
  <p>Also, following are dependencies I am using.</p>
  
  <pre><code>&lt;dependency&gt;
              &lt;groupId&gt;org.apache.mina&lt;/groupId&gt;
              &lt;artifactId&gt;mina-core&lt;/artifactId&gt;
              &lt;version&gt;2.0.7&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.mina&lt;/groupId&gt;
              &lt;artifactId&gt;mina-http&lt;/artifactId&gt;
              &lt;version&gt;2.0.7&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.mina&lt;/groupId&gt;
              &lt;artifactId&gt;mina-coap&lt;/artifactId&gt;
              &lt;version&gt;2.0.7&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
              &lt;artifactId&gt;log4j-slf4j-impl&lt;/artifactId&gt;
              &lt;version&gt;LATEST&lt;/version&gt;
          &lt;/dependency&gt;
  </code></pre>