landed the code for doing post attachments (the infamous paperclip)

This commit is contained in:
Eric J. Bowersox
2001-02-08 22:43:58 +00:00
parent 66b7fea53b
commit 70774ead7d
20 changed files with 1075 additions and 25 deletions

View File

@@ -0,0 +1,427 @@
/*
* 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.venice.ValidationException;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.servlets.format.*;
public class Attachment extends VeniceServlet
{
/*--------------------------------------------------------------------------------
* Static data members
*--------------------------------------------------------------------------------
*/
private static Category logger = Category.getInstance(Attachment.class.getName());
/*--------------------------------------------------------------------------------
* Internal functions
*--------------------------------------------------------------------------------
*/
private static SIGContext getSIGParameter(String str, UserContext user)
throws ValidationException, DataException
{
if (str==null)
{ // no SIG parameter - bail out now!
logger.error("SIG parameter not specified!");
throw new ValidationException("No SIG specified.");
} // end if
try
{ // turn the string into a SIGID, and thence to a SIGContext
int sigid = Integer.parseInt(str);
return user.getSIGContext(sigid);
} // end try
catch (NumberFormatException nfe)
{ // error in Integer.parseInt
logger.error("Cannot convert SIG parameter '" + str + "'!");
throw new ValidationException("Invalid SIG parameter.");
} // end catch
} // end getSIGParameter
private static SIGContext getSIGParameter(ServletRequest request, UserContext user)
throws ValidationException, DataException
{
return getSIGParameter(request.getParameter("sig"),user);
} // end getSIGParameter
private static SIGContext getSIGParameter(ServletMultipartHandler mphandler, UserContext user)
throws ValidationException, DataException
{
if (mphandler.isFileParam("sig"))
throw new ValidationException("Internal Error: SIG should be a normal param");
return getSIGParameter(mphandler.getValue("sig"),user);
} // end getSIGParameter
private static ConferenceContext getConferenceParameter(String str, SIGContext sig)
throws ValidationException, DataException, AccessError
{
if (str==null)
{ // no conference parameter - bail out now!
logger.error("Conference parameter not specified!");
throw new ValidationException("No conference specified.");
} // end if
try
{ // turn the string into a ConfID, and thence to a ConferenceContext
int confid = Integer.parseInt(str);
return sig.getConferenceContext(confid);
} // end try
catch (NumberFormatException nfe)
{ // error in Integer.parseInt
logger.error("Cannot convert conference parameter '" + str + "'!");
throw new ValidationException("Invalid conference parameter.");
} // end catch
} // end getConferenceParameter
private static ConferenceContext getConferenceParameter(ServletRequest request, SIGContext sig)
throws ValidationException, DataException, AccessError
{
return getConferenceParameter(request.getParameter("conf"),sig);
} // end getConferenceParameter
private static ConferenceContext getConferenceParameter(ServletMultipartHandler mphandler, SIGContext sig)
throws ValidationException, DataException, AccessError
{
if (mphandler.isFileParam("conf"))
throw new ValidationException("Internal Error: conference should be a normal param");
return getConferenceParameter(mphandler.getValue("conf"),sig);
} // end getConferenceParameter
private static TopicMessageContext getMessageParameter(String str, ConferenceContext conf)
throws ValidationException, DataException, AccessError
{
if (str==null)
{ // no conference parameter - bail out now!
logger.error("Message parameter not specified!");
throw new ValidationException("No message specified.");
} // end if
try
{ // turn the string into a postid, and thence to a TopicMessageContext
long postid = Long.parseLong(str);
return conf.getMessageByPostID(postid);
} // end try
catch (NumberFormatException nfe)
{ // error in Integer.parseInt
logger.error("Cannot convert message parameter '" + str + "'!");
throw new ValidationException("Invalid message parameter.");
} // end catch
} // end getMessageParameter
private static TopicMessageContext getMessageParameter(ServletRequest request, ConferenceContext conf)
throws ValidationException, DataException, AccessError
{
return getMessageParameter(request.getParameter("msg"),conf);
} // end getMessageParameter
private static TopicMessageContext getMessageParameter(ServletMultipartHandler mphandler,
ConferenceContext conf)
throws ValidationException, DataException, AccessError
{
if (mphandler.isFileParam("msg"))
throw new ValidationException("Internal Error: message should be a normal param");
return getMessageParameter(mphandler.getValue("msg"),conf);
} // end getMessageParameter
/*--------------------------------------------------------------------------------
* Overrides from class HttpServlet
*--------------------------------------------------------------------------------
*/
public String getServletInfo()
{
String rc = "Attachment servlet - Handles uploading and downloading attachments\n"
+ "Part of the Venice Web Communities System\n";
return rc;
} // end getServletInfo
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
UserContext user = getUserContext(request);
RenderData rdat = createRenderData(request,response);
String page_title = null;
Object content = null;
SIGContext sig = null; // SIG context
ConferenceContext conf = null; // conference context
TopicMessageContext msg = null; // message context
try
{ // this outer try is to catch ValidationException
try
{ // all commands require a SIG parameter
sig = getSIGParameter(request,user);
changeMenuSIG(request,sig);
if (logger.isDebugEnabled())
logger.debug("found SIG #" + String.valueOf(sig.getSIGID()));
} // end try
catch (DataException de)
{ // error looking up the SIG
page_title = "Database Error";
content = new ErrorBox(page_title,"Database error finding SIG: " + de.getMessage(),"top");
} // end catch
if (content==null)
{ // we got the SIG parameter OK
try
{ // all commands require a conference parameter
conf = getConferenceParameter(request,sig);
if (logger.isDebugEnabled())
logger.debug("found conf #" + String.valueOf(conf.getConfID()));
} // end try
catch (DataException de)
{ // error looking up the conference
page_title = "Database Error";
content = new ErrorBox(page_title,"Database error finding conference: " + de.getMessage(),"top");
} // end catch
} // end if
if (content==null)
{ // we got the conference parameter OK
try
{ // now we need a message parameter
msg = getMessageParameter(request,conf);
if (logger.isDebugEnabled())
logger.debug("found post #" + String.valueOf(msg.getPostID()));
} // end try
catch (DataException de)
{ // error looking up the conference
page_title = "Database Error";
content = new ErrorBox(page_title,"Database error finding message: " + de.getMessage(),"top");
} // end catch
} // end if
} // end try
catch (ValidationException ve)
{ // these all get handled in pretty much the same way
page_title = "Error";
content = new ErrorBox(null,ve.getMessage(),"top");
} // end catch
catch (AccessError ae)
{ // these all get handled in pretty much the same way
page_title = "Access Error";
content = new ErrorBox(page_title,ae.getMessage(),"top");
} // end catch
if (content==null)
{ // extract the attachment data from the message and send it out
try
{ // extract the attachment data and send it out!
rdat.sendBinaryData(msg.getAttachmentType(),msg.getAttachmentFilename(),
msg.getAttachmentLength(),msg.getAttachmentData());
return; // now we're all done!
} // end try
catch (AccessError ae)
{ // these all get handled in pretty much the same way
page_title = "Access Error";
content = new ErrorBox(page_title,ae.getMessage(),"top");
} // end catch
catch (DataException de)
{ // error looking up the conference
page_title = "Database Error";
content = new ErrorBox(page_title,"Database error retrieving attachment: " + de.getMessage(),"top");
} // end catch
} // end if
// we only get here if there were an error
BaseJSPData basedat = new BaseJSPData(page_title,"top",content);
basedat.transfer(getServletContext(),rdat);
} // end doGet
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
UserContext user = getUserContext(request);
RenderData rdat = createRenderData(request,response);
ServletMultipartHandler mphandler = null;
String target = "top";
String page_title = null;
Object content = null;
SIGContext sig = null; // SIG context
ConferenceContext conf = null; // conference context
TopicMessageContext msg = null; // message context
try
{ // this outer try is to catch ValidationException
mphandler = new ServletMultipartHandler(request);
if (mphandler.isFileParam("target"))
throw new ValidationException("Internal Error: 'target' should be a normal param");
target = mphandler.getValue("target");
try
{ // all commands require a SIG parameter
sig = getSIGParameter(mphandler,user);
changeMenuSIG(request,sig);
if (logger.isDebugEnabled())
logger.debug("found SIG #" + String.valueOf(sig.getSIGID()));
} // end try
catch (DataException de)
{ // error looking up the SIG
page_title = "Database Error";
content = new ErrorBox(page_title,"Database error finding SIG: " + de.getMessage(),target);
} // end catch
if (content==null)
{ // we got the SIG parameter OK
try
{ // all commands require a conference parameter
conf = getConferenceParameter(mphandler,sig);
if (logger.isDebugEnabled())
logger.debug("found conf #" + String.valueOf(conf.getConfID()));
} // end try
catch (DataException de)
{ // error looking up the conference
page_title = "Database Error";
content = new ErrorBox(page_title,"Database error finding conference: " + de.getMessage(),target);
} // end catch
} // end if
if (content==null)
{ // we got the conference parameter OK
try
{ // now we need a message parameter
msg = getMessageParameter(mphandler,conf);
if (logger.isDebugEnabled())
logger.debug("found post #" + String.valueOf(msg.getPostID()));
} // end try
catch (DataException de)
{ // error looking up the conference
page_title = "Database Error";
content = new ErrorBox(page_title,"Database error finding message: " + de.getMessage(),target);
} // end catch
} // end if
// also check on file and target parameter status
if (!(mphandler.isFileParam("thefile")))
throw new ValidationException("Internal error: 'thefile' should be a file param");
} // end try
catch (ValidationException ve)
{ // these all get handled in pretty much the same way
page_title = "Error";
content = new ErrorBox(null,ve.getMessage(),target);
} // end catch
catch (AccessError ae)
{ // these all get handled in pretty much the same way
page_title = "Access Error";
content = new ErrorBox(page_title,ae.getMessage(),target);
} // end catch
catch (ServletMultipartException smpe)
{ // this is kind of a special case
page_title = "Error";
content = new ErrorBox(page_title,"Internal Error: " + smpe.getMessage(),target);
} // end catch
if (content==null)
{ // we're ready to get the data and attach it
try
{ // attach the data to the message!
msg.attachData(mphandler.getContentType("thefile"),mphandler.getValue("thefile"),
mphandler.getContentSize("thefile"),mphandler.getFileContentStream("thefile"));
// go back to where we should have gone before we uploaded the message
rdat.redirectTo(target);
return;
} // end try
catch (ServletMultipartException smpe)
{ // this is kind of a special case
page_title = "Error";
content = new ErrorBox(page_title,"Internal Error: " + smpe.getMessage(),target);
} // end catch
catch (AccessError ae)
{ // these all get handled in pretty much the same way
page_title = "Access Error";
content = new ErrorBox(page_title,ae.getMessage(),target);
} // end catch
catch (DataException de)
{ // error looking up the conference
page_title = "Database Error";
content = new ErrorBox(page_title,"Database error storing attachment: " + de.getMessage(),target);
} // end catch
} // end if (we got all the parameters OK)
// we only get here if there were an error
BaseJSPData basedat = new BaseJSPData(page_title,target,content);
basedat.transfer(getServletContext(),rdat);
} // end doPost
} // end class Attachment

