001
014
015 package com.liferay.portlet.usersadmin.action;
016
017 import com.liferay.portal.ImageTypeException;
018 import com.liferay.portal.NoSuchOrganizationException;
019 import com.liferay.portal.kernel.io.ByteArrayFileInputStream;
020 import com.liferay.portal.kernel.servlet.SessionErrors;
021 import com.liferay.portal.kernel.upload.UploadException;
022 import com.liferay.portal.kernel.upload.UploadPortletRequest;
023 import com.liferay.portal.kernel.util.ParamUtil;
024 import com.liferay.portal.kernel.util.StreamUtil;
025 import com.liferay.portal.security.auth.PrincipalException;
026 import com.liferay.portal.service.LayoutSetServiceUtil;
027 import com.liferay.portal.struts.PortletAction;
028 import com.liferay.portal.util.PortalUtil;
029
030 import java.io.File;
031 import java.io.InputStream;
032
033 import javax.portlet.ActionRequest;
034 import javax.portlet.ActionResponse;
035 import javax.portlet.PortletConfig;
036 import javax.portlet.RenderRequest;
037 import javax.portlet.RenderResponse;
038
039 import org.apache.struts.action.ActionForm;
040 import org.apache.struts.action.ActionForward;
041 import org.apache.struts.action.ActionMapping;
042
043
047 public class EditOrganizationLogoAction extends PortletAction {
048
049 @Override
050 public void processAction(
051 ActionMapping actionMapping, ActionForm actionForm,
052 PortletConfig portletConfig, ActionRequest actionRequest,
053 ActionResponse actionResponse)
054 throws Exception {
055
056 try {
057 updateLogo(actionRequest);
058
059 sendRedirect(actionRequest, actionResponse);
060 }
061 catch (Exception e) {
062 if (e instanceof ImageTypeException ||
063 e instanceof NoSuchOrganizationException ||
064 e instanceof PrincipalException) {
065
066 SessionErrors.add(actionRequest, e.getClass());
067
068 setForward(actionRequest, "portlet.users_admin.error");
069 }
070 else if (e instanceof UploadException) {
071 SessionErrors.add(actionRequest, e.getClass());
072 }
073 else {
074 throw e;
075 }
076 }
077 }
078
079 @Override
080 public ActionForward render(
081 ActionMapping actionMapping, ActionForm actionForm,
082 PortletConfig portletConfig, RenderRequest renderRequest,
083 RenderResponse renderResponse)
084 throws Exception {
085
086 return actionMapping.findForward(
087 getForward(
088 renderRequest, "portlet.users_admin.edit_organization_logo"));
089 }
090
091 protected void updateLogo(ActionRequest actionRequest) throws Exception {
092 UploadPortletRequest uploadPortletRequest =
093 PortalUtil.getUploadPortletRequest(actionRequest);
094
095 long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
096
097 InputStream inputStream = null;
098
099 try {
100 File file = uploadPortletRequest.getFile("fileName");
101
102 inputStream = new ByteArrayFileInputStream(file, 1024);
103
104 inputStream.mark(0);
105
106 LayoutSetServiceUtil.updateLogo(
107 groupId, true, true, inputStream, false);
108
109 inputStream.reset();
110
111 LayoutSetServiceUtil.updateLogo(
112 groupId, false, true, inputStream, false);
113 }
114 finally {
115 StreamUtil.cleanUp(inputStream);
116 }
117 }
118
119 }