001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.request.target.resource;
018:
019: import javax.servlet.http.HttpServletResponse;
020:
021: import org.apache.wicket.IRequestTarget;
022: import org.apache.wicket.RequestCycle;
023: import org.apache.wicket.Response;
024: import org.apache.wicket.protocol.http.WebResponse;
025: import org.apache.wicket.protocol.http.request.WebErrorCodeResponseTarget;
026: import org.apache.wicket.util.resource.IResourceStream;
027: import org.apache.wicket.util.resource.ResourceStreamNotFoundException;
028: import org.slf4j.Logger;
029: import org.slf4j.LoggerFactory;
030:
031: /**
032: * Request target that responds by sending its resource stream.
033: *
034: * @author Eelco Hillenius
035: */
036: public class ResourceStreamRequestTarget implements IRequestTarget {
037: /** Logger */
038: private static final Logger log = LoggerFactory
039: .getLogger(ResourceStreamRequestTarget.class);
040:
041: /**
042: * Optional filename, used to set the content disposition header. Only
043: * meaningful when using with web requests.
044: */
045: private String fileName;
046:
047: /** the resource stream for the response. */
048: private final IResourceStream resourceStream;
049:
050: /**
051: * Construct.
052: *
053: * @param resourceStream
054: * the resource stream for the response
055: */
056: public ResourceStreamRequestTarget(IResourceStream resourceStream) {
057: this .resourceStream = resourceStream;
058: }
059:
060: /**
061: * @see org.apache.wicket.IRequestTarget#detach(org.apache.wicket.RequestCycle)
062: */
063: public void detach(RequestCycle requestCycle) {
064: }
065:
066: /**
067: * @see java.lang.Object#equals(java.lang.Object)
068: */
069: public boolean equals(Object obj) {
070: if (obj instanceof ResourceStreamRequestTarget) {
071: ResourceStreamRequestTarget that = (ResourceStreamRequestTarget) obj;
072: return resourceStream.equals(that.resourceStream)
073: && ((fileName != null) ? fileName
074: .equals(that.fileName) : true);
075: }
076: return false;
077: }
078:
079: /**
080: * @return Optional filename, used to set the content disposition header.
081: * Only meaningful when using with web requests.
082: */
083: public String getFileName() {
084: return fileName;
085: }
086:
087: /**
088: * Gets the resource stream for the response.
089: *
090: * @return the resource stream for the response
091: */
092: public final IResourceStream getResourceStream() {
093: return resourceStream;
094: }
095:
096: /**
097: * @see java.lang.Object#hashCode()
098: */
099: public int hashCode() {
100: int result = "ResourceStreamRequestTarget".hashCode();
101: result += resourceStream.hashCode();
102: result += (fileName != null) ? fileName.hashCode() : 0;
103: return 17 * result;
104: }
105:
106: /**
107: * Responds by sending the contents of the resource stream.
108: *
109: * @see org.apache.wicket.IRequestTarget#respond(org.apache.wicket.RequestCycle)
110: */
111: public void respond(RequestCycle requestCycle) {
112: // Get servlet response to use when responding with resource
113: final Response response = requestCycle.getResponse();
114:
115: configure(requestCycle, response, resourceStream);
116:
117: try {
118: response.write(resourceStream.getInputStream());
119: } catch (ResourceStreamNotFoundException e) {
120: requestCycle
121: .setRequestTarget(new WebErrorCodeResponseTarget(
122: HttpServletResponse.SC_NOT_FOUND));
123: }
124: }
125:
126: /**
127: * @param fileName
128: * Optional filename, used to set the content disposition header.
129: * Only meaningful when using with web requests.
130: *
131: * @return The this.
132: */
133: public ResourceStreamRequestTarget setFileName(String fileName) {
134: this .fileName = fileName;
135: return this ;
136: }
137:
138: /**
139: * @see java.lang.Object#toString()
140: */
141: public String toString() {
142: return "[ResourceStreamRequestTarget[resourceStream="
143: + resourceStream + ",fileName=" + fileName + "]";
144: }
145:
146: /**
147: * Configures the response, default by setting the content type and length
148: * and content disposition (in case the fileName property was set).
149: *
150: * @param requestCycle
151: * @param response
152: * the response
153: * @param resourceStream
154: * the resource stream that will be rendered
155: */
156: protected void configure(final RequestCycle requestCycle,
157: final Response response,
158: final IResourceStream resourceStream) {
159: // Configure response with content type of resource, if available
160: String responseType = resourceStream.getContentType();
161: if (responseType != null) {
162: response.setContentType(responseType + "; charset="
163: + response.getCharacterEncoding());
164: } else {
165: // otherwise detect content-type automatically
166: if (getFileName() != null) {
167: response.detectContentType(requestCycle, getFileName());
168: } else {
169: response.detectContentType(requestCycle, requestCycle
170: .getRequest().getURL());
171: }
172: }
173:
174: // WICKET-473 Allow IResourceStream.length() to return -1
175: long len = resourceStream.length();
176: if (len >= 0) {
177: // and the content length
178: response.setContentLength(len);
179: }
180:
181: // and content disposition if any
182: String file = getFileName();
183: if (file != null && (response instanceof WebResponse)) {
184: ((WebResponse) response).setAttachmentHeader(file);
185: }
186: }
187: }
|