-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathHttpCallTask.java
More file actions
194 lines (166 loc) · 6.74 KB
/
HttpCallTask.java
File metadata and controls
194 lines (166 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package net.authorize.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.concurrent.Callable;
import javax.xml.bind.JAXBException;
import javax.xml.bind.UnmarshalException;
import net.authorize.Environment;
import net.authorize.api.contract.v1.ANetApiRequest;
import net.authorize.api.contract.v1.ANetApiResponse;
import net.authorize.api.contract.v1.MessageTypeEnum;
import net.authorize.api.contract.v1.MessagesType;
import net.authorize.api.contract.v1.MessagesType.Message;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
//import net.authorize.api.controller.base.ErrorResponse;
/**
* Callable task to make http calls in future
* @author ramittal
*
*/
public class HttpCallTask implements Callable<ANetApiResponse> {
private static Log loggerSensitive = LogFactory.getLog(SensitiveLogger.class);
private static Log filteredLogger = new SensitiveFilterLogWrapper(LogFactory.getLog(HttpUtility.class));
Environment env = null;
ANetApiRequest request = null;
@SuppressWarnings("rawtypes")
Class classType = null;
//private static ANetApiResponse errorResponse = null;
private Message errorMessage = null;
/**
* Creates task to be called in future for making http call
* @param env Env to point to
* @param request Http request to send
* @param classType Expected response type if successful
*/
public <T> HttpCallTask(Environment env, ANetApiRequest request, Class<T> classType) {
this.env = env;
this.request = request;
this.classType = classType;
this.errorMessage = new Message();
}
@SuppressWarnings("unchecked")
/**
* Makes a http call, using the proxy if requested, and returns apiresponse
* with error code set appropriately
* @return ANetApiResponse successful or failed response
*/
public ANetApiResponse call() throws Exception {
ANetApiResponse response = null;
StringBuilder buffer = new StringBuilder();
org.apache.http.client.HttpClient httpCaller = null;
try {
HttpPost httppost = HttpUtility.createPostRequest(this.env, this.request);
httpCaller = HttpClient.getHttpsClient();
HttpResponse httpResponse = httpCaller.execute(httppost);
if ( null != httpResponse) {
if ( null != httpResponse.getStatusLine()) {
if ( 200 == httpResponse.getStatusLine().getStatusCode()) {
HttpEntity entity = httpResponse.getEntity();
// get the raw data being received
InputStream instream = entity.getContent();
buffer.append(HttpUtility.convertStreamToString(instream));
}
}
}
if (loggerSensitive.isDebugEnabled()) {
LogHelper.debug(loggerSensitive, "Raw Response: '%s'", buffer.toString());
} else if (filteredLogger.isDebugEnabled()) {
LogHelper.debug(filteredLogger, "Raw Response: '%s'", buffer.toString());
}
// handle HTTP errors
if (0 == buffer.length()) {
response = createErrorResponse(httpResponse, null);
} else { // i.e. if ( StringUtils.isNotEmpty(buffer.toString()))
Object localResponse = null;
try {
localResponse = XmlUtility.create(buffer.toString(), this.classType);
} catch(UnmarshalException ume) {
try {
//try deserializing to error message
localResponse = XmlUtility.create(buffer.toString(), net.authorize.api.contract.v1.ErrorResponse.class);
} catch(JAXBException jabex) {
response = createErrorResponse(httpResponse, jabex);
}
} catch(JAXBException jabex) {
response = createErrorResponse(httpResponse, jabex);
}
//ObjectFactory factory = new ObjectFactory();
//JAXBElement<ANetApiResponse> error = factory.createErrorResponse();
//check if error
if ( null == localResponse) {
try {
response = XmlUtility.create(buffer.toString(), ANetApiResponse.class);
} catch(JAXBException jabex) {
response = createErrorResponse(httpResponse, jabex);
}
} else {
if (localResponse instanceof ANetApiResponse)
{
response = (ANetApiResponse) localResponse;
} else {
LogHelper.warn( filteredLogger, "Unknown ResponseType: '%s'", localResponse);
}
}
}
} catch (ClientProtocolException cpe) {
response = createErrorResponse(null, cpe);
} catch (IOException ioe) {
response = createErrorResponse(null, ioe);
} finally {
if ( null != httpCaller) {
httpCaller.getConnectionManager().shutdown();
}
}
return response;
}
private ANetApiResponse createErrorResponse(HttpResponse httpResponse, Exception exception) {
ANetApiResponse response = new ANetApiResponse();
MessagesType aMessage = new MessagesType();
aMessage.setResultCode(MessageTypeEnum.ERROR);
response.setMessages(aMessage);
List<Message> messages = response.getMessages().getMessage();
//clear all messages
messages.clear();
setErrorResponse(messages, httpResponse);
setErrorResponse(messages, exception);
return response;
}
private void setErrorResponse(List<Message> messages, HttpResponse httpResponse) {
if ( null != httpResponse) {
messages.add(errorMessage);
String code = "Error";
String text = "Unknown Error";
if (null != httpResponse.getStatusLine())
{
LogHelper.warn( filteredLogger, "Error deserializing response to '%s'", this.classType);
code = String.format("%d", httpResponse.getStatusLine().getStatusCode());
if (null != httpResponse.getStatusLine().getReasonPhrase()) { text = httpResponse.getStatusLine().getReasonPhrase();}
}
setErrorMessageValues(code, text);
}
}
private void setErrorResponse(List<Message> messages, Exception exception) {
if ( null != exception) {
messages.add(errorMessage);
String code = "Error";
String text = "Unknown Error";
LogHelper.error( filteredLogger, "Http request execute failed: '%s'", exception.getMessage());
code = exception.getClass().getCanonicalName();
//code = exception.getClass().getTypeName();// requires java1.8
text = exception.getMessage();
setErrorMessageValues(code, text);
}
}
private void setErrorMessageValues(String code, String text) {
errorMessage.setCode(code);
errorMessage.setText(text);
LogHelper.warn(filteredLogger, "Adding ErrorMessage: Code: '%s', Text: '%s'", code, text);
}
}