-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Returning either byte array or String.
- Loading branch information
1 parent
c9812e1
commit ffe618f
Showing
2 changed files
with
17 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 15 additions & 27 deletions
42
uni-resolver-web/src/main/java/uniresolver/web/servlet/ServletUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |