Revision 038ac9a4 src/com/rackspace/cloud/servers/api/client/http/HttpBundle.java
b/src/com/rackspace/cloud/servers/api/client/http/HttpBundle.java | ||
---|---|---|
18 | 18 |
* if the call fails |
19 | 19 |
*/ |
20 | 20 |
public class HttpBundle { |
21 |
|
|
21 |
|
|
22 | 22 |
private String curlRequest; |
23 | 23 |
private HttpResponse response; |
24 |
|
|
25 |
public void setCurlRequest(HttpEntityEnclosingRequestBase request){ |
|
24 |
|
|
25 |
public void setCurlRequest(HttpEntityEnclosingRequestBase request) {
|
|
26 | 26 |
curlRequest = getCurl(request); |
27 | 27 |
} |
28 |
|
|
29 |
public void setCurlRequest(HttpRequestBase request){ |
|
28 |
|
|
29 |
public void setCurlRequest(HttpRequestBase request) {
|
|
30 | 30 |
curlRequest = getCurl(request); |
31 | 31 |
} |
32 |
|
|
33 |
public String getCurlRequest(){ |
|
32 |
|
|
33 |
public String getCurlRequest() {
|
|
34 | 34 |
return curlRequest; |
35 | 35 |
} |
36 |
|
|
37 |
public HttpResponse getResponse(){ |
|
36 |
|
|
37 |
public HttpResponse getResponse() {
|
|
38 | 38 |
return response; |
39 | 39 |
} |
40 |
|
|
41 |
public void setHttpResponse(HttpResponse response){ |
|
40 |
|
|
41 |
public void setHttpResponse(HttpResponse response) {
|
|
42 | 42 |
this.response = response; |
43 | 43 |
} |
44 |
|
|
45 |
public String getResponseText(){ |
|
44 |
|
|
45 |
public String getResponseText() {
|
|
46 | 46 |
HttpEntity responseEntity = response.getEntity(); |
47 | 47 |
StringBuilder result = new StringBuilder(); |
48 | 48 |
HeaderIterator itr = response.headerIterator(); |
49 |
while(itr.hasNext()){
|
|
49 |
while (itr.hasNext()) {
|
|
50 | 50 |
result.append(itr.nextHeader() + "\n"); |
51 | 51 |
} |
52 |
|
|
52 |
|
|
53 | 53 |
String xml = "\n\n"; |
54 | 54 |
try { |
55 | 55 |
xml = EntityUtils.toString(responseEntity); |
... | ... | |
66 | 66 |
} |
67 | 67 |
|
68 | 68 |
/* |
69 |
* convert the HttpRequest into the equivalent curl |
|
70 |
* statement |
|
69 |
* convert the HttpRequest into the equivalent curl statement |
|
71 | 70 |
*/ |
72 |
private String getCurl(HttpEntityEnclosingRequestBase message){ |
|
71 |
private String getCurl(HttpEntityEnclosingRequestBase message) {
|
|
73 | 72 |
StringBuilder result = new StringBuilder("curl -verbose -X "); |
74 | 73 |
result.append(message.getMethod()); |
75 | 74 |
HeaderIterator itr = message.headerIterator(); |
76 |
while(itr.hasNext()){
|
|
75 |
while (itr.hasNext()) {
|
|
77 | 76 |
Header header = itr.nextHeader(); |
78 | 77 |
String key = header.getName(); |
79 | 78 |
String value = header.getValue(); |
80 | 79 |
// protect authentication info from being exposed |
81 | 80 |
/**/ |
82 |
if(key.equals("X-Auth-Token") || key.equals("X-Auth-Key")){
|
|
81 |
if (key.equals("X-Auth-Token") || key.equals("X-Auth-Key")) {
|
|
83 | 82 |
value = "<secret>"; |
84 | 83 |
} |
85 | 84 |
/**/ |
... | ... | |
88 | 87 |
|
89 | 88 |
HttpEntity entity = message.getEntity(); |
90 | 89 |
String xmlBody = null; |
91 |
if(entity != null){
|
|
90 |
if (entity != null) {
|
|
92 | 91 |
try { |
93 | 92 |
xmlBody = EntityUtils.toString(entity); |
94 | 93 |
} catch (ParseException e) { |
... | ... | |
99 | 98 |
e.printStackTrace(); |
100 | 99 |
} |
101 | 100 |
} |
102 |
|
|
103 |
if(xmlBody != null && !xmlBody.equals("")){ |
|
104 |
result.append(" -d \"<?xml version=\"1.0\" encoding=\"UTF-8\"?> " + xmlBody + "\""); |
|
101 |
|
|
102 |
if (xmlBody != null && !xmlBody.equals("")) { |
|
103 |
result.append(" -d \"<?xml version=\"1.0\" encoding=\"UTF-8\"?> " |
|
104 |
+ xmlBody + "\""); |
|
105 | 105 |
} |
106 |
|
|
106 |
|
|
107 | 107 |
result.append(" " + message.getURI()); |
108 | 108 |
return result.toString(); |
109 | 109 |
} |
110 |
|
|
111 |
private String getCurl(HttpRequestBase message){ |
|
110 |
|
|
111 |
private String getCurl(HttpRequestBase message) {
|
|
112 | 112 |
StringBuilder result = new StringBuilder("curl -verbose -X "); |
113 | 113 |
result.append(message.getMethod()); |
114 | 114 |
HeaderIterator itr = message.headerIterator(); |
115 |
while(itr.hasNext()){
|
|
115 |
while (itr.hasNext()) {
|
|
116 | 116 |
Header header = itr.nextHeader(); |
117 | 117 |
String key = header.getName(); |
118 | 118 |
String value = header.getValue(); |
119 | 119 |
// protect authentication info from being exposed |
120 | 120 |
/**/ |
121 |
if(key.equals("X-Auth-Token") || key.equals("X-Auth-Key")){
|
|
121 |
if (key.equals("X-Auth-Token") || key.equals("X-Auth-Key")) {
|
|
122 | 122 |
value = "<secret>"; |
123 | 123 |
} |
124 | 124 |
/**/ |
125 | 125 |
result.append(" -H \"" + key + ": " + value + "\""); |
126 | 126 |
} |
127 | 127 |
|
128 |
/* no body for just HttpRequestBase |
|
129 |
HttpEntity entity = message.getEntity(); |
|
130 |
String xmlBody = null; |
|
131 |
try { |
|
132 |
xmlBody = EntityUtils.toString(entity); |
|
133 |
} catch (ParseException e) { |
|
134 |
// TODO Auto-generated catch block |
|
135 |
e.printStackTrace(); |
|
136 |
} catch (IOException e) { |
|
137 |
// TODO Auto-generated catch block |
|
138 |
e.printStackTrace(); |
|
139 |
} |
|
140 |
|
|
141 |
if(xmlBody != null && !xmlBody.equals("")){ |
|
142 |
result.append(" -d \"<?xml version=\"1.0\" encoding=\"UTF-8\"?> " + xmlBody + "\""); |
|
143 |
} |
|
144 |
*/ |
|
128 |
/* |
|
129 |
* no body for just HttpRequestBase HttpEntity entity = |
|
130 |
* message.getEntity(); String xmlBody = null; try { xmlBody = |
|
131 |
* EntityUtils.toString(entity); } catch (ParseException e) { // TODO |
|
132 |
* Auto-generated catch block e.printStackTrace(); } catch (IOException |
|
133 |
* e) { // TODO Auto-generated catch block e.printStackTrace(); } |
|
134 |
* |
|
135 |
* if(xmlBody != null && !xmlBody.equals("")){ |
|
136 |
* result.append(" -d \"<?xml version=\"1.0\" encoding=\"UTF-8\"?> " + |
|
137 |
* xmlBody + "\""); } |
|
138 |
*/ |
|
145 | 139 |
result.append(" " + message.getURI()); |
146 | 140 |
return result.toString(); |
147 | 141 |
} |
148 |
|
|
142 |
|
|
149 | 143 |
} |
Also available in: Unified diff