This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit 64b48e695937601c821b97edc64d02e7b5ca467c Author: Andrea Cosentino <anco...@gmail.com> AuthorDate: Fri Aug 31 14:09:37 2018 +0200 Better readResponse methods in camel-slack component --- .../slack/SlackComponentVerifierExtension.java | 19 +------- .../camel/component/slack/SlackConsumer.java | 17 +------ .../camel/component/slack/utils/SlackUtils.java | 53 ++++++++++++++++++++++ 3 files changed, 57 insertions(+), 32 deletions(-) diff --git a/components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackComponentVerifierExtension.java b/components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackComponentVerifierExtension.java index 9cdb350..7eb6650 100644 --- a/components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackComponentVerifierExtension.java +++ b/components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackComponentVerifierExtension.java @@ -16,11 +16,6 @@ */ package org.apache.camel.component.slack; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.UnsupportedEncodingException; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -29,7 +24,6 @@ import java.util.Map; import org.apache.camel.component.extension.verifier.DefaultComponentVerifierExtension; import org.apache.camel.component.extension.verifier.ResultBuilder; import org.apache.camel.component.extension.verifier.ResultErrorBuilder; -import org.apache.camel.component.extension.verifier.ResultErrorHelper; import org.apache.camel.component.slack.helper.SlackMessage; import org.apache.camel.util.ObjectHelper; import org.apache.http.HttpResponse; @@ -42,6 +36,8 @@ import org.apache.http.message.BasicNameValuePair; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; +import static org.apache.camel.component.slack.utils.SlackUtils.readResponse; + public class SlackComponentVerifierExtension extends DefaultComponentVerifierExtension { public SlackComponentVerifierExtension() { @@ -139,17 +135,6 @@ public class SlackComponentVerifierExtension extends DefaultComponentVerifierExt } } - private String readResponse(InputStream s) throws IOException, UnsupportedEncodingException { - ByteArrayOutputStream result = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; - int length; - while ((length = s.read(buffer)) != -1) { - result.write(buffer, 0, length); - } - String jsonString = result.toString(StandardCharsets.UTF_8.name()); - return jsonString; - } - protected String asJson(SlackMessage message) { Map<String, Object> jsonMap = new HashMap<>(); diff --git a/components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackConsumer.java b/components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackConsumer.java index c2b1acd..948a38c 100644 --- a/components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackConsumer.java +++ b/components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackConsumer.java @@ -16,10 +16,7 @@ */ package org.apache.camel.component.slack; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; @@ -44,6 +41,8 @@ import org.json.simple.parser.ParseException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static org.apache.camel.component.slack.utils.SlackUtils.readResponse; + public class SlackConsumer extends ScheduledBatchPollingConsumer { private static final Logger LOG = LoggerFactory.getLogger(SlackConsumer.class); @@ -159,16 +158,4 @@ public class SlackConsumer extends ScheduledBatchPollingConsumer { return jsonString; } - private String readResponse(HttpResponse response) throws IOException { - InputStream s = response.getEntity().getContent(); - ByteArrayOutputStream result = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; - int length; - while ((length = s.read(buffer)) != -1) { - result.write(buffer, 0, length); - } - String jsonString = result.toString(StandardCharsets.UTF_8.name()); - return jsonString; - } - } diff --git a/components/camel-slack/src/main/java/org/apache/camel/component/slack/utils/SlackUtils.java b/components/camel-slack/src/main/java/org/apache/camel/component/slack/utils/SlackUtils.java new file mode 100644 index 0000000..f5e049f --- /dev/null +++ b/components/camel-slack/src/main/java/org/apache/camel/component/slack/utils/SlackUtils.java @@ -0,0 +1,53 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.slack.utils; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; + +import org.apache.http.HttpResponse; + +public final class SlackUtils { + + private SlackUtils() { + } + + public static String readResponse(HttpResponse response) throws IOException { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try (InputStream s = response.getEntity().getContent()) { + byte[] buffer = new byte[1024]; + int length; + while ((length = s.read(buffer)) != -1) { + result.write(buffer, 0, length); + } + } + return result.toString(StandardCharsets.UTF_8.name()); + } + + public static String readResponse(InputStream s) throws IOException, UnsupportedEncodingException { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int length; + while ((length = s.read(buffer)) != -1) { + result.write(buffer, 0, length); + } + return result.toString(StandardCharsets.UTF_8.name()); + } +}