Modify the Http Entity Parsing Logic
authortangpeng <tang.peng5@zte.com.cn>
Mon, 13 Mar 2017 02:21:08 +0000 (10:21 +0800)
committertangpeng <tang.peng5@zte.com.cn>
Mon, 13 Mar 2017 02:21:08 +0000 (10:21 +0800)
Change-Id: Ic561d7143b97f0f3bfafed02a24167ec299d0e9f
Issue-ID: HOLMES-50
Signed-off-by: tangpeng <tang.peng5@zte.com.cn>
rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java

index 3f0e661..4295069 100644 (file)
  */\r
 package org.openo.holmes.rulemgt.bolt.enginebolt;\r
 \r
+import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;\r
+\r
 import com.fasterxml.jackson.databind.ObjectMapper;\r
 import java.io.ByteArrayOutputStream;\r
 import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.InputStreamReader;\r
+import java.io.Reader;\r
 import java.io.UnsupportedEncodingException;\r
+import java.nio.charset.Charset;\r
 import lombok.extern.slf4j.Slf4j;\r
 import org.apache.commons.lang3.StringUtils;\r
+import org.apache.http.ConnectionClosedException;\r
 import org.apache.http.HttpEntity;\r
 import org.apache.http.HttpResponse;\r
 import org.apache.http.client.methods.HttpDelete;\r
@@ -30,8 +37,10 @@ import org.apache.http.client.methods.HttpPut;
 import org.apache.http.client.methods.HttpRequestBase;\r
 import org.apache.http.entity.BufferedHttpEntity;\r
 import org.apache.http.entity.ByteArrayEntity;\r
+import org.apache.http.entity.ContentType;\r
 import org.apache.http.impl.client.CloseableHttpClient;\r
 import org.apache.http.impl.client.HttpClients;\r
+import org.apache.http.protocol.HTTP;\r
 import org.apache.http.util.EntityUtils;\r
 import org.jvnet.hk2.annotations.Service;\r
 import org.openo.holmes.common.config.MicroServiceConfig;\r
@@ -119,6 +128,46 @@ public class EngineService {
         httpRequestBase.setHeader("Content-Type", "application/json");\r
     }\r
 \r
+    public String getResponseContent(HttpResponse response) {\r
+        HttpEntity entity = response.getEntity();\r
+        InputStream is = null;\r
+        if (entity != null) {\r
+            try {\r
+                is = entity.getContent();\r
+                final ContentType contentType = ContentType.getOrDefault(entity);\r
+                Charset charset = contentType.getCharset();\r
+                if (charset == null) {\r
+                    charset = HTTP.DEF_CONTENT_CHARSET;\r
+                }\r
+                final StringBuilder b = new StringBuilder();\r
+                final char[] tmp = new char[1024];\r
+                final Reader reader = new InputStreamReader(is, charset);\r
+                try {\r
+                    int l;\r
+                    while ((l = reader.read(tmp)) != -1) {\r
+                        b.append(tmp, 0, l);\r
+                    }\r
+                } catch (ConnectionClosedException ignore) {\r
+\r
+                } catch (IOException e) {\r
+                    log.info("Failed to read the contents of the input stream of the http entity.", e);\r
+                }\r
+                return b.toString();\r
+            } catch (IOException e) {\r
+                log.info("Failed to read the contents of the http entity.", e);\r
+            } finally {\r
+                try {\r
+                    if (is != null) {\r
+                        is.close();\r
+                    }\r
+                } catch (IOException e) {\r
+                    log.info("Failed to close the input stream of the http entity.", e);\r
+                }\r
+            }\r
+        }\r
+        return "{}";\r
+    }\r
+\r
     public byte[] getData(HttpEntity httpEntity) throws IOException {\r
         log.info("Rule deployed. Package name: " + httpEntity.getContent().toString()\r
                 + ". Content length: " + httpEntity.getContentLength());\r
@@ -129,21 +178,21 @@ public class EngineService {
         return responseBytes;\r
     }\r
 \r
-    public String getResponseContent(HttpResponse httpResponse) throws CorrelationException {\r
-        byte[] dataByte;\r
-        String result = null;\r
-        try {\r
-            HttpEntity httpEntity = httpResponse.getEntity();\r
-            if (httpEntity != null) {\r
-                byte[] responseBytes = getData(httpEntity);\r
-                dataByte = responseBytes;\r
-                result = bytesToString(dataByte);\r
-            }\r
-            return result;\r
-        } catch (Exception e) {\r
-            throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_PARSE_DEPLOY_RESULT_ERROR, e);\r
-        }\r
-    }\r
+//    public String getResponseContent(HttpResponse httpResponse) throws CorrelationException {\r
+//        byte[] dataByte;\r
+//        String result = null;\r
+//        try {\r
+//            HttpEntity httpEntity = httpResponse.getEntity();\r
+//            if (httpEntity != null) {\r
+//                byte[] responseBytes = getData(httpEntity);\r
+//                dataByte = responseBytes;\r
+//                result = bytesToString(dataByte);\r
+//            }\r
+//            return result;\r
+//        } catch (Exception e) {\r
+//            throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_PARSE_DEPLOY_RESULT_ERROR, e);\r
+//        }\r
+//    }\r
 \r
     private String bytesToString(byte[] bytes) throws UnsupportedEncodingException {\r
         if (bytes != null) {\r