Search in sources :

Example 1 with AbstractHttpRequest

use of com.bluenimble.platform.http.request.impls.AbstractHttpRequest in project serverless by bluenimble.

the class OAuthHttpRequestVisitor method visit.

@Override
public void visit(HttpRequest request, HttpURLConnection connection) throws HttpRequestWriteException {
    HttpEndpoint endpoint = ((AbstractHttpRequest) request).getEndpoint();
    OAuthConsumer consumer = new DefaultOAuthConsumer(key, secret);
    HttpParameters encodedParams = new HttpParameters();
    List<HttpParameter> params = request.getParameters();
    if (params != null && !params.isEmpty()) {
        for (HttpParameter p : params) {
            if (p.getValue() != null) {
                encodedParams.put(p.getName(), OAuth.percentEncode(String.valueOf(p.getValue())));
            }
        }
    }
    encodedParams.put("realm", endpoint.getScheme() + "://" + endpoint.getHost() + endpoint.getPath());
    consumer.setAdditionalParameters(encodedParams);
    try {
        consumer.sign(connection);
    } catch (Exception e) {
        throw new HttpRequestWriteException(e.getMessage(), e);
    }
}
Also used : HttpParameters(oauth.signpost.http.HttpParameters) AbstractHttpRequest(com.bluenimble.platform.http.request.impls.AbstractHttpRequest) DefaultOAuthConsumer(oauth.signpost.basic.DefaultOAuthConsumer) DefaultOAuthConsumer(oauth.signpost.basic.DefaultOAuthConsumer) OAuthConsumer(oauth.signpost.OAuthConsumer) HttpEndpoint(com.bluenimble.platform.http.HttpEndpoint) HttpParameter(com.bluenimble.platform.http.HttpParameter) HttpRequestWriteException(com.bluenimble.platform.http.request.HttpRequestWriteException) HttpRequestWriteException(com.bluenimble.platform.http.request.HttpRequestWriteException)

Example 2 with AbstractHttpRequest

use of com.bluenimble.platform.http.request.impls.AbstractHttpRequest in project serverless by bluenimble.

the class Http method request.

