KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > web > actions > blog > AbstractResourceAction


1 /*
2  * $Id: AbstractResourceAction.java,v 1.2 2005/01/15 10:40:35 michelson Exp $
3  *
4  * Copyright (c) 2005 j2biz Group, http://www.j2biz.com Koeln / Duesseldorf ,
5  * Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License as published by the Free Software
12  * Foundation; either version 2 of the License, or (at your option) any later
13  * version.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License along with
21  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  * Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.web.actions.blog;
27
28 import java.io.BufferedInputStream JavaDoc;
29 import java.io.BufferedOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.net.FileNameMap JavaDoc;
33 import java.net.URLConnection JavaDoc;
34
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37
38 import com.j2biz.blogunity.exception.BlogunityException;
39 import com.j2biz.blogunity.i18n.I18N;
40 import com.j2biz.blogunity.i18n.I18NStatusFactory;
41 import com.j2biz.blogunity.pojo.Blog;
42 import com.j2biz.blogunity.util.ResourceUtils;
43 import com.j2biz.blogunity.web.IActionResult;
44 import com.j2biz.blogunity.web.actions.AbstractAction;
45
46 public abstract class AbstractResourceAction extends AbstractAction {
47
48     public Blog blog;
49
50     public String JavaDoc resource;
51
52     public AbstractResourceAction(Blog blog, String JavaDoc resource) {
53         this.blog = blog;
54         this.resource = resource;
55     }
56
57     public abstract String JavaDoc getResourcesDirectory();
58
59     /*
60      * (non-Javadoc)
61      *
62      * @see com.j2biz.blogunity.web.actions.AbstractAction#execute(javax.servlet.http.HttpServletRequest,
63      * javax.servlet.http.HttpServletResponse)
64      */

65     public IActionResult execute(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
66             throws BlogunityException {
67
68         String JavaDoc resourcesDir = getResourcesDirectory();
69
70         String JavaDoc relativeResourcePath = "/blogs/" + blog.getUrlName() + resourcesDir + resource;
71
72         FileNameMap JavaDoc fileNameMap = URLConnection.getFileNameMap();
73         response.setContentType(fileNameMap.getContentTypeFor(relativeResourcePath));
74
75         InputStream JavaDoc inx = ResourceUtils.getResourceAsStream(relativeResourcePath);
76
77         if (inx == null) {
78
79         throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.RESOURCE_NOT_FOUND,
80                 relativeResourcePath)); }
81
82         try {
83             BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc(inx);
84             BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc(response.getOutputStream());
85             int i = in.read();
86             while (i != -1) {
87                 out.write(i);
88                 i = in.read();
89             }
90             in.close();
91             out.close();
92         } catch (IOException JavaDoc ioe) {
93
94             throw new BlogunityException(I18NStatusFactory
95                     .create(I18N.ERRORS.THEME_READ_ERROR, ioe));
96         }
97
98         return IActionResult.NULL_RESULT;
99     }
100
101 }
Popular Tags