You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by ta...@apache.org on 2004/06/25 22:02:26 UTC

cvs commit: jakarta-jetspeed/src/java/org/apache/jetspeed/services/upload JetspeedUploadService.java

taylor      2004/06/25 13:02:26

  Added:       src/java/org/apache/jetspeed/services/upload
                        JetspeedUploadService.java
  Log:
  upload service contriibuted by Shinsuke Sugaya
  
  http://nagoya.apache.org/jira/browse/JS1-387
  
  CVS: ----------------------------------------------------------------------
  CVS: PR:
  CVS:   If this change addresses a PR in the problem report tracking
  CVS:   database, then enter the PR number(s) here.
  CVS: Obtained from:
  CVS:   If this change has been taken from another system, such as NCSA,
  CVS:   then name the system in this line, otherwise delete it.
  CVS: Submitted by:
  CVS:   If this code has been contributed to Apache by someone else; i.e.,
  CVS:   they sent us a patch or a new module, then include their name/email
  CVS:   address here. If this is your work then delete this line.
  CVS: Reviewed by:
  CVS:   If we are doing pre-commit code reviews and someone else has
  CVS:   reviewed your changes, include their name(s) here.
  CVS:   If you have not had it reviewed then delete this line.
  
  Revision  Changes    Path
  1.1                  jakarta-jetspeed/src/java/org/apache/jetspeed/services/upload/JetspeedUploadService.java
  
  Index: JetspeedUploadService.java
  ===================================================================
  /*
   * Copyright 2000-2001,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.jetspeed.services.upload;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.util.Map;
  
  import javax.servlet.http.HttpServletRequest;
  
  import org.apache.turbine.util.ParameterParser;
  import org.apache.turbine.util.TurbineException;
  import org.apache.turbine.util.upload.FileItem;
  import org.apache.turbine.services.upload.TurbineUploadService;
  import org.apache.turbine.services.upload.TurbineUpload;
  
  import org.apache.commons.fileupload.MultipartStream;
  
  import org.apache.jetspeed.om.registry.MediaTypeEntry;
  import org.apache.jetspeed.services.Registry;
  import org.apache.jetspeed.capability.CapabilityMap;
  import org.apache.jetspeed.capability.CapabilityMapFactory;
  import org.apache.jetspeed.services.resources.JetspeedResources;
  
  
  /**
   * <p> This class is an implementation of {@link 
   * org.apache.turbine.services.upload.UploadService}.
   *
   * <p> Files will be stored in temporary disk storage on in memory,
   * depending on request size, and will be available from the {@link
   * org.apache.turbine.util.ParameterParser} as {@link
   * org.apache.turbine.util.upload.FileItem}s.
   *
   * <p>This implementation of {@link 
   * org.apache.turbine.services.upload.UploadService} handles multiple
   * files per single html widget, sent using multipar/mixed encoding
   * type, as specified by RFC 1867.  Use {@link
   * org.apache.turbine.util.ParameterParser#getFileItems(String)} to
   * acquire an array of {@link
   * org.apache.turbine.util.upload.FileItem}s associated with given
   * html widget.
   *
   * @author <a href="mailto:shinsuke@yahoo.co.jp">Shinsuke SUGAYA</a>
   */
  public class JetspeedUploadService
      extends TurbineUploadService
  {
      /**
       * <p> Processes an <a href="http://rf.cx/rfc1867.html">RFC
       * 1867</a> compliant <code>multipart/form-data</code> stream.
       *
       * @param req The servlet request to be parsed.
       * @param params The ParameterParser instance to insert form
       * fields into.
       * @param path The location where the files should be stored.
       * @exception TurbineException If there are problems reading/parsing
       * the request or storing files.
       */
      public void parseRequest( HttpServletRequest req,
                                ParameterParser params,
                                String path )
          throws TurbineException
      {
          String contentType = req.getHeader(CONTENT_TYPE);
          if(!contentType.startsWith(MULTIPART_FORM_DATA))
          {
              throw new TurbineException("the request doesn't contain a " +
                  MULTIPART_FORM_DATA + " stream");
          }
          int requestSize = req.getContentLength();
          if(requestSize == -1)
          {
              throw new TurbineException("the request was rejected because " +
                  "it's size is unknown");
          }
          if(requestSize > TurbineUpload.getSizeMax())
          {
              throw new TurbineException("the request was rejected because " +
                  "it's size exceeds allowed range");
          }
  
          // get encoding info
          String encoding = JetspeedResources.getString(JetspeedResources.CONTENT_ENCODING_KEY,"US-ASCII");
          CapabilityMap cm = CapabilityMapFactory.getCapabilityMap( 
          req.getHeader("User-Agent") );
          String mimeCode = cm.getPreferredType().getCode();
          if ( mimeCode != null )
          {
              MediaTypeEntry media = (MediaTypeEntry)Registry.getEntry(Registry.MEDIA_TYPE, mimeCode);
              if ( media != null && media.getCharacterSet() != null)
              {
                  encoding = media.getCharacterSet();
              }
          }
  
          try
          {
              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
                                         + " request failed", e);
          }
  
      }
  
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jetspeed-dev-help@jakarta.apache.org