Response
Overview
Module
- package:
org.eclipse.dirigible.sdk.http - source: http/Response.java
Writes the outbound HTTP response bound to the calling thread — status, headers, cookies, body. Useful from @Controller methods that want to stream bytes, set explicit status codes, or send 30x redirects.
Most controllers can rely on the dispatcher's automatic Jackson serialization of return values and reach for this class only at the edges (file downloads, binary responses, manual error shapes). getNative() hands back the underlying HttpServletResponse when you need a Servlet-only method.
Key Features:
- Thread-bound: All methods operate on the response bound to the current thread.
- Servlet Escape Hatch:
getNative()exposes the underlyingHttpServletResponse. - Convenient Body Helpers:
print,println, andwritecover the common cases of writing text or binary bytes;getOutputStream()is available for streaming.
Example Usage:
import org.eclipse.dirigible.sdk.http.Response;
Response.setStatus(201);
Response.setContentType("application/json");
Response.setHeader("Location", "/services/java/orders/42");
Response.print("{\"id\":42}");
Response.flush();Methods
getNative()
Returns the underlying Servlet response.
javapublic static HttpServletResponse getNative();Returns
- Type:
jakarta.servlet.http.HttpServletResponse
isValid()
Returns whether an HTTP response is bound to the current thread.
javapublic static boolean isValid();Returns
- Type:
boolean
print()
Writes the given text or object to the response body.
javapublic static void print(String text); public static void print(Object o);
Parameter Type Description textStringText to write. oObjectObject to write (converted via toString()).
println()
Writes the given text followed by a newline.
javapublic static void println(String text);
Parameter Type Description textStringText to write.
write()
Writes raw bytes or a string to the response body.
javapublic static void write(byte[] bytes); public static void write(String input);
Parameter Type Description bytesbyte[]Bytes to write. inputStringString to write.
isCommitted()
Returns whether the response has been committed (headers sent).
javapublic static boolean isCommitted();Returns
- Type:
boolean
setContentType()
Sets the response Content-Type header.
javapublic static void setContentType(String contentType);
Parameter Type Description contentTypeStringA media-type value.
getContentType()
Returns the response Content-Type header.
javapublic static String getContentType();Returns
- Type:
String
flush()
Flushes any buffered output to the client.
javapublic static void flush();
close()
Closes the response output stream.
javapublic static void close();
addCookie()
Adds a cookie to the response.
javapublic static void addCookie(String cookieJson);
Parameter Type Description cookieJsonStringA JSON document describing the cookie ( name,value,path,maxAge,domain,secure,httpOnly, …).
containsHeader()
Returns whether the response already contains the named header.
javapublic static boolean containsHeader(String name);
Parameter Type Description nameStringHeader name. Returns
- Type:
boolean
encodeURL()
Encodes the given URL for use in this response, rewriting it to include the session ID if necessary.
javapublic static String encodeURL(String url);
Parameter Type Description urlStringURL to encode. Returns
- Type:
String
encodeRedirectURL()
Encodes the given URL for use in a sendRedirect call.
javapublic static String encodeRedirectURL(String url);
Parameter Type Description urlStringURL to encode. Returns
- Type:
String
sendError()
Sends an error response with the given status code (and optional message).
javapublic static void sendError(int code, String message) throws IOException; public static void sendError(int code) throws IOException;
Parameter Type Description codeintHTTP status code. messageStringOptional error message.
sendRedirect()
Sends a temporary redirect (302) response to the given location.
javapublic static void sendRedirect(String location) throws IOException;
Parameter Type Description locationStringThe redirect URL.
setCharacterEncoding()
Sets the response character encoding.
javapublic static void setCharacterEncoding(String charset);
Parameter Type Description charsetStringCharset name (e.g. UTF-8).
getCharacterEncoding()
Returns the response character encoding.
javapublic static String getCharacterEncoding();Returns
- Type:
String
setContentLength()
Sets the Content-Length header.
javapublic static void setContentLength(int length);
Parameter Type Description lengthintBody length in bytes.
setHeader()
Sets (replaces) a response header.
javapublic static void setHeader(String name, String value);
Parameter Type Description nameStringHeader name. valueStringHeader value.
addHeader()
Adds a response header (allowing multiple values).
javapublic static void addHeader(String name, String value);
Parameter Type Description nameStringHeader name. valueStringHeader value.
getHeader()
Returns the value of a response header.
javapublic static String getHeader(String name);
Parameter Type Description nameStringHeader name. Returns
- Type:
String
getHeaders()
Returns all values for a response header.
javapublic static String getHeaders(String name);
Parameter Type Description nameStringHeader name. Returns
- Type:
String- Description: A JSON-encoded list of header values.
getHeaderNames()
Returns the names of all response headers.
javapublic static String getHeaderNames();Returns
- Type:
String- Description: A JSON-encoded list of header names.
setStatus()
Sets the HTTP status code.
javapublic static void setStatus(int code);
Parameter Type Description codeintHTTP status code.
reset()
Resets buffered output, headers, and status code.
javapublic static void reset();
setLocale()
Sets the response locale.
javapublic static void setLocale(String language);
Parameter Type Description languageStringLanguage tag (e.g. en,de-DE).
getLocale()
Returns the response locale.
javapublic static String getLocale();Returns
- Type:
String
getOutputStream()
Returns the raw response output stream — useful for streaming binary responses.
javapublic static OutputStream getOutputStream() throws IOException;Returns
- Type:
java.io.OutputStream