Examples of MultipartStream


Examples of com.alu.e3.gateway.common.camel.converter.stream.MultipartStream

                 * This is just workaround. The final solution will be provided by OAPEEE-294.
                 */
                if (LOG.isDebugEnabled()) {
                  LOG.debug("MultipartStream used");
                }
                return new MultipartStream(is, contentType);
              } else {
                if (LOG.isDebugEnabled()) {
                  LOG.debug("CachedOutputStream used");
                }
                CachedOutputStream cos = new CachedOutputStream(exchange);
View Full Code Here

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

Examples of com.caucho.vfs.MultipartStream

          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

Examples of com.caucho.vfs.MultipartStream

                            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

Examples of com.caucho.vfs.MultipartStream

          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

Examples of com.caucho.vfs.MultipartStream

        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

Examples of com.caucho.vfs.MultipartStream

          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

Examples of org.apache.commons.fileupload.MultipartStream

            throw new IOException("the request was rejected because no multipart boundary was found");
        }
        byte[] boundary = contentType.substring(boundaryIndex + 9).getBytes();

        ByteArrayInputStream input = new ByteArrayInputStream(body);
        MultipartStream multi = new MultipartStream(input, boundary);

        boolean nextPart = multi.skipPreamble();
        while (nextPart) {
            try {
                output = new ByteArrayOutputStream();
                multi.readBodyData(output);
                outputArray = output.toByteArray();
                multiPartBuffer = new StringBuffer(50);
                isFile = false;
                File jarFileInTempDir;
                j = 0;

                for (int i = 0; i < outputArray.length; i++) {
                    //first check for \r\n end of line
                    if (outputArray[i] == 13 && outputArray[i + 1] == 10) {
                        //we've come to the end of a line
                        headerMap = parseMultiPartHeader(multiPartBuffer);
                        if (headerMap.get(NAME) != null) {
                            fileName = (String) headerMap.get(NAME);
                        }

                        //add the filename if there is one
                        if (fileName != null && headerMap.get(FILENAME) != null) {
                            this.formParams.put(fileName, headerMap.get(FILENAME));
                            isFile = true;
                        }

                        if (outputArray[i + 2] == 13 && outputArray[i + 3] == 10) {
                            //we've reached the blank line
                            i+=4;
                            j = i;
                            break;
                        } else {
                            i++;
                        }

                        multiPartBuffer = new StringBuffer(50);
                    } else {
                        multiPartBuffer.append((char) outputArray[i]);
                    }
                }

                //here we know that we have a file and that we need to write it
                if (isFile) {
                    //create file
                    jarFileInTempDir = new File((String) this.formParams.get(fileName));
                    if (!jarFileInTempDir.exists()) {
                        jarFileInTempDir.createNewFile();
                    }

                    //write the byte array to the file
                    fos = new FileOutputStream(jarFileInTempDir);
                    fos.write(outputArray, j, outputArray.length-j);
                    fos.close();
                } else { //form data, not a file
                    multiPartBuffer = new StringBuffer(outputArray.length-j);
                    for (int i = j; i < outputArray.length; i++) {
                        multiPartBuffer.append((char)outputArray[i]);
                    }

                    this.formParams.put(
                        fileName,
                        multiPartBuffer.toString());
                }

                nextPart = multi.readBoundary();
            } catch (MultipartStream.MalformedStreamException mse) {
                throw new IOException(mse.getMessage());
            }
        }
    }
View Full Code Here

