001    /**
002     * Copyright (c) 2000-2013 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 actionMapping, ActionForm actionForm,
053                            PortletConfig portletConfig, ActionRequest actionRequest,
054                            ActionResponse actionResponse)
055                    throws Exception {
056    
057                    try {
058                            updatePortrait(actionRequest);
059    
060                            sendRedirect(actionRequest, actionResponse);
061                    }
062                    catch (Exception e) {
063                            if (e instanceof NoSuchUserException ||
064                                    e instanceof PrincipalException) {
065    
066                                    SessionErrors.add(actionRequest, e.getClass());
067    
068                                    setForward(actionRequest, "portlet.users_admin.error");
069                            }
070                            else if (e instanceof UploadException ||
071                                             e instanceof UserPortraitSizeException ||
072                                             e instanceof UserPortraitTypeException) {
073    
074                                    SessionErrors.add(actionRequest, e.getClass());
075                            }
076                            else {
077                                    throw e;
078                            }
079                    }
080            }
081    
082            @Override
083            public ActionForward render(
084                            ActionMapping actionMapping, ActionForm actionForm,
085                            PortletConfig portletConfig, RenderRequest renderRequest,
086                            RenderResponse renderResponse)
087                    throws Exception {
088    
089                    return actionMapping.findForward(
090                            getForward(
091                                    renderRequest, "portlet.users_admin.edit_user_portrait"));
092            }
093    
094            protected void updatePortrait(ActionRequest actionRequest)
095                    throws Exception {
096    
097                    if (_log.isDebugEnabled()) {
098                            PortletRequestUtil.testMultipartWithCommonsFileUpload(
099                                    actionRequest);
100                    }
101    
102                    UploadPortletRequest uploadPortletRequest =
103                            PortalUtil.getUploadPortletRequest(actionRequest);
104    
105                    User user = PortalUtil.getSelectedUser(uploadPortletRequest);
106    
107                    InputStream inputStream = uploadPortletRequest.getFileAsStream(
108                            "fileName");
109    
110                    if (inputStream == null) {
111                            throw new UploadException();
112                    }
113    
114                    byte[] bytes = FileUtil.getBytes(inputStream);
115    
116                    UserServiceUtil.updatePortrait(user.getUserId(), bytes);
117            }
118    
119            private static Log _log = LogFactoryUtil.getLog(
120                    EditUserPortraitAction.class);
121    
122    }