diff --git a/src/main/java/com/iota/iri/service/API.java b/src/main/java/com/iota/iri/service/API.java index 9f345899ae..d10fe4add7 100644 --- a/src/main/java/com/iota/iri/service/API.java +++ b/src/main/java/com/iota/iri/service/API.java @@ -40,6 +40,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xnio.channels.StreamSinkChannel; +import org.xnio.channels.StreamSourceChannel; import org.xnio.streams.ChannelInputStream; import java.io.IOException; @@ -88,7 +89,7 @@ public class API { private Undertow server; - private final Gson gson = new GsonBuilder().create(); + private final Gson gson = new GsonBuilder().disableHtmlEscaping().create(); private volatile PearlDiver pearlDiver = new PearlDiver(); private final AtomicInteger counter = new AtomicInteger(0); @@ -220,7 +221,6 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception { */ private void sendResponse(HttpServerExchange exchange, AbstractResponse res, long beginningTime) throws IOException { res.setDuration((int) (System.currentTimeMillis() - beginningTime)); - final String response = gson.toJson(res); if (res instanceof ErrorResponse) { // bad request or invalid parameters @@ -233,7 +233,10 @@ private void sendResponse(HttpServerExchange exchange, AbstractResponse res, lon exchange.setStatusCode(500); } - setupResponseHeaders(exchange); + + setupResponseHeaders(exchange, res); + + final String response = convertResponseToClientFormat(res); ByteBuffer responseBuf = ByteBuffer.wrap(response.getBytes(StandardCharsets.UTF_8)); exchange.setResponseContentLength(responseBuf.array().length); @@ -258,6 +261,21 @@ private void sendResponse(HttpServerExchange exchange, AbstractResponse res, lon sinkChannel.resumeWrites(); } + private String convertResponseToClientFormat(AbstractResponse res) { + String response = null; + if(res instanceof IXIResponse){ + final String content = ((IXIResponse)res).getContent(); + if(content != null && StringUtils.isNotBlank(content)){ + response = content; + } + } + if(response == null){ + response = gson.toJson(res); + } + + return response; + } + /** *
* Processes an API HTTP request.
@@ -275,23 +293,48 @@ private void sendResponse(HttpServerExchange exchange, AbstractResponse res, lon
* @throws IOException If the body of this HTTP request cannot be read
*/
private void processRequest(final HttpServerExchange exchange) throws IOException {
- final ChannelInputStream cis = new ChannelInputStream(exchange.getRequestChannel());
- exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
-
final long beginningTime = System.currentTimeMillis();
- final String body = IotaIOUtils.toString(cis, StandardCharsets.UTF_8);
- AbstractResponse response;
- if (!exchange.getRequestHeaders().contains("X-IOTA-API-Version")) {
- response = ErrorResponse.create("Invalid API Version");
- } else if (body.length() > maxBodyLength) {
- response = ErrorResponse.create("Request too long");
- } else {
- response = process(body, exchange.getSourceAddress());
+ AbstractResponse response;
+ try {
+ final String body = getRequestBody(exchange);
+ if (body.length() > maxBodyLength) {
+ response = ErrorResponse.create("Request too long");
+ } else {
+ response = process(body, exchange);
+ }
+ } catch (IOException e) {
+ log.error("API Exception: {}", e.getLocalizedMessage(), e);
+ response = ErrorResponse.create(e.getLocalizedMessage());
}
sendResponse(exchange, response, beginningTime);
}
+ private String getRequestBody(final HttpServerExchange exchange) throws IOException {
+ StreamSourceChannel requestChannel = exchange.getRequestChannel();
+ final ChannelInputStream cis = new ChannelInputStream(requestChannel);
+ String body = IotaIOUtils.toString(cis, StandardCharsets.UTF_8);
+
+ if(body.length() == 0){
+ body = getQueryParamsBody(exchange.getQueryParameters());
+ } else if (!exchange.getRequestHeaders().contains("X-IOTA-API-Version")) {
+ throw new IOException ("Invalid API Version");
+ }
+ return body;
+ }
+
+ private String getQueryParamsBody(Map
@@ -27,4 +29,26 @@ public static IXIResponse create(Object myixi) {
public Object getResponse() {
return ixi;
}
+
+ private String getdefaultContentType() {
+ return "application/json";
+ }
+
+ public String getResponseContentType() {
+ Map