SearchPromoteServiceImpl.java
8.56 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
195
196
197
198
199
200
201
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.httpclient.HttpClient
* org.apache.commons.httpclient.HttpMethod
* org.apache.commons.httpclient.methods.GetMethod
* org.apache.commons.lang3.StringUtils
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Deactivate
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.commons.osgi.OsgiUtil
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.searchpromote.impl;
import com.day.cq.searchpromote.SearchPromoteException;
import com.day.cq.searchpromote.SearchPromoteService;
import com.day.cq.searchpromote.impl.DecompressingMethod;
import com.day.cq.searchpromote.impl.SearchPromoteEnvironment;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.Dictionary;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, immediate=1, label="AEM 6 - Search&Promote Integration Configuration", description="Configuration options for Search&Promote integration")
@Service
public class SearchPromoteServiceImpl
implements SearchPromoteService {
private final Logger log;
@Property(value={"https://searchandpromote.omniture.com/px/"}, label="Remote server URI", description="Server URI for template XML requests")
private static final String SERVER_URI = "cq.searchpromote.configuration.server.uri";
private static final String DEFAULT_SP_ENVIRONMENT = "stage";
@Property(value={"stage"}, label="Search&Promote Environment", description="The Search&Promote environment environment to use (stage/live)")
private static final String SP_ENVIRONMENT = "cq.searchpromote.configuration.environment";
private SearchPromoteEnvironment environment;
private String stagedParam;
private URI serverUri;
public SearchPromoteServiceImpl() {
this.log = LoggerFactory.getLogger(this.getClass());
this.stagedParam = "";
}
@Override
public URI getServerURI() {
return this.serverUri;
}
@Override
public String getSearchForm(String memberid, String accountnumber) throws SearchPromoteException {
return this.getSearchForm(memberid, accountnumber, SearchPromoteEnvironment.STAGE);
}
@Override
public String getSearchForm(String memberId, String accountNumber, SearchPromoteEnvironment environment) throws SearchPromoteException {
String staged = environment.equals((Object)SearchPromoteEnvironment.STAGE) ? "&sp_staged=1" : "";
return this.executeMethod(new MethodWrapper(new DecompressingMethod((HttpMethod)new GetMethod(this.serverUri.toString() + "/searchform/?sp_id=" + memberId + "-" + accountNumber + "&sp_fn=xml" + staged))){
@Override
public String process(int status) throws SearchPromoteException, IOException {
switch (status) {
case 200: {
return this.getMethod().getResponseBodyAsString();
}
case 404: {
throw new SearchPromoteException("Credentials invalid");
}
}
throw new SearchPromoteException("Unexpected status received: " + status);
}
});
}
@Override
public String getFacetList(String memberid, String accountnumber) throws SearchPromoteException {
return this.executeMethod(new MethodWrapper(new DecompressingMethod((HttpMethod)new GetMethod(this.serverUri.toString() + "/design/?sp_id=" + memberid + "-" + accountnumber + "&sp_fn=facets" + this.stagedParam))){
@Override
public String process(int status) throws SearchPromoteException, IOException {
return this.getMethod().getResponseBodyAsString();
}
});
}
private String executeMethod(MethodWrapper wrapper) throws SearchPromoteException {
HttpClient client = new HttpClient();
try {
int status = client.executeMethod(wrapper.getMethod());
String string = wrapper.process(status);
return string;
}
catch (UnknownHostException e) {
this.log.error(e.getMessage(), (Throwable)e);
throw new SearchPromoteException("Unknown host: " + e.getMessage(), e);
}
catch (IOException e) {
this.log.error(e.getMessage(), (Throwable)e);
throw new SearchPromoteException(e.getMessage(), e);
}
finally {
wrapper.getMethod().releaseConnection();
}
}
@Activate
protected void activate(ComponentContext componentContext) {
this.log.debug("Starting SearchPromoteConfiguration...");
Dictionary properties = componentContext.getProperties();
String uri = OsgiUtil.toString(properties.get("cq.searchpromote.configuration.server.uri"), (String)"");
if (uri.endsWith("/")) {
uri = uri.substring(0, uri.lastIndexOf("/"));
}
try {
this.serverUri = new URI(uri);
}
catch (URISyntaxException e) {
this.log.error(e.getMessage(), (Throwable)e);
}
this.environment = SearchPromoteEnvironment.valueOf(OsgiUtil.toString(properties.get("cq.searchpromote.configuration.environment"), (String)"stage").toUpperCase());
this.log.debug("Using environment {}", (Object)this.environment);
if (this.environment.equals((Object)SearchPromoteEnvironment.STAGE)) {
this.stagedParam = "&sp_staged=1";
}
this.log.debug("...SearchPromoteConfiguration started");
}
@Deactivate
protected void deactivate(ComponentContext componentContext) {
this.log.debug("Stopping SearchPromoteConfiguration...");
this.log.debug("...SearchPromoteConfiguration stopped");
}
@Override
public String getBannerAreaList(String memberid, String accountnumber) throws SearchPromoteException {
return this.executeMethod(new MethodWrapper(new DecompressingMethod((HttpMethod)new GetMethod(this.serverUri + "/design/?sp_id=" + memberid + "-" + accountnumber + "&sp_fn=bannerareas" + this.stagedParam))){
@Override
public String process(int status) throws SearchPromoteException, IOException {
return this.getMethod().getResponseBodyAsString();
}
});
}
@Override
public String callRemoteIndex(String accountNumber, String remotePassword, boolean full) throws SearchPromoteException {
String operation = null;
operation = StringUtils.isNotEmpty((CharSequence)this.stagedParam) ? (full ? "full_staged_index" : "incremental_staged_index") : (full ? "full_index" : "incremental_index");
StringBuilder uri = new StringBuilder(this.stripToRoot(this.serverUri.toString()));
uri.append("/search/cgiindex.tk?sp_a=").append(accountNumber).append("&sp_password=").append(remotePassword).append("&sp_operation=").append(operation);
this.log.debug("Calling remote index from {}", (Object)uri.toString());
return this.executeMethod(new MethodWrapper(new DecompressingMethod((HttpMethod)new GetMethod(uri.toString()))){
@Override
public String process(int status) throws SearchPromoteException, IOException {
return this.getMethod().getResponseBodyAsString();
}
});
}
private String stripToRoot(String url) {
if (url.indexOf(47) != url.lastIndexOf(47)) {
return url.substring(0, url.lastIndexOf(47));
}
return url;
}
private abstract class MethodWrapper {
final HttpMethod method;
public MethodWrapper(HttpMethod method) {
this.method = method;
}
public HttpMethod getMethod() {
return this.method;
}
public abstract String process(int var1) throws SearchPromoteException, IOException;
}
}