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