Annotation of upload-applet/RWServlet.java, revision 1.1
1.1 ! rogo 1: import java.io.*;
! 2: import java.util.*;
! 3:
! 4: import javax.servlet.*;
! 5: import javax.servlet.http.*;
! 6:
! 7: import sun.security.x509.*;
! 8: import java.security.*;
! 9: /**
! 10: * Read/Write Servlet
! 11: *
! 12: *
! 13: * @version 0.85 , 30/04/2003
! 14: * @author Robert Gordesch
! 15: */
! 16: public class RWServlet extends HttpServlet
! 17: {
! 18:
! 19: String md5 = "";
! 20: static String basePath=null;
! 21:
! 22: public void init(ServletConfig config) throws ServletException
! 23: {
! 24: basePath=config.getInitParameter("basePath");
! 25: System.out.println("path"+basePath);
! 26: }
! 27: public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
! 28: {
! 29: Enumeration header = req.getHeaderNames();
! 30: while(header.hasMoreElements())
! 31: System.out.println(header.nextElement());
! 32: System.out.println(req.getCookies()[0].getValue());
! 33: /*
! 34: //value chosen to limit denial of service
! 35: if (req.getContentLength() > 8 * 1024)
! 36: {
! 37: res.setContentType("text/html");
! 38: ServletOutputStream out = res.getOutputStream();
! 39: out.println("<html><head><title>Too big</title></head>");
! 40: out.println("<body><h1>Error - content length >8k not ");
! 41: out.println("</h1></body></html>");
! 42: } else
! 43: {
! 44: doGet(req, res);
! 45: }*/
! 46: }
! 47:
! 48: public void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
! 49: {
! 50: ServletOutputStream out = res.getOutputStream();
! 51: ServletInputStream in = req.getInputStream();
! 52: String mimeType = "text/plain";
! 53:
! 54: /* if(req.getContentLength()==-1)
! 55: {
! 56: res.setContentType(mimeType);
! 57: res.sendError(res.SC_BAD_REQUEST,"You must specifiy Content-Length!");
! 58: out.close();in.close();}*/
! 59: //else { res.setContentType(mimeType);
! 60: // res.setStatus(res.SC_CREATED);
! 61: // }
! 62: String query = req.getQueryString();
! 63: String id = "", count = "", numFiles = "", cMD5 = "";
! 64: String mode = "";
! 65: try
! 66: {
! 67: if (query != null)
! 68: {
! 69: // System.out.println(query);
! 70: // System.out.println("Query parameter got" + req.getParameter("id") + " count" + req.getParameter("count") + " nf" + req.getParameter("nf") + " md5 " + req.getParameter("md5"));
! 71: id = req.getParameter("id");
! 72: count = req.getParameter("count");
! 73: numFiles = req.getParameter("nf");
! 74: cMD5 = req.getParameter("md5");
! 75: mode = req.getParameter("mode");
! 76: }
! 77: String fileToPut = (basePath!=null) ? basePath+"/"+req.getPathInfo():req.getPathTranslated();
! 78: System.out.println("fileToWrite " + fileToPut);
! 79: /*
! 80: if (exists(fileToPut, id) && !mode.equals("update") && !mode.equals("replace"))
! 81: {
! 82: res.setContentType(mimeType);
! 83: res.setStatus(res.SC_CONFLICT,"Conflict");
! 84: //res.sendError(HttpServletResponse.SC_CONFLICT, "Conflict");
! 85: // in.close();
! 86: //out.close();
! 87: System.out.println("why here ? " );
! 88: return;
! 89: }
! 90: */
! 91:
! 92: // creates the Directorystructure
! 93: new File(fileToPut.substring(0, fileToPut.lastIndexOf(File.separator))).mkdirs();
! 94: // System.out.println("length"+req.getContentLength());
! 95: FileOutputStream outFile = new FileOutputStream(fileToPut);
! 96: cMD5 = receiveFile(outFile, in, out, req.getContentLength());
! 97: if (md5.equals(cMD5))
! 98: {
! 99: res.setHeader("MD5", md5);
! 100: res.setContentType(mimeType);
! 101: res.setStatus(res.SC_CREATED);
! 102: // all files uploaded?
! 103: if (Integer.parseInt(count) == Integer.parseInt(numFiles))// && md5.equals(cMD5))
! 104: {
! 105: int index = fileToPut.lastIndexOf(File.separator) + 1;
! 106: String source = fileToPut.substring(0, index);
! 107: String fileName = fileToPut.substring(index);
! 108: index = source.indexOf(id);
! 109: int index2 = index + id.length() + 1;
! 110: int index3 = source.indexOf(File.separator, index2);
! 111: String dest = (index3 > 0) ? source.substring(0, index) + source.substring(index2, index3) : source.substring(0, index) + fileName;
! 112: if (index3 < 0)
! 113: source += fileName;
! 114: else
! 115: source = source.substring(0, index3);
! 116: // System.out.println(source);
! 117:
! 118: // System.out.println(dest);
! 119: File f = new File(source);
! 120: if (mode.equals("replace"))
! 121: {
! 122: File origFile = new File(dest);
! 123: GregorianCalendar calendar = new GregorianCalendar();
! 124: File saveFile = new File(dest + "_" + calendar.get(calendar.DAY_OF_MONTH) + "." + (calendar.get(calendar.MONTH) + 1) + "." + calendar.get(calendar.YEAR) + "_" + calendar.get(calendar.HOUR_OF_DAY) + "." + calendar.get(calendar.MINUTE) + "." + calendar.get(calendar.SECOND));
! 125: origFile.renameTo(saveFile);
! 126: // System.out.println(saveFile+" replace");
! 127: // System.out.println(origFile);
! 128:
! 129: }
! 130: f.renameTo(new File(dest));
! 131:
! 132: new File(source.substring(0, index2)).delete();
! 133: }
! 134: in.close();
! 135: out.close();
! 136: } else
! 137: {
! 138: System.out.println("Got an error "+cMD5+" "+md5);
! 139: res.setContentType(mimeType);
! 140: res.setStatus(res.SC_NOT_ACCEPTABLE, " Checksums not identical!");
! 141: res.setHeader("MD5", md5);
! 142:
! 143: //res.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE, " Checksums not identical!");
! 144: // out.println("\r\n");
! 145: // new File(fileToPut).delete();
! 146: in.close();
! 147: out.close();
! 148: }
! 149:
! 150: // outFile.close();
! 151: // out.close();
! 152: } catch (FileNotFoundException fnf)
! 153: {
! 154:
! 155: res.sendError(HttpServletResponse.SC_NOT_FOUND, " Not Found!");
! 156: fnf.printStackTrace();
! 157: } catch (IOException ioe)
! 158: {
! 159: System.out.println("IOException: Unknown file length: " + ioe);
! 160: res.sendError(HttpServletResponse.SC_BAD_REQUEST, "File length!");
! 161: } catch (Exception e5)
! 162: {
! 163: System.out.println(e5);
! 164: e5.printStackTrace();
! 165: }
! 166: // } // to else
! 167:
! 168: }
! 169: public void doHead(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
! 170: {
! 171: System.out.println("Called head");
! 172: ServletOutputStream out = res.getOutputStream();
! 173: ServletInputStream in = req.getInputStream();
! 174: String mimeType = "text/plain";
! 175:
! 176: /* if(req.getContentLength()==-1)
! 177: {
! 178: res.setContentType(mimeType);
! 179: res.sendError(res.SC_BAD_REQUEST,"You must specifiy Content-Length!");
! 180: out.close();in.close();}*/
! 181: //else { res.setContentType(mimeType);
! 182: // res.setStatus(res.SC_CREATED);
! 183: // }
! 184: String query = req.getQueryString();
! 185: String id = "", count = "", numFiles = "", cMD5 = "";
! 186: String mode = "";
! 187: try
! 188: {
! 189: if (query != null)
! 190: {
! 191: // System.out.println(query);
! 192: // System.out.println("Query parameter got" + req.getParameter("id") + " count" + req.getParameter("count") + " nf" + req.getParameter("nf") + " md5 " + req.getParameter("md5"));
! 193: System.out.println("header ist "+req.getHeader("Connection"));
! 194: id = req.getParameter("id");
! 195: count = req.getParameter("count");
! 196: numFiles = req.getParameter("nf");
! 197: cMD5 = req.getParameter("md5");
! 198: mode = req.getParameter("mode");
! 199: }
! 200: String fileToPut = (basePath!=null) ? basePath+"/"+req.getPathInfo():req.getPathTranslated();
! 201: System.out.println("fileToWrite " + fileToPut);
! 202: if (new File(fileToPut).exists())// && !mode.equals("update") && !mode.equals("replace"))
! 203: {
! 204: res.setHeader("Connection", "keep-alive");
! 205: res.setContentType(mimeType);
! 206: res.setStatus(res.SC_OK);
! 207: //res.sendError(HttpServletResponse.SC_CONFLICT, "Conflict");
! 208: // in.close();
! 209: //out.close();
! 210: return;
! 211: } else
! 212: {
! 213: res.setHeader("Connection", "keep-alive");
! 214: res.setContentType(mimeType);
! 215: res.setStatus(res.SC_ACCEPTED);
! 216: }
! 217: } catch(Exception e) { System.out.println(e);}
! 218: }
! 219: public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
! 220: {
! 221: ServletOutputStream out = res.getOutputStream();
! 222: ServletInputStream in = req.getInputStream();
! 223: // start
! 224: String mimeType = "text/html";
! 225: String fileToGet = req.getPathTranslated(); //req.getPathInfo();
! 226: String query = req.getQueryString();
! 227: System.out.println(req.getQueryString());
! 228: try
! 229: {
! 230: if (query != null)
! 231: {
! 232: System.out.println("Query parameter got" + req.getParameter("baseURL") + " " + req.getContextPath() + req.getServletPath());
! 233: String baseURL = req.getParameter("baseURL");
! 234: res.setContentType(mimeType);
! 235: /* Cookie cookie = new Cookie("Rogo", "idiot");
! 236: cookie.setPath("http://localhost:8080");
! 237:
! 238: res.addCookie(cookie);
! 239: */
! 240:
! 241: out.println("<APPLET");
! 242:
! 243: // out.println("CODEBASE = \".\"");
! 244: out.println("CODE = \"ServerWriter.class\"");
! 245: out.println("ARCHIVE=\"upload.jar\"");
! 246: out.println("NAME = \"Upload Applet\"");
! 247: out.println(" WIDTH = 160");
! 248:
! 249: out.println(" HEIGHT = 25");
! 250:
! 251: out.println(" HSPACE = 0 ");
! 252:
! 253: out.println(" VSPACE = 0 ");
! 254:
! 255: out.println(" ALIGN = middle>");
! 256: out.println(" <PARAM NAME=\"baseURL\" VALUE=" + baseURL + ">");
! 257: out.println(" <PARAM NAME=\"servletURL\" VALUE=\"" + req.getContextPath() + req.getServletPath() + "\">");
! 258:
! 259: StringTokenizer querys = new StringTokenizer(query, "+");
! 260: out.println("</APPLET>");
! 261: }
! 262:
! 263: if (query == null)
! 264: {
! 265: File f = new File(fileToGet);
! 266: if (f.isDirectory())
! 267: {
! 268: int i = 0;
! 269: ByteArrayOutputStream tempfile = new ByteArrayOutputStream(1024);
! 270: try
! 271: {
! 272:
! 273: while (i <= f.list().length - 1)
! 274: {
! 275: tempfile.write(f.list()[i].getBytes());
! 276: tempfile.write('\n');
! 277: ++i;
! 278: } // to while
! 279:
! 280: } catch (IOException ioe)
! 281: {
! 282: System.out.println(ioe);
! 283: }
! 284: res.setHeader("Content-Length", new Integer(tempfile.size()).toString());
! 285: res.setContentType(mimeType);
! 286: tempfile.writeTo(out);
! 287: } else
! 288: {
! 289: FileInputStream inFile = new FileInputStream(fileToGet);
! 290: if (fileToGet.endsWith(".html") || fileToGet.endsWith(".htm"))
! 291: mimeType = "text/html";
! 292: res.setHeader("Content-Length", new Integer(inFile.available()).toString());
! 293: res.setContentType(mimeType);
! 294: sendFile(inFile, out, in);
! 295: inFile.close();
! 296: }
! 297: } // to sec if
! 298: } catch (FileNotFoundException fnf)
! 299: {
! 300: res.sendError(HttpServletResponse.SC_NOT_FOUND, " Not Found!");
! 301:
! 302: } catch (IOException ioe)
! 303: {
! 304: System.out.println("IOException: Unknown file length: " + ioe);
! 305: res.sendError(HttpServletResponse.SC_BAD_REQUEST, " Bad Request");
! 306: }
! 307:
! 308: // end
! 309:
! 310: }
! 311:
! 312: public String getServletInfo()
! 313: {
! 314: return "A Servlet that reads data from and writes data to the server\n" + "If the requested data is a directory it sends the list of files in the directory back";
! 315: }
! 316: public void sendFile(FileInputStream inFile, ServletOutputStream outbound, ServletInputStream inbound)
! 317: {
! 318:
! 319: try
! 320: {
! 321:
! 322: byte dataBody[] = new byte[1024];
! 323: int cnt;
! 324: while ((cnt = inFile.read(dataBody)) != -1)
! 325: {
! 326: outbound.write(dataBody, 0, cnt);
! 327: }
! 328: } catch (IOException ioe)
! 329: {
! 330: System.out.println("IOException while sending file: " + ioe);
! 331: }
! 332: try
! 333: {
! 334: // Cleanup
! 335: outbound.close();
! 336: inbound.close();
! 337: } catch (IOException ioe)
! 338: {
! 339: System.out.println("IOException closing streams in sendFile: " + ioe);
! 340: }
! 341: }
! 342: public String receiveFile(FileOutputStream outFile, ServletInputStream inbound, ServletOutputStream outbound, int length)
! 343: {
! 344: StringBuffer buf=new StringBuffer();
! 345: try
! 346: {
! 347: length-=32;
! 348: System.out.println("File size " +length+" available "+inbound.available());
! 349: byte[] b = new byte[512];
! 350: int readCount = 0;
! 351: int available = 0;
! 352: int bufSize = b.length;
! 353: int readSize = b.length;
! 354:
! 355: MessageDigest md = MessageDigest.getInstance("MD5");
! 356:
! 357: while (readCount < length)
! 358: {
! 359: available=inbound.available();
! 360:
! 361: if(available>=bufSize)
! 362: readSize=bufSize;
! 363: else
! 364: readSize=available;
! 365:
! 366: if(readCount + readSize > length)
! 367: {
! 368: inbound.read(b, 0, length - readCount); // mout.flush();
! 369: md.update(b, 0, length-readCount);
! 370: outFile.write(b,0, length - readCount);
! 371:
! 372: }
! 373: else
! 374: {
! 375: inbound.read(b,0,readSize);
! 376: md.update(b, 0, readSize);
! 377: outFile.write(b,0,readSize);
! 378:
! 379: }
! 380:
! 381: readCount += readSize;
! 382: //System.out.println("rock " + readCount);
! 383:
! 384: }
! 385: int c=0;
! 386: System.out.println("rock ");
! 387:
! 388: while((c=inbound.read())!=-1)
! 389: {
! 390: System.out.print((char)c);
! 391: buf.append((char)c);
! 392: }
! 393: md5 = getMD5(md);
! 394: System.out.println(md5);
! 395:
! 396: // System.out.println(cnt);
! 397: // one byte version
! 398: /* int cnt = 0;
! 399: // System.out.println(cnt);
! 400: while (cnt < size)
! 401: {
! 402: outFile.write(inbound.read());
! 403: ++cnt;
! 404: }*/
! 405:
! 406: } catch (IOException ioe)
! 407: {
! 408: System.out.println("IOException while receiving file: " + ioe);
! 409: }
! 410: catch (Exception e6)
! 411: {
! 412: System.out.println("error while getting MD5 "+e6);
! 413: }
! 414:
! 415: try
! 416: {
! 417: // Cleanup
! 418: outFile.flush();
! 419: outFile.close();
! 420: // outbound.close();
! 421: // inbound.close();
! 422:
! 423: } catch (IOException ioe)
! 424: {
! 425: System.out.println("IOException closing streams in receiveFile: " + ioe);
! 426: }
! 427: return buf.toString();
! 428: }
! 429: public static String getMD5(MessageDigest md) throws Exception
! 430: {
! 431: System.out.println("digest");
! 432: byte[] bytes = md.digest();
! 433: StringBuffer sb = new StringBuffer();
! 434: int decValue;
! 435: for (int i = 0; i < bytes.length; i++)
! 436: {
! 437: String hexVal = Integer.toHexString(bytes[i] & 0xFF);
! 438: if (hexVal.length() == 1)
! 439: hexVal = "0" + hexVal; // put a leading zero
! 440: sb.append(hexVal);
! 441: }
! 442: return sb.toString();
! 443: }
! 444: public static String getSHA(byte[] b) throws Exception
! 445: {
! 446: MessageDigest md = MessageDigest.getInstance("sha1");
! 447: md.update(b, 0, b.length);
! 448: //System.out.println("digest");
! 449: byte[] bytes = md.digest();
! 450: StringBuffer sb = new StringBuffer();
! 451: int decValue;
! 452: for (int i = 0; i < bytes.length; i++)
! 453: {
! 454: String hexVal = Integer.toHexString(bytes[i] & 0xFF);
! 455: if (hexVal.length() == 1)
! 456: hexVal = "0" + hexVal; // put a leading zero
! 457: sb.append(hexVal);
! 458: }
! 459:
! 460: return sb.toString();
! 461: }
! 462: public boolean exists(String fileToPut, String id)
! 463: {
! 464: int index = fileToPut.lastIndexOf(File.separator) + 1;
! 465: String source = fileToPut.substring(0, index);
! 466: String fileName = fileToPut.substring(index);
! 467: index = source.indexOf(id);
! 468: int index2 = index + id.length() + 1;
! 469: int index3 = source.indexOf(File.separator, index2);
! 470: String dest = (index3 > 0) ? source.substring(0, index) + source.substring(index2, index3) : source.substring(0, index) + fileName;
! 471: if (index3 < 0)
! 472: source += fileName;
! 473: else
! 474: source = source.substring(0, index3);
! 475: //System.out.println(source);
! 476:
! 477: // System.out.println(dest);
! 478: File f = new File(dest);
! 479: return f.exists();
! 480: }
! 481: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>