1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package com.att.nsa.mr.client.impl;
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.OutputStream;
27 import java.net.MalformedURLException;
29 import java.net.URISyntaxException;
30 import java.util.Collection;
31 import java.util.HashMap;
32 import java.util.LinkedList;
33 import java.util.List;
35 import java.util.Properties;
36 import java.util.concurrent.LinkedBlockingQueue;
37 import java.util.concurrent.ScheduledThreadPoolExecutor;
38 import java.util.concurrent.TimeUnit;
39 import java.util.zip.GZIPOutputStream;
41 import javax.ws.rs.core.MultivaluedMap;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 import org.apache.http.HttpException;
47 import org.apache.http.HttpStatus;
48 import org.json.JSONArray;
49 import org.json.JSONObject;
50 import org.json.JSONTokener;
52 import com.att.aft.dme2.api.DME2Client;
53 import com.att.aft.dme2.api.DME2Exception;
54 import com.att.nsa.mr.client.HostSelector;
55 import com.att.nsa.mr.client.MRBatchingPublisher;
56 import com.att.nsa.mr.client.response.MRPublisherResponse;
57 import com.att.nsa.mr.test.clients.ProtocolTypeConstants;
59 public class MRSimplerBatchPublisher extends MRBaseClient implements MRBatchingPublisher {
60 private static final Logger logger = LoggerFactory.getLogger(MRSimplerBatchPublisher.class);
62 public static class Builder {
66 public Builder againstUrls(Collection<String> baseUrls) {
71 public Builder onTopic(String topic) {
76 public Builder batchTo(int maxBatchSize, long maxBatchAgeMs) {
77 fMaxBatchSize = maxBatchSize;
78 fMaxBatchAgeMs = maxBatchAgeMs;
82 public Builder compress(boolean compress) {
87 public Builder httpThreadTime(int threadOccuranceTime) {
88 this.threadOccuranceTime = threadOccuranceTime;
92 public Builder allowSelfSignedCertificates(boolean allowSelfSignedCerts) {
93 fAllowSelfSignedCerts = allowSelfSignedCerts;
97 public Builder withResponse(boolean withResponse) {
98 fWithResponse = withResponse;
102 public MRSimplerBatchPublisher build() {
103 if (!fWithResponse) {
105 return new MRSimplerBatchPublisher(fUrls, fTopic, fMaxBatchSize, fMaxBatchAgeMs, fCompress,
106 fAllowSelfSignedCerts, threadOccuranceTime);
107 } catch (MalformedURLException e) {
108 throw new IllegalArgumentException(e);
112 return new MRSimplerBatchPublisher(fUrls, fTopic, fMaxBatchSize, fMaxBatchAgeMs, fCompress,
113 fAllowSelfSignedCerts, fMaxBatchSize);
114 } catch (MalformedURLException e) {
115 throw new IllegalArgumentException(e);
121 private Collection<String> fUrls;
122 private String fTopic;
123 private int fMaxBatchSize = 100;
124 private long fMaxBatchAgeMs = 1000;
125 private boolean fCompress = false;
126 private int threadOccuranceTime = 50;
127 private boolean fAllowSelfSignedCerts = false;
128 private boolean fWithResponse = false;
133 public int send(String partition, String msg) {
134 return send(new message(partition, msg));
138 public int send(String msg) {
139 return send(new message(null, msg));
143 public int send(message msg) {
144 final LinkedList<message> list = new LinkedList<message>();
150 public synchronized int send(Collection<message> msgs) {
152 throw new IllegalStateException("The publisher was closed.");
155 for (message userMsg : msgs) {
156 fPending.add(new TimestampedMessage(userMsg));
158 return getPendingMessageCount();
162 public synchronized int getPendingMessageCount() {
163 return fPending.size();
167 public void close() {
169 final List<message> remains = close(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
170 if (remains.size() > 0) {
171 getLog().warn("Closing publisher with " + remains.size() + " messages unsent. "
172 + "Consider using MRBatchingPublisher.close( long timeout, TimeUnit timeoutUnits ) to recapture unsent messages on close.");
174 } catch (InterruptedException e) {
175 getLog().warn("Possible message loss. " + e.getMessage(), e);
176 } catch (IOException e) {
177 getLog().warn("Possible message loss. " + e.getMessage(), e);
182 public List<message> close(long time, TimeUnit unit) throws IOException, InterruptedException {
183 synchronized (this) {
186 // stop the background sender
187 fExec.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
188 fExec.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
192 final long now = Clock.now();
193 final long waitInMs = TimeUnit.MILLISECONDS.convert(time, unit);
194 final long timeoutAtMs = now + waitInMs;
196 while (Clock.now() < timeoutAtMs && getPendingMessageCount() > 0) {
201 synchronized (this) {
202 final LinkedList<message> result = new LinkedList<message>();
203 fPending.drainTo(result);
209 * Possibly send a batch to the MR server. This is called by the background
210 * thread and the close() method
214 private synchronized void send(boolean force) {
215 if (force || shouldSendNow()) {
217 getLog().warn("Send failed, " + fPending.size() + " message to send.");
219 // note the time for back-off
220 fDontSendUntilMs = sfWaitAfterError + Clock.now();
225 private synchronized boolean shouldSendNow() {
226 boolean shouldSend = false;
227 if (fPending.size() > 0) {
228 final long nowMs = Clock.now();
230 shouldSend = (fPending.size() >= fMaxBatchSize);
232 final long sendAtMs = fPending.peek().timestamp + fMaxBatchAgeMs;
233 shouldSend = sendAtMs <= nowMs;
236 // however, wait after an error
237 shouldSend = shouldSend && nowMs >= fDontSendUntilMs;
243 * Method to parse published JSON Objects and Arrays
247 private JSONArray parseJSON() {
248 JSONArray jsonArray = new JSONArray();
249 for (TimestampedMessage m : fPending) {
250 JSONTokener jsonTokener = new JSONTokener(m.fMsg);
251 JSONObject jsonObject = null;
252 JSONArray tempjsonArray = null;
253 final char firstChar = jsonTokener.next();
255 if ('[' == firstChar) {
256 tempjsonArray = new JSONArray(jsonTokener);
257 if (null != tempjsonArray) {
258 for (int i = 0; i < tempjsonArray.length(); i++) {
259 jsonArray.put(tempjsonArray.getJSONObject(i));
263 jsonObject = new JSONObject(jsonTokener);
264 jsonArray.put(jsonObject);
271 private synchronized boolean sendBatch() {
272 // it's possible for this call to be made with an empty list. in this
273 // case, just return.
274 if (fPending.size() < 1) {
278 final long nowMs = Clock.now();
280 host = this.fHostSelector.selectBaseHost();
282 final String httpurl = MRConstants.makeUrl(host, fTopic, props.getProperty("Protocol"),
283 props.getProperty("partition"));
287 * final String contentType = fCompress ?
288 * MRFormat.CAMBRIA_ZIP.toString () : MRFormat.CAMBRIA.toString () ;
291 final ByteArrayOutputStream baseStream = new ByteArrayOutputStream();
292 OutputStream os = baseStream;
293 final String contentType = props.getProperty("contenttype");
294 if (contentType.equalsIgnoreCase("application/json")) {
295 JSONArray jsonArray = parseJSON();
296 os.write(jsonArray.toString().getBytes());
299 } else if (contentType.equalsIgnoreCase("text/plain")) {
300 for (TimestampedMessage m : fPending) {
301 os.write(m.fMsg.getBytes());
305 } else if (contentType.equalsIgnoreCase("application/cambria")
306 || (contentType.equalsIgnoreCase("application/cambria-zip"))) {
307 if (contentType.equalsIgnoreCase("application/cambria-zip")) {
308 os = new GZIPOutputStream(baseStream);
310 for (TimestampedMessage m : fPending) {
312 os.write(("" + m.fPartition.length()).getBytes());
314 os.write(("" + m.fMsg.length()).getBytes());
316 os.write(m.fPartition.getBytes());
317 os.write(m.fMsg.getBytes());
322 for (TimestampedMessage m : fPending) {
323 os.write(m.fMsg.getBytes());
329 final long startMs = Clock.now();
330 if (ProtocolTypeConstants.DME2.getValue().equalsIgnoreCase(protocolFlag)) {
335 getLog().info("sending " + fPending.size() + " msgs to " + url + subContextPath + ". Oldest: "
336 + (nowMs - fPending.peek().timestamp) + " ms");
337 sender.setPayload(os.toString());
338 String dmeResponse = sender.sendAndWait(5000L);
340 final String logLine = "MR reply ok (" + (Clock.now() - startMs) + " ms):" + dmeResponse.toString();
341 getLog().info(logLine);
346 if (ProtocolTypeConstants.AUTH_KEY.getValue().equalsIgnoreCase(protocolFlag)) {
347 getLog().info("sending " + fPending.size() + " msgs to " + httpurl + ". Oldest: "
348 + (nowMs - fPending.peek().timestamp) + " ms");
349 final JSONObject result = postAuth(httpurl, baseStream.toByteArray(), contentType, authKey, authDate,
350 username, password, protocolFlag);
351 // Here we are checking for error response. If HTTP status
352 // code is not within the http success response code
353 // then we consider this as error and return false
354 if (result.getInt("status") < 200 || result.getInt("status") > 299) {
357 final String logLine = "MR reply ok (" + (Clock.now() - startMs) + " ms):" + result.toString();
358 getLog().info(logLine);
363 if (ProtocolTypeConstants.AAF_AUTH.getValue().equalsIgnoreCase(protocolFlag)) {
364 getLog().info("sending " + fPending.size() + " msgs to " + httpurl + ". Oldest: "
365 + (nowMs - fPending.peek().timestamp) + " ms");
366 final JSONObject result = post(httpurl, baseStream.toByteArray(), contentType, username, password,
369 // Here we are checking for error response. If HTTP status
370 // code is not within the http success response code
371 // then we consider this as error and return false
372 if (result.getInt("status") < 200 || result.getInt("status") > 299) {
375 final String logLine = "MR reply ok (" + (Clock.now() - startMs) + " ms):" + result.toString();
376 getLog().info(logLine);
381 if (ProtocolTypeConstants.HTTPNOAUTH.getValue().equalsIgnoreCase(protocolFlag)) {
382 getLog().info("sending " + fPending.size() + " msgs to " + httpurl + ". Oldest: "
383 + (nowMs - fPending.peek().timestamp) + " ms");
384 final JSONObject result = postNoAuth(httpurl, baseStream.toByteArray(), contentType);
386 // Here we are checking for error response. If HTTP status
387 // code is not within the http success response code
388 // then we consider this as error and return false
389 if (result.getInt("status") < 200 || result.getInt("status") > 299) {
392 final String logLine = "MR reply ok (" + (Clock.now() - startMs) + " ms):" + result.toString();
393 getLog().info(logLine);
397 } catch (IllegalArgumentException x) {
398 getLog().warn(x.getMessage(), x);
399 } catch (IOException x) {
400 getLog().warn(x.getMessage(), x);
401 } catch (HttpException x) {
402 getLog().warn(x.getMessage(), x);
403 } catch (Exception x) {
404 getLog().warn(x.getMessage(), x);
409 public synchronized MRPublisherResponse sendBatchWithResponse() {
410 // it's possible for this call to be made with an empty list. in this
411 // case, just return.
412 if (fPending.size() < 1) {
413 pubResponse.setResponseCode(String.valueOf(HttpStatus.SC_BAD_REQUEST));
414 pubResponse.setResponseMessage("No Messages to send");
418 final long nowMs = Clock.now();
420 host = this.fHostSelector.selectBaseHost();
422 final String httpurl = MRConstants.makeUrl(host, fTopic, props.getProperty("Protocol"),
423 props.getProperty("partition"));
424 OutputStream os = null;
427 final ByteArrayOutputStream baseStream = new ByteArrayOutputStream();
429 final String contentType = props.getProperty("contenttype");
430 if (contentType.equalsIgnoreCase("application/json")) {
431 JSONArray jsonArray = parseJSON();
432 os.write(jsonArray.toString().getBytes());
433 } else if (contentType.equalsIgnoreCase("text/plain")) {
434 for (TimestampedMessage m : fPending) {
435 os.write(m.fMsg.getBytes());
438 } else if (contentType.equalsIgnoreCase("application/cambria")
439 || (contentType.equalsIgnoreCase("application/cambria-zip"))) {
440 if (contentType.equalsIgnoreCase("application/cambria-zip")) {
441 os = new GZIPOutputStream(baseStream);
443 for (TimestampedMessage m : fPending) {
445 os.write(("" + m.fPartition.length()).getBytes());
447 os.write(("" + m.fMsg.length()).getBytes());
449 os.write(m.fPartition.getBytes());
450 os.write(m.fMsg.getBytes());
455 for (TimestampedMessage m : fPending) {
456 os.write(m.fMsg.getBytes());
461 final long startMs = Clock.now();
462 if (ProtocolTypeConstants.DME2.getValue().equalsIgnoreCase(protocolFlag)) {
468 getLog().info("sending " + fPending.size() + " msgs to " + url + subContextPath + ". Oldest: "
469 + (nowMs - fPending.peek().timestamp) + " ms");
470 sender.setPayload(os.toString());
472 String dmeResponse = sender.sendAndWait(5000L);
474 pubResponse = createMRPublisherResponse(dmeResponse, pubResponse);
476 if (Integer.valueOf(pubResponse.getResponseCode()) < 200
477 || Integer.valueOf(pubResponse.getResponseCode()) > 299) {
481 final String logLine = String.valueOf((Clock.now() - startMs)) + dmeResponse.toString();
482 getLog().info(logLine);
485 } catch (DME2Exception x) {
486 getLog().warn(x.getMessage(), x);
487 pubResponse.setResponseCode(x.getErrorCode());
488 pubResponse.setResponseMessage(x.getErrorMessage());
489 } catch (URISyntaxException x) {
491 getLog().warn(x.getMessage(), x);
492 pubResponse.setResponseCode(String.valueOf(HttpStatus.SC_BAD_REQUEST));
493 pubResponse.setResponseMessage(x.getMessage());
494 } catch (Exception x) {
496 pubResponse.setResponseCode(String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR));
497 pubResponse.setResponseMessage(x.getMessage());
498 logger.error("exception: ", x);
505 if (ProtocolTypeConstants.AUTH_KEY.getValue().equalsIgnoreCase(protocolFlag)) {
506 getLog().info("sending " + fPending.size() + " msgs to " + httpurl + ". Oldest: "
507 + (nowMs - fPending.peek().timestamp) + " ms");
508 final String result = postAuthwithResponse(httpurl, baseStream.toByteArray(), contentType, authKey,
509 authDate, username, password, protocolFlag);
510 // Here we are checking for error response. If HTTP status
511 // code is not within the http success response code
512 // then we consider this as error and return false
514 pubResponse = createMRPublisherResponse(result, pubResponse);
516 if (Integer.valueOf(pubResponse.getResponseCode()) < 200
517 || Integer.valueOf(pubResponse.getResponseCode()) > 299) {
522 final String logLine = "MR reply ok (" + (Clock.now() - startMs) + " ms):" + result.toString();
523 getLog().info(logLine);
528 if (ProtocolTypeConstants.AAF_AUTH.getValue().equalsIgnoreCase(protocolFlag)) {
529 getLog().info("sending " + fPending.size() + " msgs to " + httpurl + ". Oldest: "
530 + (nowMs - fPending.peek().timestamp) + " ms");
531 final String result = postWithResponse(httpurl, baseStream.toByteArray(), contentType, username,
532 password, protocolFlag);
534 // Here we are checking for error response. If HTTP status
535 // code is not within the http success response code
536 // then we consider this as error and return false
537 pubResponse = createMRPublisherResponse(result, pubResponse);
539 if (Integer.valueOf(pubResponse.getResponseCode()) < 200
540 || Integer.valueOf(pubResponse.getResponseCode()) > 299) {
545 final String logLine = String.valueOf((Clock.now() - startMs));
546 getLog().info(logLine);
551 if (ProtocolTypeConstants.HTTPNOAUTH.getValue().equalsIgnoreCase(protocolFlag)) {
552 getLog().info("sending " + fPending.size() + " msgs to " + httpurl + ". Oldest: "
553 + (nowMs - fPending.peek().timestamp) + " ms");
554 final String result = postNoAuthWithResponse(httpurl, baseStream.toByteArray(), contentType);
556 // Here we are checking for error response. If HTTP status
557 // code is not within the http success response code
558 // then we consider this as error and return false
559 pubResponse = createMRPublisherResponse(result, pubResponse);
561 if (Integer.valueOf(pubResponse.getResponseCode()) < 200
562 || Integer.valueOf(pubResponse.getResponseCode()) > 299) {
567 final String logLine = String.valueOf((Clock.now() - startMs));
568 getLog().info(logLine);
572 } catch (IllegalArgumentException x) {
573 getLog().warn(x.getMessage(), x);
574 pubResponse.setResponseCode(String.valueOf(HttpStatus.SC_BAD_REQUEST));
575 pubResponse.setResponseMessage(x.getMessage());
577 } catch (IOException x) {
578 getLog().warn(x.getMessage(), x);
579 pubResponse.setResponseCode(String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR));
580 pubResponse.setResponseMessage(x.getMessage());
582 } catch (HttpException x) {
583 getLog().warn(x.getMessage(), x);
584 pubResponse.setResponseCode(String.valueOf(HttpStatus.SC_BAD_REQUEST));
585 pubResponse.setResponseMessage(x.getMessage());
587 } catch (Exception x) {
588 getLog().warn(x.getMessage(), x);
590 pubResponse.setResponseCode(String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR));
591 pubResponse.setResponseMessage(x.getMessage());
596 if (fPending.size() > 0) {
597 getLog().warn("Send failed, " + fPending.size() + " message to send.");
598 pubResponse.setPendingMsgs(fPending.size());
603 } catch (Exception x) {
604 getLog().warn(x.getMessage(), x);
605 pubResponse.setResponseCode(String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR));
606 pubResponse.setResponseMessage("Error in closing Output Stream");
614 public MRPublisherResponse createMRPublisherResponse(String reply, MRPublisherResponse mrPubResponse) {
616 if (reply.isEmpty()) {
618 mrPubResponse.setResponseCode(String.valueOf(HttpStatus.SC_BAD_REQUEST));
619 mrPubResponse.setResponseMessage("Please verify the Producer properties");
620 } else if (reply.startsWith("{")) {
621 JSONObject jObject = new JSONObject(reply);
622 if (jObject.has("message") && jObject.has("status")) {
623 String message = jObject.getString("message");
624 if (null != message) {
625 mrPubResponse.setResponseMessage(message);
627 mrPubResponse.setResponseCode(Integer.toString(jObject.getInt("status")));
629 mrPubResponse.setResponseCode(String.valueOf(HttpStatus.SC_OK));
630 mrPubResponse.setResponseMessage(reply);
632 } else if (reply.startsWith("<")) {
633 String responseCode = getHTTPErrorResponseCode(reply);
634 if (responseCode.contains("403")) {
635 responseCode = "403";
637 mrPubResponse.setResponseCode(responseCode);
638 mrPubResponse.setResponseMessage(getHTTPErrorResponseMessage(reply));
641 return mrPubResponse;
644 private final String fTopic;
645 private final int fMaxBatchSize;
646 private final long fMaxBatchAgeMs;
647 private final boolean fCompress;
648 private int threadOccuranceTime;
649 private boolean fClosed;
650 private String username;
651 private String password;
655 private HostSelector fHostSelector = null;
657 private final LinkedBlockingQueue<TimestampedMessage> fPending;
658 private long fDontSendUntilMs;
659 private final ScheduledThreadPoolExecutor fExec;
661 private String latitude;
662 private String longitude;
663 private String version;
664 private String serviceName;
666 private String partner;
667 private String routeOffer;
668 private String subContextPath;
669 private String protocol;
670 private String methodType;
672 private String dmeuser;
673 private String dmepassword;
674 private String contentType;
675 private static final long sfWaitAfterError = 10000;
676 private HashMap<String, String> DMETimeOuts;
677 private DME2Client sender;
678 public String protocolFlag = ProtocolTypeConstants.DME2.getValue();
679 private String authKey;
680 private String authDate;
681 private String handlers;
682 private Properties props;
683 public static String routerFilePath;
684 protected static final Map<String, String> headers = new HashMap<String, String>();
685 public static MultivaluedMap<String, Object> headersMap;
687 private MRPublisherResponse pubResponse;
689 public MRPublisherResponse getPubResponse() {
693 public void setPubResponse(MRPublisherResponse pubResponse) {
694 this.pubResponse = pubResponse;
697 public static String getRouterFilePath() {
698 return routerFilePath;
701 public static void setRouterFilePath(String routerFilePath) {
702 MRSimplerBatchPublisher.routerFilePath = routerFilePath;
705 public Properties getProps() {
709 public void setProps(Properties props) {
713 public String getProtocolFlag() {
717 public void setProtocolFlag(String protocolFlag) {
718 this.protocolFlag = protocolFlag;
721 private void DME2Configue() throws Exception {
725 * FileReader reader = new FileReader(new File (producerFilePath));
726 * Properties props = new Properties(); props.load(reader);
728 latitude = props.getProperty("Latitude");
729 longitude = props.getProperty("Longitude");
730 version = props.getProperty("Version");
731 serviceName = props.getProperty("ServiceName");
732 env = props.getProperty("Environment");
733 partner = props.getProperty("Partner");
734 routeOffer = props.getProperty("routeOffer");
735 subContextPath = props.getProperty("SubContextPath") + fTopic;
737 * if(props.getProperty("partition")!=null &&
738 * !props.getProperty("partition").equalsIgnoreCase("")){
739 * subContextPath=subContextPath+"?partitionKey="+props.getProperty(
742 protocol = props.getProperty("Protocol");
743 methodType = props.getProperty("MethodType");
744 dmeuser = props.getProperty("username");
745 dmepassword = props.getProperty("password");
746 contentType = props.getProperty("contenttype");
747 handlers = props.getProperty("sessionstickinessrequired");
748 routerFilePath = props.getProperty("DME2preferredRouterFilePath");
751 * Changes to DME2Client url to use Partner for auto failover
752 * between data centers When Partner value is not provided use the
753 * routeOffer value for auto failover within a cluster
756 String partitionKey = props.getProperty("partition");
758 if (partner != null && !partner.isEmpty()) {
759 url = protocol + "://" + serviceName + "?version=" + version + "&envContext=" + env + "&partner="
761 if (partitionKey != null && !partitionKey.equalsIgnoreCase("")) {
762 url = url + "&partitionKey=" + partitionKey;
764 } else if (routeOffer != null && !routeOffer.isEmpty()) {
765 url = protocol + "://" + serviceName + "?version=" + version + "&envContext=" + env + "&routeoffer="
767 if (partitionKey != null && !partitionKey.equalsIgnoreCase("")) {
768 url = url + "&partitionKey=" + partitionKey;
772 DMETimeOuts = new HashMap<String, String>();
773 DMETimeOuts.put("AFT_DME2_EP_READ_TIMEOUT_MS", props.getProperty("AFT_DME2_EP_READ_TIMEOUT_MS"));
774 DMETimeOuts.put("AFT_DME2_ROUNDTRIP_TIMEOUT_MS", props.getProperty("AFT_DME2_ROUNDTRIP_TIMEOUT_MS"));
775 DMETimeOuts.put("AFT_DME2_EP_CONN_TIMEOUT", props.getProperty("AFT_DME2_EP_CONN_TIMEOUT"));
776 DMETimeOuts.put("Content-Type", contentType);
777 System.setProperty("AFT_LATITUDE", latitude);
778 System.setProperty("AFT_LONGITUDE", longitude);
779 System.setProperty("AFT_ENVIRONMENT", props.getProperty("AFT_ENVIRONMENT"));
780 // System.setProperty("DME2.DEBUG", "true");
783 // System.setProperty("AFT_DME2_CLIENT_SSL_INCLUDE_PROTOCOLS",
784 // "SSLv3,TLSv1,TLSv1.1");
785 System.setProperty("AFT_DME2_CLIENT_SSL_INCLUDE_PROTOCOLS", "TLSv1.1,TLSv1.2");
786 System.setProperty("AFT_DME2_CLIENT_IGNORE_SSL_CONFIG", "false");
787 System.setProperty("AFT_DME2_CLIENT_KEYSTORE_PASSWORD", "changeit");
791 sender = new DME2Client(new URI(url), 5000L);
793 sender.setAllowAllHttpReturnCodes(true);
794 sender.setMethod(methodType);
795 sender.setSubContext(subContextPath);
796 sender.setCredentials(dmeuser, dmepassword);
797 sender.setHeaders(DMETimeOuts);
798 if (handlers.equalsIgnoreCase("yes")) {
799 sender.addHeader("AFT_DME2_EXCHANGE_REQUEST_HANDLERS",
800 props.getProperty("AFT_DME2_EXCHANGE_REQUEST_HANDLERS"));
801 sender.addHeader("AFT_DME2_EXCHANGE_REPLY_HANDLERS",
802 props.getProperty("AFT_DME2_EXCHANGE_REPLY_HANDLERS"));
803 sender.addHeader("AFT_DME2_REQ_TRACE_ON", props.getProperty("AFT_DME2_REQ_TRACE_ON"));
805 sender.addHeader("AFT_DME2_EXCHANGE_REPLY_HANDLERS", "com.att.nsa.mr.dme.client.HeaderReplyHandler");
807 } catch (DME2Exception x) {
808 getLog().warn(x.getMessage(), x);
809 throw new DME2Exception(x.getErrorCode(), x.getErrorMessage());
810 } catch (URISyntaxException x) {
812 getLog().warn(x.getMessage(), x);
813 throw new URISyntaxException(url, x.getMessage());
814 } catch (Exception x) {
816 getLog().warn(x.getMessage(), x);
817 throw new IllegalArgumentException(x.getMessage());
821 private MRSimplerBatchPublisher(Collection<String> hosts, String topic, int maxBatchSize, long maxBatchAgeMs,
822 boolean compress) throws MalformedURLException {
825 if (topic == null || topic.length() < 1) {
826 throw new IllegalArgumentException("A topic must be provided.");
829 fHostSelector = new HostSelector(hosts, null);
832 fMaxBatchSize = maxBatchSize;
833 fMaxBatchAgeMs = maxBatchAgeMs;
834 fCompress = compress;
836 fPending = new LinkedBlockingQueue<TimestampedMessage>();
837 fDontSendUntilMs = 0;
838 fExec = new ScheduledThreadPoolExecutor(1);
839 pubResponse = new MRPublisherResponse();
843 private MRSimplerBatchPublisher(Collection<String> hosts, String topic, int maxBatchSize, long maxBatchAgeMs,
844 boolean compress, boolean allowSelfSignedCerts, int httpThreadOccurnace) throws MalformedURLException {
847 if (topic == null || topic.length() < 1) {
848 throw new IllegalArgumentException("A topic must be provided.");
851 fHostSelector = new HostSelector(hosts, null);
854 fMaxBatchSize = maxBatchSize;
855 fMaxBatchAgeMs = maxBatchAgeMs;
856 fCompress = compress;
857 threadOccuranceTime = httpThreadOccurnace;
858 fPending = new LinkedBlockingQueue<TimestampedMessage>();
859 fDontSendUntilMs = 0;
860 fExec = new ScheduledThreadPoolExecutor(1);
861 fExec.scheduleAtFixedRate(new Runnable() {
866 }, 100, threadOccuranceTime, TimeUnit.MILLISECONDS);
869 private static class TimestampedMessage extends message {
870 public TimestampedMessage(message m) {
872 timestamp = Clock.now();
875 public final long timestamp;
878 public String getUsername() {
882 public void setUsername(String username) {
883 this.username = username;
886 public String getPassword() {
890 public void setPassword(String password) {
891 this.password = password;
894 public String getHost() {
898 public void setHost(String host) {
902 public String getContentType() {
906 public void setContentType(String contentType) {
907 this.contentType = contentType;
910 public String getAuthKey() {
914 public void setAuthKey(String authKey) {
915 this.authKey = authKey;
918 public String getAuthDate() {
922 public void setAuthDate(String authDate) {
923 this.authDate = authDate;