KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > web > servlet > UserServlet


1 /*
2  * $Id: UserServlet.java,v 1.3 2005/01/17 11:21:21 keyboardsamurai 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.servlet;
27
28 import java.io.IOException JavaDoc;
29
30 import javax.servlet.ServletException JavaDoc;
31 import javax.servlet.http.HttpServlet JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 import com.j2biz.blogunity.IConstants;
40 import com.j2biz.blogunity.exception.BlogunityException;
41 import com.j2biz.blogunity.i18n.I18N;
42 import com.j2biz.blogunity.i18n.I18NStatusFactory;
43 import com.j2biz.blogunity.web.IActionResult;
44 import com.j2biz.blogunity.web.actions.AbstractAction;
45 import com.j2biz.blogunity.web.actions.user.UserpicAction;
46 import com.j2biz.blogunity.web.actions.user.UserprofileAction;
47
48 public class UserServlet extends HttpServlet JavaDoc {
49
50     /**
51      * Comment for <code>serialVersionUID</code>
52      */

53     private static final long serialVersionUID = 3257286920219669812L;
54
55     private static final Log log = LogFactory.getLog(UserServlet.class);
56
57     private static final String JavaDoc USERS_MAPPING = "/users";
58
59     /*
60      * (non-Javadoc)
61      *
62      * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
63      * javax.servlet.http.HttpServletResponse)
64      */

65     protected void doGet(HttpServletRequest JavaDoc arg0, HttpServletResponse JavaDoc arg1)
66             throws ServletException JavaDoc, IOException JavaDoc {
67         doPost(arg0, arg1);
68     }
69
70     /*
71      * (non-Javadoc)
72      *
73      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
74      * javax.servlet.http.HttpServletResponse)
75      */

76     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
77             throws ServletException JavaDoc, IOException JavaDoc {
78
79         try {
80
81             // first find name of the requested user
82
String JavaDoc url = getRequestedURL(request);
83
84             AbstractAction action = null;
85
86             if (url.matches(IConstants.PATTERNS.USERPIC_PATTERN)) {
87
88                 if (log.isDebugEnabled()) {
89                     log.debug("Userpic requested!");
90                 }
91
92                 url = url.substring(1);
93
94                 String JavaDoc nickname = StringUtils.substringBefore(url, "/userpics/");
95                 String JavaDoc resource = StringUtils.substringAfter(url, "/userpics/");
96
97                 action = new UserpicAction(nickname, resource);
98
99             } else if (url.matches(IConstants.PATTERNS.USERPFORILE_PATTERN)) {
100                 if (log.isDebugEnabled()) {
101                     log.debug("Userprofile requested!");
102                 }
103
104                 // remove leading and trailing slashes, if exists
105
if (url.length() > 0) url = url.substring(1);
106                 if (url.endsWith("/")) url = url.substring(0, url.length() - 1);
107
108                 if (url.length() == 0) {
109
110                     // try to find id as request-parameter
111
String JavaDoc id = request.getParameter("id");
112                     if (StringUtils.isEmpty(id)) { throw new BlogunityException(I18NStatusFactory
113                             .create(I18N.ERRORS.NOT_FOUND, "User")); }
114
115                     Long JavaDoc ID;
116                     try {
117                         ID = new Long JavaDoc(id);
118                     } catch (NumberFormatException JavaDoc e) {
119                         throw new BlogunityException(I18NStatusFactory.create(
120                                 I18N.ERRORS.NOT_FOUND_BY_ID, "User"));
121                     }
122
123                     action = new UserprofileAction(ID);
124
125                 } else {
126
127                     action = new UserprofileAction(url);
128                 }
129
130             } else {
131                 throw new BlogunityException(I18NStatusFactory
132                         .create(I18N.ERRORS.NOT_FOUND, "User"));
133             }
134
135             if (action != null) {
136
137                 action.init(request, response, request.getRequestURL().toString());
138                 IActionResult result = action.execute(request, response);
139
140                 if (result != null) {
141
142                     if (result.getType() == IActionResult.FORWARD) {
143                         request.getRequestDispatcher(result.getPath()).forward(request, response);
144                     } else if (result.getType() == IActionResult.INCLUDE) {
145                         request.getRequestDispatcher(result.getPath()).include(request, response);
146                     } else if (result.getType() == IActionResult.REDIRECT) {
147                         response.sendRedirect(request.getContextPath() + result.getPath());
148                     } else {
149                         throw new BlogunityException(I18NStatusFactory
150                                 .create(I18N.ERRORS.ACTION_FORWARD_FAILED));
151                     }
152
153                 } else {
154                     if (log.isDebugEnabled()) {
155                         log.debug("Execution of the action='" + action.getClass()
156                                 + "' returned result: NULL! do nothing...");
157                     }
158                 }
159
160             }
161
162         } catch (BlogunityException e) {
163             throw new ServletException JavaDoc(e);
164         }
165
166     }
167
168     private String JavaDoc getRequestedURL(HttpServletRequest JavaDoc request) {
169
170         String JavaDoc requestUri = request.getRequestURI();
171         requestUri = requestUri.substring(request.getContextPath().length()
172                 + USERS_MAPPING.length(), requestUri.length());
173         return requestUri;
174
175     }
176 }
Popular Tags