1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.enterpriseadmin.action;
24  
25  import com.liferay.portal.NoSuchUserException;
26  import com.liferay.portal.UserPortraitException;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.servlet.SessionErrors;
30  import com.liferay.portal.kernel.upload.UploadPortletRequest;
31  import com.liferay.portal.kernel.util.FileUtil;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.security.auth.PrincipalException;
34  import com.liferay.portal.service.UserServiceUtil;
35  import com.liferay.portal.struts.PortletAction;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.util.portlet.PortletRequestUtil;
38  import com.liferay.util.servlet.UploadException;
39  
40  import java.io.File;
41  
42  import javax.portlet.ActionRequest;
43  import javax.portlet.ActionResponse;
44  import javax.portlet.PortletConfig;
45  import javax.portlet.RenderRequest;
46  import javax.portlet.RenderResponse;
47  
48  import org.apache.struts.action.ActionForm;
49  import org.apache.struts.action.ActionForward;
50  import org.apache.struts.action.ActionMapping;
51  
52  /**
53   * <a href="EditUserPortraitAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   *
57   */
58  public class EditUserPortraitAction extends PortletAction {
59  
60      public void processAction(
61              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
62              ActionRequest actionRequest, ActionResponse actionResponse)
63          throws Exception {
64  
65          try {
66              updatePortrait(actionRequest);
67  
68              sendRedirect(actionRequest, actionResponse);
69          }
70          catch (Exception e) {
71              if (e instanceof NoSuchUserException ||
72                  e instanceof PrincipalException) {
73  
74                  SessionErrors.add(actionRequest, e.getClass().getName());
75  
76                  setForward(actionRequest, "portlet.enterprise_admin.error");
77              }
78              else if (e instanceof UploadException ||
79                       e instanceof UserPortraitException) {
80  
81                  SessionErrors.add(actionRequest, e.getClass().getName());
82              }
83              else {
84                  throw e;
85              }
86          }
87      }
88  
89      public ActionForward render(
90              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
91              RenderRequest renderRequest, RenderResponse renderResponse)
92          throws Exception {
93  
94          return mapping.findForward(getForward(
95              renderRequest, "portlet.enterprise_admin.edit_user_portrait"));
96      }
97  
98      protected void updatePortrait(ActionRequest actionRequest)
99          throws Exception {
100 
101         if (_log.isDebugEnabled()) {
102             PortletRequestUtil.testMultipartWithCommonsFileUpload(
103                 actionRequest);
104         }
105 
106         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
107             actionRequest);
108 
109         User user = PortalUtil.getSelectedUser(uploadRequest);
110 
111         File file = uploadRequest.getFile("fileName");
112         byte[] bytes = FileUtil.getBytes(file);
113 
114         if ((bytes == null) || (bytes.length == 0)) {
115             throw new UploadException();
116         }
117 
118         UserServiceUtil.updatePortrait(user.getUserId(), bytes);
119     }
120 
121     private static Log _log =
122          LogFactoryUtil.getLog(EditUserPortraitAction.class);
123 
124 }