diff --git a/uni-resolver-web/src/main/java/uniresolver/web/servlet/ResolveServlet.java b/uni-resolver-web/src/main/java/uniresolver/web/servlet/ResolveServlet.java index 09f36f701..fc5680046 100644 --- a/uni-resolver-web/src/main/java/uniresolver/web/servlet/ResolveServlet.java +++ b/uni-resolver-web/src/main/java/uniresolver/web/servlet/ResolveServlet.java @@ -107,7 +107,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t if (MediaTypeUtil.isMediaTypeAcceptable(httpAcceptMediaType, ResolveResult.MEDIA_TYPE)) { - if (log.isDebugEnabled()) log.debug("Supporting HTTP media type " + httpAcceptMediaType + " via content type " + ResolveResult.MEDIA_TYPE); + if (log.isDebugEnabled()) log.debug("Supporting HTTP media type " + httpAcceptMediaType + " via default content type " + ResolveResult.MEDIA_TYPE); ServletUtil.sendResponse( response, @@ -115,7 +115,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t ResolveResult.MEDIA_TYPE, HttpBindingServerUtil.toHttpBodyStreamResult(resolveRepresentationResult)); return; - } else { + } else if (resolveRepresentationResult.getContentType() != null) { // determine representation media type diff --git a/uni-resolver-web/src/main/java/uniresolver/web/servlet/ServletUtil.java b/uni-resolver-web/src/main/java/uniresolver/web/servlet/ServletUtil.java index f9ee951ed..d14ddcb0e 100644 --- a/uni-resolver-web/src/main/java/uniresolver/web/servlet/ServletUtil.java +++ b/uni-resolver-web/src/main/java/uniresolver/web/servlet/ServletUtil.java @@ -1,61 +1,49 @@ package uniresolver.web.servlet; +import jakarta.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; -import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; import java.util.Map; public class ServletUtil { private static final Logger log = LoggerFactory.getLogger(ServletUtil.class); - static void sendResponse(HttpServletResponse response, int status, Map headers, Object body) throws IOException { + static void sendResponse(HttpServletResponse response, int status, Map headers, byte[] body) throws IOException { - if (log.isDebugEnabled()) log.debug("Sending response with status " + status + " and headers " + headers + " and body (" + (body == null ? null : body.getClass()) + ")"); + if (log.isInfoEnabled()) log.info("Sending response with status " + status + " and headers " + headers + " and body (" + (body == null ? null : body.length) + ")"); response.setStatus(status); if (headers != null) for (Map.Entry header : headers.entrySet()) response.setHeader(header.getKey(), header.getValue()); response.setHeader("Access-Control-Allow-Origin", "*"); - if (body instanceof String) { - - PrintWriter printWriter = response.getWriter(); - printWriter.write((String) body); - printWriter.flush(); - printWriter.close(); - } else if (body instanceof byte[]) { - + if (body != null) { OutputStream outputStream = response.getOutputStream(); - outputStream.write((byte[]) body); + outputStream.write(body); outputStream.flush(); outputStream.close(); - } else if (body != null) { - - PrintWriter printWriter = response.getWriter(); - printWriter.write(body.toString()); - printWriter.flush(); - printWriter.close(); } response.flushBuffer(); } - static void sendResponse(HttpServletResponse response, int status, Map headers) throws IOException { - - sendResponse(response, status, headers, null); - } - - static void sendResponse(HttpServletResponse response, int status, String contentType, Object body) throws IOException { - + static void sendResponse(HttpServletResponse response, int status, String contentType, byte[] body) throws IOException { sendResponse(response, status, Map.of("Content-Type", contentType), body); } - static void sendResponse(HttpServletResponse response, int status, Object body) throws IOException { + static void sendResponse(HttpServletResponse response, int status, String contentType, String body) throws IOException { + sendResponse(response, status, Map.of("Content-Type", contentType), body == null ? null : body.getBytes(StandardCharsets.UTF_8)); + } + static void sendResponse(HttpServletResponse response, int status, byte[] body) throws IOException { sendResponse(response, status, (Map) null, body); } + + static void sendResponse(HttpServletResponse response, int status, String body) throws IOException { + sendResponse(response, status, (Map) null, body == null ? null : body.getBytes(StandardCharsets.UTF_8)); + } }