Examples of FormBodyPart


Examples of org.apache.http.entity.mime.FormBodyPart

            hasForm=true;
            if(this.method==METHOD_GET) throw new ApplicationException("httpparam type formfield can't only be used, when method of the tag http equal post");
            if(post!=null){
              if(doMultiPart)  {
                parts.add(
                  new FormBodyPart(
                    param.getName(),
                    new StringBody(
                        param.getValueAsString(),
                        CharsetUtil.toCharset(charset)
                    )
                  )
                );
              }
              else {
                postParam.add(new BasicNameValuePair(param.getName(),param.getValueAsString()));
              }
            }
            //else if(multi!=null)multi.addParameter(param.getName(),param.getValueAsString());
          }
        // CGI
          else if(type.equals("cgi")) {
            if(param.getEncoded())
              req.addHeader(
                  urlenc(param.getName(),charset),
                  urlenc(param.getValueAsString(),charset));
                    else
                        req.addHeader(param.getName(),param.getValueAsString());
          }
            // Header
                else if(type.startsWith("head")) {
                  if(param.getName().equalsIgnoreCase("content-type")) hasContentType=true;
                 
                  if(param.getName().equalsIgnoreCase("Content-Length")) {}
                  else if(param.getName().equalsIgnoreCase("Accept-Encoding")) {
                    acceptEncoding.append(headerValue(param.getValueAsString()));
                    acceptEncoding.append(", ");
                  }
                  else req.addHeader(param.getName(),headerValue(param.getValueAsString()));
                }
        // Cookie
          else if(type.equals("cookie")) {
            HTTPEngine4Impl.addCookie(client,host,param.getName(),param.getValueAsString(),"/",charset);
          }
        // File
          else if(type.equals("file")) {
            hasForm=true;
            if(this.method==METHOD_GET) throw new ApplicationException("httpparam type file can't only be used, when method of the tag http equal post");
            String strCT = getContentType(param);
            ContentType ct = HTTPUtil.toContentType(strCT,null);
             
            String mt="text/xml";
            if(ct!=null && !StringUtil.isEmpty(ct.getMimeType(),true)) mt=ct.getMimeType();
           
            String cs=charset;
            if(ct!=null && !StringUtil.isEmpty(ct.getCharset(),true)) cs=ct.getCharset();
           
           
            if(doMultiPart) {
              try {
                Resource res = param.getFile();
                parts.add(new FormBodyPart(
                    param.getName(),
                    new ResourceBody(res, mt, res.getName(), cs)
                ));
                //parts.add(new ResourcePart(param.getName(),new ResourcePartSource(param.getFile()),getContentType(param),_charset));
              }
              catch (FileNotFoundException e) {
                throw new ApplicationException("can't upload file, path is invalid",e.getMessage());
              }
            }
          }
        // XML
          else if(type.equals("xml")) {
            ContentType ct = HTTPUtil.toContentType(param.getMimeType(),null);
             
            String mt="text/xml";
            if(ct!=null && !StringUtil.isEmpty(ct.getMimeType(),true)) mt=ct.getMimeType();
           
            String cs=charset;
            if(ct!=null && !StringUtil.isEmpty(ct.getCharset(),true)) cs=ct.getCharset();
           
            hasBody=true;
            hasContentType=true;
            req.addHeader("Content-type", mt+"; charset="+cs);
              if(eem==null)throw new ApplicationException("type xml is only supported for type post and put");
              HTTPEngine4Impl.setBody(eem, param.getValueAsString(),mt,cs);
          }
        // Body
          else if(type.equals("body")) {
            ContentType ct = HTTPUtil.toContentType(param.getMimeType(),null);
             
            String mt=null;
            if(ct!=null && !StringUtil.isEmpty(ct.getMimeType(),true)) mt=ct.getMimeType();
           
            String cs=charset;
            if(ct!=null && !StringUtil.isEmpty(ct.getCharset(),true)) cs=ct.getCharset();
           
           
            hasBody=true;
            if(eem==null)throw new ApplicationException("type body is only supported for type post and put");
            HTTPEngine4Impl.setBody(eem, param.getValue(),mt,cs);
           
          }
                else {
                    throw new ApplicationException("invalid type ["+type+"]");
                }
           
        }
       
        // post params
        if(postParam!=null && postParam.size()>0)
          post.setEntity(new org.apache.http.client.entity.UrlEncodedFormEntity(postParam,charset));
       
        if(compression){
          acceptEncoding.append("gzip");
        }
        else {
          acceptEncoding.append("deflate;q=0");
          req.setHeader("TE", "deflate;q=0");
        }
      req.setHeader("Accept-Encoding",acceptEncoding.toString());
       
       
       
        // multipart
        if(doMultiPart && eem!=null) {
          hasContentType=true;
          boolean doIt=true;
          if(!this.multiPart && parts.size()==1){
            ContentBody body = parts.get(0).getBody();
            if(body instanceof StringBody){
              StringBody sb=(StringBody)body;
              try {
                org.apache.http.entity.ContentType ct=org.apache.http.entity.ContentType.create(sb.getMimeType(),sb.getCharset());
                String str = IOUtil.toString(sb.getReader());
                StringEntity entity = new StringEntity(str,ct);
                eem.setEntity(entity);
               
              } catch (IOException e) {
                throw Caster.toPageException(e);
              }
              doIt=false;
            }
          }
          if(doIt) {
            MultipartEntity mpe = new MultipartEntity(HttpMultipartMode.STRICT);
            Iterator<FormBodyPart> it = parts.iterator();
            while(it.hasNext()) {
              FormBodyPart part = it.next();
              mpe.addPart(part.getName(),part.getBody());
            }
            eem.setEntity(mpe);
          }
            //eem.setRequestEntity(new MultipartRequestEntityFlex(parts.toArray(new Part[parts.size()]), eem.getParams(),http.multiPartType));
        }
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.