private static HttpRequest request(String verb, JsonObject spec, HttpRequestVisitor visitor) throws HttpClientException {
    verb = verb.toUpperCase();
    Class<? extends AbstractHttpRequest> requestClass = Requests.get(verb);
    if (requestClass == null) {
        throw new HttpClientException("unsupported http method " + verb);
    }
    String url = Json.getString(spec, Spec.Url);
    AbstractHttpRequest request;
    try {
        request = requestClass.getConstructor(new Class[] { HttpEndpoint.class }).newInstance(HttpUtils.createEndpoint(new URI(url)));
    } catch (Exception ex) {
        throw new HttpClientException(ex.getMessage(), ex);
    }
    request.setVisitor(visitor);
    // add timeouts
    JsonObject oTimeouts = Json.getObject(spec, Spec.timeouts.class.getSimpleName());
    if (oTimeouts != null && !oTimeouts.isEmpty()) {
        int connectTimeout = Json.getInteger(oTimeouts, Spec.timeouts.Connect, 0);
        if (connectTimeout > 100) {
            request.setConnectTimeout(connectTimeout);
        }
        int readTimeout = Json.getInteger(oTimeouts, Spec.timeouts.Read, 0);
        if (readTimeout > 100) {
            request.setReadTimeout(readTimeout);
        }
    }
    // add proxy
    JsonObject oProxy = Json.getObject(spec, Spec.proxy.class.getSimpleName());
    if (oProxy != null && !oProxy.isEmpty()) {
        Proxy.Type proxyType = Proxy.Type.valueOf(Json.getString(oProxy, Spec.proxy.Type, Proxy.Type.HTTP.name()).toUpperCase());
        int proxyPort = Json.getInteger(oProxy, Spec.proxy.Port, 8080);
        Proxy proxy = new Proxy(proxyType, new InetSocketAddress(Json.getString(oProxy, Spec.proxy.Endpoint), proxyPort));
        request.setProxy(proxy);
    }
    String contentType = null;
    // add headers
    JsonObject headers = Json.getObject(spec, Spec.Headers);
    if (headers != null && !headers.isEmpty()) {
        List<HttpHeader> hHeaders = new ArrayList<HttpHeader>();
        request.setHeaders(hHeaders);
        Iterator<String> keys = headers.keys();
        while (keys.hasNext()) {
            String h = keys.next();
            String hv = String.valueOf(headers.get(h));
            if (HttpHeaders.CONTENT_TYPE.toUpperCase().equals(h.toUpperCase())) {
                contentType = hv;
            } else {
                hHeaders.add(new HttpHeaderImpl(h, hv));
            }
        }
        headers.remove(HttpHeaders.CONTENT_TYPE);
    }
    request.setContentType(contentType);
    // add params
    JsonObject data = Json.getObject(spec, Spec.Data);
    if (data != null && !data.isEmpty()) {
        if (ContentTypes.Json.equals(contentType)) {
            HttpMessageBody body = new HttpMessageBodyImpl();
            request.setBody(body);
            body.add(new StringHttpMessageBodyPart(data.toString()));
        } else {
            List<HttpParameter> hParams = new ArrayList<HttpParameter>();
            request.setParameters(hParams);
            Iterator<String> keys = data.keys();
            while (keys.hasNext()) {
                String p = keys.next();
                hParams.add(new HttpParameterImpl(p, data.get(p)));
            }
        }
    }
    return request;
}
Also used : HttpMessageBody(com.bluenimble.platform.http.HttpMessageBody) InetSocketAddress(java.net.InetSocketAddress) StringHttpMessageBodyPart(com.bluenimble.platform.http.impls.StringHttpMessageBodyPart) ArrayList(java.util.ArrayList) JsonObject(com.bluenimble.platform.json.JsonObject) URI(java.net.URI) HttpHeaderImpl(com.bluenimble.platform.http.impls.HttpHeaderImpl) HttpParameter(com.bluenimble.platform.http.HttpParameter) HttpClientException(com.bluenimble.platform.http.HttpClientException) HttpEndpoint(com.bluenimble.platform.http.HttpEndpoint) HttpParameterImpl(com.bluenimble.platform.http.impls.HttpParameterImpl) Proxy(java.net.Proxy) HttpClientException(com.bluenimble.platform.http.HttpClientException) HttpHeader(com.bluenimble.platform.http.HttpHeader) AbstractHttpRequest(com.bluenimble.platform.http.request.impls.AbstractHttpRequest) HttpMessageBodyImpl(com.bluenimble.platform.http.impls.HttpMessageBodyImpl)

Aggregations

HttpEndpoint (com.bluenimble.platform.http.HttpEndpoint)2 HttpParameter (com.bluenimble.platform.http.HttpParameter)2 AbstractHttpRequest (com.bluenimble.platform.http.request.impls.AbstractHttpRequest)2 HttpClientException (com.bluenimble.platform.http.HttpClientException)1 HttpHeader (com.bluenimble.platform.http.HttpHeader)1 HttpMessageBody (com.bluenimble.platform.http.HttpMessageBody)1 HttpHeaderImpl (com.bluenimble.platform.http.impls.HttpHeaderImpl)1 HttpMessageBodyImpl (com.bluenimble.platform.http.impls.HttpMessageBodyImpl)1 HttpParameterImpl (com.bluenimble.platform.http.impls.HttpParameterImpl)1 StringHttpMessageBodyPart (com.bluenimble.platform.http.impls.StringHttpMessageBodyPart)1 HttpRequestWriteException (com.bluenimble.platform.http.request.HttpRequestWriteException)1 JsonObject (com.bluenimble.platform.json.JsonObject)1 InetSocketAddress (java.net.InetSocketAddress)1 Proxy (java.net.Proxy)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 OAuthConsumer (oauth.signpost.OAuthConsumer)1 DefaultOAuthConsumer (oauth.signpost.basic.DefaultOAuthConsumer)1 HttpParameters (oauth.signpost.http.HttpParameters)1