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