1 /* 2 * $Id: ImageAction.java 471754 2006-11-06 14:55:09Z husted $ 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "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 23 package org.apache.struts.webapp.exercise; 24 25 26 import org.apache.struts.action.Action; 27 import org.apache.struts.action.ActionForm; 28 import org.apache.struts.action.ActionForward; 29 import org.apache.struts.action.ActionMapping; 30 31 import javax.servlet.http.HttpServletRequest; 32 import javax.servlet.http.HttpServletResponse; 33 import java.io.InputStream; 34 import java.io.OutputStream; 35 36 37 /** 38 * Read image from resource given as ActionMapping parameter 39 * and copy to output stream. 40 * 41 * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $ 42 */ 43 44 public class ImageAction extends Action { 45 46 47 /** 48 * Read image from resource given as ActionMapping parameter 49 * and copy to output stream. 50 * 51 * @exception java.lang.Exception on input/output error 52 */ 53 public ActionForward execute( 54 ActionMapping mapping, 55 ActionForm form, 56 HttpServletRequest request, 57 HttpServletResponse response) 58 throws Exception { 59 60 // e.g. /$module/struts-power.gif 61 // :FIXME: Should compute module 62 String image = mapping.getParameter(); 63 64 byte[] buffer = new byte[2048]; 65 int bytesRead; 66 InputStream input = 67 getServlet().getServletContext().getResourceAsStream(image); 68 OutputStream 69 out = response.getOutputStream(); 70 71 while ((bytesRead = input.read(buffer)) != -1) { 72 out.write(buffer, 0, bytesRead); 73 } 74 out.close(); 75 76 return null; 77 } 78 79 }