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