KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > web > actions > user > UserpicAction


1 /*
2  * $Id: UserpicAction.java,v 1.1 2005/01/07 23:27:30 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.user;
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.util.ResourceUtils;
42 import com.j2biz.blogunity.web.IActionResult;
43 import com.j2biz.blogunity.web.actions.AbstractAction;
44
45 public class UserpicAction extends AbstractAction {
46
47     private String JavaDoc nickname;
48
49     private String JavaDoc userpic;
50
51     public UserpicAction(String JavaDoc nickname, String JavaDoc userpic) {
52         this.nickname = nickname;
53         this.userpic = userpic;
54     }
55
56     /*
57      * (non-Javadoc)
58      *
59      * @see com.j2biz.blogunity.web.actions.AbstractAction#execute(javax.servlet.http.HttpServletRequest,
60      * javax.servlet.http.HttpServletResponse)
61      */

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