01: /*
02: * Copyright ? 2006 Sun Microsystems, Inc. All rights reserved.
03: *
04: * Sun Microsystems, Inc. has intellectual property rights relating to
05: * technology embodied in the product that is described in this document.
06: * In particular, and without limitation, these intellectual property
07: * rights may include one or more of the U.S. patents listed at
08: * http://www.sun.com/patents and one or more additional patents or
09: * pending patent applications in the U.S. and in other countries.
10: *
11: * U.S. Government Rights - Commercial software. Government users are subject
12: * to the Sun Microsystems, Inc. standard license agreement and applicable
13: * provisions of the FAR and its supplements. Use is subject to license terms.
14: * This distribution may include materials developed by third parties.
15: * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
16: * trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
17: */
18: package com.sun.portal.app.blog.httpverb;
19:
20: import java.io.IOException;
21: import java.io.InputStream;
22: import java.net.HttpURLConnection;
23: import java.net.URL;
24: import org.apache.commons.httpclient.HttpClient;
25: import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
26: import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
27: import org.apache.commons.httpclient.methods.PutMethod;
28: import org.apache.commons.httpclient.methods.RequestEntity;
29:
30: public class Put extends HttpVerb {
31: private InputStream body;
32: private String contentType;
33:
34: public Put(URL url, String user, String password, InputStream body,
35: String contentType) {
36: super (url, user, password);
37: this .body = body;
38: this .contentType = contentType;
39: }
40:
41: protected int getSuccessStatus() {
42: return HttpURLConnection.HTTP_OK;
43: }
44:
45: public int execute() throws HttpVerbException {
46: try {
47: HttpClient httpClient = new HttpClient();
48: EntityEnclosingMethod method = new PutMethod(getUrl()
49: .toString());
50: addAuthHeader(method);
51:
52: RequestEntity re = new InputStreamRequestEntity(body);
53: method.setRequestEntity(re);
54:
55: method.setRequestHeader("Content-type", contentType);
56:
57: int status = httpClient.executeMethod(method);
58: setStatus(status);
59: setResponseBody(method.getResponseBodyAsStream());
60: return status;
61: } catch (IOException ioe) {
62: setStatus(-1);
63: throw new HttpVerbException(ioe);
64: }
65: }
66: }
|