Index: lams_central/src/java/org/lamsfoundation/lams/util/PortraitUtils.java =================================================================== diff -u --- lams_central/src/java/org/lamsfoundation/lams/util/PortraitUtils.java (revision 0) +++ lams_central/src/java/org/lamsfoundation/lams/util/PortraitUtils.java (revision 43ec109ac986fb54b39042c1601915fcf0b590d9) @@ -0,0 +1,84 @@ +/**************************************************************** + * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) + * ============================================================= + * License Information: http://lamsfoundation.org/licensing/lams/2.0/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2.0 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + * + * http://www.gnu.org/licenses/gpl.txt + * **************************************************************** + */ + +/* $Id$ */ +package org.lamsfoundation.lams.util; + +import java.awt.Graphics2D; +import java.awt.geom.AffineTransform; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + +import javax.imageio.ImageIO; + +import org.apache.log4j.Logger; + +/** + * @author jliew + * + */ +public class PortraitUtils { + + private static Logger log = Logger.getLogger(PortraitUtils.class); + + /* + * Resize picture to specified width and height in pixels. Maintains aspect ratio. + * Output image in the format specified by formatName. + */ + public static ByteArrayInputStream resizePicture(InputStream is, int width, int height, String formatName) { + + ByteArrayOutputStream os = null; + try { + // create buffer for source image + BufferedImage bsrc = ImageIO.read(is); + log.debug("old width: "+bsrc.getWidth()+", old height: "+bsrc.getHeight()); + + // maintain aspect ratio + double widthScale = (double)width/bsrc.getWidth(); + double heightScale = (double)height/bsrc.getHeight(); + double minScale = Math.min(widthScale,heightScale); + double newWidth = minScale*(double)bsrc.getWidth(); + double newHeight = minScale*(double)bsrc.getHeight(); + log.debug("scaling picture by "+minScale+"... new width: "+newWidth+", new height: "+newHeight); + + // create buffer for resized image + BufferedImage bdest = new BufferedImage((int)newWidth, (int)newHeight, BufferedImage.TYPE_INT_RGB); + Graphics2D g = bdest.createGraphics(); + AffineTransform at = AffineTransform.getScaleInstance(minScale, minScale); + g.drawRenderedImage(bsrc,at); + + // write new picture into a buffer usable by content repository + os = new ByteArrayOutputStream(); + ImageIO.write(bdest,formatName,os); + // alternative may be to use a File on disk as the buffer + } catch (IOException e) { + log.error(e.getStackTrace()); + return null; + } + return new ByteArrayInputStream(os.toByteArray()); + } + +} Index: lams_central/src/java/org/lamsfoundation/lams/web/PortraitAction.java =================================================================== diff -u --- lams_central/src/java/org/lamsfoundation/lams/web/PortraitAction.java (revision 0) +++ lams_central/src/java/org/lamsfoundation/lams/web/PortraitAction.java (revision 43ec109ac986fb54b39042c1601915fcf0b590d9) @@ -0,0 +1,79 @@ +/**************************************************************** + * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) + * ============================================================= + * License Information: http://lamsfoundation.org/licensing/lams/2.0/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2.0 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + * + * http://www.gnu.org/licenses/gpl.txt + * **************************************************************** + */ + +/* $Id$ */ +package org.lamsfoundation.lams.web; + +import java.util.Collections; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.log4j.Logger; +import org.apache.struts.action.Action; +import org.apache.struts.action.ActionForm; +import org.apache.struts.action.ActionForward; +import org.apache.struts.action.ActionMapping; +import org.lamsfoundation.lams.usermanagement.SupportedLocale; +import org.lamsfoundation.lams.usermanagement.User; +import org.lamsfoundation.lams.usermanagement.service.IUserManagementService; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.WebApplicationContextUtils; + +/** + * @author jliew + * + * @struts:action path="/portrait" + * name="PortraitActionForm" + * scope="request" + * validate="false" + * + * @struts:action-forward name="portrait" path=".portrait" + */ +public class PortraitAction extends Action { + + private static Logger log = Logger.getLogger(PortraitAction.class); + private static IUserManagementService service; + + public ActionForward execute(ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response) throws Exception { + + PortraitActionForm portraitForm = (PortraitActionForm)form; + Long portraitUuid = (Long)getService().getUserByLogin(request.getRemoteUser()).getPortraitUuid(); + log.debug("using portraitUuid="+portraitUuid); + // if no portrait has been uploaded, set the uuid to 0 + portraitForm.setPortraitUuid(portraitUuid==null?0:portraitUuid); + return mapping.findForward("portrait"); + } + + private IUserManagementService getService(){ + if(service==null){ + WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServlet().getServletContext()); + service = (IUserManagementService) ctx.getBean("userManagementServiceTarget"); + } + return service; + } + +} Index: lams_central/src/java/org/lamsfoundation/lams/web/PortraitActionForm.java =================================================================== diff -u --- lams_central/src/java/org/lamsfoundation/lams/web/PortraitActionForm.java (revision 0) +++ lams_central/src/java/org/lamsfoundation/lams/web/PortraitActionForm.java (revision 43ec109ac986fb54b39042c1601915fcf0b590d9) @@ -0,0 +1,56 @@ +/**************************************************************** + * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) + * ============================================================= + * License Information: http://lamsfoundation.org/licensing/lams/2.0/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2.0 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + * + * http://www.gnu.org/licenses/gpl.txt + * **************************************************************** + */ + +/* $Id$ */ +package org.lamsfoundation.lams.web; + +import org.apache.struts.action.ActionForm; +import org.apache.struts.upload.FormFile; + +/** + * @author jliew + * + * @struts.form name="PortraitActionForm" + */ +public class PortraitActionForm extends ActionForm { + + private Long portraitUuid; + private FormFile file; + + public Long getPortraitUuid() { + return portraitUuid; + } + + public void setPortraitUuid(Long uuid) { + this.portraitUuid = uuid; + } + + public FormFile getFile() { + return file; + } + + public void setFile(FormFile file) { + this.file = file; + } + +} Index: lams_central/src/java/org/lamsfoundation/lams/web/PortraitSaveAction.java =================================================================== diff -u --- lams_central/src/java/org/lamsfoundation/lams/web/PortraitSaveAction.java (revision 0) +++ lams_central/src/java/org/lamsfoundation/lams/web/PortraitSaveAction.java (revision 43ec109ac986fb54b39042c1601915fcf0b590d9) @@ -0,0 +1,136 @@ +/**************************************************************** + * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) + * ============================================================= + * License Information: http://lamsfoundation.org/licensing/lams/2.0/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2.0 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + * + * http://www.gnu.org/licenses/gpl.txt + * **************************************************************** + */ + +/* $Id$ */ +package org.lamsfoundation.lams.web; + +import java.io.ByteArrayInputStream; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.apache.struts.action.Action; +import org.apache.struts.action.ActionForm; +import org.apache.struts.action.ActionForward; +import org.apache.struts.action.ActionMapping; +import org.apache.struts.action.ActionMessage; +import org.apache.struts.action.ActionMessages; +import org.apache.struts.upload.FormFile; +import org.lamsfoundation.lams.contentrepository.NodeKey; +import org.lamsfoundation.lams.contentrepository.client.IToolContentHandler; +import org.lamsfoundation.lams.usermanagement.User; +import org.lamsfoundation.lams.usermanagement.service.IUserManagementService; +import org.lamsfoundation.lams.util.CentralToolContentHandler; +import org.lamsfoundation.lams.util.PortraitUtils; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.WebApplicationContextUtils; + +/** + * @author jliew + * + * @struts:action path="/saveportrait" + * name="PortraitActionForm" + * input=".portrait" + * scope="request" + * validate="false" + * + * @struts:action-forward name="profile" path="/profile.do?method=view" + */ +public class PortraitSaveAction extends Action { + + private static Logger log = Logger.getLogger(PortraitSaveAction.class); + private static IUserManagementService service; + private static CentralToolContentHandler centralToolContentHandler; + private static int THUMBNAIL_WIDTH = 120; + private static int THUMBNAIL_HEIGHT = 120; + + public ActionForward execute(ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response) throws Exception { + + if(isCancelled(request)){ + return mapping.findForward("profile"); + } + + ActionMessages errors = new ActionMessages(); + + WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServlet().getServletContext()); + centralToolContentHandler = (CentralToolContentHandler)wac.getBean("centralToolContentHandler"); + + PortraitActionForm portraitForm = (PortraitActionForm)form; + FormFile file = portraitForm.getFile(); + String fileName = file.getFileName(); + log.debug("got file: "+fileName+" of type: "+file.getContentType()+" with size: "+file.getFileSize()); + + // check if file is an image using the MIME content type + String mediaType = file.getContentType().split("/",2)[0]; + if (!mediaType.equals("image")) { + errors.add("file",new ActionMessage("error.portrait.not.image")); + return mapping.getInputForward(); + } + + // resize picture into new buffer + //String fileType = file.getFileName().split(".",2)[1]; + String fileType = fileName.substring(fileName.lastIndexOf('.')+1); + log.debug("fileType: "+fileType); + ByteArrayInputStream is = PortraitUtils.resizePicture(file.getInputStream(), THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, fileType); + if (is==null) { + errors.add("file",new ActionMessage("error.general.1")); + return mapping.getInputForward(); + } + + // write to content repository + NodeKey node = null; + if (file!= null && !StringUtils.isEmpty(fileName)) { + try { + //InputStream is = file.getInputStream(); + node = centralToolContentHandler.uploadFile(is, fileName, file.getContentType(), IToolContentHandler.TYPE_ONLINE); + is.close(); + } catch (Exception e) { + request.setAttribute("errorMessage", e.getMessage()); + return mapping.findForward("error.system"); + } + } + + log.debug("saved file with uuid: "+node.getUuid()+" and version: "+node.getVersion()); + + User user = (User)getService().getUserByLogin(request.getRemoteUser()); + // delete old portrait file (we only want to keep the user's current portrait) + if (user.getPortraitUuid()>0) centralToolContentHandler.deleteFile(user.getPortraitUuid()); + user.setPortraitUuid(node.getUuid()); + getService().save(user); + + return mapping.findForward("profile"); + } + + private IUserManagementService getService(){ + if(service==null){ + WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServlet().getServletContext()); + service = (IUserManagementService) ctx.getBean("userManagementServiceTarget"); + } + return service; + } +} Index: lams_central/web/WEB-INF/tiles/tiles-defs.xml =================================================================== diff -u -ra909454acb3505c97f39fbf6d0e2776ea8ffa3c4 -r43ec109ac986fb54b39042c1601915fcf0b590d9 --- lams_central/web/WEB-INF/tiles/tiles-defs.xml (.../tiles-defs.xml) (revision a909454acb3505c97f39fbf6d0e2776ea8ffa3c4) +++ lams_central/web/WEB-INF/tiles/tiles-defs.xml (.../tiles-defs.xml) (revision 43ec109ac986fb54b39042c1601915fcf0b590d9) @@ -22,6 +22,11 @@ --> + + + + + Index: lams_central/web/portrait.jsp =================================================================== diff -u --- lams_central/web/portrait.jsp (revision 0) +++ lams_central/web/portrait.jsp (revision 43ec109ac986fb54b39042c1601915fcf0b590d9) @@ -0,0 +1,40 @@ +<%@ taglib uri="tags-html" prefix="html" %> +<%@ taglib uri="tags-bean" prefix="bean" %> +<%@ taglib uri="tags-fmt" prefix="fmt" %> +<%@ taglib uri="tags-logic" prefix="logic" %> + + + + + + + +
+

+

+ + + + + + + + + + + + +
: + + &preferDownload=false" /> + + + + +
:
+   + +
+
+

+
\ No newline at end of file