001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.geoserver.ows.util;
006:
007: import java.io.IOException;
008: import java.io.Writer;
009:
010: /**
011: * Utility class performing operations related to http respones.
012: *
013: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
014: *
015: */
016: public class ResponseUtils {
017: /**
018: * Parses the passed string, and encodes the special characters (used in
019: * xml for special purposes) with the appropriate codes. e.g. '<' is
020: * changed to '<'
021: *
022: * @param inData The string to encode into xml.
023: *
024: * @return the encoded string. Returns null, if null is passed as argument
025: *
026: */
027: public static String encodeXML(String inData) {
028: //return null, if null is passed as argument
029: if (inData == null) {
030: return null;
031: }
032:
033: //if no special characters, just return
034: //(for optimization. Though may be an overhead, but for most of the
035: //strings, this will save time)
036: if ((inData.indexOf('&') == -1) && (inData.indexOf('<') == -1)
037: && (inData.indexOf('>') == -1)
038: && (inData.indexOf('\'') == -1)
039: && (inData.indexOf('\"') == -1)) {
040: return inData;
041: }
042:
043: //get the length of input String
044: int length = inData.length();
045:
046: //create a StringBuffer of double the size (size is just for guidance
047: //so as to reduce increase-capacity operations. The actual size of
048: //the resulting string may be even greater than we specified, but is
049: //extremely rare)
050: StringBuffer buffer = new StringBuffer(2 * length);
051:
052: char charToCompare;
053:
054: //iterate over the input String
055: for (int i = 0; i < length; i++) {
056: charToCompare = inData.charAt(i);
057:
058: //if the ith character is special character, replace by code
059: if (charToCompare == '&') {
060: buffer.append("&");
061: } else if (charToCompare == '<') {
062: buffer.append("<");
063: } else if (charToCompare == '>') {
064: buffer.append(">");
065: } else if (charToCompare == '\"') {
066: buffer.append(""");
067: } else if (charToCompare == '\'') {
068: buffer.append("'");
069: } else {
070: buffer.append(charToCompare);
071: }
072: }
073:
074: //return the encoded string
075: return buffer.toString();
076: }
077:
078: /**
079: * Writes <CODE>string</CODE> into writer, escaping &, ', ", <, and >
080: * with the XML excape strings.
081: */
082: public static void writeEscapedString(Writer writer, String string)
083: throws IOException {
084: for (int i = 0; i < string.length(); i++) {
085: char c = string.charAt(i);
086:
087: if (c == '<') {
088: writer.write("<");
089: } else if (c == '>') {
090: writer.write(">");
091: } else if (c == '&') {
092: writer.write("&");
093: } else if (c == '\'') {
094: writer.write("'");
095: } else if (c == '"') {
096: writer.write(""");
097: } else {
098: writer.write(c);
099: }
100: }
101: }
102:
103: /**
104: * Appends a query string to a url.
105: * <p>
106: * This method checks <code>url</code> to see if the appended query string requires a '?' or
107: * '&' to be prepended.
108: * </p>
109: *
110: * @param url The base url.
111: * @param queryString The query string to be appended, should not contain the '?' character.
112: *
113: * @return A full url with the query string appended.
114: *
115: * TODO: remove this and replace with Requetss.appendQueryString
116: */
117: public static String appendQueryString(String url,
118: String queryString) {
119: if (url.endsWith("?") || url.endsWith("&")) {
120: return url + queryString;
121: }
122:
123: if (url.indexOf('?') != -1) {
124: return url + "&" + queryString;
125: }
126:
127: return url + "?" + queryString;
128: }
129:
130: /**
131: * Strips the query string off a request url.
132: *
133: * @param url The url.
134: *
135: * @return The original minus the query string.
136: */
137: public static String stripQueryString(String url) {
138: int index = url.indexOf('?');
139:
140: if (index == -1) {
141: return url;
142: }
143:
144: return url.substring(0, index);
145: }
146:
147: /**
148: * Appends a path tpo a url.
149: * <p>
150: * This method checks <code>url</code> to see if the appended path requires a '/' to be
151: * prepended.
152: * </p>
153: * @param url The base url.
154: * @param path The path to be appended to the url.
155: *
156: * @return The full url with the path appended.
157: * TODO: remove this and replace with Requetss.appendContextPath
158: */
159: public static String appendPath(String url, String path) {
160: if (path.startsWith("/")) {
161: path = path.substring(1);
162: }
163:
164: return url.endsWith("/") ? (url + path) : (url + "/" + path);
165: }
166: }
|