Examples of org.apache.commons.fileupload.MultipartStream

        {
            byte[] boundary = contentType.substring(
                                contentType.indexOf("boundary=")+9).getBytes();
            InputStream input = (InputStream)req.getInputStream();

            MultipartStream multi = new MultipartStream(input, boundary);
            multi.setHeaderEncoding(encoding);
            boolean nextPart = multi.skipPreamble();
            while(nextPart)
            {
                Map headers = parseHeaders(multi.readHeaders());
                String fieldName = getFieldName(headers);
                if (fieldName != null)
                {
                    String subContentType = getHeader(headers, CONTENT_TYPE);
                    if (subContentType != null && subContentType
                                                .startsWith(MULTIPART_MIXED))
                    {
                        // Multiple files.
                        byte[] subBoundary =
                            subContentType.substring(
                                subContentType
                                .indexOf("boundary=")+9).getBytes();
                        multi.setBoundary(subBoundary);
                        boolean nextSubPart = multi.skipPreamble();
                        while (nextSubPart)
                        {
                            headers = parseHeaders(multi.readHeaders());
                            if (getFileName(headers) != null)
                            {
                                FileItem item = createItem(path, headers,
                                                           requestSize);
                                OutputStream os = item.getOutputStream();
                                try
                                {
                                    multi.readBodyData(os);
                                }
                                finally
                                {
                                    os.close();
                                }
                                params.append(getFieldName(headers), item);
                            }
                            else
                            {
                                // Ignore anything but files inside
                                // multipart/mixed.
                                multi.discardBodyData();
                            }
                            nextSubPart = multi.readBoundary();
                        }
                        multi.setBoundary(boundary);
                    }
                    else
                    {
                        if (getFileName(headers) != null)
                        {
                            // A single file.
                            FileItem item = createItem(path, headers,
                                                       requestSize);
                            OutputStream os = item.getOutputStream();
                            try
                            {
                                multi.readBodyData(os);
                            }
                            finally
                            {
                                os.close();
                            }
                            params.append(getFieldName(headers), item);
                        }
                        else
                        {
                            // A form field.
                            FileItem item = createItem(path, headers,
                                                       requestSize);
                            OutputStream os = item.getOutputStream();
                            try
                            {
                                multi.readBodyData(os);
                            }
                            finally
                            {
                                os.close();
                            }
                            params.append(getFieldName(headers),
                                          new String(item.get()));
                        }
                    }
                }
                else
                {
                    // Skip this part.
                    multi.discardBodyData();
                }
                nextPart = multi.readBoundary();
            }
        }
        catch(IOException e)
        {
            throw new TurbineException("Processing of " + MULTIPART_FORM_DATA
View Full Code Here

Examples of org.apache.commons.fileupload.MultipartStream

            throw new IOException("the request was rejected because no multipart boundary was found");
        }
        byte[] boundary = contentType.substring(boundaryIndex + 9).getBytes();

        ByteArrayInputStream input = new ByteArrayInputStream(body);
        MultipartStream multi = new MultipartStream(input, boundary);

        boolean nextPart = multi.skipPreamble();
        while (nextPart) {
            try {
                output = new ByteArrayOutputStream();
                multi.readBodyData(output);
                outputArray = output.toByteArray();
                multiPartBuffer = new StringBuffer(50);
                isFile = false;
                File jarFileInTempDir;
                j = 0;

                for (int i = 0; i < outputArray.length; i++) {
                    //first check for \r\n end of line
                    if (outputArray[i] == 13 && outputArray[i + 1] == 10) {
                        //we've come to the end of a line
                        headerMap = parseMultiPartHeader(multiPartBuffer);
                        if (headerMap.get(NAME) != null) {
                            fileName = (String) headerMap.get(NAME);
                        }

                        //add the filename if there is one
                        if (fileName != null && headerMap.get(FILENAME) != null) {
                            this.formParams.put(fileName, headerMap.get(FILENAME));
                            isFile = true;
                        }

                        if (outputArray[i + 2] == 13 && outputArray[i + 3] == 10) {
                            //we've reached the blank line
                            i+=4;
                            j = i;
                            break;
                        } else {
                            i++;
                        }

                        multiPartBuffer = new StringBuffer(50);
                    } else {
                        multiPartBuffer.append((char) outputArray[i]);
                    }
                }

                //here we know that we have a file and that we need to write it
                if (isFile) {
                    //create file
                    jarFileInTempDir = new File((String) this.formParams.get(fileName));
                    if (!jarFileInTempDir.exists()) {
                        jarFileInTempDir.createNewFile();
                    }

                    //write the byte array to the file
                    fos = new FileOutputStream(jarFileInTempDir);
                    fos.write(outputArray, j, outputArray.length-j);
                    fos.close();
                } else { //form data, not a file
                    multiPartBuffer = new StringBuffer(outputArray.length-j);
                    for (int i = j; i < outputArray.length; i++) {
                        multiPartBuffer.append((char)outputArray[i]);
                    }

                    this.formParams.put(
                        fileName,
                        multiPartBuffer.toString());
                }

                nextPart = multi.readBoundary();
            } catch (MultipartStream.MalformedStreamException mse) {
                throw new IOException(mse.getMessage());
            }
        }
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.