1// 2// ======================================================================== 3// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. 4// ------------------------------------------------------------------------ 5// All rights reserved. This program and the accompanying materials 6// are made available under the terms of the Eclipse Public License v1.0 7// and Apache License v2.0 which accompanies this distribution. 8// 9// The Eclipse Public License is available at 10// http://www.eclipse.org/legal/epl-v10.html 11// 12// The Apache License v2.0 is available at 13// http://www.opensource.org/licenses/apache2.0.php 14// 15// You may elect to redistribute this code under either of these licenses. 16// ======================================================================== 17// 18 19package org.eclipse.jetty.http; 20 21import java.io.IOException; 22import java.io.InputStream; 23 24import org.eclipse.jetty.io.Buffer; 25import org.eclipse.jetty.io.ByteArrayBuffer; 26import org.eclipse.jetty.util.log.Log; 27import org.eclipse.jetty.util.log.Logger; 28import org.eclipse.jetty.util.resource.Resource; 29 30/* ------------------------------------------------------------ */ 31/** HttpContent. 32 * 33 * 34 */ 35public interface HttpContent 36{ 37 Buffer getContentType(); 38 Buffer getLastModified(); 39 Buffer getIndirectBuffer(); 40 Buffer getDirectBuffer(); 41 Buffer getETag(); 42 Resource getResource(); 43 long getContentLength(); 44 InputStream getInputStream() throws IOException; 45 void release(); 46 47 /* ------------------------------------------------------------ */ 48 /* ------------------------------------------------------------ */ 49 /* ------------------------------------------------------------ */ 50 public class ResourceAsHttpContent implements HttpContent 51 { 52 private static final Logger LOG = Log.getLogger(ResourceAsHttpContent.class); 53 54 final Resource _resource; 55 final Buffer _mimeType; 56 final int _maxBuffer; 57 final Buffer _etag; 58 59 /* ------------------------------------------------------------ */ 60 public ResourceAsHttpContent(final Resource resource, final Buffer mimeType) 61 { 62 this(resource,mimeType,-1,false); 63 } 64 65 /* ------------------------------------------------------------ */ 66 public ResourceAsHttpContent(final Resource resource, final Buffer mimeType, int maxBuffer) 67 { 68 this(resource,mimeType,maxBuffer,false); 69 } 70 71 /* ------------------------------------------------------------ */ 72 public ResourceAsHttpContent(final Resource resource, final Buffer mimeType, boolean etag) 73 { 74 this(resource,mimeType,-1,etag); 75 } 76 77 /* ------------------------------------------------------------ */ 78 public ResourceAsHttpContent(final Resource resource, final Buffer mimeType, int maxBuffer, boolean etag) 79 { 80 _resource=resource; 81 _mimeType=mimeType; 82 _maxBuffer=maxBuffer; 83 _etag=etag?new ByteArrayBuffer(resource.getWeakETag()):null; 84 } 85 86 /* ------------------------------------------------------------ */ 87 public Buffer getContentType() 88 { 89 return _mimeType; 90 } 91 92 /* ------------------------------------------------------------ */ 93 public Buffer getLastModified() 94 { 95 return null; 96 } 97 98 /* ------------------------------------------------------------ */ 99 public Buffer getDirectBuffer() 100 { 101 return null; 102 } 103 104 /* ------------------------------------------------------------ */ 105 public Buffer getETag() 106 { 107 return _etag; 108 } 109 110 /* ------------------------------------------------------------ */ 111 public Buffer getIndirectBuffer() 112 { 113 InputStream inputStream = null; 114 try 115 { 116 if (_resource.length() <= 0 || _maxBuffer < _resource.length()) 117 return null; 118 ByteArrayBuffer buffer = new ByteArrayBuffer((int)_resource.length()); 119 inputStream = _resource.getInputStream(); 120 buffer.readFrom(inputStream,(int)_resource.length()); 121 return buffer; 122 } 123 catch (IOException e) 124 { 125 throw new RuntimeException(e); 126 } 127 finally 128 { 129 if (inputStream != null) 130 { 131 try 132 { 133 inputStream.close(); 134 } 135 catch (IOException e) 136 { 137 LOG.warn("Couldn't close inputStream. Possible file handle leak",e); 138 } 139 } 140 } 141 } 142 143 /* ------------------------------------------------------------ */ 144 public long getContentLength() 145 { 146 return _resource.length(); 147 } 148 149 /* ------------------------------------------------------------ */ 150 public InputStream getInputStream() throws IOException 151 { 152 return _resource.getInputStream(); 153 } 154 155 /* ------------------------------------------------------------ */ 156 public Resource getResource() 157 { 158 return _resource; 159 } 160 161 /* ------------------------------------------------------------ */ 162 public void release() 163 { 164 _resource.release(); 165 } 166 } 167} 168