Package com.caucho.vfs

Examples of com.caucho.vfs.MultipartStream


                            long uploadMax,
                            long fileUploadMax,
                            long lengthMax)
    throws IOException
  {
    MultipartStream ms = new MultipartStream(rawIs, boundary);
    ms.setEncoding(javaEncoding);
    ReadStream is;

    while ((is = ms.openRead()) != null) {
      String attr = (String) ms.getAttribute("content-disposition");

      if (attr == null || ! attr.startsWith("form-data")) {
        // XXX: is this an error?
        continue;
      }

      String name = getAttribute(attr, "name");
      String filename = getAttribute(attr, "filename");
      String contentType = getAttribute(attr, "content-type");
     
      if (contentType == null)
        contentType = ms.getAttribute("content-type");

      if (name == null) {
        // XXX: is this an error?
        continue;
      }
      else if (filename != null) {
        Path tempDir = CauchoSystem.getWorkPath().lookup("form");
        try {
          tempDir.mkdirs();
        } catch (IOException e) {
        }
        Path tempFile = tempDir.createTempFile("form", ".tmp");
        request.addCloseOnExit(tempFile);

        WriteStream os = tempFile.openWrite();

        TempBuffer tempBuffer = TempBuffer.allocate();
        byte []buf = tempBuffer.getBuffer();

        int totalLength = 0;

        try {
          int len;

          while ((len = is.read(buf, 0, buf.length)) > 0) {
            os.write(buf, 0, len);
            totalLength += len;
          }
        } finally {
          os.close();

          TempBuffer.free(tempBuffer);
          tempBuffer = null;
        }

        if (uploadMax > 0 && uploadMax < tempFile.getLength()) {
          String msg = L.l("multipart form data '{0}' too large",
                           "" + tempFile.getLength());
          request.setAttribute("caucho.multipart.form.error", msg);
          request.setAttribute("caucho.multipart.form.error.size",
                               new Long(tempFile.getLength()));
         
          tempFile.remove();
         
          throw new IOException(msg);
        } else if (fileUploadMax > 0 && fileUploadMax < tempFile.getLength()){
          String msg = L.l("multipart form data part '{0}':'{1}' is greater then the accepted value of '{2}'",
                           name, "" + tempFile.getLength(), fileUploadMax);

          tempFile.remove();

          throw new IllegalStateException(msg);
        }
        else if (tempFile.getLength() != totalLength) {
          String msg = L.l("multipart form upload failed (possibly due to full disk).");

          request.setAttribute("caucho.multipart.form.error", msg);
          request.setAttribute("caucho.multipart.form.error.size",
                               new Long(tempFile.getLength()));
         
          tempFile.remove();
         
          throw new IOException(msg);
        }

        // server/136u, server/136v, #2578
        if (table.get(name + ".filename") == null) {
          table.put(name, new String[] { tempFile.getNativePath() });
          table.put(name + ".file", new String[] { tempFile.getNativePath() });
          table.put(name + ".filename", new String[] { filename });
          table.put(name + ".content-type", new String[] { contentType });
        }
        else {
          addTable(table, name, tempFile.getNativePath());
          addTable(table, name + ".file", tempFile.getNativePath());
          addTable(table, name + ".filename", filename);
          addTable(table, name + ".content-type", contentType);
        }

      if (log.isLoggable(Level.FINE))
          log.fine("mp-file: " + name + "(filename:" + filename + ")");
      } else {
        CharBuffer value = new CharBuffer();
        int ch;
        long totalLength = 0;

        for (ch = is.readChar(); ch >= 0; ch = is.readChar()) {
          value.append((char) ch);
          totalLength++;
         
          if (lengthMax < totalLength) {
            String msg = L.l("multipart form upload failed because field '{0}' exceeds max length {1}",
                             name, lengthMax);

            request.setAttribute("caucho.multipart.form.error", msg);
            request.setAttribute("caucho.multipart.form.error.size",
                                 new Long(totalLength));
           
            throw new IOException(msg);
          }
        }
     
        if (log.isLoggable(Level.FINE))
          log.fine("mp-form: " + name + "=" + value);

        addTable(table, name, value.toString());
       
        if (contentType != null)
          addTable(table, name + ".content-type", contentType);
      }

      parts.add(request.createPart(name,
                                   new HashMap<String, List<String>>(ms.getHeaders())));
    }

    if (! ms.isComplete()) {
      throw new IOException("Incomplete form");
    }
  }
View Full Code Here


          env.warning(L.l("multipart/form-data POST is missing boundary"));
         
          return;
        }
       
        MultipartStream ms = new MultipartStream(rs, boundary);
       
        if (encoding != null)
          ms.setEncoding(encoding);

        readMultipartStream(env, ms, postArray, files,
                            addSlashesToValues, isAllowUploads);

        rs.close();
