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);
}
}
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;
}
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)));
}
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);
}
}
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);
}
Aggregations