SiteCatalystDataInsertionClient.java
4.87 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.httpclient.HttpClient
* org.apache.commons.httpclient.HttpConnectionManager
* org.apache.commons.httpclient.HttpMethod
* org.apache.commons.httpclient.MultiThreadedHttpConnectionManager
* org.apache.commons.httpclient.methods.GetMethod
* org.apache.commons.httpclient.methods.PostMethod
* org.apache.commons.httpclient.methods.RequestEntity
* org.apache.commons.httpclient.methods.StringRequestEntity
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.analytics.sitecatalyst.datainsertion.impl;
import com.day.cq.analytics.sitecatalyst.SitecatalystException;
import com.day.cq.analytics.sitecatalyst.datainsertion.DataInsertionClient;
import com.day.cq.analytics.sitecatalyst.datainsertion.DataInsertionRequest;
import com.day.cq.analytics.sitecatalyst.datainsertion.DataInsertionResponse;
import com.day.cq.analytics.sitecatalyst.datainsertion.impl.SiteCatalystDataInsertionResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SiteCatalystDataInsertionClient
implements DataInsertionClient {
private final Logger log;
private HttpClient httpClient;
public SiteCatalystDataInsertionClient() {
this.log = LoggerFactory.getLogger(this.getClass());
}
@Override
public DataInsertionResponse execute(DataInsertionRequest request, String url) throws SitecatalystException {
PostMethod method = new PostMethod(url);
try {
StringRequestEntity requestEntity = new StringRequestEntity(request.toString(), "application/x-www-form-urlencoded", "UTF-8");
method.setRequestEntity((RequestEntity)requestEntity);
int status = this.getHttpClient().executeMethod((HttpMethod)method);
switch (status) {
case 200: {
break;
}
default: {
throw new SitecatalystException("DataInsertion failed with status code [" + status + "]");
}
}
String response = this.consumeResponse((HttpMethod)method);
SiteCatalystDataInsertionResponse siteCatalystDataInsertionResponse = new SiteCatalystDataInsertionResponse(response);
return siteCatalystDataInsertionResponse;
}
catch (Exception e) {
this.log.error(e.getMessage(), (Throwable)e);
throw new SitecatalystException(e.getMessage(), e);
}
finally {
method.releaseConnection();
}
}
/*
* Enabled force condition propagation
* Lifted jumps to return sites
*/
@Override
public void execute(String url) throws SitecatalystException {
GetMethod method = new GetMethod(url);
try {
int status = this.getHttpClient().executeMethod((HttpMethod)method);
switch (status) {
case 200: {
return;
}
default: {
throw new SitecatalystException("DataInsertion failed with status code [" + status + "]");
}
}
}
catch (Exception e) {
this.log.error(e.getMessage(), (Throwable)e);
throw new SitecatalystException(e.getMessage(), e);
}
finally {
method.releaseConnection();
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private String consumeResponse(HttpMethod method) throws IOException {
StringBuffer sb;
sb = new StringBuffer();
InputStream ins = null;
try {
String line;
ins = method.getResponseBodyAsStream();
BufferedReader responseReader = new BufferedReader(new InputStreamReader(ins));
while ((line = responseReader.readLine()) != null) {
sb.append(line);
}
}
finally {
if (ins != null) {
ins.close();
}
}
return sb.toString();
}
private HttpClient getHttpClient() {
if (this.httpClient == null) {
this.httpClient = new HttpClient((HttpConnectionManager)new MultiThreadedHttpConnectionManager());
}
return this.httpClient;
}
}