1 38 package com.gargoylesoftware.htmlunit; 39 40 import java.io.IOException ; 41 import java.io.InputStream ; 42 import java.io.UnsupportedEncodingException ; 43 import java.net.MalformedURLException ; 44 import java.net.URL ; 45 import java.util.Collections ; 46 import java.util.List ; 47 48 55 public class StringWebResponse implements WebResponse { 56 private final String content_; 57 private final String encoding_ = "ISO-8859-1"; 58 private final URL url_; 59 60 64 public StringWebResponse( final String content ) { 65 content_ = content; 66 try { 67 url_ = new URL ("http://HtmlUnitStringWebResponse"); 68 } 69 catch( final MalformedURLException e ) { 70 throw new IllegalStateException (e.toString()); 72 } 73 } 74 75 80 public StringWebResponse( final String content, final URL originatingURL ) { 81 content_ = content; 82 url_ = originatingURL; 83 } 84 85 90 public int getStatusCode() { 91 return 200; 92 } 93 94 99 public String getStatusMessage() { 100 return "OK"; 101 } 102 103 104 109 public String getContentType() { 110 return "text/html"; 111 } 112 113 114 119 public String getContentAsString() { 120 return content_; 121 } 122 123 124 130 public InputStream getContentAsStream() throws IOException { 131 return TextUtil.toInputStream( content_, encoding_ ); 132 } 133 134 135 140 public URL getUrl() { 141 return url_; 142 } 143 144 145 150 public List getResponseHeaders() { 151 return Collections.EMPTY_LIST; 152 } 153 154 155 161 public String getResponseHeaderValue( final String headerName ) { 162 return ""; 163 } 164 165 166 170 public long getLoadTimeInMilliSeconds() { 171 return 0; 172 } 173 174 178 public String getContentCharSet() { 179 return encoding_; 180 } 181 182 186 public byte[] getResponseBody() { 187 try { 188 return content_.getBytes(encoding_); 189 } 190 catch( final UnsupportedEncodingException e ) { 191 e.printStackTrace(); 192 return new byte[0]; 193 } 194 } 195 } 196 197 | Popular Tags |