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.examples.http;
    21
    22  import java.net.InetSocketAddress;
    23  import java.nio.ByteBuffer;
    24  import java.util.ArrayList;
    25  import java.util.HashMap;
    26  import java.util.List;
    27  import java.util.Map;
    28
    29  import org.apache.mina.api.AbstractIoFilter;
    30  import org.apache.mina.api.IoSession;
    31  import org.apache.mina.filter.codec.ProtocolCodecFilter;
    32  import org.apache.mina.filter.logging.LoggingFilter;
    33  import org.apache.mina.filterchain.ReadFilterChainController;
    34  import org.apache.mina.http.DateUtil;
    35  import org.apache.mina.http.HttpDecoderState;
    36  import org.apache.mina.http.HttpServerDecoder;
    37  import org.apache.mina.http.HttpServerEncoder;
    38  import org.apache.mina.http.api.DefaultHttpResponse;
    39  import org.apache.mina.http.api.HttpContentChunk;
    40  import org.apache.mina.http.api.HttpEndOfContent;
    41  import org.apache.mina.http.api.HttpMethod;
    42  import org.apache.mina.http.api.HttpPdu;
    43  import org.apache.mina.http.api.HttpRequest;
    44  import org.apache.mina.http.api.HttpStatus;
    45  import org.apache.mina.http.api.HttpVersion;
    46  import org.apache.mina.transport.nio.NioTcpServer;
    47
    48  public class HttpTest {
    49
    50      public static void main(String[] args) throws Exception {
    51
    52          NioTcpServer httpServer = new NioTcpServer();
    53          httpServer.setReuseAddress(true);
    54          httpServer.setFilters(new LoggingFilter("INCOMING"),
    55                  new ProtocolCodecFilter<HttpPdu, ByteBuffer, Void, HttpDecoderState>(new HttpServerEncoder(),
    56                          new HttpServerDecoder()), new LoggingFilter("DECODED"), new DummyHttpSever());
    57
    58          httpServer.getSessionConfig().setTcpNoDelay(true);
    59
    60          httpServer.bind(new InetSocketAddress(8080));
    61
    62          // run for 20 seconds
    63          Thread.sleep(20000);
    64          httpServer.unbind();
    65
    66      }
    67
    68      private static class DummyHttpSever extends AbstractIoFilter {
    69
    70          private HttpRequest incomingRequest;
    71
    72          private List<ByteBuffer> body;
    73
    74          @Override
    75          public void messageReceived(IoSession session, Object message, ReadFilterChainController controller) {
    76              if (message instanceof HttpRequest) {
    77                  incomingRequest = (HttpRequest) message;
    78                  body = new ArrayList<ByteBuffer>();
    79
    80                  // check if this request is going to be followed by and HTTP body or not
    81                  if (incomingRequest.getMethod() != HttpMethod.POST && incomingRequest.getMethod() != HttpMethod.PUT) {
    82                      sendResponse(session, incomingRequest);
    83                  } else {
    84
    85                  }
    86              } else if (message instanceof ByteBuffer) {
    87                  body.add((ByteBuffer) message);
    88              } else if (message instanceof HttpEndOfContent) {
    89                  // we received all the post content, send the crap back
    90                  sendResponse(session, incomingRequest);
    91              }
    92
    93          }
    94
    95          public void sendResponse(IoSession session, HttpRequest request) {
    96              Map<String, String> headers = new HashMap<String, String>();
    97              headers.put("Server", "Apache MINA Dummy test server/0.0.");
    98              headers.put("Date", DateUtil.getCurrentAsString());
    99              headers.put("Connection", "Close");
    100             String strContent = "Hello ! we reply to request !";
    101             ByteBuffer content = ByteBuffer.wrap(strContent.getBytes());
    102
    103             // compute content len
    104             headers.put("Content-Length", String.valueOf(content.remaining()));
    105             session.write(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SUCCESS_OK, headers));
    106             session.write(new HttpContentChunk(content));
    107             session.write(new HttpEndOfContent());
    108             session.close(false);
    109
    110         }
    111     }
    112 }