//listQS.add(new BasicNameValuePair(translateEncoding(param.getName(), http.charset),translateEncoding(param.getValueAsString(), http.charset)));
}
// Form
else if(type.equals("formfield") || type.equals("form")) {
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));
}
if(hasBody && hasForm)
throw new ApplicationException("mixing httpparam type file/formfield and body/XML is not allowed");
if(!hasContentType) {
if(isBinary) {
if(hasBody) req.addHeader("Content-type", "application/octet-stream");
else req.addHeader("Content-type", "application/x-www-form-urlencoded; charset="+charset);
}
else {
if(hasBody)
req.addHeader("Content-type", "text/html; charset="+charset );
}
}
// set User Agent
if(!hasHeaderIgnoreCase(req,"User-Agent"))
req.setHeader("User-Agent",this.useragent);
// set timeout
if(this.timeout>0L)HTTPEngine4Impl.setTimeout(params, (int)this.timeout);
// set Username and Password
if(this.username!=null) {
if(this.password==null)this.password="";
if(AUTH_TYPE_NTLM==this.authType) {
if(StringUtil.isEmpty(this.workStation,true))
throw new ApplicationException("attribute workstation is required when authentication type is [NTLM]");
if(StringUtil.isEmpty(this.domain,true))
throw new ApplicationException("attribute domain is required when authentication type is [NTLM]");
HTTPEngine4Impl.setNTCredentials(client, this.username, this.password, this.workStation,this.domain);
}
else httpContext=HTTPEngine4Impl.setCredentials(client, httpHost, this.username, this.password,preauth);
}