Package muntjac :: Package terminal :: Module stream_resource
[hide private]
[frames] | no frames]

Source Code for Module muntjac.terminal.stream_resource

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 15   
 16  """A resource provided to the client directly by the application.""" 
 17   
 18  from muntjac.service.file_type_resolver import FileTypeResolver 
 19  from muntjac.terminal.application_resource import IApplicationResource 
 20  from muntjac.terminal.download_stream import DownloadStream 
 21   
 22   
23 -class StreamResource(IApplicationResource):
24 """C{StreamResource} is a resource provided to the client 25 directly by the application. The strean resource is fetched from URI 26 that is most often in the context of the application or window. The 27 resource is automatically registered to window in creation. 28 29 @author: Vaadin Ltd. 30 @author: Richard Lincoln 31 @version: 1.1.2 32 """ 33
34 - def __init__(self, streamSource, filename, application):
35 """Creates a new stream resource for downloading from stream. 36 37 @param streamSource: 38 the source Stream. 39 @param filename: 40 the name of the file. 41 @param application: 42 the Application object. 43 """ 44 # Source stream the downloaded content is fetched from. 45 self._streamSource = None 46 47 # Explicit mime-type. 48 self._MIMEType = None 49 50 # Filename. 51 self._filename = None 52 53 # Application. 54 self._application = application 55 56 # Default buffer size for this stream resource. 57 self._bufferSize = 0 58 59 # Default cache time for this stream resource. 60 self._cacheTime = self.DEFAULT_CACHETIME 61 62 self.setFilename(filename) 63 self.setStreamSource(streamSource) 64 # Register to application 65 application.addResource(self)
66 67
68 - def getMIMEType(self):
69 """@see: IResource.getMIMEType""" 70 if self._MIMEType is not None: 71 return self._MIMEType 72 return FileTypeResolver.getMIMEType(self._filename)
73 74
75 - def setMIMEType(self, MIMEType):
76 """Sets the mime type of the resource. 77 78 @param MIMEType: 79 the MIME type to be set. 80 """ 81 self._MIMEType = MIMEType
82 83
84 - def getStreamSource(self):
85 """Returns the source for this C{StreamResource}. 86 StreamSource is queried when the resource is about to be streamed 87 to the client. 88 89 @return: Source of the StreamResource. 90 """ 91 return self._streamSource
92 93
94 - def setStreamSource(self, streamSource):
95 """Sets the source for this C{StreamResource}. 96 C{StreamSource} is queried when the resource is 97 about to be streamed to the client. 98 99 @param streamSource: 100 the source to set. 101 """ 102 self._streamSource = streamSource
103 104
105 - def getFilename(self):
106 """Gets the filename. 107 108 @return: the filename. 109 """ 110 return self._filename
111 112
113 - def setFilename(self, filename):
114 """Sets the filename. 115 116 @param filename: 117 the filename to set. 118 """ 119 self._filename = filename
120 121
122 - def getApplication(self):
123 """@see: L{IApplicationResource.getApplication}""" 124 return self._application
125 126
127 - def getStream(self):
128 """@see: L{IApplicationResource.getStream}""" 129 ss = self.getStreamSource() 130 if ss is None: 131 return None 132 ds = DownloadStream(ss.getStream(), self.getMIMEType(), 133 self.getFilename()) 134 ds.setBufferSize(self.getBufferSize()) 135 ds.setCacheTime(self._cacheTime) 136 return ds
137 138
139 - def getBufferSize(self):
140 return self._bufferSize
141 142
143 - def setBufferSize(self, bufferSize):
144 """Sets the size of the download buffer used for this resource. 145 146 @param bufferSize: 147 the size of the buffer in bytes. 148 """ 149 self._bufferSize = bufferSize
150 151
152 - def getCacheTime(self):
153 return self._cacheTime
154 155
156 - def setCacheTime(self, cacheTime):
157 """Sets the length of cache expiration time. 158 159 This gives the adapter the possibility cache streams sent to 160 the client. The caching may be made in adapter or at the client 161 if the client supports caching. Zero or negative value disables 162 the caching of this stream. 163 164 @param cacheTime: 165 the cache time in milliseconds. 166 """ 167 self._cacheTime = cacheTime
168 169
170 -class IStreamSource(object):
171 """Interface implemented by the source of a StreamResource. 172 173 @author: Vaadin Ltd. 174 @author: Richard Lincoln 175 @version: 1.1.2 176 """ 177
178 - def getStream(self):
179 """Returns new input stream that is used for reading the resource.""" 180 pass
181