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