I have a Servlet that accepts a file upload via POST, and then parses it
using Commons FileUpload (along with some form fields to proppatch), and
writes the resultant file to Slide.� Trouble is that for a test plain text
file, the Directory Listing shows a collection instead of a text file after
upload� with the same name� the URI is /files/errmsg.txt/ and you can
navigate into it like any other folder!� Any ideas why it isn�t putting a
text file like it should be?!� It worked once, and once only, so there is
the possibility that it could be breaking the upload nearly every time�?� 
�
The bizarre thing is that it prints TWO responses from the Servlet�� Firstly
�Problem with processing uploaded file type 1� and THEN �File accepted,
thankyou!� on the same line!� It looks like this:
�
Problem with processing uploaded file type 1File accepted, thankyou!
�
Now this could just be me being crap at programming� or it could be
something more odd� anyone who has an idea, please hit me with the feedback!
�
Also, (I am using JDBCDescriptorsStore) it seems to be neglecting to write
anything to the �revision�, �property� and �latestrevision� tables for the
uploaded �file�� although the one time that it did work, it did so!
�
Any ideas from seasoned (or otherwise) Sliders will be gratefully accepted�
�
stu
�
�
�
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.apache.commons.fileupload.*;
import org.apache.util.HttpURL;
import org.apache.webdav.lib.WebdavResource;
import org.apache.commons.httpclient.*;
import org.apache.slide.common.*;
import org.apache.slide.authenticate.*;
import org.apache.slide.content.*;
import org.apache.slide.lock.*;
import org.apache.slide.security.*;
import org.apache.slide.structure.*;
�
public class ReceiveUploadServlet extends HttpServlet {
����������� 
����������� /**
���� * HTTP Date format pattern (RFC 2068, 822, 1123).
���� */
��� public static final String DATE_FORMAT = "EEE, d MMM yyyy kk:mm:ss z";
��� 
��� /**
���� * Date formatter.
���� */
��� private static final DateFormat formatter =
������� new SimpleDateFormat(DATE_FORMAT);
����������� 
����������� private String fileName;
����������� private String destPath;
����������� private String fullFileName;
����������� private String destFileName;
����������� 
����������� private HttpSession session;
����������� 
����������� protected NamespaceAccessToken token;
����������� 
����������� private Hashtable props;
����������� 
����������� private WebdavResource wr;
����������������������� 
����������� 
����������� public void doPost(HttpServletRequest request, 
����������������������� HttpServletResponse response) throws IOException {
����������������������������������� 
����������������������� session = request.getSession();
����������������������� 
����������������������� String namespaceName;
����������������������� 
����������������������� props = new Hashtable(5);
����������������������� 
����������������������� if (token == null) {
����������������������������������� 
����������������������������������� namespaceName =
request.getContextPath();
����������� if ((namespaceName == null) || (namespaceName.equals("")))
�������������� �������� namespaceName = Domain.getDefaultNamespace();
����������� while (namespaceName.startsWith("/"))
�������������� �������� namespaceName = namespaceName.substring(1);
����������� token = Domain.accessNamespace
�������������� �������� (new SecurityToken(this), namespaceName);
��������������� ������� 
����������� //}
����������� //catch (UnavailableException ue) {
����������� ����������� 
����������� //��������� System.out.println("Namespace " + namespaceName +
������������� //� ����������������� �" not accessible");����������� ��������

����������� ����������� //ue.printStackTrace();
��� ����
������� ��������������� //if (token == null) {
����������� ����������� //��������� System.out.println(
����������� ����������� //��������������������� "Could not access namespace
" + namespaceName + ".");
����������� ����������� //��������� throw new
UnavailableException("Namespace " + namespaceName +
��������������� //����������������� " not accessible"); 
����������� //}
������� ��� 
������� }
������� 
��������� 
����������������������������������� 
����������������������� boolean isMultiPart =
FileUpload.isMultipartContent(request);
����������������������� 
����������������������������������������������� 
����������������������� //Create new file upload handler
����������������������� DiskFileUpload upload = new DiskFileUpload();
����������������������� 
����������������������� //Set upload parameters
����������������������� String fileStore = new String("C:/temp");
����������������������� upload.setRepositoryPath(fileStore);
����������������������� upload.setSizeThreshold(0);
����������������������� upload.setSizeMax(10000000);
����������������������� 
����������������������� //Parse the request
����������������������� 
����������������������� try {
����������������������������������� 
����������������������������������� List items =
upload.parseRequest(request);
����������������������������������� //Process the uploaded items
����������������������������������� Iterator iter = items.iterator();
����������������������� 
����������������������������������� while (iter.hasNext()) {
����������������������������������� 
����������������������������������������������� FileItem item =
(FileItem)iter.next();
����������������������������������� 
����������������������������������������������� if
(item.isFormField()&&!item.getFieldName().equals("upload")) {
����������������������������������������������������������� 
����������������������������������������������������������� try {
����������������������������������������������������������������������� 
�����������������������������������������������������������������������
processFormField(item);
����������������������������������������������������������������������� 
����������� ����������������������������������������������� }
����������������������������������������������������������� catch
(HttpException he) {
����������������������������������������������� 
�����������������������������������������������������������������������
System.out.println("Problem creating file properties");
�����������������������������������������������������������������������
he.printStackTrace();
����������������������������������������������������������� 
����������������������������������������������������������� }
����������������������������������������������� 
����������������������������������������������� }
����������������������������������������������� else {
����������������������������������������������������������� 
����������������������������������������������������������� try {
����������������������������������������������������������������������� 
�����������������������������������������������������������������������
destPath = (String)session.getAttribute(
����������������������������������������������������������������������������
������� "slideFileDest");
����������������������� 
�����������������������������������������������������������������������
fullFileName = item.getName();
�����������������������������������������������������������������������
fileName = fullFileName.substring(
����������������������������������������������������������������������������
������� fullFileName.lastIndexOf("\\")+1, 
����������������������������������������������������������������������������
������������������� fullFileName.length());
�����������������������������������������������������������������������
String destURI = new String(destPath+"/"+fileName);
����������������������������������������������������������������������������
������������������� 
�����������������������������������������������������������������������
processUploadFile(request, item, destURI);
����������������������� 
�����������������������������������������������������������������������
System.out.println("THE DESTINATION PATH = "+destPath);
�����������������������������������������������������������������������
System.out.println("The URI is... "+destURI);
����������������������� 
�����������������������������������������������������������������������
//try {
����������������������������������� 
�����������������������������������������������������������������������
//��������� WebdavResource wr = new WebdavResource(fileUrl);
����������������������������������� 
����������������������������������������������������������������������� //}
����������� �����������������������������������������������������������
//catch (HttpException he) {
����������������������������������� 
�����������������������������������������������������������������������
//��������� he.printStackTrace();
����������������������������������� 
����������������������������������������������������������������������� //}
����������������������� 
����������������������������������������������������������������������� 
����������������������������������������������������������������������� 
����������������������������������������������������������� }
����������������������������������������������������������� catch(Exception
e) {
����������������������������������������������������������������������� 
�����������������������������������������������������������������������
showResponseMessage(
����������������������������������������������������������������������������
������� "Problem with processing uploaded file type 1",
����������������������������������������������������������������������������
������������������� request, response);
�����������������������������������������������������������������������
e.printStackTrace();
����������������������������������������������������������������������������
������� 
����������������������������������������������������������� }
����������������������������������� 
����������������������������������������������� }
����������������������������������������������� 
����������������������������������� }
����������������������������������� 
����������������������������������� 
����������������������� }
����������������������� catch (FileUploadException fue) {
����������������������������������� 
����������������������������������� showResponseMessage("Problem with Upload
type 2...",
����������������������������������������������� request, response);
����������������������������������� fue.printStackTrace();
����������������������������������� 
����������������������� }
����������������������� 
����������������������� try {
����������������������������������� wr.proppatchMethod(props);
����������������������������������� wr.proppatchMethod("lastAccessedDate",
formatter.format(new Date()));
����������������������������������� System.out.println("PATCHED ALL
PROPERTIES");
����������������������������������������������� 
����������������������� }
����������������������� catch (HttpException he) {
����������������������������������������������� 
����������������������������������� System.out.println("Problem with
patching property Hashtable");
����������������������������������� he.printStackTrace();
����������������������������������������������� 
����������������������� }
����������������������� 
����������������������� showResponseMessage("File accepted, thankyou!", 
����������������������������������� request, response);
����������������������������������������������������������������������� 
����������������������������������� ����������� 
����������� }
����������� 
����������� public void processFormField(FileItem item) throws HttpException
{
����������������������� 
����������������������� 
����������������������� 
����������������������� String name = item.getFieldName();
����������������������� String value = item.getString();
����������������������� props.put(name, value);
����������������������� 
����������������������� System.out.println("Field content = "+name+"� value
= "+value);
����������������������� 
����������� }
����������� 
����������� 
����������� public void processUploadFile(HttpServletRequest request,
FileItem item, 
����������������������� String destURI) throws Exception {
����������������������� 
����������������������� System.out.println("Filename = "+fullFileName);
����������������������� String contentType = item.getContentType();
����������������������� System.out.println("Content Type = "+contentType);
����������������������� boolean isInMemory = item.isInMemory();
����������������������� System.out.println("In Memory? =
"+Boolean.toString(isInMemory));
����������������������� long sizeInBytes = item.getSize();
����������������������� System.out.println("Size in bytes =
"+Long.toString(sizeInBytes));
����������������������� 
����������������������� //File uploadedFile = new File(destURI);
����������������������� 
����������������������� Content content = token.getContentHelper();
������� Lock lock = token.getLockHelper();
������� Security security = token.getSecurityHelper();
������� Structure structure = token.getStructureHelper();
������� 
������� //String username = security.getPrincipal(slideToken).toString();
������� String username = new String("root");
������� String password = new String("root");
����������������������� 
����������������������� token.begin();
����������������������� 
����������������������� CredentialsToken credToken = new
CredentialsToken(new
����������������������������������� String(username));
������� SlideToken slideToken = new SlideTokenImpl(credToken);
����������������������� 
����������������������� SubjectNode subject = new SubjectNode();
������� 
������� //structure.create(slideToken, subject, destPath);
������� //content.create(slideToken, destURI, false);
��������������� 
������� NodeRevisionContent revisionContent = new NodeRevisionContent();
����������������������� //revisionContent.setContent( inputStream1 );
������� //content.create(
������� //� slideToken, destURI, (new NodeRevisionDescriptor()),
revisionContent );
����������������������� 
����������������������� HttpURL hu = new HttpURL(
����������������������������������� "http://localhost:8080/slide"+destURI);
����������������������� hu.setUserInfo(username, password);
������� wr = new WebdavResource(hu, WebdavResource.ALL, 1);
������� //WebdavFile wf = new WebdavFile(wr);
�
����������������������� //if (isInMemory) {
����������������������������������������������������������������������� 
����������������������������������� byte[] data = item.get();
����������������������������������� //item.write(uploadedFile);
����������������������������������� //InputStream uploadedStream =
item.getInputStream();
����������������������������������� //FileOutputStream fos = new
FileOutputStream(destURI);
����������������������������������� //fos.write(data);
�����������������������������������
//wr.putMethod(uploadedStream);����������������������������������� 
����������������������� //}
����������������������� //else {
����������������������������������� 
����������������������������������� //byte[] data = item.get();
����������������������������������� //InputStream uploadedStream =
item.getInputStream();
����������������������������������� //FileOutputStream fos = new
FileOutputStream(destURI);
����������������������������������� //fos.write(data);
����������������������������������� wr.putMethod(data);
����������������������������������� //content.create(slideToken, destURI,
false);
����������������������� //}
����������������������� token.commit();
����������� }
����������� 
����������� 
����������� public void showResponseMessage(String message,
HttpServletRequest req, 
����������������������� HttpServletResponse resp) throws IOException {
����������������������� 
����������������������� resp.setContentType("text/html");
����������������������� 
����������������������� //Get the PrintWriter for writing out the response
����������������������� PrintWriter out = resp.getWriter();
����������������������������������� 
����������������������� //Write the HTML back to the browser
����������������������� out.println("<HTML>");
����������������������� out.println("<BODY>");
����������������������� 
����������������������� String clientHost = req.getRemoteHost();
����������������������� out.println(message);
����������������������� 
����������������������� out.println("</HTML>");
����������������������� out.println("</BODY>");
����������������������� 
����������� }
����������� 
}

Reply via email to