root / src / com / rackspace / cloud / servers / api / client / http / HttpBundle.java @ 378fe36a
History | View | Annotate | Download (4.2 kB)
1 |
package com.rackspace.cloud.servers.api.client.http; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
|
5 |
import org.apache.http.Header; |
6 |
import org.apache.http.HeaderIterator; |
7 |
import org.apache.http.HttpEntity; |
8 |
import org.apache.http.HttpResponse; |
9 |
import org.apache.http.ParseException; |
10 |
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; |
11 |
import org.apache.http.client.methods.HttpRequestBase; |
12 |
import org.apache.http.util.EntityUtils; |
13 |
|
14 |
/*
|
15 |
* HttpBundle stores a request and response for
|
16 |
* a call it allows you to be able to display the
|
17 |
* curl request and the http response to the user
|
18 |
* if the call fails
|
19 |
*/
|
20 |
public class HttpBundle { |
21 |
|
22 |
private String curlRequest; |
23 |
private HttpResponse response;
|
24 |
private String responseTextStatic; |
25 |
private String curlText; |
26 |
|
27 |
public String getResponseTextStatic() { |
28 |
return responseTextStatic;
|
29 |
} |
30 |
|
31 |
|
32 |
|
33 |
public void parseResponse(){ |
34 |
this.responseTextStatic = getResponseText();
|
35 |
} |
36 |
|
37 |
public void setCurlRequest(HttpEntityEnclosingRequestBase request) { |
38 |
curlRequest = getCurl(request); |
39 |
} |
40 |
|
41 |
public void setCurlRequest(HttpRequestBase request) { |
42 |
curlRequest = getCurl(request); |
43 |
} |
44 |
|
45 |
public String getCurlRequest() { |
46 |
return curlRequest;
|
47 |
} |
48 |
|
49 |
public HttpResponse getResponse() {
|
50 |
return response;
|
51 |
} |
52 |
|
53 |
public void setHttpResponse(HttpResponse response) { |
54 |
this.response = response;
|
55 |
} |
56 |
|
57 |
public String getResponseText() { |
58 |
HttpEntity responseEntity = response.getEntity(); |
59 |
StringBuilder result = new StringBuilder(); |
60 |
HeaderIterator itr = response.headerIterator(); |
61 |
while (itr.hasNext()) {
|
62 |
result.append(itr.nextHeader() + "\n");
|
63 |
} |
64 |
|
65 |
String xml = "\n\n"; |
66 |
try {
|
67 |
xml = EntityUtils.toString(responseEntity); |
68 |
} catch (ParseException e1) { |
69 |
// TODO Auto-generated catch block
|
70 |
e1.printStackTrace(); |
71 |
} catch (IOException e1) { |
72 |
// TODO Auto-generated catch block
|
73 |
e1.printStackTrace(); |
74 |
} |
75 |
|
76 |
result.append(xml); |
77 |
return result.toString();
|
78 |
} |
79 |
|
80 |
/*
|
81 |
* convert the HttpRequest into the equivalent curl statement
|
82 |
*/
|
83 |
private String getCurl(HttpEntityEnclosingRequestBase message) { |
84 |
StringBuilder result = new StringBuilder("curl -verbose -X "); |
85 |
result.append(message.getMethod()); |
86 |
HeaderIterator itr = message.headerIterator(); |
87 |
while (itr.hasNext()) {
|
88 |
Header header = itr.nextHeader(); |
89 |
String key = header.getName();
|
90 |
String value = header.getValue();
|
91 |
// protect authentication info from being exposed
|
92 |
/**/
|
93 |
if (key.equals("X-Auth-Token") || key.equals("X-Auth-Key")) { |
94 |
value = "<secret>";
|
95 |
} |
96 |
/**/
|
97 |
result.append(" -H \"" + key + ": " + value + "\""); |
98 |
} |
99 |
|
100 |
HttpEntity entity = message.getEntity(); |
101 |
String xmlBody = null; |
102 |
if (entity != null) { |
103 |
try {
|
104 |
xmlBody = EntityUtils.toString(entity); |
105 |
} catch (ParseException e) { |
106 |
// TODO Auto-generated catch block
|
107 |
e.printStackTrace(); |
108 |
} catch (IOException e) { |
109 |
// TODO Auto-generated catch block
|
110 |
e.printStackTrace(); |
111 |
} |
112 |
} |
113 |
|
114 |
if (xmlBody != null && !xmlBody.equals("")) { |
115 |
result.append(" -d \"<?xml version=\"1.0\" encoding=\"UTF-8\"?> "
|
116 |
+ xmlBody + "\"");
|
117 |
} |
118 |
|
119 |
result.append(" " + message.getURI());
|
120 |
return result.toString();
|
121 |
} |
122 |
|
123 |
private String getCurl(HttpRequestBase message) { |
124 |
StringBuilder result = new StringBuilder("curl -verbose -X "); |
125 |
result.append(message.getMethod()); |
126 |
HeaderIterator itr = message.headerIterator(); |
127 |
while (itr.hasNext()) {
|
128 |
Header header = itr.nextHeader(); |
129 |
String key = header.getName();
|
130 |
String value = header.getValue();
|
131 |
// protect authentication info from being exposed
|
132 |
/**/
|
133 |
if (key.equals("X-Auth-Token") || key.equals("X-Auth-Key")) { |
134 |
value = "<secret>";
|
135 |
} |
136 |
/**/
|
137 |
result.append(" -H \"" + key + ": " + value + "\""); |
138 |
} |
139 |
|
140 |
/*
|
141 |
* no body for just HttpRequestBase HttpEntity entity =
|
142 |
* message.getEntity(); String xmlBody = null; try { xmlBody =
|
143 |
* EntityUtils.toString(entity); } catch (ParseException e) { // TODO
|
144 |
* Auto-generated catch block e.printStackTrace(); } catch (IOException
|
145 |
* e) { // TODO Auto-generated catch block e.printStackTrace(); }
|
146 |
*
|
147 |
* if(xmlBody != null && !xmlBody.equals("")){
|
148 |
* result.append(" -d \"<?xml version=\"1.0\" encoding=\"UTF-8\"?> " +
|
149 |
* xmlBody + "\""); }
|
150 |
*/
|
151 |
result.append(" " + message.getURI());
|
152 |
return result.toString();
|
153 |
} |
154 |
|
155 |
} |