001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.enterpriseadmin.action;
022:
023: import com.liferay.portal.NoSuchUserException;
024: import com.liferay.portal.UserPortraitException;
025: import com.liferay.portal.kernel.util.ByteArrayMaker;
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.FileUtil;
032: import com.liferay.util.servlet.SessionErrors;
033: import com.liferay.util.servlet.UploadException;
034: import com.liferay.util.servlet.UploadPortletRequest;
035:
036: import java.io.InputStream;
037:
038: import java.util.List;
039:
040: import javax.portlet.ActionRequest;
041: import javax.portlet.ActionResponse;
042: import javax.portlet.PortletConfig;
043: import javax.portlet.RenderRequest;
044: import javax.portlet.RenderResponse;
045:
046: import org.apache.commons.fileupload.disk.DiskFileItem;
047: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
048: import org.apache.commons.fileupload.portlet.PortletFileUpload;
049: import org.apache.commons.logging.Log;
050: import org.apache.commons.logging.LogFactory;
051: import org.apache.struts.action.ActionForm;
052: import org.apache.struts.action.ActionForward;
053: import org.apache.struts.action.ActionMapping;
054:
055: /**
056: * <a href="EditUserPortraitAction.java.html"><b><i>View Source</i></b></a>
057: *
058: * @author Brian Wing Shun Chan
059: *
060: */
061: public class EditUserPortraitAction extends PortletAction {
062:
063: public void processAction(ActionMapping mapping, ActionForm form,
064: PortletConfig config, ActionRequest req, ActionResponse res)
065: throws Exception {
066:
067: try {
068: updatePortrait(req);
069:
070: sendRedirect(req, res);
071: } catch (Exception e) {
072: if (e instanceof NoSuchUserException
073: || e instanceof PrincipalException) {
074:
075: SessionErrors.add(req, e.getClass().getName());
076:
077: setForward(req, "portlet.enterprise_admin.error");
078: } else if (e instanceof UploadException
079: || e instanceof UserPortraitException) {
080:
081: SessionErrors.add(req, e.getClass().getName());
082: } else {
083: throw e;
084: }
085: }
086: }
087:
088: public ActionForward render(ActionMapping mapping, ActionForm form,
089: PortletConfig config, RenderRequest req, RenderResponse res)
090: throws Exception {
091:
092: return mapping.findForward(getForward(req,
093: "portlet.enterprise_admin.edit_user_portrait"));
094: }
095:
096: protected void testRequest(ActionRequest req) throws Exception {
097:
098: // Check if the given request is a multipart request
099:
100: boolean isMultiPartContent = PortletFileUpload
101: .isMultipartContent(req);
102:
103: if (_log.isInfoEnabled()) {
104: if (isMultiPartContent) {
105: _log.info("The given request is a multipart request");
106: } else {
107: _log
108: .info("The given request is NOT a multipart request");
109: }
110: }
111:
112: // Check for the number of file items
113:
114: DiskFileItemFactory factory = new DiskFileItemFactory();
115:
116: PortletFileUpload upload = new PortletFileUpload(factory);
117:
118: List fileItems = upload.parseRequest(req);
119:
120: if (_log.isInfoEnabled()) {
121: _log.info("Apache commons upload was able to parse "
122: + fileItems.size() + " items");
123: }
124:
125: for (int i = 0; i < fileItems.size(); i++) {
126: DiskFileItem fileItem = (DiskFileItem) fileItems.get(i);
127:
128: if (_log.isInfoEnabled()) {
129: _log.info("Item " + i + " " + fileItem);
130: }
131: }
132:
133: // Read directly from the portlet input stream
134:
135: InputStream in = req.getPortletInputStream();
136:
137: if (in != null) {
138: ByteArrayMaker out = new ByteArrayMaker();
139:
140: int c = -1;
141:
142: try {
143: while ((c = in.read()) != -1) {
144: out.write(c);
145: }
146: } finally {
147: in.close();
148: }
149:
150: byte[] bytes = out.toByteArray();
151:
152: if (_log.isInfoEnabled()) {
153: _log
154: .info("Byte array size from the raw input stream is "
155: + bytes.length);
156: }
157: }
158: }
159:
160: protected void updatePortrait(ActionRequest req) throws Exception {
161:
162: //_testRequest(req);
163:
164: UploadPortletRequest uploadReq = PortalUtil
165: .getUploadPortletRequest(req);
166:
167: User user = PortalUtil.getSelectedUser(uploadReq);
168:
169: byte[] bytes = FileUtil.getBytes(uploadReq.getFile("fileName"));
170:
171: if ((bytes == null) || (bytes.length == 0)) {
172: throw new UploadException();
173: }
174:
175: UserServiceUtil.updatePortrait(user.getUserId(), bytes);
176: }
177:
178: private static Log _log = LogFactory
179: .getLog(EditUserPortraitAction.class);
180:
181: }
|