001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027 028package org.apache.http.impl.io; 029 030import java.io.IOException; 031 032import org.apache.http.Header; 033import org.apache.http.HeaderIterator; 034import org.apache.http.HttpException; 035import org.apache.http.HttpMessage; 036import org.apache.http.io.HttpMessageWriter; 037import org.apache.http.io.SessionOutputBuffer; 038import org.apache.http.message.BasicLineFormatter; 039import org.apache.http.message.LineFormatter; 040import org.apache.http.params.HttpParams; 041import org.apache.http.util.Args; 042import org.apache.http.util.CharArrayBuffer; 043 044/** 045 * Abstract base class for HTTP message writers that serialize output to 046 * an instance of {@link SessionOutputBuffer}. 047 * 048 * @since 4.0 049 */ 050@SuppressWarnings("deprecation") 051public abstract class AbstractMessageWriter<T extends HttpMessage> implements HttpMessageWriter<T> { 052 053 protected final SessionOutputBuffer sessionBuffer; 054 protected final CharArrayBuffer lineBuf; 055 protected final LineFormatter lineFormatter; 056 057 /** 058 * Creates an instance of AbstractMessageWriter. 059 * 060 * @param buffer the session output buffer. 061 * @param formatter the line formatter. 062 * @param params HTTP parameters. 063 * 064 * @deprecated (4.3) use 065 * {@link AbstractMessageWriter#AbstractMessageWriter(SessionOutputBuffer, LineFormatter)} 066 */ 067 @Deprecated 068 public AbstractMessageWriter(final SessionOutputBuffer buffer, 069 final LineFormatter formatter, 070 final HttpParams params) { 071 super(); 072 Args.notNull(buffer, "Session input buffer"); 073 this.sessionBuffer = buffer; 074 this.lineBuf = new CharArrayBuffer(128); 075 this.lineFormatter = (formatter != null) ? formatter : BasicLineFormatter.INSTANCE; 076 } 077 078 /** 079 * Creates an instance of AbstractMessageWriter. 080 * 081 * @param buffer the session output buffer. 082 * @param formatter the line formatter If {@code null} {@link BasicLineFormatter#INSTANCE} 083 * will be used. 084 * 085 * @since 4.3 086 */ 087 public AbstractMessageWriter( 088 final SessionOutputBuffer buffer, 089 final LineFormatter formatter) { 090 super(); 091 this.sessionBuffer = Args.notNull(buffer, "Session input buffer"); 092 this.lineFormatter = (formatter != null) ? formatter : BasicLineFormatter.INSTANCE; 093 this.lineBuf = new CharArrayBuffer(128); 094 } 095 096 /** 097 * Subclasses must override this method to write out the first header line 098 * based on the {@link HttpMessage} passed as a parameter. 099 * 100 * @param message the message whose first line is to be written out. 101 * @throws IOException in case of an I/O error. 102 */ 103 protected abstract void writeHeadLine(T message) throws IOException; 104 105 @Override 106 public void write(final T message) throws IOException, HttpException { 107 Args.notNull(message, "HTTP message"); 108 writeHeadLine(message); 109 for (final HeaderIterator it = message.headerIterator(); it.hasNext(); ) { 110 final Header header = it.nextHeader(); 111 this.sessionBuffer.writeLine 112 (lineFormatter.formatHeader(this.lineBuf, header)); 113 } 114 this.lineBuf.clear(); 115 this.sessionBuffer.writeLine(this.lineBuf); 116 } 117 118}