001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/impl/entity/EntitySerializer.java $
003: * $Revision: 560343 $
004: * $Date: 2007-07-27 20:18:19 +0200 (Fri, 27 Jul 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.impl.entity;
033:
034: import java.io.IOException;
035: import java.io.OutputStream;
036:
037: import org.apache.http.HttpEntity;
038: import org.apache.http.HttpException;
039: import org.apache.http.HttpMessage;
040: import org.apache.http.entity.ContentLengthStrategy;
041: import org.apache.http.impl.io.ChunkedOutputStream;
042: import org.apache.http.impl.io.ContentLengthOutputStream;
043: import org.apache.http.impl.io.IdentityOutputStream;
044: import org.apache.http.io.SessionOutputBuffer;
045:
046: /**
047: * Default implementation of an entity serializer.
048: * <p>
049: * This entity serializer currently supports only "chunked" and "identitiy" transfer-coding</a>
050: * </p>
051: *
052: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
053: *
054: * @version $Revision: 560343 $
055: *
056: * @since 4.0
057: */
058: public class EntitySerializer {
059:
060: private final ContentLengthStrategy lenStrategy;
061:
062: public EntitySerializer(final ContentLengthStrategy lenStrategy) {
063: super ();
064: if (lenStrategy == null) {
065: throw new IllegalArgumentException(
066: "Content length strategy may not be null");
067: }
068: this .lenStrategy = lenStrategy;
069: }
070:
071: protected OutputStream doSerialize(
072: final SessionOutputBuffer outbuffer,
073: final HttpMessage message) throws HttpException,
074: IOException {
075: long len = this .lenStrategy.determineLength(message);
076: if (len == ContentLengthStrategy.CHUNKED) {
077: return new ChunkedOutputStream(outbuffer);
078: } else if (len == ContentLengthStrategy.IDENTITY) {
079: return new IdentityOutputStream(outbuffer);
080: } else {
081: return new ContentLengthOutputStream(outbuffer, len);
082: }
083: }
084:
085: public void serialize(final SessionOutputBuffer outbuffer,
086: final HttpMessage message, final HttpEntity entity)
087: throws HttpException, IOException {
088: if (outbuffer == null) {
089: throw new IllegalArgumentException(
090: "Session output buffer may not be null");
091: }
092: if (message == null) {
093: throw new IllegalArgumentException(
094: "HTTP message may not be null");
095: }
096: if (entity == null) {
097: throw new IllegalArgumentException(
098: "HTTP entity may not be null");
099: }
100: OutputStream outstream = doSerialize(outbuffer, message);
101: entity.writeTo(outstream);
102: outstream.close();
103: }
104:
105: }
|