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 |
|
package info.magnolia.module.groovy.remote; |
35 |
|
|
36 |
|
import java.io.BufferedReader; |
37 |
|
import java.io.File; |
38 |
|
import java.io.FileReader; |
39 |
|
import java.io.IOException; |
40 |
|
import java.io.InputStreamReader; |
41 |
|
import java.io.UnsupportedEncodingException; |
42 |
|
import java.util.ArrayList; |
43 |
|
import java.util.List; |
44 |
|
|
45 |
|
import org.apache.http.HttpEntity; |
46 |
|
import org.apache.http.HttpResponse; |
47 |
|
import org.apache.http.NameValuePair; |
48 |
|
import org.apache.http.client.ClientProtocolException; |
49 |
|
import org.apache.http.client.entity.UrlEncodedFormEntity; |
50 |
|
import org.apache.http.client.methods.HttpGet; |
51 |
|
import org.apache.http.client.methods.HttpPost; |
52 |
|
import org.apache.http.impl.client.DefaultHttpClient; |
53 |
|
import org.apache.http.message.BasicNameValuePair; |
54 |
|
import org.apache.http.protocol.HTTP; |
55 |
|
import org.apache.http.util.EntityUtils; |
56 |
|
import org.slf4j.Logger; |
57 |
|
import org.slf4j.LoggerFactory; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
|
|
| 0% |
Uncovered Elements: 136 (136) |
Complexity: 29 |
Complexity Density: 0.27 |
|
63 |
|
public class RemoteClientConsole { |
64 |
|
|
65 |
|
private static final Logger log = LoggerFactory.getLogger(RemoteClientConsole.class); |
66 |
|
|
67 |
|
private final String logingUri = "/.magnolia/pages/adminCentral.html?"; |
68 |
|
private final String groovyUri = "/.magnolia/pages/groovyInteractiveConsole.html?"; |
69 |
|
|
70 |
|
private DefaultHttpClient httpclient; |
71 |
|
private String magnoliaUri; |
72 |
|
private String user; |
73 |
|
private String password; |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
@param |
79 |
|
@param |
80 |
|
@param |
81 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
82 |
0 |
public RemoteClientConsole(String magnoliaUri, String user, String password) {... |
83 |
0 |
super(); |
84 |
0 |
connect(magnoliaUri, user, password); |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
@param |
92 |
|
@param |
93 |
|
@param |
94 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
95 |
0 |
public boolean connect(String magnoliaUri, String user, String password) {... |
96 |
0 |
this.user = user; |
97 |
0 |
this.password = password; |
98 |
0 |
this.magnoliaUri = magnoliaUri; |
99 |
0 |
log.debug("set private global variable magnoliaUri='" + magnoliaUri + "', user='" + user + "', passwoed='" + password + "'"); |
100 |
0 |
return connect(); |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
|
|
| 0% |
Uncovered Elements: 34 (34) |
Complexity: 6 |
Complexity Density: 0.2 |
|
107 |
0 |
private boolean connect() {... |
108 |
|
|
109 |
0 |
boolean res = false; |
110 |
0 |
HttpPost httpost = null; |
111 |
0 |
HttpResponse response = null; |
112 |
0 |
HttpEntity entity = null; |
113 |
0 |
log.debug("Try to connect to magnoliaUri='" + magnoliaUri); |
114 |
|
|
115 |
0 |
if (isConnected()) { |
116 |
0 |
log.warn("Still connected as we try to create a new connection. Will be disconnected first"); |
117 |
0 |
disconnect(); |
118 |
|
} |
119 |
|
|
120 |
0 |
try { |
121 |
0 |
httpclient = new DefaultHttpClient(); |
122 |
|
|
123 |
0 |
httpost = new HttpPost(this.magnoliaUri + logingUri); |
124 |
|
|
125 |
0 |
List<NameValuePair> nvps = new ArrayList<NameValuePair>(); |
126 |
0 |
nvps.add(new BasicNameValuePair("mgnlUserId", this.user)); |
127 |
0 |
nvps.add(new BasicNameValuePair("mgnlUserPSWD", this.password)); |
128 |
0 |
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); |
129 |
|
|
130 |
0 |
response = httpclient.execute(httpost); |
131 |
|
|
132 |
0 |
System.out.println("Login form get: " + response.getStatusLine()); |
133 |
0 |
if (response.getStatusLine().toString().endsWith("200 OK")) { |
134 |
0 |
res = true; |
135 |
0 |
log.debug("Correctly connected to magnoliaUri='" + magnoliaUri); |
136 |
|
} else { |
137 |
0 |
log.warn("Could not establish a connection to magnoliaUri='" + magnoliaUri); |
138 |
|
} |
139 |
|
|
140 |
0 |
entity = response.getEntity(); |
141 |
0 |
EntityUtils.consume(entity); |
142 |
|
|
143 |
|
} catch (ClientProtocolException cpe) { |
144 |
0 |
log.error("", cpe); |
145 |
|
|
146 |
0 |
cpe.printStackTrace(); |
147 |
|
} catch (UnsupportedEncodingException uee) { |
148 |
0 |
log.error("", uee); |
149 |
|
|
150 |
0 |
uee.printStackTrace(); |
151 |
|
} catch (IOException ioe) { |
152 |
0 |
log.error("", ioe); |
153 |
|
|
154 |
0 |
ioe.printStackTrace(); |
155 |
|
} |
156 |
|
|
157 |
0 |
return res; |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
@return |
164 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
165 |
0 |
public boolean disconnect() {... |
166 |
0 |
boolean res = false; |
167 |
0 |
if (httpclient != null && httpclient.getConnectionManager() != null) { |
168 |
0 |
httpclient.getConnectionManager().shutdown(); |
169 |
0 |
log.debug("Correctly disconnected from magnoliaUri='" + magnoliaUri); |
170 |
0 |
res = true; |
171 |
|
} |
172 |
0 |
return res; |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
@return |
179 |
|
|
|
|
| 0% |
Uncovered Elements: 23 (23) |
Complexity: 6 |
Complexity Density: 0.32 |
|
180 |
0 |
public boolean isConnected() {... |
181 |
|
|
182 |
0 |
boolean res = false; |
183 |
0 |
HttpGet httpget = null; |
184 |
0 |
HttpEntity entity = null; |
185 |
0 |
try { |
186 |
0 |
httpget = new HttpGet(magnoliaUri); |
187 |
0 |
if (httpclient == null) |
188 |
0 |
return res; |
189 |
0 |
HttpResponse response = httpclient.execute(httpget); |
190 |
|
|
191 |
|
|
192 |
0 |
if (!response.getStatusLine().toString().endsWith("401 Unauthorized")) { |
193 |
0 |
res = true; |
194 |
|
} |
195 |
|
|
196 |
0 |
entity = response.getEntity(); |
197 |
0 |
EntityUtils.consume(entity); |
198 |
|
|
199 |
|
} catch (ClientProtocolException cpe) { |
200 |
0 |
log.error("", cpe); |
201 |
|
|
202 |
0 |
cpe.printStackTrace(); |
203 |
|
} catch (UnsupportedEncodingException uee) { |
204 |
0 |
log.error("", uee); |
205 |
|
|
206 |
0 |
uee.printStackTrace(); |
207 |
|
} catch (IOException ioe) { |
208 |
0 |
log.error("", ioe); |
209 |
|
|
210 |
0 |
ioe.printStackTrace(); |
211 |
|
} |
212 |
|
|
213 |
0 |
return res; |
214 |
|
} |
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
@param |
221 |
|
@return |
222 |
|
|
|
|
| 0% |
Uncovered Elements: 45 (45) |
Complexity: 10 |
Complexity Density: 0.29 |
|
223 |
0 |
public String execute(Object inputObject) {... |
224 |
|
|
225 |
0 |
HttpPost httpost = null; |
226 |
0 |
HttpResponse response = null; |
227 |
0 |
StringBuffer sb = new StringBuffer(); |
228 |
0 |
BufferedReader rd = null; |
229 |
0 |
HttpEntity entity = null; |
230 |
|
|
231 |
0 |
try { |
232 |
|
|
233 |
0 |
httpost = new HttpPost(magnoliaUri + groovyUri); |
234 |
|
|
235 |
0 |
List<NameValuePair> nvps = new ArrayList<NameValuePair>(); |
236 |
0 |
if (inputObject instanceof File) { |
237 |
0 |
nvps.add(new BasicNameValuePair("code", readFileAsString((File) inputObject))); |
238 |
0 |
} else if (inputObject instanceof String) { |
239 |
0 |
nvps.add(new BasicNameValuePair("code", (String) inputObject)); |
240 |
|
} else { |
241 |
0 |
return "Input parameter is not a String or a File"; |
242 |
|
} |
243 |
0 |
nvps.add(new BasicNameValuePair("command", "evaluateGroovy")); |
244 |
0 |
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); |
245 |
|
|
246 |
0 |
response = httpclient.execute(httpost); |
247 |
|
|
248 |
|
|
249 |
0 |
entity = response.getEntity(); |
250 |
0 |
if (entity != null) { |
251 |
|
|
252 |
0 |
rd = new BufferedReader(new InputStreamReader(entity.getContent())); |
253 |
0 |
String line; |
254 |
0 |
while ((line = rd.readLine()) != null) { |
255 |
0 |
sb.append(line); |
256 |
|
} |
257 |
0 |
EntityUtils.consume(entity); |
258 |
|
} |
259 |
|
else { |
260 |
0 |
sb.append("No result"); |
261 |
|
} |
262 |
|
|
263 |
|
} catch (ClientProtocolException cpe) { |
264 |
0 |
log.error("", cpe); |
265 |
|
|
266 |
0 |
cpe.printStackTrace(); |
267 |
|
} catch (UnsupportedEncodingException uee) { |
268 |
0 |
log.error("", uee); |
269 |
|
|
270 |
0 |
uee.printStackTrace(); |
271 |
|
} catch (IOException ioe) { |
272 |
0 |
log.error("", ioe); |
273 |
|
|
274 |
0 |
ioe.printStackTrace(); |
275 |
|
} finally { |
276 |
0 |
if (rd != null) { |
277 |
0 |
try { |
278 |
0 |
rd.close(); |
279 |
|
} catch (IOException e) { |
280 |
|
|
281 |
0 |
e.printStackTrace(); |
282 |
|
} |
283 |
|
} |
284 |
|
} |
285 |
0 |
return sb.toString(); |
286 |
|
|
287 |
|
} |
288 |
|
|
289 |
|
|
290 |
|
|
291 |
|
|
292 |
|
@throws |
293 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 2 |
Complexity Density: 0.2 |
|
294 |
0 |
private String readFileAsString(File file) throws java.io.IOException {... |
295 |
0 |
StringBuffer fileData = new StringBuffer(1000); |
296 |
0 |
BufferedReader reader = null; |
297 |
0 |
try { |
298 |
0 |
reader = new BufferedReader(new FileReader(file)); |
299 |
0 |
char[] buf = new char[1024]; |
300 |
0 |
int numRead = 0; |
301 |
0 |
while ((numRead = reader.read(buf)) != -1) { |
302 |
0 |
fileData.append(buf, 0, numRead); |
303 |
|
} |
304 |
|
} finally { |
305 |
0 |
reader.close(); |
306 |
|
} |
307 |
|
|
308 |
0 |
return fileData.toString(); |
309 |
|
} |
310 |
|
} |