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.portalsettings.action;
016    
017    import com.liferay.portal.ImageTypeException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.image.ImageBag;
021    import com.liferay.portal.kernel.image.ImageToolUtil;
022    import com.liferay.portal.kernel.json.JSONFactoryUtil;
023    import com.liferay.portal.kernel.json.JSONObject;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.portlet.PortletResponseUtil;
027    import com.liferay.portal.kernel.repository.model.FileEntry;
028    import com.liferay.portal.kernel.servlet.SessionErrors;
029    import com.liferay.portal.kernel.upload.UploadException;
030    import com.liferay.portal.kernel.upload.UploadPortletRequest;
031    import com.liferay.portal.kernel.util.Constants;
032    import com.liferay.portal.kernel.util.MimeTypesUtil;
033    import com.liferay.portal.kernel.util.ParamUtil;
034    import com.liferay.portal.kernel.util.StreamUtil;
035    import com.liferay.portal.kernel.util.TempFileUtil;
036    import com.liferay.portal.kernel.util.Validator;
037    import com.liferay.portal.model.Company;
038    import com.liferay.portal.security.auth.PrincipalException;
039    import com.liferay.portal.service.CompanyServiceUtil;
040    import com.liferay.portal.struts.PortletAction;
041    import com.liferay.portal.theme.ThemeDisplay;
042    import com.liferay.portal.util.PortalUtil;
043    import com.liferay.portal.util.WebKeys;
044    import com.liferay.portlet.documentlibrary.FileSizeException;
045    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
046    import com.liferay.portlet.documentlibrary.NoSuchFileException;
047    
048    import java.awt.Rectangle;
049    import java.awt.image.BufferedImage;
050    import java.awt.image.RenderedImage;
051    
052    import java.io.InputStream;
053    
054    import javax.portlet.ActionRequest;
055    import javax.portlet.ActionResponse;
056    import javax.portlet.MimeResponse;
057    import javax.portlet.PortletConfig;
058    import javax.portlet.PortletRequest;
059    import javax.portlet.RenderRequest;
060    import javax.portlet.RenderResponse;
061    import javax.portlet.ResourceRequest;
062    import javax.portlet.ResourceResponse;
063    
064    import org.apache.struts.action.ActionForm;
065    import org.apache.struts.action.ActionForward;
066    import org.apache.struts.action.ActionMapping;
067    
068    /**
069     * @author Brian Wing Shun Chan
070     */
071    public class EditCompanyLogoAction extends PortletAction {
072    
073            @Override
074            public void processAction(
075                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
076                            ActionRequest actionRequest, ActionResponse actionResponse)
077                    throws Exception {
078    
079                    try {
080                            String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
081    
082                            if (cmd.equals(Constants.ADD_TEMP)) {
083                                    addTempImageFile(actionRequest);
084                            }
085                            else {
086                                    saveTempImageFile(actionRequest);
087    
088                                    sendRedirect(actionRequest, actionResponse);
089                            }
090                    }
091                    catch (Exception e) {
092                            if (e instanceof PrincipalException) {
093                                    SessionErrors.add(actionRequest, e.getClass());
094    
095                                    setForward(actionRequest, "portlet.portal_settings.error");
096                            }
097                            else if (e instanceof FileSizeException ||
098                                             e instanceof ImageTypeException) {
099    
100                                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
101    
102                                    jsonObject.putException(e);
103    
104                                    writeJSON(actionRequest, actionResponse, jsonObject);
105                            }
106                            else if (e instanceof NoSuchFileException ||
107                                             e instanceof UploadException) {
108    
109                                    SessionErrors.add(actionRequest, e.getClass());
110                            }
111                            else {
112                                    throw e;
113                            }
114                    }
115            }
116    
117            @Override
118            public ActionForward render(
119                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
120                            RenderRequest renderRequest, RenderResponse renderResponse)
121                    throws Exception {
122    
123                    return mapping.findForward(
124                            getForward(
125                                    renderRequest, "portlet.portal_settings.edit_company_logo"));
126            }
127    
128            @Override
129            public void serveResource(
130                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
131                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
132                    throws Exception {
133    
134                    try {
135                            String cmd = ParamUtil.getString(resourceRequest, Constants.CMD);
136    
137                            if (cmd.equals(Constants.GET_TEMP)) {
138                                    FileEntry tempFileEntry = getTempImageFileEntry(
139                                            resourceRequest);
140    
141                                    serveTempImageFile(
142                                            resourceResponse, tempFileEntry.getContentStream());
143                            }
144                    }
145                    catch (NoSuchFileEntryException nsfee) {
146                    }
147                    catch (Exception e) {
148                            _log.error(e);
149                    }
150            }
151    
152            protected void addTempImageFile(PortletRequest portletRequest)
153                    throws Exception {
154    
155                    UploadPortletRequest uploadPortletRequest =
156                            PortalUtil.getUploadPortletRequest(portletRequest);
157    
158                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
159                            WebKeys.THEME_DISPLAY);
160    
161                    String contentType = uploadPortletRequest.getContentType("fileName");
162    
163                    if (!MimeTypesUtil.isWebImage(contentType)) {
164                            throw new ImageTypeException();
165                    }
166    
167                    try {
168                            TempFileUtil.deleteTempFile(
169                                    themeDisplay.getScopeGroupId(), themeDisplay.getUserId(),
170                                    getTempImageFileName(portletRequest), getTempImageFolderName());
171                    }
172                    catch (Exception e) {
173                    }
174    
175                    InputStream inputStream = null;
176    
177                    try {
178                            inputStream = uploadPortletRequest.getFileAsStream("fileName");
179    
180                            TempFileUtil.addTempFile(
181                                    themeDisplay.getScopeGroupId(), themeDisplay.getUserId(),
182                                    getTempImageFileName(portletRequest), getTempImageFolderName(),
183                                    inputStream, contentType);
184                    }
185                    finally {
186                            StreamUtil.cleanUp(inputStream);
187                    }
188            }
189    
190            protected RenderedImage getCroppedRenderedImage(
191                    RenderedImage renderedImage, int height, int width, int x, int y) {
192    
193                    Rectangle rectangle = new Rectangle(width, height);
194    
195                    Rectangle croppedRectangle = rectangle.intersection(
196                            new Rectangle(renderedImage.getWidth(), renderedImage.getHeight()));
197    
198                    BufferedImage bufferedImage = ImageToolUtil.getBufferedImage(
199                            renderedImage);
200    
201                    return bufferedImage.getSubimage(
202                            x, y, croppedRectangle.width, croppedRectangle.height);
203            }
204    
205            protected FileEntry getTempImageFileEntry(PortletRequest portletRequest)
206                    throws PortalException, SystemException {
207    
208                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
209                            WebKeys.THEME_DISPLAY);
210    
211                    return TempFileUtil.getTempFile(
212                            themeDisplay.getScopeGroupId(), themeDisplay.getUserId(),
213                            getTempImageFileName(portletRequest), getTempImageFolderName());
214            }
215    
216            protected String getTempImageFileName(PortletRequest portletRequest) {
217                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
218                            WebKeys.THEME_DISPLAY);
219    
220                    return String.valueOf(themeDisplay.getCompanyId());
221            }
222    
223            protected String getTempImageFolderName() {
224                    Class<?> clazz = getClass();
225    
226                    return clazz.getName();
227            }
228    
229            protected void saveTempImageFile(ActionRequest actionRequest)
230                    throws Exception {
231    
232                    FileEntry tempFileEntry = null;
233    
234                    InputStream tempImageStream = null;
235    
236                    try {
237                            tempFileEntry = getTempImageFileEntry(actionRequest);
238    
239                            tempImageStream = tempFileEntry.getContentStream();
240    
241                            ImageBag imageBag = ImageToolUtil.read(tempImageStream);
242    
243                            RenderedImage renderedImage = imageBag.getRenderedImage();
244    
245                            String cropRegionJSON = ParamUtil.getString(
246                                    actionRequest, "cropRegion");
247    
248                            if (Validator.isNotNull(cropRegionJSON)) {
249                                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
250                                            cropRegionJSON);
251    
252                                    int height = jsonObject.getInt("height");
253                                    int width = jsonObject.getInt("width");
254                                    int x = jsonObject.getInt("x");
255                                    int y = jsonObject.getInt("y");
256    
257                                    renderedImage = getCroppedRenderedImage(
258                                            renderedImage, height, width, x, y);
259                            }
260    
261                            byte[] bytes = ImageToolUtil.getBytes(
262                                    renderedImage, imageBag.getType());
263    
264                            saveTempImageFile(actionRequest, bytes);
265                    }
266                    catch (NoSuchFileEntryException nsfee) {
267                            throw new UploadException(nsfee);
268                    }
269                    finally {
270                            StreamUtil.cleanUp(tempImageStream);
271    
272                            if (tempFileEntry != null) {
273                                    TempFileUtil.deleteTempFile(tempFileEntry.getFileEntryId());
274                            }
275                    }
276            }
277    
278            protected void saveTempImageFile(
279                            PortletRequest portletRequest, byte[] bytes)
280                    throws Exception {
281    
282                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
283                            WebKeys.THEME_DISPLAY);
284    
285                    Company company = CompanyServiceUtil.updateLogo(
286                            themeDisplay.getCompanyId(), bytes);
287    
288                    themeDisplay.setCompany(company);
289            }
290    
291            protected void serveTempImageFile(
292                            MimeResponse mimeResponse, InputStream tempImageStream)
293                    throws Exception {
294    
295                    ImageBag imageBag = ImageToolUtil.read(tempImageStream);
296    
297                    byte[] bytes = ImageToolUtil.getBytes(
298                            imageBag.getRenderedImage(), imageBag.getType());
299    
300                    String contentType = MimeTypesUtil.getExtensionContentType(
301                            imageBag.getType());
302    
303                    mimeResponse.setContentType(contentType);
304    
305                    PortletResponseUtil.write(mimeResponse, bytes);
306            }
307    
308            private static Log _log = LogFactoryUtil.getLog(
309                    EditCompanyLogoAction.class);
310    
311    }