diff options
Diffstat (limited to 'src/main/java')
4 files changed, 82 insertions, 8 deletions
diff --git a/src/main/java/com/att/nsa/mr/client/impl/DmaapClientUtil.java b/src/main/java/com/att/nsa/mr/client/impl/DmaapClientUtil.java index 6adf236..17a37d9 100644 --- a/src/main/java/com/att/nsa/mr/client/impl/DmaapClientUtil.java +++ b/src/main/java/com/att/nsa/mr/client/impl/DmaapClientUtil.java @@ -70,5 +70,10 @@ public class DmaapClientUtil { return target.request().get(); } + + public static Response postResponsewtNoAuth(WebTarget target, byte[] data, String contentType) { + return target.request().post(Entity.entity(data, contentType)); + + } } diff --git a/src/main/java/com/att/nsa/mr/client/impl/MRBaseClient.java b/src/main/java/com/att/nsa/mr/client/impl/MRBaseClient.java index 0cffa7d..c3d1c26 100644 --- a/src/main/java/com/att/nsa/mr/client/impl/MRBaseClient.java +++ b/src/main/java/com/att/nsa/mr/client/impl/MRBaseClient.java @@ -109,6 +109,20 @@ public class MRBaseClient extends HttpClient implements MRClient { "Authentication Failed: Username/password/AuthKey/AuthDate parameter(s) cannot be null or empty."); } } + + public JSONObject postNoAuth(final String path, final byte[] data, String contentType) + throws HttpException, JSONException { + WebTarget target = null; + Response response = null; + if (contentType == null) { + contentType = "text/pain"; + } + target = DmaapClientUtil.getTarget(path); + + response = DmaapClientUtil.postResponsewtNoAuth(target, data, contentType); + + return getResponseDataInJson(response); + } public String postWithResponse(final String path, final byte[] data, final String contentType, final String username, final String password, final String protocolFlag) @@ -122,13 +136,29 @@ public class MRBaseClient extends HttpClient implements MRClient { response = DmaapClientUtil.getResponsewtBasicAuth(target, encoding); - responseData = (String)response.getEntity(); + responseData = (String)response.readEntity(String.class); return responseData; } else { throw new HttpException( "Authentication Failed: Username/password/AuthKey/AuthDate parameter(s) cannot be null or empty."); } } + + public String postNoAuthWithResponse(final String path, final byte[] data, String contentType) + throws HttpException, JSONException { + + String responseData = null; + WebTarget target = null; + Response response = null; + if (contentType == null) { + contentType = "text/pain"; + } + target = DmaapClientUtil.getTarget(path); + + response = DmaapClientUtil.postResponsewtNoAuth(target, data, contentType); + responseData = (String) response.readEntity(String.class); + return responseData; + } public JSONObject postAuth(final String path, final byte[] data, final String contentType, final String authKey, final String authDate, final String username, final String password, final String protocolFlag) @@ -154,7 +184,7 @@ public class MRBaseClient extends HttpClient implements MRClient { Response response=null; target = DmaapClientUtil.getTarget(path, username, password); response = DmaapClientUtil.postResponsewtCambriaAuth(target, authKey, authDate, data, contentType); - responseData = (String)response.getEntity(); + responseData = (String)response.readEntity(String.class); return responseData; } else { @@ -208,7 +238,7 @@ public class MRBaseClient extends HttpClient implements MRClient { fLog.info("TransactionId : " + transactionid); } - responseData = (String)response.getEntity(); + responseData = (String)response.readEntity(String.class); return responseData; } else { throw new HttpException( @@ -262,7 +292,7 @@ public class MRBaseClient extends HttpClient implements MRClient { fLog.info("TransactionId : " + transactionid); } - responseData = (String)response.getEntity(); + responseData = (String)response.readEntity(String.class); return responseData; } else { throw new HttpException( @@ -285,7 +315,7 @@ public class MRBaseClient extends HttpClient implements MRClient { fLog.info("TransactionId : " + transactionid); } - responseData = (String)response.getEntity(); + responseData = (String)response.readEntity(String.class); return responseData; } @@ -325,7 +355,7 @@ public class MRBaseClient extends HttpClient implements MRClient { jsonObject.put("status", response.getStatus()); return jsonObject; } - String responseData = (String)response.getEntity(); + String responseData = (String)response.readEntity(String.class); JSONTokener jsonTokener = new JSONTokener(responseData); JSONObject jsonObject = null; diff --git a/src/main/java/com/att/nsa/mr/client/impl/MRSimplerBatchPublisher.java b/src/main/java/com/att/nsa/mr/client/impl/MRSimplerBatchPublisher.java index c234678..2f7680b 100644 --- a/src/main/java/com/att/nsa/mr/client/impl/MRSimplerBatchPublisher.java +++ b/src/main/java/com/att/nsa/mr/client/impl/MRSimplerBatchPublisher.java @@ -378,6 +378,23 @@ public class MRSimplerBatchPublisher extends MRBaseClient implements MRBatchingP fPending.clear(); return true; } + + if (ProtocolTypeConstants.HTTPNOAUTH.getValue().equalsIgnoreCase(protocolFlag)) { + getLog().info("sending " + fPending.size() + " msgs to " + httpurl + ". Oldest: " + + (nowMs - fPending.peek().timestamp) + " ms"); + final JSONObject result = postNoAuth(httpurl, baseStream.toByteArray(), contentType); + + // Here we are checking for error response. If HTTP status + // code is not within the http success response code + // then we consider this as error and return false + if (result.getInt("status") < 200 || result.getInt("status") > 299) { + return false; + } + final String logLine = "MR reply ok (" + (Clock.now() - startMs) + " ms):" + result.toString(); + getLog().info(logLine); + fPending.clear(); + return true; + } } catch (IllegalArgumentException x) { getLog().warn(x.getMessage(), x); } catch (IOException x) { @@ -531,6 +548,28 @@ public class MRSimplerBatchPublisher extends MRBaseClient implements MRBatchingP fPending.clear(); return pubResponse; } + + if (ProtocolTypeConstants.HTTPNOAUTH.getValue().equalsIgnoreCase(protocolFlag)) { + getLog().info("sending " + fPending.size() + " msgs to " + httpurl + ". Oldest: " + + (nowMs - fPending.peek().timestamp) + " ms"); + final String result = postNoAuthWithResponse(httpurl, baseStream.toByteArray(), contentType); + + // Here we are checking for error response. If HTTP status + // code is not within the http success response code + // then we consider this as error and return false + pubResponse = createMRPublisherResponse(result, pubResponse); + + if (Integer.valueOf(pubResponse.getResponseCode()) < 200 + || Integer.valueOf(pubResponse.getResponseCode()) > 299) { + + return pubResponse; + } + + final String logLine = String.valueOf((Clock.now() - startMs)); + getLog().info(logLine); + fPending.clear(); + return pubResponse; + } } catch (IllegalArgumentException x) { getLog().warn(x.getMessage(), x); pubResponse.setResponseCode(String.valueOf(HttpStatus.SC_BAD_REQUEST)); @@ -573,7 +612,7 @@ public class MRSimplerBatchPublisher extends MRBaseClient implements MRBatchingP return pubResponse; } - private MRPublisherResponse createMRPublisherResponse(String reply, MRPublisherResponse mrPubResponse) { + public MRPublisherResponse createMRPublisherResponse(String reply, MRPublisherResponse mrPubResponse) { if (reply.isEmpty()) { diff --git a/src/main/java/com/att/nsa/mr/test/clients/SimpleExamplePublisherWithResponse.java b/src/main/java/com/att/nsa/mr/test/clients/SimpleExamplePublisherWithResponse.java index 4914688..9d179b2 100644 --- a/src/main/java/com/att/nsa/mr/test/clients/SimpleExamplePublisherWithResponse.java +++ b/src/main/java/com/att/nsa/mr/test/clients/SimpleExamplePublisherWithResponse.java @@ -61,7 +61,7 @@ import com.att.nsa.mr.client.response.MRPublisherResponse; } } - public void publishMessage ( String producerFilePath , int count ) throws IOException, InterruptedException, Exception + public void publishMessage ( String producerFilePath , int count ) throws IOException, InterruptedException { // create our publisher final MRBatchingPublisher pub = MRClientFactory.createBatchingPublisher (producerFilePath,true); |