001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.services;
016:
017: import java.io.BufferedInputStream;
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.io.OutputStream;
021: import java.net.URL;
022: import java.net.URLConnection;
023: import java.util.Map;
024:
025: import org.apache.tapestry.internal.TapestryInternalUtils;
026: import org.apache.tapestry.ioc.Resource;
027: import org.apache.tapestry.services.Response;
028:
029: public class ResourceStreamerImpl implements ResourceStreamer {
030: private final Response _response;
031:
032: private final Map<String, String> _configuration;
033:
034: private final int _bufferSize = 1000;
035:
036: public ResourceStreamerImpl(final Response response,
037: Map<String, String> configuration) {
038: _response = response;
039: _configuration = configuration;
040: }
041:
042: public void streamResource(Resource resource) throws IOException {
043: URL url = resource.toURL();
044:
045: URLConnection connection = url.openConnection();
046:
047: int contentLength = connection.getContentLength();
048:
049: if (contentLength >= 0)
050: _response.setContentLength(contentLength);
051:
052: // Could get this from the ResourceCache, but can't imagine
053: // it's very expensive.
054:
055: long lastModified = connection.getLastModified();
056:
057: _response.setDateHeader("Last-Modified", lastModified);
058:
059: String contentType = connection.getContentType();
060:
061: if ("content/unknown".equals(contentType))
062: contentType = null;
063:
064: if (contentType == null) {
065: String file = resource.getFile();
066: int dotx = file.lastIndexOf('.');
067:
068: if (dotx > 0) {
069: String extension = file.substring(dotx + 1);
070:
071: contentType = _configuration.get(extension);
072: }
073:
074: if (contentType == null)
075: contentType = "application/octet-stream";
076: }
077:
078: InputStream is = null;
079:
080: try {
081: connection.connect();
082:
083: is = new BufferedInputStream(connection.getInputStream());
084:
085: OutputStream os = _response.getOutputStream(contentType);
086:
087: byte[] buffer = new byte[_bufferSize];
088:
089: while (true) {
090: int length = is.read(buffer);
091:
092: if (length < 0)
093: break;
094:
095: os.write(buffer, 0, length);
096: }
097:
098: is.close();
099: is = null;
100:
101: os.flush();
102: } finally {
103: TapestryInternalUtils.close(is);
104: }
105:
106: }
107: }
|