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.URL;
23: import org.apache.commons.codec.binary.Base64;
24: import org.apache.commons.httpclient.HttpClient;
25: import org.apache.commons.httpclient.HttpMethod;
26:
27: public abstract class HttpVerb {
28: private URL url;
29: private String user;
30: private String password;
31: private InputStream responseBody;
32: private int status = -1;
33:
34: public HttpVerb(URL url, String user, String password) {
35: this .url = url;
36: this .user = user;
37: this .password = password;
38: }
39:
40: public abstract int execute() throws HttpVerbException;
41:
42: protected abstract int getSuccessStatus();
43:
44: public boolean success() {
45: return getStatus() == getSuccessStatus();
46: }
47:
48: public boolean success(int status) {
49: return status == getSuccessStatus();
50: }
51:
52: protected void addAuthHeader(HttpMethod method) {
53: String credentials = getUser() + ":" + getPassword();
54: method.setRequestHeader("Authorization", "Basic "
55: + new String(Base64
56: .encodeBase64(credentials.getBytes())));
57: }
58:
59: protected URL getUrl() {
60: return url;
61: }
62:
63: protected String getUser() {
64: return user;
65: }
66:
67: protected String getPassword() {
68: return password;
69: }
70:
71: public InputStream getResponseBody() {
72: return responseBody;
73: }
74:
75: protected void setResponseBody(InputStream responseBody) {
76: this .responseBody = responseBody;
77: }
78:
79: public int getStatus() {
80: return status;
81: }
82:
83: protected void setStatus(int status) {
84: this.status = status;
85: }
86: }
|