use of org.apache.http.entity.mime.content.ContentBody in project Lucee by lucee.
the class HttpGetWithBody method _doEndTag.
private void _doEndTag() throws PageException, IOException {
long start = System.nanoTime();
HttpClientBuilder builder = HTTPEngine4Impl.getHttpClientBuilder();
ssl(builder);
// redirect
if (redirect)
builder.setRedirectStrategy(new DefaultRedirectStrategy());
else
builder.disableRedirectHandling();
// cookies
BasicCookieStore cookieStore = new BasicCookieStore();
builder.setDefaultCookieStore(cookieStore);
ConfigWeb cw = pageContext.getConfig();
HttpRequestBase req = null;
HttpContext httpContext = null;
CacheHandler cacheHandler = null;
String cacheId = null;
// HttpRequestBase req = init(pageContext.getConfig(),this,client,params,url,port);
{
if (StringUtil.isEmpty(charset, true))
charset = ((PageContextImpl) pageContext).getWebCharset().name();
else
charset = charset.trim();
// check if has fileUploads
boolean doUploadFile = false;
for (int i = 0; i < this.params.size(); i++) {
if ((this.params.get(i)).getType() == HttpParamBean.TYPE_FILE) {
doUploadFile = true;
break;
}
}
// parse url (also query string)
int len = this.params.size();
StringBuilder sbQS = new StringBuilder();
for (int i = 0; i < len; i++) {
HttpParamBean param = this.params.get(i);
int type = param.getType();
// URL
if (type == HttpParamBean.TYPE_URL) {
if (sbQS.length() > 0)
sbQS.append('&');
sbQS.append(param.getEncoded() ? urlenc(param.getName(), charset) : param.getName());
sbQS.append('=');
sbQS.append(param.getEncoded() ? urlenc(param.getValueAsString(), charset) : param.getValueAsString());
}
}
String host = null;
HttpHost httpHost;
try {
URL _url = HTTPUtil.toURL(url, port, encoded);
httpHost = new HttpHost(_url.getHost(), _url.getPort());
host = _url.getHost();
url = _url.toExternalForm();
if (sbQS.length() > 0) {
// no existing QS
if (StringUtil.isEmpty(_url.getQuery())) {
url += "?" + sbQS;
} else {
url += "&" + sbQS;
}
}
} catch (MalformedURLException mue) {
throw Caster.toPageException(mue);
}
// cache
if (cachedWithin != null) {
cacheId = createCacheId();
cacheHandler = pageContext.getConfig().getCacheHandlerCollection(Config.CACHE_TYPE_HTTP, null).getInstanceMatchingObject(cachedWithin, null);
if (cacheHandler instanceof CacheHandlerPro) {
CacheItem cacheItem = ((CacheHandlerPro) cacheHandler).get(pageContext, cacheId, cachedWithin);
if (cacheItem instanceof HTTPCacheItem) {
pageContext.setVariable(result, ((HTTPCacheItem) cacheItem).getData());
return;
}
} else if (cacheHandler != null) {
// TODO this else block can be removed when all cache handlers implement CacheHandlerPro
CacheItem cacheItem = cacheHandler.get(pageContext, cacheId);
if (cacheItem instanceof HTTPCacheItem) {
pageContext.setVariable(result, ((HTTPCacheItem) cacheItem).getData());
return;
}
}
}
// cache not found, process and cache result if needed
// select best matching method (get,post, post multpart (file))
boolean isBinary = false;
boolean doMultiPart = doUploadFile || this.multiPart;
HttpEntityEnclosingRequest eeReqPost = null;
HttpEntityEnclosingRequest eeReq = null;
if (this.method == METHOD_GET) {
req = new HttpGetWithBody(url);
eeReq = (HttpEntityEnclosingRequest) req;
} else if (this.method == METHOD_HEAD) {
req = new HttpHead(url);
} else if (this.method == METHOD_DELETE) {
isBinary = true;
req = new HttpDeleteWithBody(url);
eeReq = (HttpEntityEnclosingRequest) req;
} else if (this.method == METHOD_PUT) {
isBinary = true;
HttpPut put = new HttpPut(url);
eeReqPost = put;
req = put;
eeReq = put;
} else if (this.method == METHOD_TRACE) {
isBinary = true;
req = new HttpTrace(url);
} else if (this.method == METHOD_OPTIONS) {
isBinary = true;
req = new HttpOptions(url);
} else if (this.method == METHOD_PATCH) {
isBinary = true;
eeReq = HTTPPatchFactory.getHTTPPatch(url);
req = (HttpRequestBase) eeReq;
} else {
isBinary = true;
eeReqPost = new HttpPost(url);
req = (HttpPost) eeReqPost;
eeReq = eeReqPost;
}
boolean hasForm = false;
boolean hasBody = false;
boolean hasContentType = false;
// Set http params
ArrayList<FormBodyPart> parts = new ArrayList<FormBodyPart>();
StringBuilder acceptEncoding = new StringBuilder();
java.util.List<NameValuePair> postParam = eeReqPost != null ? new ArrayList<NameValuePair>() : null;
for (int i = 0; i < len; i++) {
HttpParamBean param = this.params.get(i);
int type = param.getType();
// URL
if (type == HttpParamBean.TYPE_URL) {
// listQS.add(new BasicNameValuePair(translateEncoding(param.getName(), http.charset),translateEncoding(param.getValueAsString(),
// http.charset)));
} else // Form
if (type == HttpParamBean.TYPE_FORM) {
hasForm = true;
if (this.method == METHOD_GET)
throw new ApplicationException("httpparam with type formfield can only be used when the method attribute of the parent http tag is set to post");
if (eeReqPost != 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());
} else // CGI
if (type == HttpParamBean.TYPE_CGI) {
if (param.getEncoded())
req.addHeader(urlenc(param.getName(), charset), urlenc(param.getValueAsString(), charset));
else
req.addHeader(param.getName(), param.getValueAsString());
} else // Header
if (type == HttpParamBean.TYPE_HEADER) {
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()));
} else // Cookie
if (type == HttpParamBean.TYPE_COOKIE) {
HTTPEngine4Impl.addCookie(cookieStore, host, param.getName(), param.getValueAsString(), "/", charset);
} else // File
if (type == HttpParamBean.TYPE_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");
// if(param.getFile()==null) 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());
}
}
} else // XML
if (type == HttpParamBean.TYPE_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 (eeReq == null)
throw new ApplicationException("type xml is only supported for methods get, delete, post, and put");
HTTPEngine4Impl.setBody(eeReq, param.getValueAsString(), mt, cs);
} else // Body
if (type == HttpParamBean.TYPE_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 (eeReq == null)
throw new ApplicationException("type body is only supported for methods get, delete, post, and put");
HTTPEngine4Impl.setBody(eeReq, param.getValue(), mt, cs);
} else {
throw new ApplicationException("invalid type [" + type + "]");
}
}
// post params
if (postParam != null && postParam.size() > 0)
eeReqPost.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 && eeReq != 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);
eeReq.setEntity(entity);
} catch (IOException e) {
throw Caster.toPageException(e);
}
doIt = false;
}
}
if (doIt) {
MultipartEntityBuilder mpeBuilder = MultipartEntityBuilder.create().setStrictMode();
// enabling the line below will append charset=... to the Content-Type header
// if (!StringUtil.isEmpty(charset, true))
// mpeBuilder.setCharset(CharsetUtil.toCharset(charset));
Iterator<FormBodyPart> it = parts.iterator();
while (it.hasNext()) {
FormBodyPart part = it.next();
mpeBuilder.addPart(part);
}
eeReq.setEntity(mpeBuilder.build());
}
// 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
setTimeout(builder, checkRemainingTimeout());
// 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(builder, this.username, this.password, this.workStation, this.domain);
} else
httpContext = HTTPEngine4Impl.setCredentials(builder, httpHost, this.username, this.password, preauth);
}
// set Proxy
ProxyData proxy = null;
if (!StringUtil.isEmpty(this.proxyserver)) {
proxy = ProxyDataImpl.getInstance(this.proxyserver, this.proxyport, this.proxyuser, this.proxypassword);
}
if (pageContext.getConfig().isProxyEnableFor(host)) {
proxy = pageContext.getConfig().getProxyData();
}
HTTPEngine4Impl.setProxy(builder, req, proxy);
}
CloseableHttpClient client = null;
try {
if (httpContext == null)
httpContext = new BasicHttpContext();
Struct cfhttp = new StructImpl();
cfhttp.setEL(ERROR_DETAIL, "");
pageContext.setVariable(result, cfhttp);
// ///////////////////////////////////////// EXECUTE /////////////////////////////////////////////////
client = builder.build();
Executor4 e = new Executor4(pageContext, this, client, httpContext, req, redirect);
HTTPResponse4Impl rsp = null;
if (timeout == null || timeout.getMillis() <= 0) {
try {
rsp = e.execute(httpContext);
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
if (!throwonerror) {
if (t instanceof SocketTimeoutException)
setRequestTimeout(cfhttp);
else
setUnknownHost(cfhttp, t);
return;
}
throw toPageException(t, rsp);
}
} else {
e.start();
try {
synchronized (this) {
// print.err(timeout);
this.wait(timeout.getMillis());
}
} catch (InterruptedException ie) {
throw Caster.toPageException(ie);
}
if (e.t != null) {
if (!throwonerror) {
setUnknownHost(cfhttp, e.t);
return;
}
throw toPageException(e.t, rsp);
}
rsp = e.response;
if (!e.done) {
req.abort();
if (throwonerror)
throw new HTTPException("408 Request Time-out", "a timeout occurred in tag http", 408, "Time-out", rsp == null ? null : rsp.getURL());
setRequestTimeout(cfhttp);
return;
// throw new ApplicationException("timeout");
}
}
// ///////////////////////////////////////// EXECUTE /////////////////////////////////////////////////
Charset responseCharset = CharsetUtil.toCharset(rsp.getCharset());
int statCode = 0;
// Write Response Scope
// String rawHeader=httpMethod.getStatusLine().toString();
String mimetype = null;
String contentEncoding = null;
// status code
cfhttp.set(STATUSCODE, ((rsp.getStatusCode() + " " + rsp.getStatusText()).trim()));
cfhttp.set(STATUS_CODE, new Double(statCode = rsp.getStatusCode()));
cfhttp.set(STATUS_TEXT, (rsp.getStatusText()));
cfhttp.set(HTTP_VERSION, (rsp.getProtocolVersion()));
// responseHeader
lucee.commons.net.http.Header[] headers = rsp.getAllHeaders();
StringBuffer raw = new StringBuffer(rsp.getStatusLine() + " ");
Struct responseHeader = new StructImpl();
Struct cookie;
Array setCookie = new ArrayImpl();
Query cookies = new QueryImpl(new String[] { "name", "value", "path", "domain", "expires", "secure", "httpOnly" }, 0, "cookies");
for (int i = 0; i < headers.length; i++) {
lucee.commons.net.http.Header header = headers[i];
// print.ln(header);
raw.append(header.toString() + " ");
if (header.getName().equalsIgnoreCase("Set-Cookie")) {
setCookie.append(header.getValue());
parseCookie(cookies, header.getValue());
} else {
// print.ln(header.getName()+"-"+header.getValue());
Object value = responseHeader.get(KeyImpl.getInstance(header.getName()), null);
if (value == null)
responseHeader.set(KeyImpl.getInstance(header.getName()), header.getValue());
else {
Array arr = null;
if (value instanceof Array) {
arr = (Array) value;
} else {
arr = new ArrayImpl();
responseHeader.set(KeyImpl.getInstance(header.getName()), arr);
arr.appendEL(value);
}
arr.appendEL(header.getValue());
}
}
// Content-Type
if (header.getName().equalsIgnoreCase("Content-Type")) {
mimetype = header.getValue();
if (mimetype == null)
mimetype = NO_MIMETYPE;
}
// Content-Encoding
if (header.getName().equalsIgnoreCase("Content-Encoding")) {
contentEncoding = header.getValue();
}
}
cfhttp.set(RESPONSEHEADER, responseHeader);
cfhttp.set(KeyConstants._cookies, cookies);
responseHeader.set(STATUS_CODE, new Double(statCode = rsp.getStatusCode()));
responseHeader.set(EXPLANATION, (rsp.getStatusText()));
if (setCookie.size() > 0)
responseHeader.set(SET_COOKIE, setCookie);
// is text
boolean isText = mimetype == null || mimetype == NO_MIMETYPE || HTTPUtil.isTextMimeType(mimetype);
// is multipart
boolean isMultipart = MultiPartResponseUtils.isMultipart(mimetype);
cfhttp.set(KeyConstants._text, Caster.toBoolean(isText));
// boolean responseProvideCharset=false;
if (!StringUtil.isEmpty(mimetype, true)) {
if (isText) {
String[] types = HTTPUtil.splitMimeTypeAndCharset(mimetype, null);
if (types[0] != null)
cfhttp.set(KeyConstants._mimetype, types[0]);
if (types[1] != null)
cfhttp.set(CHARSET, types[1]);
} else
cfhttp.set(KeyConstants._mimetype, mimetype);
} else
cfhttp.set(KeyConstants._mimetype, NO_MIMETYPE);
// File
Resource file = null;
if (strFile != null && strPath != null) {
file = ResourceUtil.toResourceNotExisting(pageContext, strPath).getRealResource(strFile);
} else if (strFile != null) {
file = ResourceUtil.toResourceNotExisting(pageContext, strFile);
} else if (strPath != null) {
file = ResourceUtil.toResourceNotExisting(pageContext, strPath);
// Resource dir = file.getParentResource();
if (file.isDirectory()) {
// TODO was getName()
file = file.getRealResource(req.getURI().getPath());
// ->http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/URI.html#getName()
}
}
if (file != null)
pageContext.getConfig().getSecurityManager().checkFileLocation(file);
// filecontent
InputStream is = null;
if (isText && getAsBinary != GET_AS_BINARY_YES) {
String str;
try {
// read content
if (method != METHOD_HEAD) {
is = rsp.getContentAsStream();
if (is != null && isGzipEncoded(contentEncoding))
is = rsp.getStatusCode() != 200 ? new CachingGZIPInputStream(is) : new GZIPInputStream(is);
}
try {
try {
str = is == null ? "" : IOUtil.toString(is, responseCharset, checkRemainingTimeout().getMillis());
} catch (EOFException eof) {
if (is instanceof CachingGZIPInputStream) {
str = IOUtil.toString(is = ((CachingGZIPInputStream) is).getRawData(), responseCharset, checkRemainingTimeout().getMillis());
} else
throw eof;
}
} catch (UnsupportedEncodingException uee) {
str = IOUtil.toString(is, (Charset) null, checkRemainingTimeout().getMillis());
}
} catch (IOException ioe) {
throw Caster.toPageException(ioe);
} finally {
IOUtil.closeEL(is);
}
if (str == null)
str = "";
if (resolveurl) {
// if(e.redirectURL!=null)url=e.redirectURL.toExternalForm();
str = new URLResolver().transform(str, e.response.getTargetURL(), false);
}
cfhttp.set(KeyConstants._filecontent, str);
try {
if (file != null) {
IOUtil.write(file, str, ((PageContextImpl) pageContext).getWebCharset(), false);
}
} catch (IOException e1) {
}
if (name != null) {
Query qry = CSVParser.toQuery(str, delimiter, textqualifier, columns, firstrowasheaders);
pageContext.setVariable(name, qry);
}
} else // Binary
{
byte[] barr = null;
if (isGzipEncoded(contentEncoding)) {
if (method != METHOD_HEAD) {
is = rsp.getContentAsStream();
is = rsp.getStatusCode() != 200 ? new CachingGZIPInputStream(is) : new GZIPInputStream(is);
}
try {
try {
barr = is == null ? new byte[0] : IOUtil.toBytes(is);
} catch (EOFException eof) {
if (is instanceof CachingGZIPInputStream)
barr = IOUtil.toBytes(((CachingGZIPInputStream) is).getRawData());
else
throw eof;
}
} catch (IOException t) {
throw Caster.toPageException(t);
} finally {
IOUtil.closeEL(is);
}
} else {
try {
if (method != METHOD_HEAD)
barr = rsp.getContentAsByteArray();
else
barr = new byte[0];
} catch (IOException t) {
throw Caster.toPageException(t);
}
}
// IF Multipart response get file content and parse parts
if (barr != null) {
if (isMultipart) {
cfhttp.set(KeyConstants._filecontent, MultiPartResponseUtils.getParts(barr, mimetype));
} else {
cfhttp.set(KeyConstants._filecontent, barr);
}
} else
cfhttp.set(KeyConstants._filecontent, "");
if (file != null) {
try {
if (barr != null)
IOUtil.copy(new ByteArrayInputStream(barr), file, true);
} catch (IOException ioe) {
throw Caster.toPageException(ioe);
}
}
}
// header
cfhttp.set(KeyConstants._header, raw.toString());
if (!isStatusOK(rsp.getStatusCode())) {
String msg = rsp.getStatusCode() + " " + rsp.getStatusText();
cfhttp.setEL(ERROR_DETAIL, msg);
if (throwonerror) {
throw new HTTPException(msg, null, rsp.getStatusCode(), rsp.getStatusText(), rsp.getURL());
}
}
// TODO: check if we can use statCode instead of rsp.getStatusCode() everywhere and cleanup the code
if (cacheHandler != null && rsp.getStatusCode() == 200) {
// add to cache
cacheHandler.set(pageContext, cacheId, cachedWithin, new HTTPCacheItem(cfhttp, url, System.nanoTime() - start));
}
} finally {
if (client != null)
client.close();
}
}
use of org.apache.http.entity.mime.content.ContentBody in project groovity by disney.
the class Http method tag.
@SuppressWarnings({ "rawtypes", "unchecked" })
public Object tag(Map attributes, Closure body) throws Exception {
Object url = resolve(attributes, "url");
if (url == null) {
throw new RuntimeException("<g:http> requires 'url' attribute");
}
Object var = resolve(attributes, VAR);
String method = "GET";
Object methodAtt = resolve(attributes, "method");
if (methodAtt != null) {
method = methodAtt.toString();
}
boolean followRedirects = true;
Object redirectsAtt = resolve(attributes, "redirects");
if (redirectsAtt != null) {
followRedirects = Boolean.parseBoolean(redirectsAtt.toString());
}
CookieOption cookieOption = CookieOption.DEFAULT;
Object cookiesAtt = resolve(attributes, "cookies");
if (cookiesAtt != null) {
cookieOption = CookieOption.valueOf(cookiesAtt.toString().toUpperCase());
}
Object timeout = resolve(attributes, TIMEOUT);
final int timeoutSeconds = timeout == null ? -1 : timeout instanceof Number ? ((Number) timeout).intValue() : Integer.parseInt(timeout.toString());
Object target = resolve(attributes, "to");
if (target instanceof Class) {
if (!Object.class.equals(target)) {
target = ((Class) target).newInstance();
}
}
if (target == null) {
target = Object.class;
}
Object async = resolve(attributes, "async");
if (async != null && !(async instanceof Boolean)) {
async = Boolean.valueOf(async.toString());
}
HttpEntity dataEntity = null;
Object data = resolve(attributes, "data");
HttpClientContext clientContext = resolve(attributes, "context", HttpClientContext.class);
if (clientContext == null) {
clientContext = HttpClientContext.create();
}
if (clientContext.getCookieStore() == null) {
// we don't want to let cookies be shared across contexts
clientContext.setCookieStore(new BasicCookieStore());
}
if (clientContext.getAuthCache() == null) {
// we also don't want to share credentials across contexts
clientContext.setAuthCache(new BasicAuthCache());
}
final HttpClientContext fContext = clientContext;
ScriptHelper context = getScriptHelper(body);
Object oldOut = get(context, OUT);
// execute body to assemble URL params, headers, post body
Map variables = context.getBinding().getVariables();
URI uri;
URIBuilder builder;
ArrayList<Header> headers;
Optional<UserPass> userPass;
Optional<HttpSignatureSigner> signer;
Optional<HttpRequestInterceptor> interceptor;
try {
builder = new URIBuilder(url.toString());
bind(context, Uri.CURRENT_URI_BUILDER, builder);
headers = new ArrayList<Header>();
bind(context, com.disney.groovity.tags.Header.CURRENT_LIST_FOR_HEADERS, headers);
Credentials.acceptCredentials(variables);
Signature.acceptSigner(variables);
acceptInterceptor(variables);
StringWriter sw = new StringWriter();
bind(context, OUT, sw);
try {
Object rval = body.call();
if (rval instanceof Writable) {
((Writable) rval).writeTo(sw);
}
} finally {
bind(context, OUT, oldOut);
userPass = Credentials.resolveCredentials(variables);
signer = Signature.resolveSigner(variables);
interceptor = resolveInterceptor(variables);
}
String val = sw.toString();
if (val.trim().length() > 0) {
dataEntity = new StringEntity(val);
}
uri = builder.build();
if (userPass.isPresent()) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(userPass.get().getUser(), new String(userPass.get().getPass())));
clientContext.setCredentialsProvider(credsProvider);
}
} catch (URISyntaxException e1) {
throw new RuntimeException("Invalid URI " + url, e1);
} finally {
unbind(context, Uri.CURRENT_URI_BUILDER);
unbind(context, com.disney.groovity.tags.Header.CURRENT_LIST_FOR_HEADERS);
}
final HttpRequestBase request = "POST".equalsIgnoreCase(method) ? new HttpPost(uri) : "PUT".equalsIgnoreCase(method) ? new HttpPut(uri) : "HEAD".equalsIgnoreCase(method) ? new HttpHead(uri) : "DELETE".equalsIgnoreCase(method) ? new HttpDelete(uri) : "OPTIONS".equalsIgnoreCase(method) ? new HttpOptions(uri) : new HttpGet(uri);
if (headers.size() > 0) {
request.setHeaders(headers.toArray(new Header[0]));
}
if (request instanceof HttpEntityEnclosingRequest) {
if (data != null) {
// decide on strategy to convert data to entity
if (data instanceof HttpEntity) {
dataEntity = (HttpEntity) data;
} else {
// look at content type for a hint
Header targetType = request.getFirstHeader("Content-Type");
if (targetType != null && targetType.getValue().contains("json")) {
CharArrayWriter caw = new CharArrayWriter();
new ModelJsonWriter(caw).visit(data);
dataEntity = new StringEntity(caw.toString());
} else if (targetType != null && targetType.getValue().contains("xml")) {
if (data instanceof groovy.util.Node) {
dataEntity = new StringEntity(XmlUtil.serialize((groovy.util.Node) data));
} else if (data instanceof GPathResult) {
dataEntity = new StringEntity(XmlUtil.serialize((GPathResult) data));
} else if (data instanceof Element) {
dataEntity = new StringEntity(XmlUtil.serialize((Element) data));
} else if (data instanceof Document) {
dataEntity = new StringEntity(XmlUtil.serialize(((Document) data).getDocumentElement()));
} else {
// if it's not an XML model assume it's a well formed XML string
dataEntity = new StringEntity(data.toString());
}
} else if ((targetType != null && targetType.getValue().contains("x-www-form-urlencoded")) || (targetType == null && (data instanceof Map || data instanceof List))) {
// key/value pairs, accept a map, a list of maps, or a list of NameValuePairs
Iterator source = data instanceof Map ? ((Map) data).entrySet().iterator() : ((List) data).iterator();
ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
while (source.hasNext()) {
Object next = source.next();
if (next instanceof Map.Entry) {
Map.Entry entry = (Entry) next;
pairs.add(new BasicNameValuePair(entry.getKey().toString(), entry.getValue() != null ? entry.getValue().toString() : ""));
} else if (next instanceof NameValuePair) {
pairs.add((NameValuePair) next);
} else if (next instanceof Map) {
Iterator<Map.Entry> sub = ((Map) next).entrySet().iterator();
while (sub.hasNext()) {
Map.Entry se = sub.next();
pairs.add(new BasicNameValuePair(se.getKey().toString(), se.getValue() != null ? se.getValue().toString() : ""));
}
}
}
dataEntity = new UrlEncodedFormEntity(pairs);
} else if (targetType != null && targetType.getValue().contains("multipart/form-data")) {
// list of maps, each map must contain "name" and "body", plus optional "type" and "filename"
Iterator<Map> parts = ((List<Map>) data).iterator();
MultipartEntityBuilder meBuilder = MultipartEntityBuilder.create();
while (parts.hasNext()) {
Map part = parts.next();
Object pbody = part.get("body");
String name = (String) part.get("name");
String type = (String) part.get("type");
String filename = (String) part.get("filename");
ContentType ct = type != null ? ContentType.parse(type) : null;
if (pbody instanceof File) {
if (ct == null) {
ct = ContentType.DEFAULT_BINARY;
}
meBuilder.addBinaryBody(name, (File) pbody, ct, filename);
} else if (pbody instanceof byte[]) {
if (ct == null) {
ct = ContentType.DEFAULT_BINARY;
}
meBuilder.addBinaryBody(name, (byte[]) pbody, ct, filename);
} else if (pbody instanceof ContentBody) {
meBuilder.addPart(name, (ContentBody) pbody);
} else if (pbody instanceof InputStream) {
if (ct == null) {
ct = ContentType.DEFAULT_BINARY;
}
meBuilder.addBinaryBody(name, (InputStream) pbody, ct, filename);
} else {
if (ct == null) {
ct = ContentType.DEFAULT_TEXT;
}
meBuilder.addTextBody(name, pbody.toString(), ct);
}
}
dataEntity = meBuilder.build();
} else {
// no help from content type header, check for modeled XML
if (data instanceof groovy.util.Node) {
dataEntity = new StringEntity(XmlUtil.serialize((groovy.util.Node) data), ContentType.APPLICATION_XML);
} else if (data instanceof GPathResult) {
dataEntity = new StringEntity(XmlUtil.serialize((GPathResult) data), ContentType.APPLICATION_XML);
} else if (data instanceof Element) {
dataEntity = new StringEntity(XmlUtil.serialize((Element) data), ContentType.APPLICATION_XML);
} else if (data instanceof Document) {
dataEntity = new StringEntity(XmlUtil.serialize(((Document) data).getDocumentElement()), ContentType.APPLICATION_XML);
} else if (data instanceof byte[]) {
dataEntity = new ByteArrayEntity((byte[]) data);
} else if (data instanceof InputStream) {
dataEntity = new InputStreamEntity((InputStream) data);
} else if (data instanceof File) {
dataEntity = new FileEntity((File) data);
} else {
// best option left is to post the toString value of the data
dataEntity = new StringEntity(data.toString());
}
}
}
}
if (dataEntity != null) {
((HttpEntityEnclosingRequest) request).setEntity(dataEntity);
}
}
RequestConfig.Builder configBuilder = request.getConfig() == null ? RequestConfig.custom() : RequestConfig.copy(request.getConfig());
if (!followRedirects) {
configBuilder.setRedirectsEnabled(followRedirects);
}
configBuilder.setCookieSpec(cookieOption.getCookieSpec());
request.setConfig(configBuilder.build());
final String varName = var != null ? var.toString() : null;
ResponseHandler handler = null;
try {
Function handlerFunction = (Function) get(body, Handler.HANDLER_BINDING);
if (handlerFunction != null) {
handler = new ResponseHandler<Object>() {
@Override
public Object handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
return handlerFunction.apply(response);
}
};
}
unbind(body, Handler.HANDLER_BINDING);
} catch (Exception e) {
}
if (handler == null) {
handler = new AutoParsingResponseHandler(target);
}
final List<HttpRequestInterceptor> interceptors = new ArrayList<>();
if (signer.isPresent()) {
interceptors.add(signer.get());
}
if (interceptor.isPresent()) {
interceptors.add(interceptor.get());
}
final ResponseHandler rHandler = handler;
final boolean isAsync = (async != null && Boolean.TRUE.equals(async));
Callable<Object> requester = new Callable() {
public Object call() throws Exception {
TimeoutTask timeoutTask = null;
if (timeoutSeconds > 0) {
timeoutTask = new TimeoutTask(request);
timeoutTimer.schedule(timeoutTask, timeoutSeconds * 1000);
}
try {
Binding oldThreadBinding = null;
if (isAsync) {
oldThreadBinding = ScriptHelper.THREAD_BINDING.get();
Binding asyncBinding = new Binding();
asyncBinding.setVariable("request", request);
ScriptHelper.THREAD_BINDING.set(asyncBinding);
}
try {
for (HttpRequestInterceptor interceptor : interceptors) {
interceptor.process(request, null);
}
return httpClient.execute(request, rHandler, fContext);
} finally {
if (isAsync) {
if (oldThreadBinding == null) {
ScriptHelper.THREAD_BINDING.remove();
} else {
ScriptHelper.THREAD_BINDING.set(oldThreadBinding);
}
}
}
} catch (HttpResponseException e) {
if (isAsync) {
log.error("Async HTTP response error for " + request.getURI() + ": " + e.getMessage());
}
throw e;
} catch (Exception e) {
if (request.isAborted()) {
if (isAsync) {
log.error("Async <g:http> request timed out for " + request.getURI());
}
throw new TimeoutException("Timed out executing <g:http> for " + request.getURI());
} else {
if (isAsync) {
log.error("Async <g:http> request error for " + request.getURI(), e);
}
throw new RuntimeException("Error executing <g:http> for " + request.getURI(), e);
}
} finally {
if (timeoutTask != null) {
timeoutTask.cancel();
}
}
}
};
Object responseVar = null;
if (isAsync) {
// return the Future to the calling code
Future<Object> f = asyncExecutor.submit(requester);
responseVar = new Future<Object>() {
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return f.cancel(mayInterruptIfRunning);
}
@Override
public boolean isCancelled() {
return f.isCancelled();
}
@Override
public boolean isDone() {
return f.isDone();
}
@Override
public Object get() throws InterruptedException, ExecutionException {
GroovityStatistics.startExecution("http(async)");
try {
return f.get();
} finally {
GroovityStatistics.endExecution();
}
}
@Override
public Object get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
GroovityStatistics.startExecution("http(async)");
try {
return f.get(timeout, unit);
} finally {
GroovityStatistics.endExecution();
}
}
};
} else {
// return the parsed/handled response object
GroovityStatistics.startExecution("http(sync)");
try {
responseVar = requester.call();
} finally {
GroovityStatistics.endExecution();
}
}
if (varName != null) {
bind(context, varName, responseVar);
}
return responseVar;
}
use of org.apache.http.entity.mime.content.ContentBody in project product-iots by wso2.
the class HTTPInvoker method uploadFile.
public static HTTPResponse uploadFile(String url, String fileName, String fileContentType) {
HttpPost post = null;
HttpResponse response = null;
HTTPResponse httpResponse = new HTTPResponse();
CloseableHttpClient httpclient = null;
try {
httpclient = (CloseableHttpClient) createHttpClient();
post = new HttpPost(url);
File file = new File(fileName);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, fileContentType);
mpEntity.addPart("file", cbFile);
post.setEntity(mpEntity);
post.setHeader(Constants.Header.AUTH, OAUTH_BEARER + oAuthToken);
// post.setHeader(Constants.Header.CONTENT_TYPE, "multipart/form-data");
post.setHeader("Accept", Constants.ContentType.APPLICATION_JSON);
response = httpclient.execute(post);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
BufferedReader rd = null;
try {
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
} catch (IOException e) {
e.printStackTrace();
}
StringBuffer result = new StringBuffer();
String line = "";
try {
while ((line = rd.readLine()) != null) {
result.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
httpResponse.setResponseCode(response.getStatusLine().getStatusCode());
httpResponse.setResponse(result.toString());
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
return httpResponse;
}
use of org.apache.http.entity.mime.content.ContentBody in project stanbol by apache.
the class MultipartRequestTest method testUploadMultipleContents.
/**
* This uploads the HTML as well as the plain text version of an content.
* This allows it CMS to parse already available alternate content versions
* in a single request. Stanbol can than still use the original content
* (e.g. to extract metadata) but other engines that require the alternate
* version (e.g. plain text version) of an document will directly use the
* parsed version .<p>
* This UnitTest ensures this by adding a "secret" extension the to plain
* text version and than checks if the two entities mentioned in that
* part are included in the extracted entities.
* @throws IOException
*/
@Test
public void testUploadMultipleContents() throws IOException {
// It is a secret, that Berlin is the capital of Germany
String extraTextConent = TEXT_CONTENT + "\nIt is a secret, that the city of Berlin is the capital of Germany since 1990.";
// The multipartBuilder used to construct the contentItem for the contentItem
MultipartEntityBuilder ciBuilder = MultipartEntityBuilder.create();
String boundary = "contentItem-47jjksnbue73fnis";
ciBuilder.setBoundary(boundary);
// use a small extension to deal with multipart/alternate
Map<String, ContentBody> alternates = new LinkedHashMap<String, ContentBody>();
alternates.put("http://www.example.com/test.html", new StringBody(HTML_CONTENT, ContentType.TEXT_HTML.withCharset(UTF8)));
alternates.put("http://www.example.com/test.txt", new StringBody(extraTextConent, ContentType.TEXT_PLAIN.withCharset(UTF8)));
ciBuilder.addPart("content", new MultipartContentBody(alternates, "contentParts", ContentType.create("multipart/alternate")));
String receivedContent = executor.execute(builder.buildPostRequest(getEndpoint()).withHeader("Accept", "text/rdf+nt").withEntity(ciBuilder.build())).assertStatus(200).assertContentRegexp(// and the expected enhancements in the metadata
"http://purl.org/dc/terms/creator.*LanguageDetectionEnhancementEngine", "http://purl.org/dc/terms/language.*en", "http://fise.iks-project.eu/ontology/entity-label.*Paris", "http://purl.org/dc/terms/creator.*org.apache.stanbol.enhancer.engines.opennlp.*NamedEntityExtractionEnhancementEngine", "http://fise.iks-project.eu/ontology/entity-label.*Bob Marley", // check also for expeted entities extracted from the secret Text part!
"http://fise.iks-project.eu/ontology/entity-label.*Berlin", "http://fise.iks-project.eu/ontology/entity-label.*Germany").getContent();
log.debug("Content:\n{}\n", receivedContent);
}
use of org.apache.http.entity.mime.content.ContentBody in project stanbol by apache.
the class ContentItemWriter method writeTo.
@Override
public void writeTo(ContentItem ci, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
// (0) handle default dataType
Map<String, Object> reqProp = ContentItemHelper.getRequestPropertiesContentPart(ci);
boolean omitMetadata = isOmitMetadata(reqProp);
if (!MULTIPART.isCompatible(mediaType)) {
// two possible cases
if (!omitMetadata) {
// (1) just return the RDF data
// (1.a) Backward support for default dataType if no Accept header is set
StringBuilder ctb = new StringBuilder();
if (mediaType.isWildcardType() || TEXT_PLAIN_TYPE.isCompatible(mediaType) || APPLICATION_OCTET_STREAM_TYPE.isCompatible(mediaType)) {
ctb.append(APPLICATION_LD_JSON);
} else {
ctb.append(mediaType.getType()).append('/').append(mediaType.getSubtype());
}
ctb.append(";charset=").append(UTF8.name());
String contentType = ctb.toString();
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, contentType);
try {
serializer.serialize(entityStream, ci.getMetadata(), contentType);
} catch (UnsupportedSerializationFormatException e) {
throw new WebApplicationException("The enhancement results " + "cannot be serialized in the requested media type: " + mediaType.toString(), Response.Status.NOT_ACCEPTABLE);
}
} else {
// (2) return a single content part
Entry<IRI, Blob> contentPart = getBlob(ci, Collections.singleton(mediaType.toString()));
if (contentPart == null) {
// no alternate content with the requeste media type
throw new WebApplicationException("The requested enhancement chain has not created an " + "version of the parsed content in the reuqest media type " + mediaType.toString(), Response.Status.UNSUPPORTED_MEDIA_TYPE);
} else {
// found -> stream the content to the client
// NOTE: This assumes that the presence of a charset
// implies reading/writing character streams
String requestedCharset = mediaType.getParameters().get("charset");
String blobCharset = contentPart.getValue().getParameter().get("charset");
Charset readerCharset = blobCharset == null ? UTF8 : Charset.forName(blobCharset);
Charset writerCharset = requestedCharset == null ? null : Charset.forName(requestedCharset);
if (writerCharset != null && !writerCharset.equals(readerCharset)) {
// we need to transcode
Reader reader = new InputStreamReader(contentPart.getValue().getStream(), readerCharset);
Writer writer = new OutputStreamWriter(entityStream, writerCharset);
IOUtils.copy(reader, writer);
IOUtils.closeQuietly(reader);
} else {
// no transcoding
if (requestedCharset == null && blobCharset != null) {
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, mediaType.toString() + "; charset=" + blobCharset);
}
InputStream in = contentPart.getValue().getStream();
IOUtils.copy(in, entityStream);
IOUtils.closeQuietly(in);
}
}
}
} else {
// multipart mime requested!
final String charsetName = mediaType.getParameters().get("charset");
final Charset charset = charsetName != null ? Charset.forName(charsetName) : UTF8;
MediaType rdfFormat;
String rdfFormatString = getRdfFormat(reqProp);
if (rdfFormatString == null || rdfFormatString.isEmpty()) {
rdfFormat = DEFAULT_RDF_FORMAT;
} else {
try {
rdfFormat = MediaType.valueOf(rdfFormatString);
if (rdfFormat.getParameters().get("charset") == null) {
// use the charset of the default RDF format
rdfFormat = new MediaType(rdfFormat.getType(), rdfFormat.getSubtype(), DEFAULT_RDF_FORMAT.getParameters());
}
} catch (IllegalArgumentException e) {
throw new WebApplicationException("The specified RDF format '" + rdfFormatString + "' (used to serialize all RDF parts of " + "multipart MIME responses) is not a well formated MIME type", Response.Status.BAD_REQUEST);
}
}
// (1) setting the correct header
String contentType = String.format("%s/%s; charset=%s; boundary=%s", mediaType.getType(), mediaType.getSubtype(), charset.toString(), CONTENT_ITEM_BOUNDARY);
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, contentType);
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.setBoundary(CONTENT_ITEM_BOUNDARY);
// (2) serialising the metadata
if (!isOmitMetadata(reqProp)) {
entityBuilder.addPart("metadata", new ClerezzaContentBody(ci.getUri().getUnicodeString(), ci.getMetadata(), rdfFormat));
// entity.addBodyPart(new FormBodyPart("metadata", new ClerezzaContentBody(
// ci.getUri().getUnicodeString(), ci.getMetadata(),
// rdfFormat)));
}
// (3) serialising the Content (Bloby)
// (3.a) Filter based on parameter
List<Entry<IRI, Blob>> includedBlobs = filterBlobs(ci, reqProp);
// (3.b) Serialise the filtered
if (!includedBlobs.isEmpty()) {
Map<String, ContentBody> contentParts = new LinkedHashMap<String, ContentBody>();
for (Entry<IRI, Blob> entry : includedBlobs) {
Blob blob = entry.getValue();
ContentType ct = ContentType.create(blob.getMimeType());
String cs = blob.getParameter().get("charset");
if (StringUtils.isNotBlank(cs)) {
ct = ct.withCharset(cs);
}
contentParts.put(entry.getKey().getUnicodeString(), new InputStreamBody(blob.getStream(), ct));
}
// add all the blobs
entityBuilder.addPart("content", new MultipartContentBody(contentParts, CONTENT_PARTS_BOUNDERY, MULTIPART_ALTERNATE));
}
// else no content to include
Set<String> includeContentParts = getIncludedContentPartURIs(reqProp);
if (includeContentParts != null) {
// (4) serialise the Request Properties
if (includeContentParts.isEmpty() || includeContentParts.contains(REQUEST_PROPERTIES_URI.getUnicodeString())) {
JSONObject object;
try {
object = toJson(reqProp);
} catch (JSONException e) {
String message = "Unable to convert Request Properties " + "to JSON (values : " + reqProp + ")!";
log.error(message, e);
throw new WebApplicationException(message, Response.Status.INTERNAL_SERVER_ERROR);
}
entityBuilder.addTextBody(REQUEST_PROPERTIES_URI.getUnicodeString(), object.toString(), ContentType.APPLICATION_JSON.withCharset(UTF8));
}
// (5) additional RDF metadata stored in contentParts
for (Entry<IRI, Graph> entry : getContentParts(ci, Graph.class).entrySet()) {
if (includeContentParts.isEmpty() || includeContentParts.contains(entry.getKey())) {
entityBuilder.addPart(entry.getKey().getUnicodeString(), new // no file name
ClerezzaContentBody(// no file name
null, entry.getValue(), rdfFormat));
}
// else ignore this content part
}
}
entityBuilder.build().writeTo(entityStream);
}
}
Aggregations