@Override public OutputStream writeResponseStatusAndHeaders(final long contentLength, final ContainerResponse context) throws ContainerException { final javax.ws.rs.core.Response.StatusType statusInfo = context.getStatusInfo(); final int code = statusInfo.getStatusCode(); final String reason = statusInfo.getReasonPhrase() == null ? HttpStatus.getMessage(code) : statusInfo.getReasonPhrase(); response.setStatusWithReason(code, reason); if (contentLength != -1 && contentLength < Integer.MAX_VALUE) { response.setContentLength((int) contentLength); } for (final Map.Entry<String, List<String>> e : context.getStringHeaders().entrySet()) { for (final String value : e.getValue()) { response.addHeader(e.getKey(), value); } } try { return response.getOutputStream(); } catch (final IOException ioe) { throw new ContainerException("Error during writing out the response headers.", ioe); } }
res = createResponse(Response.Status.RANGE_NOT_SATISFIABLE, NanoHTTPD.MIME_PLAINTEXT, ""); res.addHeader("Content-Range", "bytes 0-0/" + fileLen); res.addHeader("ETag", etag); } else { if (endAt < 0) { res.addHeader("Content-Length", "" + dataLen); res.addHeader("Content-Range", "bytes " + startFrom + "-" + endAt + "/" + fileLen); res.addHeader("ETag", etag); res = createResponse(Response.Status.OK, mime, new FileInputStream(file)); res.addHeader("Content-Length", "" + fileLen); res.addHeader("ETag", etag);
res.addHeader("Accept-Ranges", "bytes"); return res;
@Override public void sendResponse(Response response) throws IOException { // Add call headers Parameter header; for (final Iterator<Parameter> iter = getResponseHeaders().iterator(); iter .hasNext();) { header = iter.next(); getConnection().getResponse().addHeader(header.getName(), header.getValue()); } // Set the status code in the response. We do this after adding the // headers because when we have to rely on the 'sendError' method, // the Servlet containers are expected to commit their response. if (Status.isError(getStatusCode()) && (response.getEntity() == null)) { try { getConnection().getResponse().sendError(getStatusCode(), getReasonPhrase()); } catch (IOException ioe) { getLogger().log(Level.WARNING, "Unable to set the response error status", ioe); } } else { // Send the response entity getConnection().getResponse().setStatus(getStatusCode()); super.sendResponse(response); } } }