use of lucee.commons.net.http.HTTPResponse in project Lucee by lucee.
the class HTTPClient method getMetaData.
private Struct getMetaData(PageContext pc) {
if (meta == null) {
pc = ThreadLocalPageContext.get(pc);
InputStream is = null;
HTTPResponse rsp = null;
try {
rsp = HTTPEngine.get(metaURL, username, password, -1, false, "UTF-8", createUserAgent(pc), proxyData, null);
MimeType mt = getMimeType(rsp, null);
int format = MimeType.toFormat(mt, -1);
if (format == -1)
throw new ApplicationException("cannot convert response with mime type [" + mt + "] to a CFML Object");
is = rsp.getContentAsStream();
Struct data = Caster.toStruct(ReqRspUtil.toObject(pc, IOUtil.toBytes(is, false), format, mt.getCharset(), null));
Object oUDF = data.get(KeyConstants._functions, null);
Object oAACF = data.get(ComponentPageImpl.ACCEPT_ARG_COLL_FORMATS, null);
if (oUDF != null && oAACF != null) {
meta = Caster.toStruct(oUDF);
String[] strFormats = ListUtil.listToStringArray(Caster.toString(oAACF), ',');
argumentsCollectionFormat = UDFUtil.toReturnFormat(strFormats, UDF.RETURN_FORMAT_JSON);
} else {
meta = data;
}
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
throw new PageRuntimeException(Caster.toPageException(t));
} finally {
IOUtil.closeEL(is);
HTTPEngine.closeEL(rsp);
}
}
return meta;
}
use of lucee.commons.net.http.HTTPResponse in project Lucee by lucee.
the class HTTPClient method _callWithNamedValues.
private Object _callWithNamedValues(PageContext pc, Key methodName, Struct args) throws PageException {
// prepare request
Map<String, String> formfields = new HashMap<String, String>();
formfields.put("method", methodName.getString());
formfields.put("returnformat", "cfml");
String str;
try {
if (UDF.RETURN_FORMAT_JSON == argumentsCollectionFormat) {
Charset cs = pc.getWebCharset();
str = new JSONConverter(true, cs).serialize(pc, args, false);
formfields.put("argumentCollectionFormat", "json");
} else if (UDF.RETURN_FORMAT_SERIALIZE == argumentsCollectionFormat) {
str = new ScriptConverter().serialize(args);
formfields.put("argumentCollectionFormat", "cfml");
} else {
// Json interpreter also accepts cfscript
str = new ScriptConverter().serialize(args);
}
} catch (ConverterException e) {
throw Caster.toPageException(e);
}
// add aparams to request
formfields.put("argumentCollection", str);
/*
Iterator<Entry<Key, Object>> it = args.entryIterator();
Entry<Key, Object> e;
while(it.hasNext()){
e = it.next();
formfields.put(e.getKey().getString(), Caster.toString(e.getValue()));
}*/
Map<String, String> headers = new HashMap<String, String>();
// application/java disabled for the moment, it is not working when we have different lucee versions
headers.put("accept", "application/cfml,application/json");
HTTPResponse rsp = null;
InputStream is = null;
try {
// call remote cfc
rsp = HTTPEngine.post(url, username, password, -1, false, "UTF-8", createUserAgent(pc), proxyData, headers, formfields);
// read result
Header[] rspHeaders = rsp.getAllHeaders();
MimeType mt = getMimeType(rspHeaders, null);
int format = MimeType.toFormat(mt, -1);
if (format == -1) {
if (rsp.getStatusCode() != 200) {
boolean hasMsg = false;
String msg = rsp.getStatusText();
for (int i = 0; i < rspHeaders.length; i++) {
if (rspHeaders[i].getName().equalsIgnoreCase("exception-message")) {
msg = rspHeaders[i].getValue();
hasMsg = true;
}
}
is = rsp.getContentAsStream();
ApplicationException ae = new ApplicationException("remote component throws the following error:" + msg);
if (!hasMsg)
ae.setAdditional(KeyImpl.init("respone-body"), IOUtil.toString(is, mt.getCharset()));
throw ae;
}
throw new ApplicationException("cannot convert response with mime type [" + mt + "] to a CFML Object");
}
is = rsp.getContentAsStream();
return ReqRspUtil.toObject(pc, IOUtil.toBytes(is, false), format, mt.getCharset(), null);
} catch (IOException ioe) {
throw Caster.toPageException(ioe);
} finally {
IOUtil.closeEL(is);
HTTPEngine.closeEL(rsp);
}
}
use of lucee.commons.net.http.HTTPResponse in project Lucee by lucee.
the class DeployHandler method downloadExtension.
public static Resource downloadExtension(Config config, ExtensionDefintion ed, Log log) {
String apiKey = config.getIdentification().getApiKey();
URL url;
RHExtensionProvider[] providers = ((ConfigImpl) config).getRHExtensionProviders();
for (int i = 0; i < providers.length; i++) {
HTTPResponse rsp = null;
try {
url = providers[i].getURL();
StringBuilder qs = new StringBuilder();
addQueryParam(qs, "ioid", apiKey);
addQueryParam(qs, "version", ed.getVersion());
url = new URL(url, "/rest/extension/provider/full/" + ed.getId() + qs);
rsp = HTTPEngine.get(url, null, null, -1, false, "UTF-8", "", null, new Header[] { new HeaderImpl("accept", "application/cfml") });
if (rsp.getStatusCode() != 200)
throw new IOException("failed to load extension: " + ed);
// copy it locally
Resource res = SystemUtil.getTempDirectory().getRealResource(ed.getId() + "-" + ed.getVersion() + ".lex");
ResourceUtil.touch(res);
IOUtil.copy(rsp.getContentAsStream(), res, true);
return res;
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
if (log != null)
log.error("extension", t);
} finally {
HTTPEngine.closeEL(rsp);
}
}
return null;
}
use of lucee.commons.net.http.HTTPResponse in project Lucee by lucee.
the class HTTPUtil method length.
/**
* return the length of a file defined by a url.
* @param dataUrl
* @return
* @throws IOException
*/
public static long length(URL url) throws IOException {
HTTPResponse http = HTTPEngine.head(url, null, null, -1, true, null, Constants.NAME, null, null);
long len = http.getContentLength();
HTTPEngine.closeEL(http);
return len;
}
use of lucee.commons.net.http.HTTPResponse in project Lucee by lucee.
the class HTTPResource method getInputStream.
@Override
public InputStream getInputStream() throws IOException {
// ResourceUtil.checkGetInputStreamOK(this);
// provider.lock(this);
provider.read(this);
HTTPResponse method = getHTTPResponse(true);
try {
return IOUtil.toBufferedInputStream(method.getContentAsStream());
} catch (IOException e) {
// provider.unlock(this);
throw e;
} finally {
HTTPEngine.closeEL(method);
}
}
Aggregations