Skip to content

Commit

Permalink
refactor: Returning either byte array or String.
Browse files Browse the repository at this point in the history
  • Loading branch information
peacekeeper committed Aug 15, 2023
1 parent c9812e1 commit ffe618f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ 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,
HttpBindingServerUtil.httpStatusCodeForResult(resolveRepresentationResult),
ResolveResult.MEDIA_TYPE,
HttpBindingServerUtil.toHttpBodyStreamResult(resolveRepresentationResult));
return;
} else {
} else if (resolveRepresentationResult.getContentType() != null) {

// determine representation media type

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> headers, Object body) throws IOException {
static void sendResponse(HttpServletResponse response, int status, Map<String, String> 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<String, String> 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<String, String> 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<String, String>) null, body);
}

static void sendResponse(HttpServletResponse response, int status, String body) throws IOException {
sendResponse(response, status, (Map<String, String>) null, body == null ? null : body.getBytes(StandardCharsets.UTF_8));
}
}

0 comments on commit ffe618f

Please sign in to comment.