View Full Code Here

                            String javaEncoding,
                            long uploadMax,
                            long fileUploadMax)
    throws IOException
  {
    MultipartStream ms = new MultipartStream(rawIs, boundary);
    ms.setEncoding(javaEncoding);
    ReadStream is;

    while ((is = ms.openRead()) != null) {
      String attr = (String) ms.getAttribute("content-disposition");

      if (attr == null || ! attr.startsWith("form-data")) {
        // XXX: is this an error?
        continue;
      }

      String name = getAttribute(attr, "name");
      String filename = getAttribute(attr, "filename");

      if (name == null) {
        // XXX: is this an error?
        continue;
      }
      else if (filename != null) {
        String contentType = (String) ms.getAttribute("content-type");

        Path tempDir = CauchoSystem.getWorkPath().lookup("form");
        try {
          tempDir.mkdirs();
        } catch (IOException e) {
        }
        Path tempFile = tempDir.createTempFile("form", ".tmp");
        request.addCloseOnExit(tempFile);

        WriteStream os = tempFile.openWrite();

  TempBuffer tempBuffer = TempBuffer.allocate();
  byte []buf = tempBuffer.getBuffer();

  int totalLength = 0;

        try {
    int len;
   
    while ((len = is.read(buf, 0, buf.length)) > 0) {
      os.write(buf, 0, len);
      totalLength += len;
    }
        } finally {
          os.close();

    TempBuffer.free(tempBuffer);
          tempBuffer = null;
        }

        if (uploadMax > 0 && uploadMax < tempFile.getLength()) {
          String msg = L.l("multipart form data '{0}' too large",
                           "" + tempFile.getLength());
          request.setAttribute("caucho.multipart.form.error", msg);
          request.setAttribute("caucho.multipart.form.error.size",
             new Long(tempFile.getLength()));
         
          tempFile.remove();
         
          throw new IOException(msg);
        } else if (fileUploadMax > 0 && fileUploadMax < tempFile.getLength()){
          String msg = L.l("multipart form data part '{0}':'{1}' is greater then the accepted value of '{2}'",
                           name, "" + tempFile.getLength(), fileUploadMax);

          tempFile.remove();

          throw new IllegalStateException(msg);
        }
  else if (tempFile.getLength() != totalLength) {
          String msg = L.l("multipart form upload failed (possibly due to full disk).");
   
          request.setAttribute("caucho.multipart.form.error", msg);
          request.setAttribute("caucho.multipart.form.error.size",
             new Long(tempFile.getLength()));
         
          tempFile.remove();
         
          throw new IOException(msg);
  }

  // server/136u, server/136v, #2578
  if (table.get(name + ".filename") == null) {
    table.put(name, new String[] { tempFile.getNativePath() });
    table.put(name + ".file", new String[] { tempFile.getNativePath() });
    table.put(name + ".filename", new String[] { filename });
    table.put(name + ".content-type", new String[] { contentType });
  }
  else {
    addTable(table, name, tempFile.getNativePath());
    addTable(table, name + ".file", tempFile.getNativePath());
    addTable(table, name + ".filename", filename);
    addTable(table, name + ".content-type", contentType);
  }

      if (log.isLoggable(Level.FINE))
          log.fine("mp-file: " + name + "(filename:" + filename + ")");
      } else {
        CharBuffer value = new CharBuffer();
        int ch;

        for (ch = is.readChar(); ch >= 0; ch = is.readChar())
          value.append((char) ch);
     
        if (log.isLoggable(Level.FINE))
          log.fine("mp-form: " + name + "=" + value);

        addTable(table, name, value.toString());
      }

      parts.add(request.createPart(name,
                                   new HashMap<String, List<String>>(ms.getHeaders())));
    }

    if (! ms.isComplete()) {
      throw new IOException("Incomplete form");
    }
  }
View Full Code Here

          env.warning(L.l("multipart/form-data POST is missing boundary"));
         
          return;
        }
       
        MultipartStream ms = new MultipartStream(rs, boundary);
       
        if (encoding != null)
          ms.setEncoding(encoding);

        readMultipartStream(env, ms, postArray, files,
                            addSlashesToValues, isAllowUploads);

        rs.close();
View Full Code Here

        if (contentType != null
            && contentType.startsWith("multipart/form-data")) {
          String boundary = getBoundary(contentType);

          ReadStream rs = new ReadStream(new VfsStream(is, null));
          MultipartStream ms = new MultipartStream(rs, boundary);
         
          if (encoding != null)
            ms.setEncoding(encoding);

          readMultipartStream(env, ms, postArray, files, addSlashesToValues, encoding);

          rs.close();
        }
View Full Code Here

          env.warning(L.l("multipart/form-data POST is missing boundary"));
         
          return;
        }
       
        MultipartStream ms = new MultipartStream(rs, boundary);
       
        if (encoding != null)
          ms.setEncoding(encoding);

        readMultipartStream(env, ms, postArray, files,
                            addSlashesToValues, isAllowUploads);

        rs.close();
View Full Code Here

TOP

Related Classes of com.caucho.vfs.MultipartStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.