diff options
author | Ade Lee <alee@redhat.com> | 2011-12-08 21:15:59 -0500 |
---|---|---|
committer | Ade Lee <alee@redhat.com> | 2011-12-08 21:15:59 -0500 |
commit | 171aaece4f23709d33d180cf36eb3af5e454b0c9 (patch) | |
tree | 1485f9f0a7bd10de4ff25030db575dbb8dafae74 /pki/base/silent/src/com/netscape/pkisilent/http/HTTPResponse.java | |
parent | adad2fcee8a29fdb82376fbce07dedb11fccc182 (diff) | |
download | pki-171aaece4f23709d33d180cf36eb3af5e454b0c9.tar.gz pki-171aaece4f23709d33d180cf36eb3af5e454b0c9.tar.xz pki-171aaece4f23709d33d180cf36eb3af5e454b0c9.zip |
Revert "Formatting"
This reverts commit 32150d3ee32f8ac27118af7c792794b538c78a2f.
Diffstat (limited to 'pki/base/silent/src/com/netscape/pkisilent/http/HTTPResponse.java')
-rw-r--r-- | pki/base/silent/src/com/netscape/pkisilent/http/HTTPResponse.java | 605 |
1 files changed, 337 insertions, 268 deletions
diff --git a/pki/base/silent/src/com/netscape/pkisilent/http/HTTPResponse.java b/pki/base/silent/src/com/netscape/pkisilent/http/HTTPResponse.java index 67a3cf745..08358f357 100644 --- a/pki/base/silent/src/com/netscape/pkisilent/http/HTTPResponse.java +++ b/pki/base/silent/src/com/netscape/pkisilent/http/HTTPResponse.java @@ -1,5 +1,4 @@ package com.netscape.pkisilent.http; - // --- BEGIN COPYRIGHT BLOCK --- // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -18,299 +17,369 @@ package com.netscape.pkisilent.http; // All rights reserved. // --- END COPYRIGHT BLOCK --- + import java.util.ArrayList; import java.util.StringTokenizer; import com.netscape.pkisilent.common.Utilities; -public class HTTPResponse { - // The set of cookie values included in this response. - ArrayList<String> cookieValueList; - - // The names of the headers included in this response. - ArrayList<String> headerNameList; - - // The values of the headers included in this response. - ArrayList<String> headerValueList; - - // The actual data associated with this response. - byte[] responseData; - - // The HTML document included in the response, if appropriate. - HTMLDocument htmlDocument; - - // The number of bytes contained in the content of the response. - int contentLength; - - // The HTTP status code for the response. - int statusCode; - - // The MIME type of the response. - String contentType; - - // The protocol version string for this response. - String protolVersion; - - // The response message for this response. - String responseMessage; - - // Parsed Content Name/Value pair info - ArrayList<String> contentName; - ArrayList<String> contentValue; - - /** - * Creates a new HTTP response with the provided status code. - * - * @param statusCode The HTTP status code for this response. - * @param protocolVersion The protocol and version for this response. - * @param responseMessage The message associated with this response. - */ - public HTTPResponse(int statusCode, String protocolVersion, - String responseMessage) { - this.statusCode = statusCode; - this.protolVersion = protocolVersion; - this.responseMessage = responseMessage; - - htmlDocument = null; - contentType = null; - contentLength = -1; - responseData = new byte[0]; - cookieValueList = new ArrayList<String>(); - headerNameList = new ArrayList<String>(); - headerValueList = new ArrayList<String>(); - contentName = new ArrayList<String>(); - contentValue = new ArrayList<String>(); - } - - /** - * Retrieves the status code for this HTTP response. - * - * @return The status code for this HTTP response. - */ - public int getStatusCode() { - return statusCode; - } - - /** - * Retrieves the protocol version for this HTTP response. - * - * @return The protocol version for this HTTP response. - */ - public String getProtocolVersion() { - return protolVersion; - } - - /** - * Retrieves the response message for this HTTP response. - * - * @return The response message for this HTTP response. - */ - public String getResponseMessage() { - return responseMessage; - } - /** - * Retrieves the value of the header with the specified name. If the - * specified header has more than one value, then only the first will be - * retrieved. - * - * @return The value of the header with the specified name, or - * <CODE>null</CODE> if no such header is available. - */ - public String getHeader(String headerName) { - String lowerName = headerName.toLowerCase(); - - for (int i = 0; i < headerNameList.size(); i++) { - if (lowerName.equals(headerNameList.get(i))) { - return headerValueList.get(i); - } - } - - return null; - } +public class HTTPResponse +{ + // The set of cookie values included in this response. + ArrayList<String> cookieValueList; - /** - * Retrieves the set of values for the specified header. - * - * @return The set of values for the specified header. - */ - public String[] getHeaderValues(String headerName) { - ArrayList<String> valueList = new ArrayList<String>(); - String lowerName = headerName.toLowerCase(); - - for (int i = 0; i < headerNameList.size(); i++) { - if (lowerName.equals(headerNameList.get(i))) { - valueList.add(headerValueList.get(i)); - } - } - - String[] values = new String[valueList.size()]; - valueList.toArray(values); - return values; - } + // The names of the headers included in this response. + ArrayList<String> headerNameList; - /** - * Adds a header with the given name and value to this response. - * - * @param headerName The name of the header to add to this response. - * @param headerValue The value of the header to add to this response. - */ - public void addHeader(String headerName, String headerValue) { - String lowerName = headerName.toLowerCase(); - headerNameList.add(lowerName); - headerValueList.add(headerValue); - - if (lowerName.equals("content-length")) { - try { - contentLength = Integer.parseInt(headerValue); - } catch (NumberFormatException nfe) { - } - } else if (lowerName.equals("content-type")) { - contentType = headerValue; - } else if (lowerName.equals("set-cookie")) { - cookieValueList.add(headerValue); - } - } - - /** - * Retrieves a two-dimensional array containing the header data for this - * response, with each element being an array containing a name/value pair. - * - * @return A two-dimensional array containing the header data for this - * response. - */ - public String[][] getHeaderElements() { - String[][] headerElements = new String[headerNameList.size()][2]; - for (int i = 0; i < headerNameList.size(); i++) { - headerElements[i][0] = headerNameList.get(i); - headerElements[i][1] = headerValueList.get(i); - } - - return headerElements; - } + // The values of the headers included in this response. + ArrayList<String> headerValueList; - /** - * Retrieves the raw data included in this HTTP response. If the response - * did not include any data, an empty array will be returned. - * - * @return The raw data included in this HTTP response. - */ - public byte[] getResponseData() { - return responseData; - } + // The actual data associated with this response. + byte[] responseData; - public String getHTML() { - String htmlString = new String(responseData); - return htmlString; - } + // The HTML document included in the response, if appropriate. + HTMLDocument htmlDocument; - public String getHTMLwithoutTags() { - String htmlString = new String(responseData); - HTMLDocument htmldocument = new HTMLDocument(htmlString); - return htmldocument.getTextData(); + // The number of bytes contained in the content of the response. + int contentLength; + + // The HTTP status code for the response. + int statusCode; + + // The MIME type of the response. + String contentType; + + // The protocol version string for this response. + String protolVersion; + + // The response message for this response. + String responseMessage; + + // Parsed Content Name/Value pair info + ArrayList<String> contentName; + ArrayList<String> contentValue; + + + + /** + * Creates a new HTTP response with the provided status code. + * + * @param statusCode The HTTP status code for this response. + * @param protocolVersion The protocol and version for this response. + * @param responseMessage The message associated with this response. + */ + public HTTPResponse(int statusCode, String protocolVersion, + String responseMessage) + { + this.statusCode = statusCode; + this.protolVersion = protocolVersion; + this.responseMessage = responseMessage; + + htmlDocument = null; + contentType = null; + contentLength = -1; + responseData = new byte[0]; + cookieValueList = new ArrayList<String>(); + headerNameList = new ArrayList<String>(); + headerValueList = new ArrayList<String>(); + contentName = new ArrayList<String>(); + contentValue = new ArrayList<String>(); + } + + + + /** + * Retrieves the status code for this HTTP response. + * + * @return The status code for this HTTP response. + */ + public int getStatusCode() + { + return statusCode; + } + + + + /** + * Retrieves the protocol version for this HTTP response. + * + * @return The protocol version for this HTTP response. + */ + public String getProtocolVersion() + { + return protolVersion; + } + + + + /** + * Retrieves the response message for this HTTP response. + * + * @return The response message for this HTTP response. + */ + public String getResponseMessage() + { + return responseMessage; + } + + + + /** + * Retrieves the value of the header with the specified name. If the + * specified header has more than one value, then only the first will be + * retrieved. + * + * @return The value of the header with the specified name, or + * <CODE>null</CODE> if no such header is available. + */ + public String getHeader(String headerName) + { + String lowerName = headerName.toLowerCase(); + + for (int i=0; i < headerNameList.size(); i++) + { + if (lowerName.equals(headerNameList.get(i))) + { + return headerValueList.get(i); + } } - public void parseContent() { - // parse the responseData byte[] buffer and split content into name - // value pair - String htmlString = new String(responseData); - StringTokenizer st = new StringTokenizer(htmlString, "\n"); - Utilities ut = new Utilities(); + return null; + } - while (st.hasMoreTokens()) { - String line = st.nextToken(); - // format for line assumed to be name="value"; format - int eqPos = line.indexOf('='); - if (eqPos != -1) { - String name = line.substring(0, eqPos); - String tempval = line.substring(eqPos + 1).trim(); - String value = ut.cleanupQuotes(ut.removechar(tempval)); - // add to array - this.contentName.add(name.trim()); - this.contentValue.add(value); - } - - } + /** + * Retrieves the set of values for the specified header. + * + * @return The set of values for the specified header. + */ + public String[] getHeaderValues(String headerName) + { + ArrayList<String> valueList = new ArrayList<String>(); + String lowerName = headerName.toLowerCase(); + for (int i=0; i < headerNameList.size(); i++) + { + if (lowerName.equals(headerNameList.get(i))) + { + valueList.add(headerValueList.get(i)); + } } - public String getContentValue(String headerName) { - for (int i = 0; i < contentName.size(); i++) { - if (headerName.equals(contentName.get(i))) { - return contentValue.get(i); - } - } - - return null; + String[] values = new String[valueList.size()]; + valueList.toArray(values); + return values; + } + + + + /** + * Adds a header with the given name and value to this response. + * + * @param headerName The name of the header to add to this response. + * @param headerValue The value of the header to add to this response. + */ + public void addHeader(String headerName, String headerValue) + { + String lowerName = headerName.toLowerCase(); + headerNameList.add(lowerName); + headerValueList.add(headerValue); + + if (lowerName.equals("content-length")) + { + try + { + contentLength = Integer.parseInt(headerValue); + } catch (NumberFormatException nfe) {} } - - public ArrayList<String> getContentNames() { - return contentName; - } - - public ArrayList<String> getContentValues() { - return contentValue; + else if (lowerName.equals("content-type")) + { + contentType = headerValue; } - - /** - * Sets the actual data associated with this response. - * - * @param responseData The actual data associated with this response. - */ - public void setResponseData(byte[] responseData) { - if (responseData == null) { - this.responseData = new byte[0]; - } else { - this.responseData = responseData; - } + else if (lowerName.equals("set-cookie")) + { + cookieValueList.add(headerValue); } - - /** - * Retrieves the content length associated with this response. - * - * @return The content length associated with this response, or -1 if no - * content length is available. - */ - public int getContentLength() { - return contentLength; + } + + + + /** + * Retrieves a two-dimensional array containing the header data for this + * response, with each element being an array containing a name/value pair. + * + * @return A two-dimensional array containing the header data for this + * response. + */ + public String[][] getHeaderElements() + { + String[][] headerElements = new String[headerNameList.size()][2]; + for (int i=0; i < headerNameList.size(); i++) + { + headerElements[i][0] = headerNameList.get(i); + headerElements[i][1] = headerValueList.get(i); } - /** - * Retrieves the content type associated with this response. - * - * @return The content type associated with this response, or - * <CODE>null</CODE> if no content type is available. - */ - public String getContentType() { - return contentType; + return headerElements; + } + + + + /** + * Retrieves the raw data included in this HTTP response. If the response did + * not include any data, an empty array will be returned. + * + * @return The raw data included in this HTTP response. + */ + public byte[] getResponseData() + { + return responseData; + } + + + public String getHTML() + { + String htmlString = new String(responseData); + return htmlString; + } + + public String getHTMLwithoutTags() + { + String htmlString = new String(responseData); + HTMLDocument htmldocument = new HTMLDocument(htmlString); + return htmldocument.getTextData(); + } + public void parseContent() + { + // parse the responseData byte[] buffer and split content into name + // value pair + String htmlString = new String(responseData); + StringTokenizer st = new StringTokenizer(htmlString, "\n"); + Utilities ut = new Utilities(); + + while(st.hasMoreTokens()) + { + String line = st.nextToken(); + // format for line assumed to be name="value"; format + + int eqPos = line.indexOf('=') ; + if(eqPos != -1) + { + String name = line.substring(0,eqPos); + String tempval = line.substring(eqPos+1).trim(); + String value = ut.cleanupQuotes(ut.removechar(tempval)); + + // add to array + this.contentName.add(name.trim()); + this.contentValue.add(value); + } + + } + + } + + + + public String getContentValue(String headerName) + { + for (int i=0; i < contentName.size(); i++) + { + if (headerName.equals(contentName.get(i))) + { + return contentValue.get(i); + } + } + + return null; + } + + public ArrayList<String> getContentNames() + { + return contentName; + } + + public ArrayList<String> getContentValues() + { + return contentValue; + } + + /** + * Sets the actual data associated with this response. + * + * @param responseData The actual data associated with this response. + */ + public void setResponseData(byte[] responseData) + { + if (responseData == null) + { + this.responseData = new byte[0]; } - - /** - * Retrieves an array containing the values of the cookies that should be - * set based on the information in this response. - * - * @return An array containing the values of the cookies that should be set - * based on the information in this response. - */ - public String[] getCookieValues() { - String[] cookieValues = new String[cookieValueList.size()]; - cookieValueList.toArray(cookieValues); - return cookieValues; + else + { + this.responseData = responseData; } + } + + + + /** + * Retrieves the content length associated with this response. + * + * @return The content length associated with this response, or -1 if no + * content length is available. + */ + public int getContentLength() + { + return contentLength; + } + + + + /** + * Retrieves the content type associated with this response. + * + * @return The content type associated with this response, or + * <CODE>null</CODE> if no content type is available. + */ + public String getContentType() + { + return contentType; + } + + + + /** + * Retrieves an array containing the values of the cookies that should be set + * based on the information in this response. + * + * @return An array containing the values of the cookies that should be set + * based on the information in this response. + */ + public String[] getCookieValues() + { + String[] cookieValues = new String[cookieValueList.size()]; + cookieValueList.toArray(cookieValues); + return cookieValues; + } + + public String getCookieValue(String headerName) + { + for (int i=0; i < cookieValueList.size(); i++) + { + System.out.println("cookie list: " + cookieValueList.get(i)); + + String temp = cookieValueList.get(i); + if (temp.startsWith(headerName)) + { + return cookieValueList.get(i); + } + } + + return null; + } - public String getCookieValue(String headerName) { - for (int i = 0; i < cookieValueList.size(); i++) { - System.out.println("cookie list: " + cookieValueList.get(i)); - - String temp = cookieValueList.get(i); - if (temp.startsWith(headerName)) { - return cookieValueList.get(i); - } - } - - return null; - } } + |