Search in sources :

Example 1 with HTTPResponse

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;
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) InputStream(java.io.InputStream) HTTPResponse(lucee.commons.net.http.HTTPResponse) PageRuntimeException(lucee.runtime.exp.PageRuntimeException) MimeType(lucee.commons.lang.mimetype.MimeType) Struct(lucee.runtime.type.Struct)

Example 2 with HTTPResponse

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);
    }
}
Also used : ConverterException(lucee.runtime.converter.ConverterException) HashMap(java.util.HashMap) InputStream(java.io.InputStream) HTTPResponse(lucee.commons.net.http.HTTPResponse) Charset(java.nio.charset.Charset) IOException(java.io.IOException) MimeType(lucee.commons.lang.mimetype.MimeType) ApplicationException(lucee.runtime.exp.ApplicationException) Header(lucee.commons.net.http.Header) ScriptConverter(lucee.runtime.converter.ScriptConverter) JSONConverter(lucee.runtime.converter.JSONConverter)

Example 3 with HTTPResponse

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;
}
Also used : HeaderImpl(lucee.commons.net.http.httpclient.HeaderImpl) HTTPResponse(lucee.commons.net.http.HTTPResponse) Resource(lucee.commons.io.res.Resource) IOException(java.io.IOException) URL(java.net.URL) RHExtensionProvider(lucee.runtime.extension.RHExtensionProvider) Header(lucee.commons.net.http.Header)

Example 4 with HTTPResponse

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;
}
Also used : HTTPResponse(lucee.commons.net.http.HTTPResponse)

Example 5 with HTTPResponse

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);
    }
}
Also used : HTTPResponse(lucee.commons.net.http.HTTPResponse) IOException(java.io.IOException)

Aggregations

HTTPResponse (lucee.commons.net.http.HTTPResponse)10 IOException (java.io.IOException)8 Header (lucee.commons.net.http.Header)5 URL (java.net.URL)4 InputStream (java.io.InputStream)3 Resource (lucee.commons.io.res.Resource)3 ApplicationException (lucee.runtime.exp.ApplicationException)3 Charset (java.nio.charset.Charset)2 HashMap (java.util.HashMap)2 MimeType (lucee.commons.lang.mimetype.MimeType)2 HeaderImpl (lucee.commons.net.http.httpclient.HeaderImpl)2 ConverterException (lucee.runtime.converter.ConverterException)2 JSONConverter (lucee.runtime.converter.JSONConverter)2 PageException (lucee.runtime.exp.PageException)2 RHExtensionProvider (lucee.runtime.extension.RHExtensionProvider)2 Struct (lucee.runtime.type.Struct)2 MalformedURLException (java.net.MalformedURLException)1 Log (lucee.commons.io.log.Log)1 Credentials (lucee.commons.security.Credentials)1 PageContext (lucee.runtime.PageContext)1