Search in sources :

Example 1 with HttpHeader

use of com.bluenimble.platform.http.HttpHeader in project serverless by bluenimble.

the class BlueNimbleHttpRequestVisitor method visit.

@Override
public void visit(HttpRequest request, HttpURLConnection connection) throws HttpRequestWriteException {
    // sign request
    List<HttpHeader> headers = request.getHeaders();
    if (headers == null) {
        headers = new ArrayList<HttpHeader>();
        request.setHeaders(headers);
    }
    AccessSecretKeysBasedHttpRequestSigner signer = new AccessSecretKeysBasedHttpRequestSigner("m>h>p>d>k>t", "Bearer", accessKey, secretKey);
    String timestamp = Lang.utc();
    headers.add(new HttpHeaderImpl(ApiHeaders.Timestamp, timestamp));
    signer.getData().put('t', timestamp);
    try {
        signer.sign(request);
    } catch (HttpRequestSignerException e) {
        throw new HttpRequestWriteException(e.getMessage(), e);
    }
}
Also used : HttpHeader(com.bluenimble.platform.http.HttpHeader) AccessSecretKeysBasedHttpRequestSigner(com.bluenimble.platform.http.auth.impls.AccessSecretKeysBasedHttpRequestSigner) HttpRequestSignerException(com.bluenimble.platform.http.auth.HttpRequestSignerException) HttpHeaderImpl(com.bluenimble.platform.http.impls.HttpHeaderImpl) HttpRequestWriteException(com.bluenimble.platform.http.request.HttpRequestWriteException)

Example 2 with HttpHeader

use of com.bluenimble.platform.http.HttpHeader 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)

Example 3 with HttpHeader

use of com.bluenimble.platform.http.HttpHeader in project serverless by bluenimble.

the class KeyHttpRequestSigner method sign.

@Override
public void sign(HttpRequest request) throws HttpRequestSignerException {
    List<HttpHeader> headers = request.getHeaders();
    if (headers == null) {
        headers = new ArrayList<HttpHeader>();
        request.setHeaders(headers);
    }
    headers.add(new HttpHeaderImpl(HttpHeaders.AUTHORIZATION, type + " " + (encode ? new String(BASE64.encode(key.getBytes())).replaceAll("\n", "") : key)));
}
Also used : HttpHeader(com.bluenimble.platform.http.HttpHeader) HttpHeaderImpl(com.bluenimble.platform.http.impls.HttpHeaderImpl)

Example 4 with HttpHeader

use of com.bluenimble.platform.http.HttpHeader in project serverless by bluenimble.

the class DefaultHttpClient method send.

