1 22 23 package org.apache.webdav.lib.methods; 24 25 import java.io.ByteArrayInputStream ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.OutputStream ; 32 import java.net.URL ; 33 import org.apache.commons.httpclient.ChunkedOutputStream; 34 import org.apache.commons.httpclient.HttpConnection; 35 import org.apache.commons.httpclient.HttpConstants; 36 import org.apache.commons.httpclient.HttpException; 37 import org.apache.commons.httpclient.HttpMethodBase; 38 import org.apache.commons.httpclient.HttpState; 39 import org.apache.commons.httpclient.HttpStatus; 40 41 42 48 public abstract class HttpRequestBodyMethodBase extends HttpMethodBase { 49 50 51 53 54 57 public HttpRequestBodyMethodBase() { 58 super(); 59 } 60 61 62 63 72 public HttpRequestBodyMethodBase(String uri) { 73 super(uri); 74 } 75 76 77 79 80 83 private byte[] data = null; 84 85 86 89 private File file = null; 90 91 92 95 private URL url = null; 96 97 98 100 101 106 public void setRequestBody(File file) throws IOException { 107 checkNotUsed(); 108 this.file = file; 109 } 110 111 116 public void setRequestBody(URL url) throws IOException { 117 checkNotUsed(); 118 this.url = url; 119 } 120 121 126 public void setRequestBody(byte[] bodydata) { 127 checkNotUsed(); 128 this.data = bodydata; 129 } 130 131 136 public void setRequestBody(String bodydata) { 137 checkNotUsed(); 138 setRequestBody(HttpConstants.getContentBytes(bodydata, getRequestCharSet())); 139 } 140 141 149 public void setRequestBody(InputStream is) 150 throws IOException { 151 152 checkNotUsed(); 153 byte[] buffer = new byte[4096]; 154 ByteArrayOutputStream os = new ByteArrayOutputStream (); 155 int nb = 0; 156 while (true) { 157 nb = is.read(buffer); 158 if (nb == -1) { 159 break; 160 } 161 os.write(buffer, 0, nb); 162 } 163 data = os.toByteArray(); 164 } 165 166 172 public boolean readContinueCode() { 173 if (getStatusLine() == null) { 174 return false; 175 } 176 if(null != getRequestHeader("expect") && 177 getStatusLine().getStatusCode() != HttpStatus.SC_CONTINUE) { 178 return false; 179 } 180 return true; 181 } 182 183 191 protected boolean writeRequestBody(HttpState state, HttpConnection conn) 192 throws IOException , HttpException { 193 OutputStream out = conn.getRequestOutputStream(); 194 if (isHttp11() && (null == getRequestHeader("Content-Length"))) { 195 out = new ChunkedOutputStream(out); 196 } 197 198 InputStream inputStream = null; 199 if (file != null && file.exists()) { 200 inputStream = new FileInputStream (file); 201 } else if (url != null) { 202 inputStream = url.openConnection().getInputStream(); 203 } else if(data != null){ 204 inputStream = new ByteArrayInputStream (data); 205 } else { 206 return true; 207 } 208 209 byte[] buffer = new byte[4096]; 210 int nb = 0; 211 while (true) { 212 nb = inputStream.read(buffer); 213 if (nb == -1) { 214 break; 215 } 216 out.write(buffer, 0, nb); 217 } 218 out.flush(); 219 return true; 220 } 221 222 228 protected int getRequestContentLength() { 229 if(null != data) { 230 return data.length; 231 } else if(null != file && file.exists()) { 232 return (int)(file.length()); 233 } else if(url != null) { 234 return -1; 235 } else { 236 return 0; 237 } 238 } 239 240 241 246 protected boolean isRequestContentAlreadySet() { 247 return (data != null) || (file != null) || (url != null); 248 } 249 250 254 public void recycle() { 255 super.recycle(); 256 data = null; 257 url = null; 258 file = null; 259 } 260 } 261 262 | Popular Tags |