forked from hpcc-systems/hpcc4j
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseHPCCWsClient.java
995 lines (865 loc) · 33.8 KB
/
BaseHPCCWsClient.java
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
package org.hpccsystems.ws.client;
import java.io.ByteArrayInputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.Stub;
import org.apache.axis2.kernel.http.HTTPConstants;
import org.apache.axis2.transport.http.impl.httpclient4.HttpTransportPropertiesImpl;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hpccsystems.ws.client.platform.Version;
import org.hpccsystems.ws.client.utils.Connection;
import org.hpccsystems.ws.client.utils.DataSingleton;
import org.hpccsystems.ws.client.utils.EqualsUtil;
import org.hpccsystems.ws.client.utils.HashCodeUtil;
import org.hpccsystems.ws.client.utils.Utils;
import org.hpccsystems.ws.client.wrappers.ArrayOfECLExceptionWrapper;
import org.hpccsystems.ws.client.wrappers.ArrayOfEspExceptionWrapper;
import org.hpccsystems.ws.client.wrappers.EspSoapFaultWrapper;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.SpanBuilder;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import io.opentelemetry.semconv.HttpAttributes;
import io.opentelemetry.semconv.ServerAttributes;
/**
* Defines functionality common to all HPCC Systems web service clients.
*
* Typically implemented by specialized HPCC Web service clients.
*/
public abstract class BaseHPCCWsClient extends DataSingleton
{
public static final String PROJECT_NAME = "WsClient";
private static OpenTelemetry globalOTel = null;
/** Constant <code>log</code> */
protected static final Logger log = LogManager.getLogger(BaseHPCCWsClient.class);
/** Constant <code>DEAFULTECLWATCHPORT="8010"</code> */
public static final String DEAFULTECLWATCHPORT = "8010";
/** Constant <code>DEFAULTECLWATCHTLSPORT="18010"</code> */
public static final String DEFAULTECLWATCHTLSPORT = "18010";
/** Constant <code>DEFAULTSERVICEPORT="DEAFULTECLWATCHPORT"</code> */
public static String DEFAULTSERVICEPORT = DEAFULTECLWATCHPORT;
protected Connection wsconn = null;
protected boolean verbose = false;
protected String initErrMessage = "";
protected Version targetHPCCBuildVersion = null;
protected Double targetESPInterfaceVer = null;
protected Boolean targetsContainerizedHPCC = null;
public boolean isTargetHPCCContainerized() throws Exception
{
if (targetsContainerizedHPCC == null)
{
if (wsconn == null)
throw new Exception("BaseHPCCWsClient: Cannot get target HPCC containerized mode, client connection has not been initialized.");
targetsContainerizedHPCC = getTargetHPCCIsContainerized(wsconn);
}
return targetsContainerizedHPCC;
}
@WithSpan
private boolean getTargetHPCCIsContainerized(Connection conn) throws Exception
{
if (wsconn == null)
throw new Exception("Cannot get target HPCC containerized mode, client connection has not been initialized.");
String response = wsconn.sendGetRequest("wssmc/getbuildinfo");//throws
if (response == null || response.isEmpty())
throw new Exception("Cannot get target HPCC containerized mode, received empty " + wsconn.getBaseUrl() + " wssmc/getbuildinfo response");
setUpContainerizedParser();
Document document = null;
synchronized(m_XMLParser)
{
document = m_XMLParser.parse(new ByteArrayInputStream(response.getBytes(StandardCharsets.UTF_8)));
}
if (document == null)
throw new Exception("Cannot parse HPCC isContainerizedMode response.");
NodeList namedValuesList = (NodeList) m_containerizedXpathExpression.evaluate(document,XPathConstants.NODESET);
for(int i = 0; i < namedValuesList.getLength(); i++)
{
Node ithNamedValuePair = namedValuesList.item(i);
NodeList nameAndValue = ithNamedValuePair.getChildNodes();
if (nameAndValue.getLength() == 2)
{
String name = null;
String value = null;
if (nameAndValue.item(0).getNodeName().equalsIgnoreCase("Name"))
{
name = nameAndValue.item(0).getFirstChild().getNodeValue();
value = nameAndValue.item(1).getFirstChild().getNodeValue();
}
else
{
name = nameAndValue.item(1).getFirstChild().getNodeValue();
value = nameAndValue.item(0).getFirstChild().getNodeValue();
}
if (name.equalsIgnoreCase("CONTAINERIZED"))
{
if (value.equalsIgnoreCase("ON"))
return true;
else
return false;
}
}
}
return false; //No CONTAINERIZED entry has to be assumed to mean target is not CONTAINERIZED
}
/**
* Gets the target HPCC build version
*
* @return the HPCC version
*/
public Version getTargetHPCCBuildVersion()
{
return targetHPCCBuildVersion;
}
@WithSpan
private String getTargetHPCCBuildVersionString() throws Exception
{
if (wsconn == null)
throw new Exception("Cannot get target HPCC build version, client connection has not been initialized.");
String response = wsconn.sendGetRequest("WsSMC/Activity?rawxml_");//throws IOException if http != ok
if (response == null || response.isEmpty())
throw new Exception("Cannot get target HPCC build version, received empty " + wsconn.getBaseUrl() + " wssmc/activity response");
String header = response.substring(0, 100).trim(); //crude, but can prevent wasteful overhead
if (header.startsWith("<html"))
throw new Exception("Received invalid HTML response, expected XML HPCC build version: \"" + header + "\"...");
setUpBuildVersionParser();
String versionString = null;
Document document = null;
try
{
synchronized(m_XMLParser)
{
document = m_XMLParser.parse(new ByteArrayInputStream(response.getBytes(StandardCharsets.UTF_8)));
}
}
catch (Exception e)
{
throw new Exception("Could not parse XML HPCC Version response: \"" + response.substring(0, 100) + "\"...", e);
}
if (document == null)
throw new Exception("Could not parse XML HPCC Version response: \"" + response.substring(0, 100) + "\"...");
try
{
versionString = (String) m_buildVersionXpathExpression.evaluate(document,XPathConstants.STRING);
}
catch (XPathExpressionException e)
{
throw new Exception("Could not extract build version from HPCC Build/Version response: \"" + response + "\"");
}
return versionString;
}
public SpanBuilder getWsClientSpanBuilder(String spanName)
{
SpanBuilder spanBuilder = getWsClientTracer().spanBuilder(spanName)
.setAttribute(ServerAttributes.SERVER_ADDRESS, wsconn.getHost())
.setAttribute(ServerAttributes.SERVER_PORT, Long.getLong(wsconn.getPort()))
.setAttribute(HttpAttributes.HTTP_REQUEST_METHOD, HttpAttributes.HttpRequestMethodValues.GET)
.setSpanKind(SpanKind.CLIENT);
return spanBuilder;
}
static public void injectCurrentSpanTraceParentHeader(Stub clientStub)
{
if (clientStub != null)
{
injectCurrentSpanTraceParentHeader(clientStub._getServiceClient().getOptions());
}
}
static public void injectCurrentSpanTraceParentHeader(Options options)
{
if (options != null)
{
W3CTraceContextPropagator.getInstance().inject(Context.current(), options, Options::setProperty);
}
}
/**
* Performs all Otel initialization
*/
private void initOTel()
{
/*
* If using the OpenTelemetry SDK, you may want to instantiate the OpenTelemetry toprovide configuration, for example of Resource or Sampler. See OpenTelemetrySdk and OpenTelemetrySdk.builder for information on how to construct theSDK's OpenTelemetry implementation.
* WARNING: Due to the inherent complications around initialization order involving this classand its single global instance, we strongly recommend *not* using GlobalOpenTelemetry unless youhave a use-case that absolutely requires it. Please favor using instances of OpenTelemetrywherever possible.
* If you are using the OpenTelemetry javaagent, it is generally best to only callGlobalOpenTelemetry.get() once, and then pass the resulting reference where you need to use it.
*/
globalOTel = GlobalOpenTelemetry.get();
}
public Tracer getWsClientTracer()
{
if (globalOTel == null)
initOTel();
return globalOTel.getTracer(PROJECT_NAME);
}
/**
* All instances of HPCCWsXYZClient should utilize this init function
* Attempts to establish the target HPCC build version and its container mode
*
* Populates initErrMessage if any issues are encountered.
* @param connection the WsClient connection
* @param fetchVersionAndContainerMode services can choose not to fetch build version/containerized mode
* @return true if target HPCC cluster is Containerized, otherwise false
*/
protected boolean initBaseWsClient(Connection connection, boolean fetchVersionAndContainerMode)
{
initOTel();
boolean success = true;
initErrMessage = "";
setActiveConnectionInfo(connection);
if (fetchVersionAndContainerMode)
{
try
{
targetHPCCBuildVersion = new Version(getTargetHPCCBuildVersionString());
}
catch (Exception e)
{
initErrMessage = "BaseHPCCWsClient: Could not stablish target HPCC bulid version, review all HPCC connection values";
if (!e.getLocalizedMessage().isEmpty())
initErrMessage = initErrMessage + "\n" + e.getLocalizedMessage();
success = false;
}
try
{
targetsContainerizedHPCC = getTargetHPCCIsContainerized(wsconn);
}
catch (Exception e)
{
initErrMessage = initErrMessage + "\nBaseHPCCWsClient: Could not determine target HPCC Containerization mode, review all HPCC connection values";
if (!e.getLocalizedMessage().isEmpty())
initErrMessage = initErrMessage + "\n" + e.getLocalizedMessage();
success = false;
}
}
if (!initErrMessage.isEmpty())
log.error(initErrMessage);
return success;
}
protected Stub stub;
static private XPathExpression m_containerizedXpathExpression = null;
static private XPathExpression m_buildVersionXpathExpression = null;
static private XPathExpression m_serviceInterfaceVersionXpathExpression = null;
static private DocumentBuilder m_XMLParser = null;
/**
* Gets the default stub.
*
* @return the default stub
* @throws org.apache.axis2.AxisFault
* the axis fault
*/
abstract public Stub getDefaultStub() throws AxisFault;
/**
* Gets the service version.
*
* @param client
* the client
* @return the service version
*/
public static String getServiceVersion(BaseHPCCWsClient client)
{
String ver = null;
if (client != null)
{
Stub stub;
try
{
stub = client.getDefaultStub();
ver = getServiceVersion(stub);
}
catch (AxisFault e)
{
e.printStackTrace();
}
}
return ver;
}
/**
* Gets the service version.
*
* @param stub
* the stub
* @return the service version
*/
public static String getServiceVersion(Stub stub)
{
String ver = null;
if (stub != null)
{
String address = getServiceWSDLURL(stub);
if (address != null && !address.isEmpty())
{
ver = Utils.parseVersionFromWSDLURL(address);
}
}
return ver;
}
/**
* Gets the service WSDLURL.
*
* @param stub
* the stub
* @return the service WSDLURL
*/
public static String getServiceWSDLURL(Stub stub)
{
String address = null;
if (stub != null)
{
Options options = stub._getServiceClient().getOptions();
if (options != null)
{
address = options.getTo().getAddress();
}
}
return address;
}
/**
* Gets the service WSDL port.
*
* @param stub
* the stub
* @return the service WSDL port
* @throws java.net.MalformedURLException
* the malformed URL exception
*/
public static int getServiceWSDLPort(Stub stub) throws MalformedURLException
{
int port = -1;
if (stub != null)
{
String address = getServiceWSDLURL(stub);
if (address != null && !address.isEmpty())
{
port = (new URL(address)).getPort();
}
}
return port;
}
/**
* Gets the connection URL.
*
* @return the connection URL
* @throws java.lang.Exception
* the exception
*/
public URL getConnectionURL() throws Exception
{
URL address = null;
verifyStub();
Options opt = stub._getServiceClient().getOptions();
EndpointReference toAddress = opt.getTo();
if (toAddress != null) address = new URL(toAddress.getAddress());
return address;
}
/**
* Sets the verbose.
*
* @param verbose
* - sets verbose mode
*/
public void setVerbose(boolean verbose)
{
this.verbose = verbose;
}
/**
* Gets the verbose.
*
* @return the verbose
*/
public boolean getVerbose()
{
return this.verbose;
}
/**
* Should be called after instantiation to confirm
* Successful initialization.
*
* The client init can fail due to many different types of issues
* including invalid connectivity options, invalid credentials, etc
*
* @return true, if successful
*/
public boolean hasInitError()
{
return !initErrMessage.isEmpty();
}
/**
* Returns error message encountered during initialization of wsdfuclient.
* Empty string if no error encountered
*
* @return the inits the error
*/
public String getInitError()
{
return initErrMessage;
}
/**
* Provides Stub object if available, otherwise throws
* Object can be used to access the web service methods directly.
*
* @return the stub
* @throws java.lang.Exception
* the exception
*/
protected Stub verifyStub() throws Exception
{
if (stub != null)
{
injectCurrentSpanTraceParentHeader(stub);
return stub;
}
else
throw new Exception("WS Client Stub not available." + (hasInitError() ? "\n" + initErrMessage : ""));
}
/*
* (non-Javadoc)
*
* @see org.hpccsystems.ws.client.utils.DataSingleton#equals(java.lang.Object)
*/
/** {@inheritDoc} */
@Override
public boolean equals(Object aThat)
{
if (this == aThat) return true;
if (!(aThat instanceof BaseHPCCWsClient)) return false;
if (!(aThat.getClass().isInstance(this))) return false;
BaseHPCCWsClient that = (BaseHPCCWsClient) aThat;
Options thatopt;
try
{
Stub thatStub = that.verifyStub();
thatopt = thatStub._getServiceClient().getOptions();
}
catch (Exception e)
{
thatopt = null;
}
if (thatopt == null) return false;
Options thisoptions = stub._getServiceClient().getOptions();
HttpTransportPropertiesImpl.Authenticator thisauth = (HttpTransportPropertiesImpl.Authenticator) thisoptions
.getProperty(HTTPConstants.AUTHENTICATE);
HttpTransportPropertiesImpl.Authenticator thatauth = (HttpTransportPropertiesImpl.Authenticator) thatopt
.getProperty(HTTPConstants.AUTHENTICATE);
if (!EqualsUtil.areSameNullState(thisauth, thatauth)) return false;
return EqualsUtil.areEqual(thisoptions.getTo().toString(), thatopt.getTo().toString())
&& EqualsUtil.areEqual(thisoptions.getProperty(HTTPConstants.SO_TIMEOUT), thatopt.getProperty(HTTPConstants.SO_TIMEOUT))
&& EqualsUtil.areEqual(thisoptions.getProperty(HTTPConstants.CONNECTION_TIMEOUT),
thatopt.getProperty(HTTPConstants.CONNECTION_TIMEOUT))
&& EqualsUtil.areEqual(thisoptions.getProperty(HTTPConstants.CHUNKED),
thatopt.getProperty(HTTPConstants.CHUNKED))
&& (thisauth != null
? (EqualsUtil.areEqual(thisauth.getUsername(), thatauth.getUsername())
&& EqualsUtil.areEqual(thisauth.getPassword(), thatauth.getPassword()))
: true);
}
/*
* (non-Javadoc)
*
* @see org.hpccsystems.ws.client.utils.DataSingleton#hashCode()
*/
/** {@inheritDoc} */
@Override
public int hashCode()
{
int result = HashCodeUtil.SEED;
if (hasInitError()) return result = HashCodeUtil.hash(result, getInitError());
Options ops = stub._getServiceClient().getOptions();
result = HashCodeUtil.hash(result, ops.getTo());
HttpTransportPropertiesImpl.Authenticator thisauth = (HttpTransportPropertiesImpl.Authenticator) ops.getProperty(HTTPConstants.AUTHENTICATE);
result = HashCodeUtil.hash(result, thisauth == null ? "" : thisauth.getUsername());
result = HashCodeUtil.hash(result, thisauth == null ? "" : thisauth.getPassword());
result = HashCodeUtil.hash(result, ops.getProperty(HTTPConstants.SO_TIMEOUT));
result = HashCodeUtil.hash(result, ops.getProperty(HTTPConstants.CONNECTION_TIMEOUT));
return result;
}
/*
* (non-Javadoc)
*
* @see org.hpccsystems.ws.client.utils.DataSingleton#isComplete()
*/
/** {@inheritDoc} */
@Override
protected boolean isComplete()
{
// TODO Auto-generated method stub
return false;
}
/*
* (non-Javadoc)
*
* @see org.hpccsystems.ws.client.utils.DataSingleton#fastRefresh()
*/
/** {@inheritDoc} */
@Override
protected void fastRefresh()
{
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
*
* @see org.hpccsystems.ws.client.utils.DataSingleton#fullRefresh()
*/
/** {@inheritDoc} */
@Override
protected void fullRefresh()
{
// TODO Auto-generated method stub
}
/**
* Sets the stub connection TO.
*
* @param millis
* the new stub connection TO
* @throws org.apache.axis2.AxisFault
* the axis fault
*/
protected void setStubConnectionTO(int millis) throws AxisFault
{
Options opt = stub._getServiceClient().getOptions();
opt.setProperty(HTTPConstants.CONNECTION_TIMEOUT, millis);
stub._getServiceClient().setOptions(opt);
}
/**
* Gets the stub connection TO.
*
* @return the stub connection TO
* @throws org.apache.axis2.AxisFault
* the axis fault
*/
protected Integer getStubConnectionTO() throws AxisFault
{
Integer to = null;
if (stub != null)
{
Options opt = stub._getServiceClient().getOptions();
try
{
to = (Integer) opt.getProperty(HTTPConstants.CONNECTION_TIMEOUT);
}
catch (Exception e)
{}
}
return to;
}
final static UsernamePasswordCredentials emptyCreds = new UsernamePasswordCredentials("", null);
/**
* Sets the stub options defaults preemptiveauth to 'true';
*
* @param thestub
* The Axis generated service stub
* @param connection
* The connection
* @return the stub
* @throws org.apache.axis2.AxisFault
* the axis fault
*/
static public Stub setStubOptions(Stub thestub, Connection connection) throws AxisFault
{
//Add "rawxml_" query param to request ESP to suppress any default redirects
Options opt = thestub._getServiceClient().getOptions();
EndpointReference toRef = opt.getTo();
String toAddress = toRef.getAddress() + (toRef.getAddress().contains("?") ? "&" : "?") + "rawxml_";
toRef.setAddress(toAddress);
opt.setTo(toRef);
opt.setProperty(HTTPConstants.SO_TIMEOUT, connection.getSocketTimeoutMilli());
opt.setProperty(HTTPConstants.CONNECTION_TIMEOUT, connection.getConnectTimeoutMilli());
opt = setClientAuth(connection.getUserName(), connection.getPassword(), opt);
opt.setProperty(HTTPConstants.CHUNKED, Boolean.FALSE);
if (connection.getPreemptiveHTTPAuthenticate())
{
//Axis2 now forces connection authenticate, even if target is not secure
CredentialsProvider credsProvider = new BasicCredentialsProvider();
if (connection.hasCredentials())
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(connection.getUserName(), connection.getPassword()));
else
credsProvider.setCredentials(AuthScope.ANY, emptyCreds);//if no credentials provided, allow empty user/null pass
HttpClientBuilder builder = HttpClientBuilder.create();
builder.addInterceptorFirst(new HPCCPreemptiveAuthInterceptor());
builder.setDefaultCredentialsProvider(credsProvider);
CloseableHttpClient httpClient = builder.build();
opt.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);
}
thestub._getServiceClient().setOptions(opt);
return thestub;
}
/**
* Sets the client auth.
*
* @param user
* the user
* @param pass
* the pass
* @param opt
* the opt
* @return the options
*/
static public Options setClientAuth(String user, String pass, Options opt)
{
if (user != null && pass != null && opt != null)
{
HttpTransportPropertiesImpl.Authenticator basicAuth = new HttpTransportPropertiesImpl.Authenticator();
basicAuth.setUsername(user);
basicAuth.setPassword(pass);
basicAuth.setPreemptiveAuthentication(true);
opt.setProperty(HTTPConstants.AUTHENTICATE, basicAuth);
opt.setProperty(HttpTransportPropertiesImpl.Authenticator.BASIC, basicAuth);
}
return opt;
}
/**
* Logs and throws EspSoapFaultWrapper.
*
* @param e
* the e
* @throws org.hpccsystems.ws.client.wrappers.EspSoapFaultWrapper
* the esp soap fault wrapper
*/
protected void handleEspSoapFaults(EspSoapFaultWrapper e) throws EspSoapFaultWrapper
{
if (e != null) handleEspSoapFaults(e, null);
}
/**
* Logs and throws EspSoapFaultWrapper, if local message provided, added as wsclientmessage.
*
* @param e
* the e
* @param message
* the message
* @throws org.hpccsystems.ws.client.wrappers.EspSoapFaultWrapper
* the esp soap fault wrapper
*/
protected void handleEspSoapFaults(EspSoapFaultWrapper e, String message) throws EspSoapFaultWrapper
{
if (e != null)
{
if (message != null && !message.isEmpty()) e.setWsClientMessage(message);
log.error(e.toString());
throw e;
}
}
/**
* Handle esp exceptions.
*
* @param exp
* the exp
* @param message
* the message
* @throws org.hpccsystems.ws.client.wrappers.ArrayOfEspExceptionWrapper
* the array of esp exception wrapper
*/
protected void handleEspExceptions(ArrayOfEspExceptionWrapper exp, String message) throws ArrayOfEspExceptionWrapper
{
if (exp == null || exp.getExceptions() == null || exp.getExceptions().size() <= 0) return;
if (message != null && !message.isEmpty()) exp.setWsClientMessage(message);
log.error(exp.toString());
throw exp;
}
/**
* Handle esp exceptions.
*
* @param exp
* the exp
* @throws org.hpccsystems.ws.client.wrappers.ArrayOfEspExceptionWrapper
* the array of esp exception wrapper
*/
protected void handleEspExceptions(ArrayOfEspExceptionWrapper exp) throws ArrayOfEspExceptionWrapper
{
handleEspExceptions(exp, null);
}
/**
* Logs and throws arrayofeclexceptionwrapper without localized message response from WS client.
*
* @param eclexceptions
* the eclexceptions
* @throws java.lang.Exception
* the exception
* @throws org.hpccsystems.ws.client.wrappers.ArrayOfECLExceptionWrapper
* the array of ECL exception wrapper
*/
protected void handleECLExceptions(ArrayOfECLExceptionWrapper eclexceptions) throws Exception, ArrayOfECLExceptionWrapper
{
handleECLExceptions(eclexceptions, null);
}
/**
* Logs and throws arrayofeclexceptionwrapper with localized message response from WS client.
*
* @param eclExceptions
* - the array of ECLException objects to throw
* @param message
* - the prefix message
* @throws java.lang.Exception
* the exception
* @throws org.hpccsystems.ws.client.wrappers.ArrayOfECLExceptionWrapper
* the array of ECL exception wrapper
*/
protected void handleECLExceptions(ArrayOfECLExceptionWrapper eclExceptions, String message) throws Exception, ArrayOfECLExceptionWrapper
{
if (eclExceptions == null || eclExceptions.getECLException() == null || eclExceptions.getECLException().size() <= 0) return;
if (message != null && !message.isEmpty()) eclExceptions.setWsClientMessage(message);
log.error(eclExceptions.toString());
throw eclExceptions;
}
/**
* Provides the target ESP Interface version
*
* @return The runtime ESP interface default version
*/
public double getTargetESPInterfaceVersion()
{
if (targetESPInterfaceVer == null)
loadESPRuntimeInterfaceVer();
return targetESPInterfaceVer;
}
/**
* Stores active connection information for post-initialization use
*
* @param conn Connection object
*/
protected void setActiveConnectionInfo(Connection conn)
{
wsconn = conn;
}
/**
* All implementations must provide the target web service URI
*
* @return a {@link java.lang.String} object.
*/
public abstract String getServiceURI();
protected void setUpBuildVersionParser() throws ParserConfigurationException, XPathExpressionException
{
if(m_XMLParser != null && m_buildVersionXpathExpression != null)
return;
m_XMLParser = Utils.newSafeXMLDocBuilder();
if (m_XMLParser == null)
throw new XPathExpressionException ("Could not create new version parser");
XPath versionXpath = XPathFactory.newInstance().newXPath();
m_buildVersionXpathExpression = versionXpath.compile("/ActivityResponse/Build");
if (m_buildVersionXpathExpression == null)
throw new XPathExpressionException ("Could not Compile m_buildVersionXpathExpression");
}
protected void setUpContainerizedParser() throws ParserConfigurationException, XPathExpressionException
{
if(m_XMLParser != null && m_containerizedXpathExpression != null)
return;
m_XMLParser = Utils.newSafeXMLDocBuilder();
if (m_XMLParser == null)
throw new XPathExpressionException ("Could not create new version parser");
XPath versionXpath = XPathFactory.newInstance().newXPath();
m_containerizedXpathExpression = versionXpath.compile("/GetBuildInfoResponse/BuildInfo/NamedValue");
if (m_containerizedXpathExpression == null)
throw new XPathExpressionException ("Could not Compile m_containerizedXpathExpression");
}
protected void setUpversionParser() throws ParserConfigurationException, XPathExpressionException
{
if(m_XMLParser != null && m_serviceInterfaceVersionXpathExpression != null)
return;
m_XMLParser = Utils.newSafeXMLDocBuilder();
if (m_XMLParser == null)
throw new XPathExpressionException ("Could not create new version parser");
XPath versionXpath = XPathFactory.newInstance().newXPath();
m_serviceInterfaceVersionXpathExpression = versionXpath.compile("string(/VersionInfo/Version)");
if (m_serviceInterfaceVersionXpathExpression == null)
throw new XPathExpressionException ("Could not Compile versionXpathExpression");
}
/**
* Attempts to retrieve the default WSDL version of the target runtime ESP service
* Appends the target ESP service path and the "version_" literal to the connection's base URL
*/
protected void loadESPRuntimeInterfaceVer()
{
if (wsconn != null)
{
if (getServiceURI() == null || getServiceURI().isEmpty())
log.warn("Could not load ESP interface version, ensure target ws name is provided");
else
{
String response = null;
try
{
response = wsconn.sendGetRequest(getServiceURI()+"/version_");
}
catch (Exception httpGetException)
{
log.error("Encountered error fetching ESP interface version for " + wsconn.getBaseUrl() + getServiceURI() + "\n" + httpGetException.getLocalizedMessage());
}
if (response == null || response.isEmpty())
{
log.error("Received empty ESP interface version response (" + wsconn.getBaseUrl() + getServiceURI() + ")");
}
else
{
try
{
setUpversionParser();
Document document = null;
synchronized(m_XMLParser)
{
document = m_XMLParser.parse(new ByteArrayInputStream(response.getBytes(StandardCharsets.UTF_8)));
}
if (document == null)
throw new Exception("Cannot parse ESP Interface version response.");
targetESPInterfaceVer = (double)m_serviceInterfaceVersionXpathExpression.evaluate(document, XPathConstants.NUMBER);
log.info(wsconn.getBaseUrl() + getServiceURI() + " version: " + targetESPInterfaceVer);
}
catch (Exception e)
{
log.error("Encountered error parsing ESP interface version for " + wsconn.getBaseUrl() + getServiceURI() + "\n" + e.getLocalizedMessage());
}
}
}
}
else
{
log.warn("Could not load ESP interface version, ensure client is properly initialized");
}
}
/**
* Determine if target HPCC's build version is compatible with a given version.
*
* @param major a int.
* @param minor a int.
* @param point a int.
* @return boolean true if server build version >= input version
*/
protected boolean compatibilityCheck(int major, int minor, int point)
{
if (targetHPCCBuildVersion == null)
return false;
return targetHPCCBuildVersion.isEqualOrNewerThan(major, minor, point);
}
/**
* Determine if target HPCC's build version is compatible with a given version.
*
* @param input
* the input
* @return boolean true if server build version >= input version
*/
protected boolean compatibilityCheck(Version input)
{
if (targetHPCCBuildVersion == null || input == null)
return false;
return targetHPCCBuildVersion.isEqualOrNewerThan(input);
}
}