public HttpResponse send(final HttpRequest request) throws HttpClientException {
    HttpURLConnection hc = null;
    try {
        if (request == null || request.getURI() == null) {
            throw new HttpClientException("No request to proceed");
        }
        URL url = request.getURI().toURL();
        URLConnection connection = null;
        if (request.getProxy() != null) {
            connection = url.openConnection(request.getProxy());
        } else {
            connection = url.openConnection();
        }
        hc = (HttpURLConnection) connection;
        if (hc instanceof HttpsURLConnection) {
            HttpsURLConnection https = (HttpsURLConnection) hc;
            if (trustAll) {
                https.setSSLSocketFactory(TrustAllSocketFactory);
                https.setHostnameVerifier(TrustAllHostVerifier);
            }
        }
        hc.setConnectTimeout(request.getConnectTimeout());
        hc.setReadTimeout(request.getReadTimeout());
        hc.setRequestMethod(request.getName());
        if (request.getName().equals(HttpMethods.POST) || request.getName().equals(HttpMethods.PUT)) {
            connection.setDoOutput(true);
        }
        if (!(connection instanceof HttpURLConnection)) {
            throw new HttpClientException("Only Http request can be handled");
        }
        setRequestCookie(request);
        request.write(hc);
        InputStream iobody = null;
        int status = hc.getResponseCode();
        HttpResponseImpl response = new HttpResponseImpl(request.getId());
        response.setStatus(status);
        addResponseHeaders(response, hc);
        String charset = request.getCharset();
        HttpHeader cth = response.getHeader(HttpHeaders.CONTENT_TYPE);
        if (cth != null) {
            String[] values = cth.getValues();
            if (values != null && values.length > 0) {
                String contentType = values[0];
                response.setContentType(contentType);
                for (String param : contentType.replace(" ", "").split(";")) {
                    if (param.startsWith("charset=")) {
                        charset = param.split("=", 2)[1];
                        break;
                    }
                }
            }
        }
        response.setCharset(charset);
        if (request.getSuccessCodes().contains(String.valueOf(status))) {
            iobody = hc.getInputStream();
        } else {
            iobody = hc.getErrorStream();
        }
        if (GZip.equals(hc.getContentEncoding())) {
            iobody = new GZIPInputStream(iobody);
        }
        response.setBody(new HttpMessageBodyImpl(new InputStreamHttpMessageBodyPart(iobody)));
        updateCookies(response);
        return response;
    } catch (Throwable th) {
        throw new HttpClientException(th);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) GZIPInputStream(java.util.zip.GZIPInputStream) HttpURLConnection(java.net.HttpURLConnection) HttpClientException(com.bluenimble.platform.http.HttpClientException) HttpHeader(com.bluenimble.platform.http.HttpHeader) HttpResponseImpl(com.bluenimble.platform.http.response.impls.HttpResponseImpl) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 5 with HttpHeader

use of com.bluenimble.platform.http.HttpHeader in project serverless by bluenimble.

the class DefaultHttpClient method setRequestCookie.

protected void setRequestCookie(HttpRequest request) {
    if (cookies == null) {
        return;
    }
    if (request.getHeaders() == null) {
        request.setHeaders(new ArrayList<HttpHeader>());
    }
    List<String> cookieValues = new ArrayList<String>();
    for (String cookie : cookies) {
        cookieValues.add(cookie.split(Lang.SEMICOLON, 2)[0]);
    }
    HttpHeader cookieHeader = new HttpHeaderImpl(HttpHeaders.COOKIE, cookieValues);
    request.getHeaders().add(cookieHeader);
}
Also used : HttpHeader(com.bluenimble.platform.http.HttpHeader) ArrayList(java.util.ArrayList)

Aggregations

HttpHeader (com.bluenimble.platform.http.HttpHeader)12 HttpHeaderImpl (com.bluenimble.platform.http.impls.HttpHeaderImpl)6 HttpEndpoint (com.bluenimble.platform.http.HttpEndpoint)4 HttpParameter (com.bluenimble.platform.http.HttpParameter)3 HttpParameterImpl (com.bluenimble.platform.http.impls.HttpParameterImpl)3 JsonObject (com.bluenimble.platform.json.JsonObject)3 InputStream (java.io.InputStream)3 URI (java.net.URI)3 ArrayList (java.util.ArrayList)3 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)2 StreamPointer (com.bluenimble.platform.cli.command.parser.converters.StreamPointer)2 YamlObject (com.bluenimble.platform.cli.impls.YamlObject)2 HttpClientException (com.bluenimble.platform.http.HttpClientException)2 HttpMessageBody (com.bluenimble.platform.http.HttpMessageBody)2 HttpRequestSignerException (com.bluenimble.platform.http.auth.HttpRequestSignerException)2 AccessSecretKeysBasedHttpRequestSigner (com.bluenimble.platform.http.auth.impls.AccessSecretKeysBasedHttpRequestSigner)2 HttpMessageBodyImpl (com.bluenimble.platform.http.impls.HttpMessageBodyImpl)2 StringHttpMessageBodyPart (com.bluenimble.platform.http.impls.StringHttpMessageBodyPart)2 HttpRequest (com.bluenimble.platform.http.request.HttpRequest)2 FileInputStream (java.io.FileInputStream)2