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.http;
21  
22  import java.nio.ByteBuffer;
23  import java.nio.charset.Charset;
24  import java.util.Map;
25  
26  import org.apache.mina.codec.StatelessProtocolEncoder;
27  import org.apache.mina.http.api.HttpContentChunk;
28  import org.apache.mina.http.api.HttpEndOfContent;
29  import org.apache.mina.http.api.HttpPdu;
30  import org.apache.mina.http.api.HttpPduEncodingVisitor;
31  import org.apache.mina.http.api.HttpRequest;
32  import org.apache.mina.http.api.HttpResponse;
33  
34  /**
35   * In charge of encoding HTTP message into bytes.
36   * 
37   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
38   */
39  public class HttpServerEncoder implements StatelessProtocolEncoder<HttpPdu, ByteBuffer> {
40  
41      /**
42       * {@inheritDoc}
43       */
44      @Override
45      public Void createEncoderState() {
46          // steless, so it's not used
47          return null;
48      }
49  
50      /**
51       * {@inheritDoc}
52       */
53      @Override
54      public ByteBuffer encode(HttpPdu message, Void context) {
55          return message.encode(visitor);
56      }
57  
58      private HttpPduEncodingVisitor visitor = new HttpPduEncodingVisitor() {
59  
60          /**
61           * {@inheritDoc}
62           */
63          @Override
64          public ByteBuffer visit(HttpResponse msg) {
65              StringBuilder sb = new StringBuilder(msg.getStatus().line());
66  
67              for (Map.Entry<String, String> header : msg.getHeaders().entrySet()) {
68                  sb.append(header.getKey());
69                  sb.append(": ");
70                  sb.append(header.getValue());
71                  sb.append("\r\n");
72              }
73              sb.append("\r\n");
74              byte[] bytes = sb.toString().getBytes(Charset.forName("UTF-8"));
75              return ByteBuffer.wrap(bytes);
76          }
77  
78          @Override
79          public ByteBuffer visit(HttpContentChunk msg) {
80              return msg.getContent();
81          }
82  
83          @Override
84          public ByteBuffer visit(HttpEndOfContent msg) {
85              return null;
86          }
87  
88          @Override
89          public ByteBuffer visit(HttpRequest msg) {
90              throw new IllegalStateException("cannot encode that on server side");
91          }
92      };
93  }