View File

@@ -453,9 +453,10 @@ public class ConfOperations extends VeniceServlet
final String yes = "Y";
if (yes.equals(request.getParameter("attach")))
{ // we need to upload an attachment for this post
// TODO: jump somewhere to upload an attachment
rdat.redirectTo(on_error);
return;
TopicMessageContext msg = topic.getMessage(0); // load the "zero post"
content = new AttachmentForm(sig,conf,msg,on_error);
page_title = "Upload Attachment";
} // end if
else

View File

@@ -276,12 +276,12 @@ public class PostMessage extends VeniceServlet
{ // no slippage - post the message!!!
TopicMessageContext msg = topic.postNewMessage(0,request.getParameter("pseud"),raw_postdata);
if (yes.equals(request.getParameter("attach")))
{ // we have an attachment to upload...
// TODO: do something to upload the attachment
rdat.redirectTo("confdisp?sig=" + String.valueOf(sig.getSIGID()) + "&conf="
+ String.valueOf(conf.getConfID()) + "&top="
+ String.valueOf(topic.getTopicNumber()) + "&rnm=1");
return;
{ // we have an attachment to upload...display the "Upload Attachment" form
String target = "confdisp?sig=" + String.valueOf(sig.getSIGID()) + "&conf="
+ String.valueOf(conf.getConfID()) + "&top="
+ String.valueOf(topic.getTopicNumber()) + "&rnm=1";
content = new AttachmentForm(sig,conf,msg,target);
page_title = "Upload Attachment";
} // end if
else

View File

@@ -0,0 +1,118 @@
/*
* 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.format;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.htmlcheck.*;
import com.silverwrist.venice.core.*;
public class AttachmentForm implements JSPRender
{
/*--------------------------------------------------------------------------------
* Static data members
*--------------------------------------------------------------------------------
*/
// Attribute name for request attribute
protected static final String ATTR_NAME = "com.silverwrist.venice.content.AttachmentForm";
/*--------------------------------------------------------------------------------
* Attributes
*--------------------------------------------------------------------------------
*/
private int sigid;
private int confid;
private long postid;
private String target;
/*--------------------------------------------------------------------------------
* Constructor
*--------------------------------------------------------------------------------
*/
public AttachmentForm(SIGContext sig, ConferenceContext conf, TopicMessageContext msg, String target)
{
this.sigid = sig.getSIGID();
this.confid = conf.getConfID();
this.postid = msg.getPostID();
this.target = target;
} // end constructor
/*--------------------------------------------------------------------------------
* External static functions
*--------------------------------------------------------------------------------
*/
public static AttachmentForm retrieve(ServletRequest request)
{
return (AttachmentForm)(request.getAttribute(ATTR_NAME));
} // end retrieve
/*--------------------------------------------------------------------------------
* Implementations from interface JSPRender
*--------------------------------------------------------------------------------
*/
public void store(ServletRequest request)
{
request.setAttribute(ATTR_NAME,this);
} // end store
public String getTargetJSPName()
{
return "attach_form.jsp";
} // end getTargetJSPName
/*--------------------------------------------------------------------------------
* External operations
*--------------------------------------------------------------------------------
*/
public int getSIGID()
{
return sigid;
} // end getSIGID
public int getConfID()
{
return confid;
} // end getConfID
public long getPostID()
{
return postid;
} // end getPostID
public String getTarget()
{
return target;
} // end getTarget
} // end class AttachmentForm

View File

@@ -299,4 +299,25 @@ public class RenderData
} // end nullResponse
public void sendBinaryData(String type, String filename, int length, InputStream data) throws IOException
{
response.setContentType(type);
response.setContentLength(length);
if (filename!=null) // make sure we pass the filename along, too
response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\";");
// Copy the contents of the "data" stream to the output.
ServletOutputStream stm = response.getOutputStream();
byte[] buffer = new byte[4096];
int rd = data.read(buffer);
while (rd>=0)
{ // simple read-write loop to shove data out the door
if (rd>0)
stm.write(buffer,0,rd);
rd = data.read(buffer);
} // end while
} // end sendBinaryData
} // end class RenderData