Package org.apache.http.entity.mime

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


               HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
               String parameterName = arg.getName();
               if (arg.isSkippable(parameterName)){
                   continue;
               }
               FormBodyPart formPart;
               StringBody stringBody = new StringBody(arg.getValue(),
                       Charset.forName(contentEncoding == null ? "US-ASCII" : contentEncoding));
               formPart = new FormBodyPart(arg.getName(), stringBody);                  
               multiPart.addPart(formPart);
            }

            // Add any files
            // Cannot retrieve parts once added to the MultiPartEntity, so have to save them here.
View Full Code Here

public class TestMultipartForm {

    @Test
    public void testMultipartFormStringParts() throws Exception {
        HttpMultipart multipart = new HttpMultipart("form-data", "foo");
        FormBodyPart p1 = new FormBodyPart(
                "field1",
                new StringBody("this stuff"));
        FormBodyPart p2 = new FormBodyPart(
                "field2",
                new StringBody("that stuff", Charset.forName("UTF-8")));
        FormBodyPart p3 = new FormBodyPart(
                "field3",
                new StringBody("all kind of stuff"));

        multipart.addBodyPart(p1);
        multipart.addBodyPart(p2);
View Full Code Here

        } finally {
            writer.close();
        }

        HttpMultipart multipart = new HttpMultipart("form-data", "foo");
        FormBodyPart p1 = new FormBodyPart(
                "field1",
                new FileBody(tmpfile));
        FormBodyPart p2 = new FormBodyPart(
                "field2",
                new InputStreamBody(new FileInputStream(tmpfile), "file.tmp"));

        multipart.addBodyPart(p1);
        multipart.addBodyPart(p2);
View Full Code Here

        } finally {
            writer.close();
        }

        HttpMultipart multipart = new HttpMultipart("form-data", null, "foo", HttpMultipartMode.STRICT);
        FormBodyPart p1 = new FormBodyPart(
                "field1",
                new FileBody(tmpfile));
        FormBodyPart p2 = new FormBodyPart(
                "field2",
                new FileBody(tmpfile, "test-file", "text/plain", "ANSI_X3.4-1968"));
        FormBodyPart p3 = new FormBodyPart(
                "field3",
                new InputStreamBody(new FileInputStream(tmpfile), "file.tmp"));

        multipart.addBodyPart(p1);
        multipart.addBodyPart(p2);
View Full Code Here

        } finally {
            writer.close();
        }

        HttpMultipart multipart = new HttpMultipart("form-data", Charset.forName("UTF-8"), "foo", HttpMultipartMode.BROWSER_COMPATIBLE);
        FormBodyPart p1 = new FormBodyPart(
                "field1",
                new InputStreamBody(new FileInputStream(tmpfile), s1 + ".tmp"));
        FormBodyPart p2 = new FormBodyPart(
                "field2",
                new InputStreamBody(new FileInputStream(tmpfile), s2 + ".tmp"));

        multipart.addBodyPart(p1);
        multipart.addBodyPart(p2);
View Full Code Here

    public void testMultipartFormStringPartsMultiCharsets() throws Exception {
        String s1 = constructString(SWISS_GERMAN_HELLO);
        String s2 = constructString(RUSSIAN_HELLO);

        HttpMultipart multipart = new HttpMultipart("form-data", "foo");
        FormBodyPart p1 = new FormBodyPart(
                "field1",
                new StringBody(s1, Charset.forName("ISO-8859-1")));
        FormBodyPart p2 = new FormBodyPart(
                "field2",
                new StringBody(s2, Charset.forName("KOI8-R")));

        multipart.addBodyPart(p1);
        multipart.addBodyPart(p2);
View Full Code Here

                Field.parse("Content-Type: multipart/form-data; boundary=foo"));
        message.setHeader(header);
       
        HttpMultipart multipart = new HttpMultipart();
        multipart.setParent(message);
        FormBodyPart p1 = new FormBodyPart(
                "field1",
                new StringBody("this stuff"));
        FormBodyPart p2 = new FormBodyPart(
                "field2",
                new StringBody("that stuff", Charset.forName("US-ASCII")));
        FormBodyPart p3 = new FormBodyPart(
                "field3",
                new StringBody("all kind of stuff"));
       
        multipart.addBodyPart(p1);
        multipart.addBodyPart(p2);
View Full Code Here

            writer.close();
        }
       
        HttpMultipart multipart = new HttpMultipart();
        multipart.setParent(message);
        FormBodyPart p1 = new FormBodyPart(
                "field1",
                new FileBody(tmpfile));
        FormBodyPart p2 = new FormBodyPart(
                "field2",
                new InputStreamBody(new FileInputStream(tmpfile), "file.tmp"));
       
        multipart.addBodyPart(p1);
        multipart.addBodyPart(p2);
View Full Code Here

            writer.close();
        }
       
        HttpMultipart multipart = new HttpMultipart();
        multipart.setParent(message);
        FormBodyPart p1 = new FormBodyPart(
                "field1",
                new FileBody(tmpfile));
        FormBodyPart p2 = new FormBodyPart(
                "field2",
                new InputStreamBody(new FileInputStream(tmpfile), "file.tmp"));
       
        multipart.addBodyPart(p1);
        multipart.addBodyPart(p2);
View Full Code Here

TOP

Related Classes of org.apache.http.entity.mime.FormBodyPart

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.