Eric J. Bowersox 004dcaf7ec added administrative control of user photos - ability to replace or clear
a user's photo, keep the user from uploading a new one
2001-11-29 07:46:57 +00:00

204 lines
7.4 KiB
Java

/*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
*
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* The Original Code is the Venice Web Communities System.
*
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
* Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
*
* Contributor(s):
*/
package com.silverwrist.venice.servlets;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log4j.*;
import com.silverwrist.util.StringUtil;
import com.silverwrist.util.ServletMultipartHandler;
import com.silverwrist.util.ServletMultipartException;
import com.silverwrist.util.image.*;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.servlets.format.*;
public class AdminUserPhoto extends VeniceServlet
{
/*--------------------------------------------------------------------------------
* Static data members
*--------------------------------------------------------------------------------
*/
private static Category logger = Category.getInstance(AdminUserPhoto.class);
/*--------------------------------------------------------------------------------
* Overrides from class HttpServlet
*--------------------------------------------------------------------------------
*/
public String getServletInfo()
{
String rc = "AdminUserPhoto servlet - changes the user photo for a user\n"
+ "Part of the Venice Web Communities System\n";
return rc;
} // end getServletInfo
/*--------------------------------------------------------------------------------
* Overrides from class VeniceServlet
*--------------------------------------------------------------------------------
*/
protected VeniceContent doVeniceGet(HttpServletRequest request, VeniceEngine engine,
UserContext user, RenderData rdat)
throws ServletException, IOException, VeniceServletResult
{
try
{ // get the user to be modified
AdminOperations adm = user.getAdminInterface();
String s_uid = request.getParameter("uid");
if (s_uid==null)
throw new ErrorBox(null,"User ID parameter not found.","sysadmin?cmd=UF");
AdminUserContext admuser = adm.getUserContext(Integer.parseInt(s_uid));
if (request.getParameter("null")!=null)
{ // null the photo out and return
ContactInfo ci = admuser.getContactInfo();
ci.setPhotoURL(null);
admuser.putContactInfo(ci);
throw new RedirectResult("sysadmin?cmd=UM&uid=" + admuser.getUID());
} // end if
return new AdminUserPhotoData(engine,admuser,rdat);
} // end try
catch (AccessError ae)
{ // an access error generally means we're not an administrator
return new ErrorBox("Access Error","You do not have permission to administer the system.",null);
} // end catch
catch (DataException de)
{ // error pulling the audit records
return new ErrorBox("Database Error","Unable to retrieve user information: " + de.getMessage(),
"sysadmin?cmd=UF");
} // end catch
catch (NumberFormatException nfe)
{ // this is if we get a bogus UID
return new ErrorBox(null,"Invalid user ID parameter.","sysadmin?cmd=UF");
} // end catch
} // end doVeniceGet
protected VeniceContent doVenicePost(HttpServletRequest request, ServletMultipartHandler mphandler,
VeniceEngine engine, UserContext user, RenderData rdat)
throws ServletException, IOException, VeniceServletResult
{
AdminUserContext admuser;
try
{ // get the user to be modified
AdminOperations adm = user.getAdminInterface();
String s_uid = mphandler.getValue("uid");
if (s_uid==null)
throw new ErrorBox(null,"User ID parameter not found.","sysadmin?cmd=UF");
admuser = adm.getUserContext(Integer.parseInt(s_uid));
} // end try
catch (AccessError ae)
{ // an access error generally means we're not an administrator
return new ErrorBox("Access Error","You do not have permission to administer the system.",null);
} // end catch
catch (DataException de)
{ // error pulling the audit records
return new ErrorBox("Database Error","Unable to retrieve user information: " + de.getMessage(),
"sysadmin?cmd=UF");
} // end catch
catch (NumberFormatException nfe)
{ // this is if we get a bogus UID
return new ErrorBox(null,"Invalid user ID parameter.","sysadmin?cmd=UF");
} // end catch
if (isImageButtonClicked(mphandler,"cancel"))
throw new RedirectResult("sysadmin?cmd=UM&uid=" + admuser.getUID());
if (isImageButtonClicked(mphandler,"upload"))
{ // uploading the image here!
// also check on file parameter status
if (!(mphandler.isFileParam("thepic")))
{ // bogus file parameter
logger.error("Internal Error: 'thepic' should be a file param");
return new ErrorBox(null,"Internal Error: 'thepic' should be a file param",
"sysadmin?cmd=UM&uid=" + admuser.getUID());
} // end if
if (!(mphandler.getContentType("thepic").startsWith("image/")))
{ // must be an image type we uploaded!
logger.error("Error: 'thepic' not an image type");
return new ErrorBox(null,"You did not upload an image file. Try again.",
"sysadmin?cmd=UM&uid=" + admuser.getUID());
} // end if
try
{ // get the real picture (normalized to 100x100 size)
ImageLengthPair real_pic = ImageNormalizer.normalizeImage(mphandler.getFileContentStream("thepic"),
engine.getUserPhotoSize(),"jpeg");
// set the user photo data!
ContactInfo ci = admuser.getContactInfo();
ci.setPhotoData(request.getContextPath() + "/imagedata/","image/jpeg",real_pic.getLength(),
real_pic.getData());
admuser.putContactInfo(ci);
// Jump back to the profile form.
throw new RedirectResult("sysadmin?cmd=UM&uid=" + admuser.getUID());
} // end try
catch (ServletMultipartException smpe)
{ // the servlet multipart parser screwed up
logger.error("Servlet multipart error:",smpe);
return new ErrorBox(null,"Internal Error: " + smpe.getMessage(),
"sysadmin?cmd=UM&uid=" + admuser.getUID());
} // end catch
catch (ImageNormalizerException ine)
{ // the image was not valid
logger.error("Image normalizer error:",ine);
return new ErrorBox(null,ine.getMessage(),"admuserphoto?uid=" + admuser.getUID());
} // end catch
catch (DataException de)
{ // error in the database!
logger.error("DataException:",de);
return new ErrorBox("Database Error","Database error storing user photo: " + de.getMessage(),
"sysadmin?cmd=UM&uid=" + admuser.getUID());
} // end catch
} // end if
else
{ // the button must be wrong!
logger.error("no known button click on AdminUserPhoto.doPost");
return new ErrorBox("Internal Error","Unknown command button pressed",
"sysadmin?cmd=UM&uid=" + admuser.getUID());
} // end else
} // end doVenicePost
} // end class AdminUserPhoto