package net.sourceforge.fenixedu.presentationTier.Action.person; import java.io.DataOutputStream; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sourceforge.fenixedu.applicationTier.IUserView; import net.sourceforge.fenixedu.applicationTier.Filtro.exception.FenixFilterException; import net.sourceforge.fenixedu.applicationTier.Servico.exceptions.FenixServiceException; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.Photograph; import net.sourceforge.fenixedu.domain.User; import net.sourceforge.fenixedu.presentationTier.Action.base.FenixDispatchAction; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import pt.ist.fenixWebFramework.security.UserView; /** * @author - Shezad Anavarali (shezad@ist.utl.pt) * */ public class RetrievePersonalPhotoAction extends FenixDispatchAction { public static ActionForward retrieve(final HttpServletRequest request, final HttpServletResponse response, final Person person) { if (person != null) { final Photograph personalPhoto = person.getPersonalPhoto(); if (personalPhoto != null) { if (person.isPhotoAvailableToCurrentUser()) { writePhoto(response, personalPhoto); } } } return null; } public static ActionForward retrieveByUUID(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws FenixFilterException, FenixServiceException { final String uuid = request.getParameter("uuid"); final User user = User.readUserByUserUId(uuid); return user == null ? null : retrieve(request, response, user.getPerson()); } public ActionForward retrieveOwnPhoto(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { final IUserView userView = UserView.getUser(); final Photograph personalPhoto = userView.getPerson().getPersonalPhotoEvenIfPending(); if (personalPhoto != null) { writePhoto(response, personalPhoto); } return null; } public ActionForward retrieveByID(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws FenixFilterException, FenixServiceException { final Integer personID = Integer.valueOf(request.getParameter("personCode")); final Person person = (Person) rootDomainObject.readPartyByOID(personID); return retrieve(request, response, person); } public ActionForward retrievePendingByID(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws FenixFilterException, FenixServiceException { final Integer photoID = Integer.valueOf(request.getParameter("photoCode")); Photograph photo = rootDomainObject.readPhotographByOID(photoID); if (photo != null) { writePhoto(response, photo); } return null; } protected static void writePhoto(final HttpServletResponse response, final Photograph personalPhoto) { try { response.setContentType(personalPhoto.getContentType().getMimeType()); final DataOutputStream dos = new DataOutputStream(response.getOutputStream()); dos.write(personalPhoto.getContents()); dos.close(); } catch (IOException e) { } } }