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.nio.charset.CharsetEncoder;
25  import java.util.Map;
26  
27  import org.apache.mina.core.buffer.IoBuffer;
28  import org.apache.mina.core.session.IoSession;
29  import org.apache.mina.filter.codec.ProtocolEncoder;
30  import org.apache.mina.filter.codec.ProtocolEncoderOutput;
31  import org.apache.mina.http.api.HttpEndOfContent;
32  import org.apache.mina.http.api.HttpRequest;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  public class HttpClientEncoder implements ProtocolEncoder {
37      private static final Logger LOG = LoggerFactory.getLogger(HttpClientCodec.class);
38      private static final CharsetEncoder ENCODER = Charset.forName("UTF-8").newEncoder();
39  
40      public void encode(IoSession session, Object message, ProtocolEncoderOutput out)
41              throws Exception {
42          LOG.debug("encode {}", message.getClass().getCanonicalName());
43          if (message instanceof HttpRequest) {
44              LOG.debug("HttpRequest");
45              HttpRequest msg = (HttpRequest)message;
46              StringBuilder sb = new StringBuilder(msg.getMethod().toString());
47              sb.append(" ");
48              sb.append(msg.getRequestPath());
49              if (!"".equals(msg.getQueryString())) {
50                  sb.append("?");
51                  sb.append(msg.getQueryString());
52              }
53              sb.append(" ");
54              sb.append(msg.getProtocolVersion());
55              sb.append("\r\n");
56  
57              for (Map.Entry<String, String> header : msg.getHeaders().entrySet()) {
58                  sb.append(header.getKey());
59                  sb.append(": ");
60                  sb.append(header.getValue());
61                  sb.append("\r\n");
62              }
63              sb.append("\r\n");
64              // Java 6 >> byte[] bytes = sb.toString().getBytes(Charset.forName("UTF-8"));
65              // byte[] bytes = sb.toString().getBytes();
66              // out.write(ByteBuffer.wrap(bytes));
67              IoBuffer buf = IoBuffer.allocate(sb.length()).setAutoExpand(true);
68              buf.putString(sb.toString(), ENCODER);
69              buf.flip();
70              out.write(buf);
71          } else if (message instanceof ByteBuffer) {
72              LOG.debug("Body");
73              out.write(message);
74          } else if (message instanceof HttpEndOfContent) {
75              LOG.debug("End of Content");
76              // end of HTTP content
77              // keep alive ?
78          }
79  
80      }
81  
82      public void dispose(IoSession arg0) throws Exception {
83          // TODO Auto-generated method stub
84  
85      }
86  
87  }