Skip to content
Snippets Groups Projects

Add support for object-base returns in API middleware

3 files
+ 59
24
Compare changes
  • Side-by-side
  • Inline
Files
3
/*********************************************************************
/*********************************************************************
* Copyright (c) 2019 Eclipse Foundation.
* Copyright (c) 2019. 2023 Eclipse Foundation.
*
*
* This program and the accompanying materials are made
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* available under the terms of the Eclipse Public License 2.0
@@ -11,8 +11,14 @@
@@ -11,8 +11,14 @@
**********************************************************************/
**********************************************************************/
package org.eclipsefoundation.core.service.impl;
package org.eclipsefoundation.core.service.impl;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStream;
 
import java.io.InputStreamReader;
 
import java.io.Reader;
 
import java.io.StringWriter;
 
import java.io.Writer;
import java.net.URI;
import java.net.URI;
 
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.LinkedList;
import java.util.List;
import java.util.List;
import java.util.function.Function;
import java.util.function.Function;
@@ -100,7 +106,13 @@ public class DefaultAPIMiddleware implements APIMiddleware {
@@ -100,7 +106,13 @@ public class DefaultAPIMiddleware implements APIMiddleware {
try {
try {
if (o instanceof InputStream
if (o instanceof InputStream
&& (r.getHeaderString("Content-Encoding") != null && r.getHeaderString("Content-Encoding").equalsIgnoreCase("GZIP"))) {
&& (r.getHeaderString("Content-Encoding") != null && r.getHeaderString("Content-Encoding").equalsIgnoreCase("GZIP"))) {
out.addAll(objectMapper.readerForListOf(type).readValue(new GZIPInputStream((InputStream) o)));
// extract content so we can determine whether its a list or single object
 
String content = convertToString(new GZIPInputStream((InputStream) o));
 
if (content.startsWith("[")) {
 
out.addAll(objectMapper.readerForListOf(type).readValue(content));
 
} else {
 
out.add(objectMapper.readerFor(type).readValue(content));
 
}
} else if (o instanceof InputStream) {
} else if (o instanceof InputStream) {
out.addAll(objectMapper.readerForListOf(type).readValue((InputStream) o));
out.addAll(objectMapper.readerForListOf(type).readValue((InputStream) o));
} else if (type.isAssignableFrom(o.getClass())) {
} else if (type.isAssignableFrom(o.getClass())) {
@@ -114,6 +126,23 @@ public class DefaultAPIMiddleware implements APIMiddleware {
@@ -114,6 +126,23 @@ public class DefaultAPIMiddleware implements APIMiddleware {
return out;
return out;
}
}
 
/**
 
* Converts UTF-8 GZIP'd content to a String to be later analyzed and consumed.
 
*
 
* @param gzipInputStream the GZIP'd content
 
* @return a string containing the uncompressed data
 
* @throws IOException if there is an error reading the GZIP stream
 
*/
 
private String convertToString(GZIPInputStream gzipInputStream) throws IOException {
 
try (Reader reader = new InputStreamReader(gzipInputStream, StandardCharsets.UTF_8); Writer writer = new StringWriter()) {
 
char[] buffer = new char[10240];
 
for (int length = 0; (length = reader.read(buffer)) > 0;) {
 
writer.write(buffer, 0, length);
 
}
 
return writer.toString();
 
}
 
}
 
private String getURISafely(URI uri) {
private String getURISafely(URI uri) {
if (uri == null) {
if (uri == null) {
return "";
return "";
Loading