001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.mina.http; 021 022import java.nio.ByteBuffer; 023import java.nio.charset.Charset; 024import java.nio.charset.CharsetEncoder; 025import java.util.Map; 026 027import org.apache.mina.core.buffer.IoBuffer; 028import org.apache.mina.core.session.IoSession; 029import org.apache.mina.filter.codec.ProtocolEncoder; 030import org.apache.mina.filter.codec.ProtocolEncoderOutput; 031import org.apache.mina.http.api.HttpEndOfContent; 032import org.apache.mina.http.api.HttpRequest; 033import org.slf4j.Logger; 034import org.slf4j.LoggerFactory; 035 036public class HttpClientEncoder implements ProtocolEncoder { 037 private static final Logger LOG = LoggerFactory.getLogger(HttpClientCodec.class); 038 private static final CharsetEncoder ENCODER = Charset.forName("UTF-8").newEncoder(); 039 040 public void encode(IoSession session, Object message, ProtocolEncoderOutput out) 041 throws Exception { 042 LOG.debug("encode {}", message.getClass().getCanonicalName()); 043 if (message instanceof HttpRequest) { 044 LOG.debug("HttpRequest"); 045 HttpRequest msg = (HttpRequest)message; 046 StringBuilder sb = new StringBuilder(msg.getMethod().toString()); 047 sb.append(" "); 048 sb.append(msg.getRequestPath()); 049 if (!"".equals(msg.getQueryString())) { 050 sb.append("?"); 051 sb.append(msg.getQueryString()); 052 } 053 sb.append(" "); 054 sb.append(msg.getProtocolVersion()); 055 sb.append("\r\n"); 056 057 for (Map.Entry<String, String> header : msg.getHeaders().entrySet()) { 058 sb.append(header.getKey()); 059 sb.append(": "); 060 sb.append(header.getValue()); 061 sb.append("\r\n"); 062 } 063 sb.append("\r\n"); 064 // Java 6 >> byte[] bytes = sb.toString().getBytes(Charset.forName("UTF-8")); 065 // byte[] bytes = sb.toString().getBytes(); 066 // out.write(ByteBuffer.wrap(bytes)); 067 IoBuffer buf = IoBuffer.allocate(sb.length()).setAutoExpand(true); 068 buf.putString(sb.toString(), ENCODER); 069 buf.flip(); 070 out.write(buf); 071 } else if (message instanceof ByteBuffer) { 072 LOG.debug("Body"); 073 out.write(message); 074 } else if (message instanceof HttpEndOfContent) { 075 LOG.debug("End of Content"); 076 // end of HTTP content 077 // keep alive ? 078 } 079 080 } 081 082 public void dispose(IoSession arg0) throws Exception { 083 // TODO Auto-generated method stub 084 085 } 086 087}