1 /**********************************************************************
2 * HttpResponseParser.java
3 * created on 02.11.2006 by netseeker
4 * $Id: HttpResponseParser.java,v 1.1 2006/11/06 08:51:38 netseeker Exp $
5 * $Log: HttpResponseParser.java,v $
6 * Revision 1.1 2006/11/06 08:51:38 netseeker
7 * fixed java 1.4 support
8 * moved HTTP support classes to package de.netseeker.ejoe.http
9 *
10 * Revision 1.2 2006/11/05 23:56:27 netseeker
11 * added support of gzip encoding in case of http if the HTTP header does request it but the header byte did not (usually browser requests)
12 * added support of persistent http connections
13 *
14 * Revision 1.1 2006/11/05 16:30:37 netseeker
15 * finished the partial HTTP support
16 *
17 *
18 * ====================================================================
19 *
20 * Copyright 2005-2006 netseeker aka Michael Manske
21 *
22 * Licensed under the Apache License, Version 2.0 (the "License");
23 * you may not use this file except in compliance with the License.
24 * You may obtain a copy of the License at
25 *
26 * http://www.apache.org/licenses/LICENSE-2.0
27 *
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
33 * ====================================================================
34 *
35 * This file is part of the EJOE framework.
36 * For more information on the author, please see
37 * <http://www.manskes.de/>.
38 *
39 *********************************************************************/
40 package de.netseeker.ejoe.http;
41
42 import java.nio.ByteBuffer;
43 import java.util.logging.Level;
44 import java.util.logging.Logger;
45 import java.util.regex.Matcher;
46 import java.util.regex.Pattern;
47
48 /***
49 * Parser class for HTTP responses send by EJServer
50 *
51 * @author netseeker
52 * @since 0.3.9.1
53 */
54 public class HttpResponseParser extends HttpHeaderParser
55 {
56 private static final Logger logger = Logger.getLogger( HttpResponseParser.class.getName() );
57
58 private static Pattern pEncoding = Pattern.compile( ".*^Content-Type://s([^ ]+)+$.*", Pattern.CASE_INSENSITIVE
59 | Pattern.MULTILINE | Pattern.DOTALL );
60
61 private int code;
62
63 /***
64 * Creates a new parser using the given ByteBuffer containing HTTP response data
65 *
66 * @param buf ByteBuffer containing a HTTP response
67 */
68 public HttpResponseParser(ByteBuffer buf)
69 {
70 super( buf );
71 code = extractCode();
72 Matcher matcher = pEncoding.matcher( getCharHeader() );
73 setCompression( (matcher.matches() && (matcher.group( 1 ).indexOf( "gzip" ) > -1)) );
74 }
75
76 /***
77 * Returns the response status code, eg. 200 OK
78 *
79 * @return the code
80 */
81 public int getCode()
82 {
83 return code;
84 }
85
86 /***
87 * Extracts the http status code from the underlying buffer
88 *
89 * @return the response status code
90 */
91 protected int extractCode()
92 {
93 try
94 {
95 String[] tokens = pLineEnd.split( getCharHeader(), 2 );
96 tokens = tokens[0].split( " " );
97 return Integer.parseInt( tokens[1].trim() );
98 }
99 catch ( Exception e )
100 {
101 logger.log( Level.WARNING, "Failed to determine HTTP status code!", e );
102 }
103
104 return -1;
105 }
106
107 }