01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: **/package org.araneaframework.http.widget;
16:
17: import java.util.Map;
18: import javax.servlet.http.HttpServletResponse;
19: import org.araneaframework.InputData;
20: import org.araneaframework.OutputData;
21: import org.araneaframework.Path;
22: import org.araneaframework.core.Assert;
23: import org.araneaframework.core.BaseApplicationWidget;
24: import org.araneaframework.http.util.ServletUtil;
25:
26: /**
27: * Widget that serves content.
28: *
29: * @author Alar Kvell (alar@araneaframework.org)
30: */
31: public class DownloaderWidget extends BaseApplicationWidget {
32: protected byte[] data;
33: protected String contentType;
34: protected Map headers;
35:
36: public DownloaderWidget(byte[] data, String contentType) {
37: Assert.notNullParam(data, "data");
38: Assert.notEmptyParam(contentType, "contentType");
39: this .data = data;
40: this .contentType = contentType;
41: }
42:
43: /** @since 1.1 */
44: public DownloaderWidget(byte[] data, Map headers) {
45: Assert.notNullParam(data, "data");
46: this .data = data;
47: this .headers = headers;
48: }
49:
50: public byte[] getData() {
51: return data;
52: }
53:
54: public String getContentType() {
55: return contentType;
56: }
57:
58: /** @since 1.1 */
59: public Map getHeaders() {
60: return headers;
61: }
62:
63: protected void action(Path path, InputData input, OutputData output)
64: throws Exception {
65: HttpServletResponse response = ServletUtil.getResponse(output);
66: response.setContentType(getContentType());
67: response.setContentLength(getData().length);
68: response.getOutputStream().write(getData());
69: }
70:
71: }
|