/* ------------------------------------------------------------ */ protected Buffer getIndirectBuffer(Resource resource) { try { int len = (int) resource.length(); if (len < 0) { LOG.warn("invalid resource: " + String.valueOf(resource) + " " + len); return null; } Buffer buffer = new IndirectNIOBuffer(len); InputStream is = resource.getInputStream(); buffer.readFrom(is, len); is.close(); return buffer; } catch (IOException e) { LOG.warn(e); return null; } }
/* ------------------------------------------------------------ */ public void sendContent(Object content) throws IOException { Resource resource = null; if (isClosed()) throw new IOException("Closed"); if (super._generator.isWritten()) throw new IllegalStateException("!empty"); // Convert HTTP content to contentl if (content instanceof HttpContent) { HttpContent httpContent = (HttpContent) content; Buffer contentType = httpContent.getContentType(); if (contentType != null && !_responseFields.containsKey(HttpHeaders.CONTENT_TYPE_BUFFER)) { String enc = _response.getSetCharacterEncoding(); if (enc == null) _responseFields.add(HttpHeaders.CONTENT_TYPE_BUFFER, contentType); else { if (contentType instanceof CachedBuffer) { CachedBuffer content_type = ((CachedBuffer) contentType).getAssociate(enc); if (content_type != null) _responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER, content_type); else { _responseFields.put( HttpHeaders.CONTENT_TYPE_BUFFER, contentType + ";charset=" + QuotedStringTokenizer.quoteIfNeeded(enc, ";= ")); } } else { _responseFields.put( HttpHeaders.CONTENT_TYPE_BUFFER, contentType + ";charset=" + QuotedStringTokenizer.quoteIfNeeded(enc, ";= ")); } } } if (httpContent.getContentLength() > 0) _responseFields.putLongField( HttpHeaders.CONTENT_LENGTH_BUFFER, httpContent.getContentLength()); Buffer lm = httpContent.getLastModified(); long lml = httpContent.getResource().lastModified(); if (lm != null) _responseFields.put(HttpHeaders.LAST_MODIFIED_BUFFER, lm); else if (httpContent.getResource() != null) { if (lml != -1) _responseFields.putDateField(HttpHeaders.LAST_MODIFIED_BUFFER, lml); } boolean direct = _connector instanceof NIOConnector && ((NIOConnector) _connector).getUseDirectBuffers() && !(_connector instanceof SslConnector); content = direct ? httpContent.getDirectBuffer() : httpContent.getIndirectBuffer(); if (content == null) content = httpContent.getInputStream(); } else if (content instanceof Resource) { resource = (Resource) content; _responseFields.putDateField(HttpHeaders.LAST_MODIFIED_BUFFER, resource.lastModified()); content = resource.getInputStream(); } // Process content. if (content instanceof Buffer) { super._generator.addContent((Buffer) content, Generator.LAST); commitResponse(Generator.LAST); } else if (content instanceof InputStream) { InputStream in = (InputStream) content; try { int max = super._generator.prepareUncheckedAddContent(); Buffer buffer = super._generator.getUncheckedBuffer(); int len = buffer.readFrom(in, max); while (len >= 0) { super._generator.completeUncheckedAddContent(); _out.flush(); max = super._generator.prepareUncheckedAddContent(); buffer = super._generator.getUncheckedBuffer(); len = buffer.readFrom(in, max); } super._generator.completeUncheckedAddContent(); _out.flush(); } finally { if (resource != null) resource.release(); else in.close(); } } else throw new IllegalArgumentException("unknown content type?"); }