예제 #1
0
파일: Post.java 프로젝트: hofmeister/Webi
  private static void putRequestMap(
      Env env,
      ArrayValue post,
      ArrayValue files,
      HttpServletRequest request,
      boolean addSlashesToValues,
      boolean isAllowUploads) {
    // this call consumes the inputstream
    Map<String, String[]> map = request.getParameterMap();

    if (map == null) return;

    long maxFileSize = Long.MAX_VALUE;

    Value maxFileSizeV = post.get(MAX_FILE_SIZE);
    if (maxFileSizeV.isNull()) maxFileSize = maxFileSizeV.toLong();

    if (isAllowUploads) {
      for (Map.Entry<String, String[]> entry : map.entrySet()) {
        String key = entry.getKey();

        int len = key.length();

        if (len < 10 || !key.endsWith(".filename")) continue;

        String name = key.substring(0, len - 9);

        String[] fileNames = request.getParameterValues(name + ".filename");
        String[] tmpNames = request.getParameterValues(name + ".file");
        String[] mimeTypes = request.getParameterValues(name + ".content-type");

        for (int i = 0; i < fileNames.length; i++) {
          long fileLength = new FilePath(tmpNames[i]).getLength();

          addFormFile(
              env,
              files,
              name,
              fileNames[i],
              tmpNames[i],
              mimeTypes[i],
              fileLength,
              addSlashesToValues,
              maxFileSize);
        }
      }
    }

    ArrayList<String> keys = new ArrayList<String>();

    keys.addAll(request.getParameterMap().keySet());

    Collections.sort(keys);

    for (String key : keys) {
      String[] value = request.getParameterValues(key);

      Post.addFormValue(env, post, key, value, addSlashesToValues);
    }
  }
예제 #2
0
파일: JMSQueue.java 프로젝트: dlitz/resin
  public boolean send(@NotNull Value value, @Optional JMSQueue replyTo) throws JMSException {
    Message message = null;

    if (value.isArray()) {
      message = _session.createMapMessage();

      ArrayValue array = (ArrayValue) value;

      Set<Map.Entry<Value, Value>> entrySet = array.entrySet();

      for (Map.Entry<Value, Value> entry : entrySet) {
        if (entry.getValue() instanceof BinaryValue) {
          byte[] bytes = ((BinaryValue) entry.getValue()).toBytes();

          ((MapMessage) message).setBytes(entry.getKey().toString(), bytes);
        } else {
          // every primitive except for bytes can be translated from a string
          ((MapMessage) message).setString(entry.getKey().toString(), entry.getValue().toString());
        }
      }
    } else if (value instanceof BinaryValue) {
      message = _session.createBytesMessage();

      byte[] bytes = ((BinaryValue) value).toBytes();

      ((BytesMessage) message).writeBytes(bytes);
    } else if (value.isLongConvertible()) {
      message = _session.createStreamMessage();

      ((StreamMessage) message).writeLong(value.toLong());
    } else if (value.isDoubleConvertible()) {
      message = _session.createStreamMessage();

      ((StreamMessage) message).writeDouble(value.toDouble());
    } else if (value.toJavaObject() instanceof String) {
      message = _session.createTextMessage();

      ((TextMessage) message).setText(value.toString());
    } else if (value.toJavaObject() instanceof Serializable) {
      message = _session.createObjectMessage();

      ((ObjectMessage) message).setObject((Serializable) value.toJavaObject());
    } else {
      return false;
    }

    if (replyTo != null) message.setJMSReplyTo(replyTo._destination);

    _producer.send(message);

    return true;
  }
예제 #3
0
파일: Post.java 프로젝트: hofmeister/Webi
  private static void readMultipartStream(
      Env env,
      MultipartStream ms,
      ArrayValue postArray,
      ArrayValue files,
      boolean addSlashesToValues,
      boolean isAllowUploads)
      throws IOException {
    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", addSlashesToValues);
      String filename = getAttribute(attr, "filename", addSlashesToValues);

      if (filename != null) {
        int slashIndex = filename.lastIndexOf('/');
        int slashIndex2 = filename.lastIndexOf('\\');

        slashIndex = Math.max(slashIndex, slashIndex2);

        if (slashIndex >= 0) filename = filename.substring(slashIndex + 1);
      }

      int bracketIndex = -1;

      if (name != null) bracketIndex = name.lastIndexOf(']');

      if (bracketIndex >= 0 && bracketIndex < name.length() - 1) {
        // php/085c
      } else if (filename == null) {
        StringValue value = env.createStringBuilder();

        value.appendReadAll(is, Integer.MAX_VALUE);

        if (name != null) {
          addFormValue(env, postArray, name, value, null, addSlashesToValues);
        } else {
          env.warning(L.l("file upload is missing name and filename"));
        }
      } else {
        if (!isAllowUploads) {
          continue;
        }

        String tmpName = "";
        long tmpLength = 0;

        // A POST file upload with an empty string as the filename does not
        // create a temp file in the upload directory.

        if (filename.length() > 0) {
          Path tmpPath = env.getUploadDirectory().createTempFile("php", ".tmp");

          env.addRemovePath(tmpPath);

          WriteStream os = tmpPath.openWrite();
          try {
            os.writeStream(is);
          } finally {
            os.close();
          }

          tmpName = tmpPath.getFullPath();
          tmpLength = tmpPath.getLength();
        }

        // php/0865
        //
        // A header like "Content-Type: image/gif" indicates the mime type
        // for an uploaded file.

        String mimeType = getAttribute(attr, "mime-type", addSlashesToValues);
        if (mimeType == null) {
          mimeType = (String) ms.getAttribute("content-type");

          // php/085f
          if (mimeType != null && mimeType.endsWith(";"))
            mimeType = mimeType.substring(0, mimeType.length() - 1);
        }

        // php/0864
        //
        // mime type is empty string when no file is uploaded.

        if (filename.length() == 0) {
          mimeType = "";
        }

        long maxFileSize = Long.MAX_VALUE;

        Value maxFileSizeV = postArray.get(MAX_FILE_SIZE);
        if (!maxFileSizeV.isNull()) maxFileSize = maxFileSizeV.toLong();

        if (name != null) {
          addFormFile(
              env,
              files,
              name,
              filename,
              tmpName,
              mimeType,
              tmpLength,
              addSlashesToValues,
              maxFileSize);
        } else {
          addFormFile(
              env, files, filename, tmpName, mimeType, tmpLength, addSlashesToValues, maxFileSize);
        }
      }
    }
  }
예제 #4
0
 /**
  * Returns the long value associated with the specified key.
  *
  * @param key The key to look up.
  * @param defaultValue The default value.
  * @return The long value associated with the specified key.
  */
 long getLong(String key, int defaultValue) {
   if (toUpdate.containsKey(key)) return Value.toLong(toUpdate.get(key).toString());
   return getContent().getLong(key, defaultValue);
 }