mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Add helper method for pretty-printing JSON in TaskUtil class. Improve logging in GenericRepository.
This commit is contained in:
@@ -8,6 +8,7 @@ import com.intellij.tasks.Task;
|
||||
import com.intellij.tasks.TaskRepositorySubtype;
|
||||
import com.intellij.tasks.TaskRepositoryType;
|
||||
import com.intellij.tasks.impl.BaseRepositoryImpl;
|
||||
import com.intellij.tasks.impl.TaskUtil;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.net.HTTPMethod;
|
||||
import com.intellij.util.xmlb.annotations.AbstractCollection;
|
||||
@@ -21,6 +22,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.InputStream;
|
||||
import java.util.*;
|
||||
|
||||
import static com.intellij.tasks.generic.GenericRepositoryUtil.concat;
|
||||
@@ -205,16 +207,26 @@ public class GenericRepository extends BaseRepositoryImpl {
|
||||
Header contentType = method.getResponseHeader("Content-Type");
|
||||
if (contentType != null && contentType.getValue().contains("charset")) {
|
||||
// ISO-8859-1 if charset wasn't specified in response
|
||||
responseBody = method.getResponseBodyAsString();
|
||||
responseBody = StringUtil.notNullize(method.getResponseBodyAsString());
|
||||
}
|
||||
else {
|
||||
responseBody = StreamUtil.readText(method.getResponseBodyAsStream(), "utf-8");
|
||||
InputStream stream = method.getResponseBodyAsStream();
|
||||
responseBody = stream == null? "": StreamUtil.readText(stream, "utf-8");
|
||||
}
|
||||
}
|
||||
finally {
|
||||
method.releaseConnection();
|
||||
}
|
||||
LOG.debug(responseBody);
|
||||
switch (getResponseType()) {
|
||||
case XML:
|
||||
TaskUtil.prettyFormatXmlToLog(LOG, responseBody);
|
||||
break;
|
||||
case JSON:
|
||||
TaskUtil.prettyFormatJsonToLog(LOG, responseBody);
|
||||
break;
|
||||
default:
|
||||
LOG.debug(responseBody);
|
||||
}
|
||||
LOG.debug("Status code is " + method.getStatusCode());
|
||||
if (method.getStatusCode() != HttpStatus.SC_OK) {
|
||||
throw new Exception("Request failed with HTTP error: " + method.getStatusText());
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package com.intellij.tasks.impl;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.util.JDOMUtil;
|
||||
@@ -145,23 +148,23 @@ public class TaskUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Print pretty-formatted XML in {@code logger} if its level is DEBUG or below
|
||||
* Print pretty-formatted XML to {@code logger} if its level is DEBUG or below
|
||||
*/
|
||||
public static void prettyFormatXmlToLog(@NotNull Logger logger, @NotNull Element element) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
// alternatively
|
||||
//new XMLOutputter(Format.getPrettyFormat()).outputString(root)
|
||||
logger.debug(JDOMUtil.createOutputter("\n").outputString(element));
|
||||
logger.debug("\n" + JDOMUtil.createOutputter("\n").outputString(element));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse and print pretty-formatted XML in {@code logger} if its level is DEBUG or below
|
||||
* Parse and print pretty-formatted XML to {@code logger} if its level is DEBUG or below
|
||||
*/
|
||||
public static void prettyFormatXmlToLog(@NotNull Logger logger, @NotNull InputStream xmlStream) {
|
||||
public static void prettyFormatXmlToLog(@NotNull Logger logger, @NotNull InputStream xml) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
try {
|
||||
logger.debug(JDOMUtil.createOutputter("\n").outputString(JDOMUtil.loadDocument(xmlStream)));
|
||||
logger.debug("\n" + JDOMUtil.createOutputter("\n").outputString(JDOMUtil.loadDocument(xml)));
|
||||
}
|
||||
catch (Exception e) {
|
||||
logger.debug(e);
|
||||
@@ -170,16 +173,26 @@ public class TaskUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse and print pretty-formatted XML in {@code logger} if its level is DEBUG or below
|
||||
* Parse and print pretty-formatted XML to {@code logger} if its level is DEBUG or below
|
||||
*/
|
||||
public static void prettyFormatXmlToLog(@NotNull Logger logger, @NotNull String xmlString) {
|
||||
public static void prettyFormatXmlToLog(@NotNull Logger logger, @NotNull String xml) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
try {
|
||||
logger.debug(JDOMUtil.createOutputter("\n").outputString(JDOMUtil.loadDocument(xmlString)));
|
||||
logger.debug("\n" + JDOMUtil.createOutputter("\n").outputString(JDOMUtil.loadDocument(xml)));
|
||||
}
|
||||
catch (Exception e) {
|
||||
logger.debug(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse and print pretty-formatted Json to {@code logger} if its level is DEBUG or below
|
||||
*/
|
||||
public static void prettyFormatJsonToLog(@NotNull Logger logger, @NotNull String json) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
logger.debug("\n" + gson.toJson(gson.fromJson(json, JsonElement.class)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user