Package org.nutz.lang.util

Examples of org.nutz.lang.util.NutMap


  private NutMap keys;

  public void parse(Reader reader) {
    nodes = new LinkedList<SegmentNode>();
    context = Lang.context();
    keys = new NutMap();
    StringBuilder org = new StringBuilder();
    StringBuilder sb = new StringBuilder();
    int b;
    try {
      while (-1 != (b = reader.read())) {
View Full Code Here


    if (log.isDebugEnabled())
      log.debug("info created");
    /*
     * 创建参数表
     */
    NutMap params = Uploads.createParamsMap(req);
    if (log.isDebugEnabled())
      log.debugf("Params map created - %s params", params.size());
    /*
     * 解析边界
     */
    String firstBoundary = "--" + Http.multipart.getBoundary(req.getContentType());
    RemountBytes firstBoundaryBytes = RemountBytes.create(firstBoundary);
    String itemEndl = "\r\n--" + Http.multipart.getBoundary(req.getContentType());
    RemountBytes itemEndlBytes = RemountBytes.create(itemEndl);
    RemountBytes nameEndlBytes = RemountBytes.create("\r\n\r\n");

    if (Http.multipart.getBoundary(req.getContentType()) == null) {
      if (log.isInfoEnabled())
        log.info("boundary no found!!");
      return params;
    }

    if (log.isDebugEnabled())
      log.debug("boundary: " + itemEndl);

    /*
     * 准备缓冲环,并跳过开始标记
     */
    MarkMode mm;
    BufferRing br;
    try {
      ServletInputStream ins = req.getInputStream();
      // 构建 3 个环节点的缓冲环
      br = new BufferRing(ins, 3, bufferSize);
      // 初始加载
      info.current = br.load();
      // 跳过开始的标记
      mm = br.mark(firstBoundaryBytes);
      // 这是不可能的,应该立即退出
      if (mm != MarkMode.FOUND) {
        if (log.isWarnEnabled())
          log.warnf("Fail to find the firstBoundary (%s) in stream, quit!", firstBoundary);
        return params;
      }
      br.skipMark();
      if (log.isDebugEnabled())
        log.debug("skip first boundary");
    }
    catch (IOException e) {
      throw Lang.wrapThrow(e);
    }

    /**
     * ========================================================<br>
     * 进入循环
     */
    if (log.isDebugEnabled())
      log.debug("Reading...");
    try {
      FieldMeta meta;
      do {
        info.current = br.load();
        // 标记项目头
        mm = br.mark(nameEndlBytes);
        String s = br.dumpAsString(charset);

        // 肯定碰到了 "--\r\n", 这标志着整个流结束了
        if ("--".equals(s) || MarkMode.STREAM_END == mm) {
          break;
        }
        // 找到头的结束标志
        else if (MarkMode.FOUND == mm) {
          meta = new FieldMeta(s);
        }
        // 这是不可能的,抛错
        else {
          throw new UploadInvalidFormatException("Fail to found nameEnd!");
        }
        if(log.isDebugEnabled())
          log.debugf("Upload File info: FilePath=[%s],fieldName=[%s]",meta.getFileLocalPath(),meta.getName());
        // 作为文件读取
        if (meta.isFile()) {
          if (log.isDebugEnabled())
            log.debugf("Upload Info: name=%s,content_type=%s", meta.getFileLocalName(),meta.getContentType());
          // 检查是否通过文件名过滤
          if (!context.isNameAccepted(meta.getFileLocalName())) {
            throw new UploadUnsupportedFileNameException(meta);
          }
          // 检查是否通过文件类型过滤
          if (!context.isContentTypeAccepted(meta.getContentType())) {
            throw new UploadUnsupportedFileTypeException(meta);
          }

          // 上传的是一个空文件
          if ("\"\"".equals(meta.getName()) || Strings.isBlank(meta.getFileLocalPath())) {
            do {
              info.current = br.load();
              mm = br.mark(itemEndlBytes);
              assertStreamNotEnd(mm);
              br.skipMark();
            } while (mm == MarkMode.NOT_FOUND);
          }
          // 保存临时文件
          else {
            File tmp = tmps.createFile(meta.getFileExtension());
            OutputStream ops = null;
            try {
              ops = new BufferedOutputStreamnew FileOutputStream(tmp),
                              bufferSize * 2);
              // 需要限制文件大小
              if (maxFileSize > 0) {
                long maxPos = info.current + maxFileSize;
                do {
                  info.current = br.load();
                  mm = br.mark(itemEndlBytes);
                  assertStreamNotEnd(mm);
                  if (info.current > maxPos) {
                    throw new UploadOutOfSizeException(meta);
                  }
                  br.dump(ops);
                  if(info.stop)
                    throw new UploadStopException(info);
                } while (mm == MarkMode.NOT_FOUND);
              }
              // 不限制文件大小
              else {
                do {
                  info.current = br.load();
                  mm = br.mark(itemEndlBytes);
                  assertStreamNotEnd(mm);
                  br.dump(ops);
                  if(info.stop)
                    throw new UploadStopException(info);
                } while (mm == MarkMode.NOT_FOUND);
              }
            }
            finally {
              Streams.safeFlush(ops);
              Streams.safeClose(ops);
            }
            // 如果是空文件,不保存
            if (context.isIgnoreNull() && tmp.length() == 0) {}
            // 默认,空文件也保存
            else {
              params.add(meta.getName(), new TempFile(meta, tmp));
            }
          }
        }
        // 作为提交值读取
        else {
          StringBuilder sb = new StringBuilder();
          do {
            info.current = br.load();
            mm = br.mark(itemEndlBytes);
            assertStreamNotEnd(mm);
            sb.append(br.dumpAsString(charset));
          } while (mm == MarkMode.NOT_FOUND);
          params.add(meta.getName(), sb.toString());
          if (log.isDebugEnabled())
            log.debugf"Found a param, name=[%s] value=[%s]",
                  meta.getName(),
                  sb.toString());
        }
View Full Code Here

   * @param req
   *            请求对象
   * @return 参数 MAP
   */
  public static NutMap createParamsMap(HttpServletRequest req) {
    NutMap params = new NutMap();
    // parse query strings
    Enumeration<?> en = req.getParameterNames();
    while (en.hasMoreElements()) {
      String key = en.nextElement().toString();
      params.put(key, req.getParameter(key));
    }
    return params;
  }
View Full Code Here

    private NutMap keys;

    public void parse(Reader reader) {
        nodes = new LinkedList<SegmentNode>();
        context = Lang.context();
        keys = new NutMap();
        StringBuilder org = new StringBuilder();
        StringBuilder sb = new StringBuilder();
        int b;
        try {
            while (-1 != (b = reader.read())) {
View Full Code Here

        if (log.isDebugEnabled())
            log.debug("info created");
        /*
         * 创建参数表
         */
        NutMap params = Uploads.createParamsMap(req);
        if (log.isDebugEnabled())
            log.debugf("Params map created - %s params", params.size());
        /*
         * 解析边界
         */
        String firstBoundary = "--" + Http.multipart.getBoundary(req.getContentType());
        RemountBytes firstBoundaryBytes = RemountBytes.create(firstBoundary);
        String itemEndl = "\r\n--" + Http.multipart.getBoundary(req.getContentType());
        RemountBytes itemEndlBytes = RemountBytes.create(itemEndl);
        RemountBytes nameEndlBytes = RemountBytes.create("\r\n\r\n");

        if (Http.multipart.getBoundary(req.getContentType()) == null) {
            if (log.isInfoEnabled())
                log.info("boundary no found!!");
            return params;
        }

        if (log.isDebugEnabled())
            log.debug("boundary: " + itemEndl);

        /*
         * 准备缓冲环,并跳过开始标记
         */
        MarkMode mm;
        BufferRing br;
        try {
            ServletInputStream ins = req.getInputStream();
            // 构建 3 个环节点的缓冲环
            br = new BufferRing(ins, 3, bufferSize);
            // 初始加载
            info.current = br.load();
            // 跳过开始的标记
            mm = br.mark(firstBoundaryBytes);
            // 这是不可能的,应该立即退出
            if (mm != MarkMode.FOUND) {
                if (log.isWarnEnabled())
                    log.warnf("Fail to find the firstBoundary (%s) in stream, quit!", firstBoundary);
                return params;
            }
            br.skipMark();
            if (log.isDebugEnabled())
                log.debug("skip first boundary");
        }
        catch (IOException e) {
            throw Lang.wrapThrow(e);
        }

        /**
         * ========================================================<br>
         * 进入循环
         */
        if (log.isDebugEnabled())
            log.debug("Reading...");
        try {
            FieldMeta meta;
            do {
                info.current = br.load();
                // 标记项目头
                mm = br.mark(nameEndlBytes);
                String s = br.dumpAsString(charset);

                // 肯定碰到了 "--\r\n", 这标志着整个流结束了
                if ("--".equals(s) || MarkMode.STREAM_END == mm) {
                    break;
                }
                // 找到头的结束标志
                else if (MarkMode.FOUND == mm) {
                    meta = new FieldMeta(s);
                }
                // 这是不可能的,抛错
                else {
                    throw new UploadInvalidFormatException("Fail to found nameEnd!");
                }
                if(log.isDebugEnabled())
                    log.debugf("Upload File info: FilePath=[%s],fieldName=[%s]",meta.getFileLocalPath(),meta.getName());
                // 作为文件读取
                if (meta.isFile()) {
                    if (log.isDebugEnabled())
                        log.debugf("Upload Info: name=%s,content_type=%s", meta.getFileLocalName(),meta.getContentType());
                    // 检查是否通过文件名过滤
                    if (!context.isNameAccepted(meta.getFileLocalName())) {
                        throw new UploadUnsupportedFileNameException(meta);
                    }
                    // 检查是否通过文件类型过滤
                    if (!context.isContentTypeAccepted(meta.getContentType())) {
                        throw new UploadUnsupportedFileTypeException(meta);
                    }

                    // 上传的是一个空文件
                    if ("\"\"".equals(meta.getName()) || Strings.isBlank(meta.getFileLocalPath())) {
                        do {
                            info.current = br.load();
                            mm = br.mark(itemEndlBytes);
                            assertStreamNotEnd(mm);
                            br.skipMark();
                        } while (mm == MarkMode.NOT_FOUND);
                    }
                    // 保存临时文件
                    else {
                        File tmp = tmps.createFile(meta.getFileExtension());
                        OutputStream ops = null;
                        try {
                            ops = new BufferedOutputStream(    new FileOutputStream(tmp),
                                                            bufferSize * 2);
                            // 需要限制文件大小
                            if (maxFileSize > 0) {
                                long maxPos = info.current + maxFileSize;
                                do {
                                    info.current = br.load();
                                    mm = br.mark(itemEndlBytes);
                                    assertStreamNotEnd(mm);
                                    if (info.current > maxPos) {
                                        throw new UploadOutOfSizeException(meta);
                                    }
                                    br.dump(ops);
                                    if(info.stop)
                                        throw new UploadStopException(info);
                                } while (mm == MarkMode.NOT_FOUND);
                            }
                            // 不限制文件大小
                            else {
                                do {
                                    info.current = br.load();
                                    mm = br.mark(itemEndlBytes);
                                    assertStreamNotEnd(mm);
                                    br.dump(ops);
                                    if(info.stop)
                                        throw new UploadStopException(info);
                                } while (mm == MarkMode.NOT_FOUND);
                            }
                        }
                        finally {
                            Streams.safeFlush(ops);
                            Streams.safeClose(ops);
                        }
                        // 如果是空文件,不保存
                        if (context.isIgnoreNull() && tmp.length() == 0) {}
                        // 默认,空文件也保存
                        else {
                            params.add(meta.getName(), new TempFile(meta, tmp));
                        }
                    }
                }
                // 作为提交值读取
                else {
                    StringBuilder sb = new StringBuilder();
                    do {
                        info.current = br.load();
                        mm = br.mark(itemEndlBytes);
                        assertStreamNotEnd(mm);
                        sb.append(br.dumpAsString(charset));
                    } while (mm == MarkMode.NOT_FOUND);
                    params.add(meta.getName(), sb.toString());
                    if (log.isDebugEnabled())
                        log.debugf(    "Found a param, name=[%s] value=[%s]",
                                    meta.getName(),
                                    sb.toString());
                }
View Full Code Here

     *            是否把所有key的首字母都小写
     *
     * @return 一个 Map 对象
     */
    public static NutMap asMap(Element ele, final boolean lowFirst) {
        final NutMap map = new NutMap();
        eachChildren(ele, new Each<Element>() {
            public void invoke(int index, Element _ele, int length)
                    throws ExitLoop, ContinueLoop, LoopException {
                String key = _ele.getNodeName();
                if (lowFirst)
                    key = Strings.lowerFirst(key);
                Map<String, Object> tmp = asMap(_ele, lowFirst);
                if (!tmp.isEmpty()) {
                    map.setv(key, tmp);
                    return;
                }
                String val = getText(_ele);
                if (!Strings.isBlank(val)) {
                    map.setv(key, val);
                }
            }
        });
        return map;
    }
View Full Code Here

                     + "regionList : ['(2,3)','(6,8]']"
                     + "}";
        JX jx = Json.fromJson(JX.class, str);

        String str2 = Json.toJson(jx);
        NutMap map = Json.fromJson(NutMap.class, str2);
        assertEquals(jx.getRegion().toString(), map.get("region"));

        String[] ss = map.getArray("regionArray", String.class);
        assertEquals(2, ss.length);
        assertEquals("(2,3)", ss[0]);
        assertEquals("[6,9)", ss[1]);

        List<String> list = map.getList("regionList", String.class);
        assertEquals(2, list.size());
        assertEquals("(2,3)", list.get(0));
        assertEquals("(6,8]", list.get(1));
    }
View Full Code Here

     * @param v
     *            值
     * @return Map 对象
     */
    public static NutMap map(String key, Object v) {
        return new NutMap().addv(key, v);
    }
View Full Code Here

     * @param req
     *            请求对象
     * @return 参数 MAP
     */
    public static NutMap createParamsMap(HttpServletRequest req) {
        NutMap params = new NutMap();
        // parse query strings
        Enumeration<?> en = req.getParameterNames();
        while (en.hasMoreElements()) {
            String key = en.nextElement().toString();
            params.put(key, req.getParameter(key));
        }
        return params;
    }
View Full Code Here

                throw new UploadOutOfSizeException(meta);
            if (maxSize > 0 && tmp.length() > maxSize)
                throw new UploadOutOfSizeException(meta);
           
           
            NutMap params = Uploads.createParamsMap(req);
           
            //检查空文件
            if (tmp.length() == 0 && context.isIgnoreNull()) {
                if (log.isDebugEnabled())
                    log.debug("emtry file , drop it ~~");
                tmp.delete();
            } else {
                params.put(meta.getName(), new TempFile(meta, tmp));
            }
            return params;
        } catch (FileNotFoundException e) {
            // 不可能发生?!
            throw Lang.wrapThrow(e);
View Full Code Here

TOP

Related Classes of org.nutz.lang.util.NutMap

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.