Class ResponseWriter
- All Implemented Interfaces:
Serializable
For internal use only. May be renamed or removed in a future release.
- Since:
- 1.0.
- Author:
- Vaadin Ltd
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionResponseWriter(DeploymentConfiguration deploymentConfiguration) Create a response writer with the given deployment configuration. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanacceptsBrotliResource(jakarta.servlet.http.HttpServletRequest request) Returns whether it is ok to serve a Brotli version of the given resource.protected booleanacceptsGzippedResource(jakarta.servlet.http.HttpServletRequest request) Returns whether it is ok to serve a gzipped version of the given resource.parseRanges(String range, long resourceLength, Object resource) Parses the value of aRangerequest header the same way aswriteResponseContents(java.lang.String, java.net.URL, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)does for static files.static voidwriteRanges(List<Pair<Long, Long>> ranges, long resourceLength, InputStream dataStream, URL resourceUrl, OutputStream outputStream, jakarta.servlet.http.HttpServletResponse response) Writes a range response for ranges returned byparseRanges(String, long, Object), the same way aswriteResponseContents(java.lang.String, java.net.URL, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)does for static files:206 Partial Contentwith a single range or amultipart/byterangesbody, or416 Range Not Satisfiableif the list is empty.voidwriteResponseContents(String filenameWithPath, URL resourceUrl, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Writes the contents and content type (if available) of the given resourceUrl to the response.
-
Constructor Details
-
ResponseWriter
Create a response writer with the given deployment configuration.- Parameters:
deploymentConfiguration- the deployment configuration to use, notnull- Since:
- 1.3
-
-
Method Details
-
writeResponseContents
public void writeResponseContents(String filenameWithPath, URL resourceUrl, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException Writes the contents and content type (if available) of the given resourceUrl to the response.WARNING: note that this should not be used for a
resourceUrlthat represents a directory! For security reasons, the directory contents should not be ever written into theresponse, and the implementation which is used for setting the content length relies onURLConnection.getContentLengthLong()method which returns incorrect values for directories.- Parameters:
filenameWithPath- the name of the file being sentresourceUrl- the URL to the file, reported by the servlet containerrequest- the request object to read fromresponse- the response object to write to- Throws:
IOException- if the servlet container threw an exception while locating the resource
-
parseRanges
Parses the value of aRangerequest header the same way aswriteResponseContents(java.lang.String, java.net.URL, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)does for static files.Returns
nullif the header should be ignored and the whole content sent, which RFC 9110 allows: when it is not a validbytesrange, or the length of the resource is unknown. Otherwise returns the requested ranges as inclusive first and last byte positions, clamped to the length. Ranges that start past the end are dropped, so an empty list means that none of them is satisfiable. Ranges beyond the limits on count and overlap are dropped as well.- Parameters:
range- the value of theRangerequest header, notnullresourceLength- the length of the resource, or-1if unknownresource- the resource, used in log messages- Returns:
- the ranges to send, or
nullif the header is ignored
-
writeRanges
public static void writeRanges(List<Pair<Long, Long>> ranges, long resourceLength, InputStream dataStream, URL resourceUrl, OutputStream outputStream, jakarta.servlet.http.HttpServletResponse response) throws IOExceptionWrites a range response for ranges returned byparseRanges(String, long, Object), the same way aswriteResponseContents(java.lang.String, java.net.URL, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)does for static files:206 Partial Contentwith a single range or amultipart/byterangesbody, or416 Range Not Satisfiableif the list is empty.The content is read from the given stream, skipping to each range. The resource is opened again from its URL only if the ranges are not in ascending order. The status and headers of a single range are set only after reading its first bytes, so content that ends before the range starts fails before anything is committed.
- Parameters:
ranges- the ranges to send, notnullresourceLength- the length of the resourcedataStream- the content of the resource, positioned at its first byte, notnull; not closed by this methodresourceUrl- the URL of the resource, to read it again for ranges that are not in ascending order, ornullif the ranges are in ascending orderoutputStream- the stream to write the body to, usually the output stream of the response, notnull; not flushed or closed by this methodresponse- the response to set the status and headers of, notnull- Throws:
IOException- if reading the resource or writing the response fails, or the resource ends before a range does
-
acceptsGzippedResource
protected boolean acceptsGzippedResource(jakarta.servlet.http.HttpServletRequest request) Returns whether it is ok to serve a gzipped version of the given resource.If this method returns true, the browser is ok with receiving a gzipped version of the resource. In other cases, an uncompressed file must be sent.
- Parameters:
request- the request for the resource- Returns:
- true if the servlet should attempt to serve a gzipped version of the resource, false otherwise
-
acceptsBrotliResource
protected boolean acceptsBrotliResource(jakarta.servlet.http.HttpServletRequest request) Returns whether it is ok to serve a Brotli version of the given resource.If this method returns true, the browser is ok with receiving a Brotli version of the resource. In other cases, an uncompressed or gzipped file must be sent.
- Parameters:
request- the request for the resource- Returns:
- true if the servlet should attempt to serve a Brotli version of the resource, false otherwise
- Since:
- 1.3
-