View Javadoc

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 org.apache.http.impl.nio.codecs;
29  
30  import org.apache.http.HttpException;
31  import org.apache.http.HttpResponse;
32  import org.apache.http.HttpResponseFactory;
33  import org.apache.http.ParseException;
34  import org.apache.http.StatusLine;
35  import org.apache.http.annotation.NotThreadSafe;
36  import org.apache.http.config.MessageConstraints;
37  import org.apache.http.impl.DefaultHttpResponseFactory;
38  import org.apache.http.message.BasicLineParser;
39  import org.apache.http.message.LineParser;
40  import org.apache.http.message.ParserCursor;
41  import org.apache.http.nio.NHttpMessageParser;
42  import org.apache.http.nio.reactor.SessionInputBuffer;
43  import org.apache.http.params.HttpParams;
44  import org.apache.http.util.Args;
45  import org.apache.http.util.CharArrayBuffer;
46  
47  /**
48   * Default {@link NHttpMessageParser} implementation for {@link HttpResponse}s.
49   *
50   * @since 4.1
51   */
52  @SuppressWarnings("deprecation")
53  @NotThreadSafe
54  public class DefaultHttpResponseParser extends AbstractMessageParser<HttpResponse> {
55  
56      private final HttpResponseFactory responseFactory;
57  
58      /**
59       * @deprecated (4.3) use
60       *   {@link DefaultHttpResponseParser#DefaultHttpResponseParser(
61       *   SessionInputBuffer, LineParser, HttpResponseFactory, MessageConstraints)}
62       */
63      @Deprecated
64      public DefaultHttpResponseParser(
65              final SessionInputBuffer buffer,
66              final LineParser parser,
67              final HttpResponseFactory responseFactory,
68              final HttpParams params) {
69          super(buffer, parser, params);
70          Args.notNull(responseFactory, "Response factory");
71          this.responseFactory = responseFactory;
72      }
73  
74      /**
75       * Creates an instance of DefaultHttpResponseParser.
76       *
77       * @param buffer the session input buffer.
78       * @param parser the line parser. If <code>null</code> {@link BasicLineParser#INSTANCE} will
79       *   be used.
80       * @param responseFactory the response factory. If <code>null</code>
81       *   {@link DefaultHttpResponseFactory#INSTANCE} will be used.
82       * @param constraints Message constraints. If <code>null</code>
83       *   {@link MessageConstraints#DEFAULT} will be used.
84       *
85       * @since 4.3
86       */
87      public DefaultHttpResponseParser(
88              final SessionInputBuffer buffer,
89              final LineParser parser,
90              final HttpResponseFactory responseFactory,
91              final MessageConstraints constraints) {
92          super(buffer, parser, constraints);
93          this.responseFactory = responseFactory != null ? responseFactory :
94              DefaultHttpResponseFactory.INSTANCE;
95      }
96  
97      /**
98       * @since 4.3
99       */
100     public DefaultHttpResponseParser(final SessionInputBuffer buffer, final MessageConstraints constraints) {
101         this(buffer, null, null, constraints);
102     }
103 
104     /**
105      * @since 4.3
106      */
107     public DefaultHttpResponseParser(final SessionInputBuffer buffer) {
108         this(buffer, null);
109     }
110 
111     @Override
112     protected HttpResponse createMessage(final CharArrayBuffer buffer)
113             throws HttpException, ParseException {
114         final ParserCursor cursor = new ParserCursor(0, buffer.length());
115         final StatusLine statusline = lineParser.parseStatusLine(buffer, cursor);
116         return this.responseFactory.newHttpResponse(statusline, null);
117     }
118 
119 }