|
1 /* |
|
2 * ==================================================================== |
|
3 * Licensed to the Apache Software Foundation (ASF) under one |
|
4 * or more contributor license agreements. See the NOTICE file |
|
5 * distributed with this work for additional information |
|
6 * regarding copyright ownership. The ASF licenses this file |
|
7 * to you under the Apache License, Version 2.0 (the |
|
8 * "License"); you may not use this file except in compliance |
|
9 * with the License. You may obtain a copy of the License at |
|
10 * |
|
11 * http://www.apache.org/licenses/LICENSE-2.0 |
|
12 * |
|
13 * Unless required by applicable law or agreed to in writing, |
|
14 * software distributed under the License is distributed on an |
|
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
16 * KIND, either express or implied. See the License for the |
|
17 * specific language governing permissions and limitations |
|
18 * under the License. |
|
19 * ==================================================================== |
|
20 * |
|
21 * This software consists of voluntary contributions made by many |
|
22 * individuals on behalf of the Apache Software Foundation. For more |
|
23 * information on the Apache Software Foundation, please see |
|
24 * <http://www.apache.org/>. |
|
25 * |
|
26 */ |
|
27 |
|
28 package ch.boye.httpclientandroidlib.impl.io; |
|
29 |
|
30 import java.io.IOException; |
|
31 import java.util.Iterator; |
|
32 |
|
33 import ch.boye.httpclientandroidlib.Header; |
|
34 import ch.boye.httpclientandroidlib.HttpException; |
|
35 import ch.boye.httpclientandroidlib.HttpMessage; |
|
36 import ch.boye.httpclientandroidlib.io.HttpMessageWriter; |
|
37 import ch.boye.httpclientandroidlib.io.SessionOutputBuffer; |
|
38 import ch.boye.httpclientandroidlib.message.LineFormatter; |
|
39 import ch.boye.httpclientandroidlib.message.BasicLineFormatter; |
|
40 import ch.boye.httpclientandroidlib.params.HttpParams; |
|
41 import ch.boye.httpclientandroidlib.util.CharArrayBuffer; |
|
42 |
|
43 /** |
|
44 * Abstract base class for HTTP message writers that serialize output to |
|
45 * an instance of {@link SessionOutputBuffer}. |
|
46 * |
|
47 * @since 4.0 |
|
48 */ |
|
49 public abstract class AbstractMessageWriter implements HttpMessageWriter { |
|
50 |
|
51 protected final SessionOutputBuffer sessionBuffer; |
|
52 protected final CharArrayBuffer lineBuf; |
|
53 protected final LineFormatter lineFormatter; |
|
54 |
|
55 /** |
|
56 * Creates an instance of AbstractMessageWriter. |
|
57 * |
|
58 * @param buffer the session output buffer. |
|
59 * @param formatter the line formatter. |
|
60 * @param params HTTP parameters. |
|
61 */ |
|
62 public AbstractMessageWriter(final SessionOutputBuffer buffer, |
|
63 final LineFormatter formatter, |
|
64 final HttpParams params) { |
|
65 super(); |
|
66 if (buffer == null) { |
|
67 throw new IllegalArgumentException("Session input buffer may not be null"); |
|
68 } |
|
69 this.sessionBuffer = buffer; |
|
70 this.lineBuf = new CharArrayBuffer(128); |
|
71 this.lineFormatter = (formatter != null) ? |
|
72 formatter : BasicLineFormatter.DEFAULT; |
|
73 } |
|
74 |
|
75 /** |
|
76 * Subclasses must override this method to write out the first header line |
|
77 * based on the {@link HttpMessage} passed as a parameter. |
|
78 * |
|
79 * @param message the message whose first line is to be written out. |
|
80 * @throws IOException in case of an I/O error. |
|
81 */ |
|
82 protected abstract void writeHeadLine(HttpMessage message) throws IOException; |
|
83 |
|
84 public void write( |
|
85 final HttpMessage message) throws IOException, HttpException { |
|
86 if (message == null) { |
|
87 throw new IllegalArgumentException("HTTP message may not be null"); |
|
88 } |
|
89 writeHeadLine(message); |
|
90 for (Iterator it = message.headerIterator(); it.hasNext(); ) { |
|
91 Header header = (Header) it.next(); |
|
92 this.sessionBuffer.writeLine |
|
93 (lineFormatter.formatHeader(this.lineBuf, header)); |
|
94 } |
|
95 this.lineBuf.clear(); |
|
96 this.sessionBuffer.writeLine(this.lineBuf); |
|
97 } |
|
98 |
|
99 } |