01: /*
02: * $Id: ImageAction.java 471754 2006-11-06 14:55:09Z husted $
03: *
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
20: */
21:
22: package org.apache.struts.webapp.exercise;
23:
24: import org.apache.struts.action.Action;
25: import org.apache.struts.action.ActionForm;
26: import org.apache.struts.action.ActionForward;
27: import org.apache.struts.action.ActionMapping;
28:
29: import javax.servlet.http.HttpServletRequest;
30: import javax.servlet.http.HttpServletResponse;
31: import java.io.InputStream;
32: import java.io.OutputStream;
33:
34: /**
35: * Read image from resource given as ActionMapping parameter
36: * and copy to output stream.
37: *
38: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
39: */
40:
41: public class ImageAction extends Action {
42:
43: /**
44: * Read image from resource given as ActionMapping parameter
45: * and copy to output stream.
46: *
47: * @exception java.lang.Exception on input/output error
48: */
49: public ActionForward execute(ActionMapping mapping,
50: ActionForm form, HttpServletRequest request,
51: HttpServletResponse response) throws Exception {
52:
53: // e.g. /$module/struts-power.gif
54: // :FIXME: Should compute module
55: String image = mapping.getParameter();
56:
57: byte[] buffer = new byte[2048];
58: int bytesRead;
59: InputStream input = getServlet().getServletContext()
60: .getResourceAsStream(image);
61: OutputStream out = response.getOutputStream();
62:
63: while ((bytesRead = input.read(buffer)) != -1) {
64: out.write(buffer, 0, bytesRead);
65: }
66: out.close();
67:
68: return null;
69: }
70:
71: }
|