001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.usersadmin.action;
016    
017    import com.liferay.portal.NoSuchUserException;
018    import com.liferay.portal.UserPortraitSizeException;
019    import com.liferay.portal.UserPortraitTypeException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.servlet.SessionErrors;
023    import com.liferay.portal.kernel.upload.UploadException;
024    import com.liferay.portal.kernel.upload.UploadPortletRequest;
025    import com.liferay.portal.kernel.util.FileUtil;
026    import com.liferay.portal.model.User;
027    import com.liferay.portal.security.auth.PrincipalException;
028    import com.liferay.portal.service.UserServiceUtil;
029    import com.liferay.portal.struts.PortletAction;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.util.portlet.PortletRequestUtil;
032    
033    import java.io.InputStream;
034    
035    import javax.portlet.ActionRequest;
036    import javax.portlet.ActionResponse;
037    import javax.portlet.PortletConfig;
038    import javax.portlet.RenderRequest;
039    import javax.portlet.RenderResponse;
040    
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionForward;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     */
048    public class EditUserPortraitAction extends PortletAction {
049    
050            @Override
051            public void processAction(
052                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
053                            ActionRequest actionRequest, ActionResponse actionResponse)
054                    throws Exception {
055    
056                    try {
057                            updatePortrait(actionRequest);
058    
059                            sendRedirect(actionRequest, actionResponse);
060                    }
061                    catch (Exception e) {
062                            if (e instanceof NoSuchUserException ||
063                                    e instanceof PrincipalException) {
064    
065                                    SessionErrors.add(actionRequest, e.getClass().getName());
066    
067                                    setForward(actionRequest, "portlet.users_admin.error");
068                            }
069                            else if (e instanceof UploadException ||
070                                             e instanceof UserPortraitSizeException ||
071                                             e instanceof UserPortraitTypeException) {
072    
073                                    SessionErrors.add(actionRequest, e.getClass().getName());
074                            }
075                            else {
076                                    throw e;
077                            }
078                    }
079            }
080    
081            @Override
082            public ActionForward render(
083                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
084                            RenderRequest renderRequest, RenderResponse renderResponse)
085                    throws Exception {
086    
087                    return mapping.findForward(getForward(
088                            renderRequest, "portlet.users_admin.edit_user_portrait"));
089            }
090    
091            protected void updatePortrait(ActionRequest actionRequest)
092                    throws Exception {
093    
094                    if (_log.isDebugEnabled()) {
095                            PortletRequestUtil.testMultipartWithCommonsFileUpload(
096                                    actionRequest);
097                    }
098    
099                    UploadPortletRequest uploadPortletRequest =
100                            PortalUtil.getUploadPortletRequest(actionRequest);
101    
102                    User user = PortalUtil.getSelectedUser(uploadPortletRequest);
103    
104                    InputStream inputStream = uploadPortletRequest.getFileAsStream(
105                            "fileName");
106    
107                    if (inputStream == null) {
108                            throw new UploadException();
109                    }
110    
111                    byte[] bytes = FileUtil.getBytes(inputStream);
112    
113                    UserServiceUtil.updatePortrait(user.getUserId(), bytes);
114            }
115    
116            private static Log _log = LogFactoryUtil.getLog(
117                    EditUserPortraitAction.class);
118    
119    }