Eric J. Bowersox de971c9c7b continued the separation by shifting all the exceptions into their own package
(preparatory to establishing some sort of service-based architecture)
2001-11-19 02:20:22 +00:00

175 lines
6.3 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.net.URLEncoder;
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 UserPhoto extends VeniceServlet
{
/*--------------------------------------------------------------------------------
* Static data members
*--------------------------------------------------------------------------------
*/
private static Category logger = Category.getInstance(UserPhoto.class);
/*--------------------------------------------------------------------------------
* Overrides from class HttpServlet
*--------------------------------------------------------------------------------
*/
public String getServletInfo()
{
String rc = "UserPhoto 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
{
String tgt = request.getParameter("tgt"); // target location
if (tgt==null)
tgt = "top"; // go back to the Top screen if nothing else
try
{ // create the display
return new UserPhotoData(engine,user,rdat,tgt);
} // end try
catch (DataException de)
{ // error getting at the data stream from the attachment
return new ErrorBox("Database Error","Database error generating display: " + de.getMessage(),null);
} // end catch
} // end doVeniceGet
protected VeniceContent doVenicePost(HttpServletRequest request, ServletMultipartHandler mphandler,
VeniceEngine engine, UserContext user, RenderData rdat)
throws ServletException, IOException, VeniceServletResult
{
// Get target URL for the operation
if (mphandler.isFileParam("tgt"))
{ // bogus target URL
logger.error("Internal Error: 'tgt' should be a normal param");
return new ErrorBox(null,"Internal Error: 'tgt' should be a normal param","top");
} // end if
String tgt = mphandler.getValue("tgt");
if (tgt==null)
tgt = "top"; // go back to the Top screen if nothing else
if (isImageButtonClicked(mphandler,"cancel"))
throw new RedirectResult("account?cmd=P&tgt=" + URLEncoder.encode(tgt));
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",
"account?cmd=P&tgt=" + URLEncoder.encode(tgt));
} // 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.",
"userphoto?tgt=" + URLEncoder.encode(tgt));
} // 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 = user.getContactInfo();
ci.setPhotoData(request.getContextPath() + "/imagedata/","image/jpeg",real_pic.getLength(),
real_pic.getData());
user.putContactInfo(ci);
// Jump back to the profile form.
throw new RedirectResult("account?cmd=P&tgt=" + URLEncoder.encode(tgt));
} // 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(),
"account?cmd=P&tgt=" + URLEncoder.encode(tgt));
} // end catch
catch (ImageNormalizerException ine)
{ // the image was not valid
logger.error("Image normalizer error:",ine);
return new ErrorBox(null,ine.getMessage(),"userphoto?tgt=" + URLEncoder.encode(tgt));
} // 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(),
"account?cmd=P&tgt=" + URLEncoder.encode(tgt));
} // end catch
catch (EmailException ee)
{ // email exception (WTF?)
logger.error("Email exception (shouldn't happen):",ee);
return new ErrorBox(null,"Internal Error: " + ee.getMessage(),
"account?cmd=P&tgt=" + URLEncoder.encode(tgt));
} // end catch
} // end if
else
{ // the button must be wrong!
logger.error("no known button click on UserPhoto.doPost");
return new ErrorBox("Internal Error","Unknown command button pressed",
"account?cmd=P&tgt=" + URLEncoder.encode(tgt));
} // end else
} // end doVenicePost
} // end class UserPhoto