Esempio n. 1
0
 public static void attributeArray_setitem(
     SWIGTYPE_p_p_InternalAttribute ary, int index, InternalAttribute value) {
   sqlitedriverJNI.attributeArray_setitem(
       SWIGTYPE_p_p_InternalAttribute.getCPtr(ary),
       index,
       InternalAttribute.getCPtr(value),
       value);
 }
 /**
  * Finalize the request by preparing the Header in the request and returns the request ready to be
  * sent.<br>
  * Once finalized, no data must be added.<br>
  * If the request does not need chunk (isChunked() == false), this request is the only object to
  * send to the remote server.
  *
  * @return the request object (chunked or not according to size of body)
  * @throws ErrorDataEncoderException if the encoding is in error or if the finalize were already
  *     done
  */
 public HttpRequest finalizeRequest() throws ErrorDataEncoderException {
   // Finalize the multipartHttpDatas
   if (!headerFinalized) {
     if (isMultipart) {
       InternalAttribute internal = new InternalAttribute();
       if (duringMixedMode) {
         internal.addValue("\r\n--" + multipartMixedBoundary + "--");
       }
       internal.addValue("\r\n--" + multipartDataBoundary + "--\r\n");
       multipartHttpDatas.add(internal);
       multipartMixedBoundary = null;
       currentFileUpload = null;
       duringMixedMode = false;
       globalBodySize += internal.size();
     }
     headerFinalized = true;
   } else {
     throw new ErrorDataEncoderException("Header already encoded");
   }
   List<String> contentTypes = request.getHeaders(HttpHeaders.Names.CONTENT_TYPE);
   List<String> transferEncoding = request.getHeaders(HttpHeaders.Names.TRANSFER_ENCODING);
   if (contentTypes != null) {
     request.removeHeader(HttpHeaders.Names.CONTENT_TYPE);
     for (String contentType : contentTypes) {
       // "multipart/form-data; boundary=--89421926422648"
       if (contentType.toLowerCase().startsWith(HttpHeaders.Values.MULTIPART_FORM_DATA)) {
         // ignore
       } else if (contentType
           .toLowerCase()
           .startsWith(HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED)) {
         // ignore
       } else {
         request.addHeader(HttpHeaders.Names.CONTENT_TYPE, contentType);
       }
     }
   }
   if (isMultipart) {
     String value =
         HttpHeaders.Values.MULTIPART_FORM_DATA
             + "; "
             + HttpHeaders.Values.BOUNDARY
             + "="
             + multipartDataBoundary;
     request.addHeader(HttpHeaders.Names.CONTENT_TYPE, value);
   } else {
     // Not multipart
     request.addHeader(
         HttpHeaders.Names.CONTENT_TYPE, HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED);
   }
   // Now consider size for chunk or not
   long realSize = globalBodySize;
   if (isMultipart) {
     iterator = multipartHttpDatas.listIterator();
   } else {
     realSize -= 1; // last '&' removed
     iterator = multipartHttpDatas.listIterator();
   }
   request.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(realSize));
   if (realSize > HttpPostBodyUtil.chunkSize) {
     isChunked = true;
     if (transferEncoding != null) {
       request.removeHeader(HttpHeaders.Names.TRANSFER_ENCODING);
       for (String v : transferEncoding) {
         if (v.equalsIgnoreCase(HttpHeaders.Values.CHUNKED)) {
           // ignore
         } else {
           request.addHeader(HttpHeaders.Names.TRANSFER_ENCODING, v);
         }
       }
     }
     request.addHeader(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
     request.setContent(ChannelBuffers.EMPTY_BUFFER);
   } else {
     // get the only one body and set it to the request
     HttpChunk chunk = nextChunk();
     request.setContent(chunk.getContent());
   }
   return request;
 }
  /**
   * Add the InterfaceHttpData to the Body list
   *
   * @param data
   * @throws NullPointerException for data
   * @throws ErrorDataEncoderException if the encoding is in error or if the finalize were already
   *     done
   */
  public void addBodyHttpData(InterfaceHttpData data) throws ErrorDataEncoderException {
    if (headerFinalized) {
      throw new ErrorDataEncoderException("Cannot add value once finalized");
    }
    if (data == null) {
      throw new NullPointerException("data");
    }
    bodyListDatas.add(data);
    if (!isMultipart) {
      if (data instanceof Attribute) {
        Attribute attribute = (Attribute) data;
        try {
          // name=value& with encoded name and attribute
          String key = encodeAttribute(attribute.getName(), charset);
          String value = encodeAttribute(attribute.getValue(), charset);
          Attribute newattribute = factory.createAttribute(request, key, value);
          multipartHttpDatas.add(newattribute);
          globalBodySize += newattribute.getName().length() + 1 + newattribute.length() + 1;
        } catch (IOException e) {
          throw new ErrorDataEncoderException(e);
        }
      } else if (data instanceof FileUpload) {
        // since not Multipart, only name=filename => Attribute
        FileUpload fileUpload = (FileUpload) data;
        // name=filename& with encoded name and filename
        String key = encodeAttribute(fileUpload.getName(), charset);
        String value = encodeAttribute(fileUpload.getFilename(), charset);
        Attribute newattribute = factory.createAttribute(request, key, value);
        multipartHttpDatas.add(newattribute);
        globalBodySize += newattribute.getName().length() + 1 + newattribute.length() + 1;
      }
      return;
    }
    /*
     * Logic:
     * if not Attribute:
     *      add Data to body list
     *      if (duringMixedMode)
     *          add endmixedmultipart delimiter
     *          currentFileUpload = null
     *          duringMixedMode = false;
     *      add multipart delimiter, multipart body header and Data to multipart list
     *      reset currentFileUpload, duringMixedMode
     * if FileUpload: take care of multiple file for one field => mixed mode
     *      if (duringMixeMode)
     *          if (currentFileUpload.name == data.name)
     *              add mixedmultipart delimiter, mixedmultipart body header and Data to multipart list
     *          else
     *              add endmixedmultipart delimiter, multipart body header and Data to multipart list
     *              currentFileUpload = data
     *              duringMixedMode = false;
     *      else
     *          if (currentFileUpload.name == data.name)
     *              change multipart body header of previous file into multipart list to
     *                      mixedmultipart start, mixedmultipart body header
     *              add mixedmultipart delimiter, mixedmultipart body header and Data to multipart list
     *              duringMixedMode = true
     *          else
     *              add multipart delimiter, multipart body header and Data to multipart list
     *              currentFileUpload = data
     *              duringMixedMode = false;
     * Do not add last delimiter! Could be:
     * if duringmixedmode: endmixedmultipart + endmultipart
     * else only endmultipart
     */
    if (data instanceof Attribute) {
      if (duringMixedMode) {
        InternalAttribute internal = new InternalAttribute();
        internal.addValue("\r\n--" + multipartMixedBoundary + "--");
        multipartHttpDatas.add(internal);
        multipartMixedBoundary = null;
        currentFileUpload = null;
        duringMixedMode = false;
      }
      InternalAttribute internal = new InternalAttribute();
      if (multipartHttpDatas.size() > 0) {
        // previously a data field so CRLF
        internal.addValue("\r\n");
      }
      internal.addValue("--" + multipartDataBoundary + "\r\n");
      // content-disposition: form-data; name="field1"
      Attribute attribute = (Attribute) data;
      internal.addValue(
          HttpPostBodyUtil.CONTENT_DISPOSITION
              + ": "
              + HttpPostBodyUtil.FORM_DATA
              + "; "
              + HttpPostBodyUtil.NAME
              + "=\""
              + encodeAttribute(attribute.getName(), charset)
              + "\"\r\n");
      Charset localcharset = attribute.getCharset();
      if (localcharset != null) {
        // Content-Type: charset=charset
        internal.addValue(
            HttpHeaders.Names.CONTENT_TYPE
                + ": "
                + HttpHeaders.Values.CHARSET
                + "="
                + localcharset
                + "\r\n");
      }
      // CRLF between body header and data
      internal.addValue("\r\n");
      multipartHttpDatas.add(internal);
      multipartHttpDatas.add(data);
      globalBodySize += attribute.length() + internal.size();
    } else if (data instanceof FileUpload) {
      FileUpload fileUpload = (FileUpload) data;
      InternalAttribute internal = new InternalAttribute();
      if (multipartHttpDatas.size() > 0) {
        // previously a data field so CRLF
        internal.addValue("\r\n");
      }
      boolean localMixed = false;
      if (duringMixedMode) {
        if (currentFileUpload != null && currentFileUpload.getName().equals(fileUpload.getName())) {
          // continue a mixed mode

          localMixed = true;
        } else {
          // end a mixed mode

          // add endmixedmultipart delimiter, multipart body header and
          // Data to multipart list
          internal.addValue("--" + multipartMixedBoundary + "--");
          multipartHttpDatas.add(internal);
          multipartMixedBoundary = null;
          // start a new one (could be replaced if mixed start again from here
          internal = new InternalAttribute();
          internal.addValue("\r\n");
          localMixed = false;
          // new currentFileUpload and no more in Mixed mode
          currentFileUpload = fileUpload;
          duringMixedMode = false;
        }
      } else {
        if (currentFileUpload != null && currentFileUpload.getName().equals(fileUpload.getName())) {
          // create a new mixed mode (from previous file)

          // change multipart body header of previous file into multipart list to
          // mixedmultipart start, mixedmultipart body header

          // change Internal (size()-2 position in multipartHttpDatas)
          // from (line starting with *)
          // --AaB03x
          // * Content-Disposition: form-data; name="files"; filename="file1.txt"
          // Content-Type: text/plain
          // to (lines starting with *)
          // --AaB03x
          // * Content-Disposition: form-data; name="files"
          // * Content-Type: multipart/mixed; boundary=BbC04y
          // *
          // * --BbC04y
          // * Content-Disposition: file; filename="file1.txt"
          // Content-Type: text/plain
          initMixedMultipart();
          InternalAttribute pastAttribute =
              (InternalAttribute) multipartHttpDatas.get(multipartHttpDatas.size() - 2);
          // remove past size
          globalBodySize -= pastAttribute.size();
          String replacement =
              HttpPostBodyUtil.CONTENT_DISPOSITION
                  + ": "
                  + HttpPostBodyUtil.FORM_DATA
                  + "; "
                  + HttpPostBodyUtil.NAME
                  + "=\""
                  + encodeAttribute(fileUpload.getName(), charset)
                  + "\"\r\n";
          replacement +=
              HttpHeaders.Names.CONTENT_TYPE
                  + ": "
                  + HttpPostBodyUtil.MULTIPART_MIXED
                  + "; "
                  + HttpHeaders.Values.BOUNDARY
                  + "="
                  + multipartMixedBoundary
                  + "\r\n\r\n";
          replacement += "--" + multipartMixedBoundary + "\r\n";
          replacement +=
              HttpPostBodyUtil.CONTENT_DISPOSITION
                  + ": "
                  + HttpPostBodyUtil.FILE
                  + "; "
                  + HttpPostBodyUtil.FILENAME
                  + "=\""
                  + encodeAttribute(fileUpload.getFilename(), charset)
                  + "\"\r\n";
          pastAttribute.setValue(replacement, 1);
          // update past size
          globalBodySize += pastAttribute.size();

          // now continue
          // add mixedmultipart delimiter, mixedmultipart body header and
          // Data to multipart list
          localMixed = true;
          duringMixedMode = true;
        } else {
          // a simple new multipart
          // add multipart delimiter, multipart body header and Data to multipart list
          localMixed = false;
          currentFileUpload = fileUpload;
          duringMixedMode = false;
        }
      }

      if (localMixed) {
        // add mixedmultipart delimiter, mixedmultipart body header and
        // Data to multipart list
        internal.addValue("--" + multipartMixedBoundary + "\r\n");
        // Content-Disposition: file; filename="file1.txt"
        internal.addValue(
            HttpPostBodyUtil.CONTENT_DISPOSITION
                + ": "
                + HttpPostBodyUtil.FILE
                + "; "
                + HttpPostBodyUtil.FILENAME
                + "=\""
                + encodeAttribute(fileUpload.getFilename(), charset)
                + "\"\r\n");

      } else {
        internal.addValue("--" + multipartDataBoundary + "\r\n");
        // Content-Disposition: form-data; name="files"; filename="file1.txt"
        internal.addValue(
            HttpPostBodyUtil.CONTENT_DISPOSITION
                + ": "
                + HttpPostBodyUtil.FORM_DATA
                + "; "
                + HttpPostBodyUtil.NAME
                + "=\""
                + encodeAttribute(fileUpload.getName(), charset)
                + "\"; "
                + HttpPostBodyUtil.FILENAME
                + "=\""
                + encodeAttribute(fileUpload.getFilename(), charset)
                + "\"\r\n");
      }
      // Content-Type: image/gif
      // Content-Type: text/plain; charset=ISO-8859-1
      // Content-Transfer-Encoding: binary
      internal.addValue(HttpHeaders.Names.CONTENT_TYPE + ": " + fileUpload.getContentType());
      String contentTransferEncoding = fileUpload.getContentTransferEncoding();
      if (contentTransferEncoding != null
          && contentTransferEncoding.equals(
              HttpPostBodyUtil.TransferEncodingMechanism.BINARY.value)) {
        internal.addValue(
            "\r\n"
                + HttpHeaders.Names.CONTENT_TRANSFER_ENCODING
                + ": "
                + HttpPostBodyUtil.TransferEncodingMechanism.BINARY.value
                + "\r\n\r\n");
      } else if (fileUpload.getCharset() != null) {
        internal.addValue(
            "; " + HttpHeaders.Values.CHARSET + "=" + fileUpload.getCharset() + "\r\n\r\n");
      } else {
        internal.addValue("\r\n\r\n");
      }
      multipartHttpDatas.add(internal);
      multipartHttpDatas.add(data);
      globalBodySize += fileUpload.length() + internal.size();
    }
  }