1 25 26 package com.j2biz.blogunity.web.servlet; 27 28 import java.io.IOException ; 29 30 import javax.servlet.ServletException ; 31 import javax.servlet.http.HttpServlet ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 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 { 49 50 53 private static final long serialVersionUID = 3257286920219669812L; 54 55 private static final Log log = LogFactory.getLog(UserServlet.class); 56 57 private static final String USERS_MAPPING = "/users"; 58 59 65 protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) 66 throws ServletException , IOException { 67 doPost(arg0, arg1); 68 } 69 70 76 protected void doPost(HttpServletRequest request, HttpServletResponse response) 77 throws ServletException , IOException { 78 79 try { 80 81 String 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 nickname = StringUtils.substringBefore(url, "/userpics/"); 95 String 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 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 String id = request.getParameter("id"); 112 if (StringUtils.isEmpty(id)) { throw new BlogunityException(I18NStatusFactory 113 .create(I18N.ERRORS.NOT_FOUND, "User")); } 114 115 Long ID; 116 try { 117 ID = new Long (id); 118 } catch (NumberFormatException 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 (e); 164 } 165 166 } 167 168 private String getRequestedURL(HttpServletRequest request) { 169 170 String requestUri = request.getRequestURI(); 171 requestUri = requestUri.substring(request.getContextPath().length() 172 + USERS_MAPPING.length(), requestUri.length()); 173 return requestUri; 174 175 } 176 } | Popular Tags |