private static void handleUnsupportedVersion(final Response response) throws IOException { response.setStatus(400); response.setMessage("Bad Request"); response.addHeader(WebSocketEngine.SEC_WS_VERSION, Version.getSupportedWireProtocolVersions()); response.sendHeaders(); response.flush(); }
/** * Return the value for the specified header, or <code>null</code> if this * header has not been set. If more than one value was added for this * name, only the first is returned; use getHeaderValues() to retrieve all * of them. * * @param name Header name to look up */ public String getHeader(String name) { return response.getMimeHeaders().getHeader(name); }
@Override public void setHeaders(Response response) { response.setMessage("WebSocket Protocol Handshake"); response.setHeader("Upgrade", "WebSocket"); response.setHeader(SERVER_SEC_WS_LOCATION_HEADER, getLocation()); response.setHeader(SERVER_SEC_WS_ORIGIN_HEADER, getOrigin()); } }
public void timeout(boolean forceClose){ // If the buffers are empty, commit the response header try{ completionHandler.cancelled(attachment); } finally { if (forceClose &&!response.isCommitted()){ try{ response.sendHeaders(); response.flush(); response.finish(); } catch (IOException ex){ // Swallow? } } } }
int statusCode = response.getStatus(); if ((statusCode == 204) || (statusCode == 205) || (statusCode == 304)) { response.setContentLength(-1); MimeHeaders headers = response.getMimeHeaders(); if (!entityBody) { response.setContentLength(-1); } else { String contentType = response.getContentType(); if (contentType != null) { headers.setValue("Content-Type").setString(contentType); String contentLanguage = response.getContentLanguage(); if (contentLanguage != null) { headers.setValue("Content-Language") int contentLength = response.getContentLength(); if (contentLength != -1) { headers.setValue("Content-Length").setInt(contentLength); (outputFilters[Constants.CHUNKED_FILTER]); contentDelimitation = true; response.addHeader("Transfer-Encoding", "chunked"); } else { outputBuffer.addActiveFilter response.setHeader("Content-Encoding", "gzip");
res.setStatus(302); res.setMessage("Moved Temporarily"); res.setHeader("Location", req.scheme() + "://" + req.serverName() + ":" + req.getServerPort() + "/index.html"); res.setHeader("Connection", "close"); res.setHeader("Cache-control", "private"); res.sendHeaders(); return; res.setStatus(404); customizedErrorPage(req,res); return; res.setStatus(200); String ct= MimeType.get(ext); if( ct!=null) { res.setContentType(ct); res.setContentType(MimeType.get("html")); res.setContentLengthLong(length); res.sendHeaders(); OutputBuffer outputBuffer = res.getOutputBuffer(); res.flush();
res.setStatus(302); res.setMessage("Moved Temporarily"); res.setHeader("Location", req.scheme() + "://" + req.serverName() + ":" + req.getServerPort() + "/index.html"); res.setHeader("Connection", "close"); res.setHeader("Cache-control", "private"); res.sendHeaders(); return; res.setStatus(404); customizedErrorPage(req,res); return; res.setStatus(200); String ct= MimeType.get(ext); if( ct!=null) { res.setContentType(ct); res.setContentType(MimeType.get("html")); res.setContentLength((int)resource.length()); res.sendHeaders(); while ((rd = fis.read(b)) > 0) { chunk.setBytes(b, 0, rd); res.doWrite(chunk);
logger.log(Level.FINE, "File not found " + resource); res.setStatus(404); if (commitErrorResponse) { customizedErrorPage(req, res); res.setStatus(200); String substr; int dot = uri.lastIndexOf("."); String ct = MimeType.get(ext, defaultContentType); if (ct != null) { res.setContentType(ct); res.setContentType(defaultContentType); res.setContentLengthLong(length); res.sendHeaders(); OutputBuffer outputBuffer = res.getOutputBuffer(); res.flush(); while ((rd = fis.read(b)) > 0) { chunk.setBytes(b, 0, rd); res.doWrite(chunk);
res.setStatus(404); return; res.setStatus(200); String ct= MimeType.get(ext); if( ct!=null) { res.setContentType(ct); res.setContentType(MimeType.get("html")); res.setContentLength((int)resource.length()); res.sendHeaders(); while ((rd = fis.read(b)) > 0) { chunk.setBytes(b, 0, rd); res.doWrite(chunk); res.finish();
public void respond(WebSocketApplication application, Response response) { response.setStatus(101); response.setMessage("Web Socket Protocol Handshake"); response.setHeader("Upgrade", "websocket"); response.setHeader("Connection", "Upgrade"); setHeaders(response); if (!getSubProtocol().isEmpty()) { response.setHeader(WebSocketEngine.SEC_WS_PROTOCOL_HEADER, join(application.getSupportedProtocols(getSubProtocol()))); } try { response.sendHeaders(); response.flush(); } catch (IOException e) { throw new HandshakeException(e.getMessage(), e); } }
res.setMessage("Not Found"); res.setStatus(404); ByteBuffer bb = HtmlHelper.getErrorPage("Not Found", "HTTP/1.1 404 Not Found\r\n", "Grizzly"); res.setContentLength(bb.limit()); res.setContentType("text/html"); res.flushHeaders(); res.doWrite(chunk);
} else if (state == INITIAL_STATE) { response.sendHeaders(); response.action(ActionCode.ACTION_CLIENT_FLUSH, response); if (response.isExceptionPresent()) { throw new ClientAbortException (response.getErrorException());
/** * Close the output buffer. This tries to calculate the response size if * the response has not been committed yet. * * @throws IOException An underlying IOException occurred */ public void close() throws IOException { if (closed) return; if (suspended) return; if ((!coyoteResponse.isCommitted()) && (coyoteResponse.getContentLength() == -1)) { // Flushing the char buffer if (state == CHAR_STATE) { cb.flushBuffer(); state = BYTE_STATE; } // If this didn't cause a commit of the response, the final content // length can be calculated if (!coyoteResponse.isCommitted()) { coyoteResponse.setContentLength(bb.getLength()); } } doFlush(false); closed = true; coyoteResponse.finish(); }
/** * Send the cached resource. */ protected void sendCache(Request request, FileCacheEntry entry) throws IOException{ boolean flushBody = checkIfHeaders(request, entry); request.getResponse().setContentType(entry.contentType); request.getResponse().setContentLength(Integer.valueOf(entry.contentLength)); if (flushBody) { ByteBuffer sliced = entry.bb.slice(); ByteBuffer ob = ((SocketChannelOutputBuffer)request.getResponse() .getOutputBuffer()).getOutputByteBuffer(); int left = ob.remaining(); // It's better to execute a byte copy than two network operation. if (left > sliced.limit()){ request.getResponse().action(ActionCode.ACTION_COMMIT, null); ob.put(sliced); ((SocketChannelOutputBuffer)request.getResponse() .getOutputBuffer()).flushBuffer(); } else { request.getResponse().flush(); OutputWriter.flushChannel(request.getResponse().getChannel(),sliced); } } else { request.getResponse().flush(); } }
/** * Set the HTTP status and message to be returned with this response. * * @param status The new HTTP status * @param message The associated text message * */ public void setStatus(int status, String message) { if (isCommitted()) return; // Ignore any call from an included servlet if (included) return; response.setStatus(status); response.setMessage(message); }
int status = response.getStatus(); if (statusDropsConnection(response.getStatus())) { try { ByteBuffer bb = HtmlHelper.getErrorPage(messageDropConnection(status), "HTTP/1.1 " + response.getStatus() + " " + messageDropConnection(status) + "\r\n", serverName); response.setContentLength(bb.limit()); response.setContentType("text/html"); response.flushHeaders(); if (response.getChannel() != null) { response.getChannel().write(bb); } else { byte b[] = new byte[bb.limit()]; ByteChunk chunk = new ByteChunk(); chunk.setBytes(b, 0, b.length); response.doWrite(chunk);
/** * Invoke the {@link Adapter}, which usualy invoke the Servlet * Container. */ public void invokeAdapter(){ // Process the request in the adapter if (!error) { try { adapter.service(request, response); // Handle when the response was committed before a serious // error occurred. Throwing a ServletException should both // set the status to 500 and set the errorException. // If we fail here, then the response is likely already // committed, so we can't try and set headers. if(keepAlive && !error) { // Avoid checking twice. error = response.getErrorException() != null || statusDropsConnection(response.getStatus()); } } catch (InterruptedIOException e) { error = true; } catch (Throwable t) { logger.log(Level.SEVERE, sm.getString("processorTask.serviceError"), t); // 500 - Internal Server Error response.setStatus(500); error = true; } } }
response.getMimeHeaders().getValue("Content-Encoding"); int contentLength = response.getContentLength(); if ((contentLength == -1) || (contentLength > compressionMinSize)) { response.getContentType()));
/** * Write all headers into our internal buffer but don't flush them to the * client yet. You must invoke {@link #flush} in order to make that operation. */ public void sendHeaders() throws IOException { action(ActionCode.ACTION_COMMIT, this); commited = true; }
public void resume(){ completionHandler.resumed(attachment); try{ response.sendHeaders(); response.flush(); response.finish(); } catch (IOException ex){ LoggerUtils.getLogger().log(Level.FINEST,"resume",ex); } }