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