1   /**
2    * Copyright (c) 2000-2008 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.servlet.SessionErrors;
28  import com.liferay.portal.kernel.upload.UploadPortletRequest;
29  import com.liferay.portal.kernel.util.ByteArrayMaker;
30  import com.liferay.portal.kernel.util.FileUtil;
31  import com.liferay.portal.model.User;
32  import com.liferay.portal.security.auth.PrincipalException;
33  import com.liferay.portal.service.UserServiceUtil;
34  import com.liferay.portal.struts.PortletAction;
35  import com.liferay.portal.util.PortalUtil;
36  import com.liferay.util.servlet.UploadException;
37  
38  import java.io.File;
39  import java.io.InputStream;
40  
41  import java.util.List;
42  
43  import javax.portlet.ActionRequest;
44  import javax.portlet.ActionResponse;
45  import javax.portlet.PortletConfig;
46  import javax.portlet.RenderRequest;
47  import javax.portlet.RenderResponse;
48  
49  import org.apache.commons.fileupload.disk.DiskFileItem;
50  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
51  import org.apache.commons.fileupload.portlet.PortletFileUpload;
52  import org.apache.commons.logging.Log;
53  import org.apache.commons.logging.LogFactory;
54  import org.apache.struts.action.ActionForm;
55  import org.apache.struts.action.ActionForward;
56  import org.apache.struts.action.ActionMapping;
57  
58  /**
59   * <a href="EditUserPortraitAction.java.html"><b><i>View Source</i></b></a>
60   *
61   * @author Brian Wing Shun Chan
62   *
63   */
64  public class EditUserPortraitAction extends PortletAction {
65  
66      public void processAction(
67              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
68              ActionRequest actionRequest, ActionResponse actionResponse)
69          throws Exception {
70  
71          try {
72              updatePortrait(actionRequest);
73  
74              sendRedirect(actionRequest, actionResponse);
75          }
76          catch (Exception e) {
77              if (e instanceof NoSuchUserException ||
78                  e instanceof PrincipalException) {
79  
80                  SessionErrors.add(actionRequest, e.getClass().getName());
81  
82                  setForward(actionRequest, "portlet.enterprise_admin.error");
83              }
84              else if (e instanceof UploadException ||
85                       e instanceof UserPortraitException) {
86  
87                  SessionErrors.add(actionRequest, e.getClass().getName());
88              }
89              else {
90                  throw e;
91              }
92          }
93      }
94  
95      public ActionForward render(
96              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
97              RenderRequest renderRequest, RenderResponse renderResponse)
98          throws Exception {
99  
100         return mapping.findForward(getForward(
101             renderRequest, "portlet.enterprise_admin.edit_user_portrait"));
102     }
103 
104     protected void testRequest(ActionRequest actionRequest) throws Exception {
105 
106         // Check if the given request is a multipart request
107 
108         boolean multiPartContent = PortletFileUpload.isMultipartContent(
109             actionRequest);
110 
111         if (_log.isInfoEnabled()) {
112             if (multiPartContent) {
113                 _log.info("The given request is a multipart request");
114             }
115             else {
116                 _log.info("The given request is NOT a multipart request");
117             }
118         }
119 
120         // Check for the number of file items
121 
122         DiskFileItemFactory factory = new DiskFileItemFactory();
123 
124         PortletFileUpload upload = new PortletFileUpload(factory);
125 
126         List<DiskFileItem> fileItems = upload.parseRequest(actionRequest);
127 
128         if (_log.isInfoEnabled()) {
129             _log.info(
130                 "Apache commons upload was able to parse " + fileItems.size() +
131                     " items");
132         }
133 
134         for (int i = 0; i < fileItems.size(); i++) {
135             DiskFileItem fileItem = fileItems.get(i);
136 
137             if (_log.isInfoEnabled()) {
138                 _log.info("Item " + i + " " + fileItem);
139             }
140         }
141 
142         // Read directly from the portlet input stream
143 
144         InputStream is = actionRequest.getPortletInputStream();
145 
146         if (is != null) {
147             ByteArrayMaker bam = new ByteArrayMaker();
148 
149             int c = -1;
150 
151             try {
152                 while ((c = is.read()) != -1) {
153                     bam.write(c);
154                 }
155             }
156             finally {
157                 is.close();
158             }
159 
160             byte[] bytes = bam.toByteArray();
161 
162             if (_log.isInfoEnabled()) {
163                 _log.info(
164                     "Byte array size from the raw input stream is " +
165                         bytes.length);
166             }
167         }
168     }
169 
170     protected void updatePortrait(ActionRequest actionRequest)
171         throws Exception {
172 
173         //_testRequest(req);
174 
175         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
176             actionRequest);
177 
178         User user = PortalUtil.getSelectedUser(uploadRequest);
179 
180         File file = uploadRequest.getFile("fileName");
181         byte[] bytes = FileUtil.getBytes(file);
182 
183         if ((bytes == null) || (bytes.length == 0)) {
184             throw new UploadException();
185         }
186 
187         UserServiceUtil.updatePortrait(user.getUserId(), bytes);
188     }
189 
190     private static Log _log = LogFactory.getLog(EditUserPortraitAction.class);
191 
192 }