// Needed for specifying HTTP pre-emptive authentication:
HttpContext httpContext = null;
// Create all the builder objects:
final HttpClientBuilder hcBuilder = HttpClientBuilder.create();
final RequestConfig.Builder rcBuilder = RequestConfig.custom();
final RequestBuilder reqBuilder;
switch(request.getMethod()){
case GET:
reqBuilder = RequestBuilder.get();
break;
case POST:
reqBuilder = RequestBuilder.post();
break;
case PUT:
reqBuilder = RequestBuilder.put();
break;
case PATCH:
reqBuilder = RequestBuilder.create("PATCH");
break;
case DELETE:
reqBuilder = RequestBuilder.delete();
break;
case HEAD:
reqBuilder = RequestBuilder.head();
break;
case OPTIONS:
reqBuilder = RequestBuilder.options();
break;
case TRACE:
reqBuilder = RequestBuilder.trace();
break;
default:
throw new IllegalStateException("Method not defined!");
}
// Retry handler (no-retries):
hcBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
// Url:
final URL url = IDNUtil.getIDNizedURL(request.getUrl());
final String urlHost = url.getHost();
final int urlPort = url.getPort()==-1?url.getDefaultPort():url.getPort();
final String urlProtocol = url.getProtocol();
final String urlStr = url.toString();
reqBuilder.setUri(urlStr);
// Set HTTP version:
HTTPVersion httpVersion = request.getHttpVersion();
ProtocolVersion protocolVersion =
httpVersion==HTTPVersion.HTTP_1_1? new ProtocolVersion("HTTP", 1, 1):
new ProtocolVersion("HTTP", 1, 0);
reqBuilder.setVersion(protocolVersion);
// Set request timeout (default 1 minute--60000 milliseconds)
IGlobalOptions options = ServiceLocator.getInstance(IGlobalOptions.class);
rcBuilder.setConnectionRequestTimeout(
Integer.parseInt(options.getProperty("request-timeout-in-millis")));
// Set proxy
ProxyConfig proxy = ProxyConfig.getInstance();
proxy.acquire();
if (proxy.isEnabled()) {
final HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort(), "http");
if (proxy.isAuthEnabled()) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(proxy.getHost(), proxy.getPort()),
new UsernamePasswordCredentials(proxy.getUsername(), new String(proxy.getPassword())));
hcBuilder.setDefaultCredentialsProvider(credsProvider);
}
hcBuilder.setProxy(proxyHost);
}
proxy.release();
// HTTP Authentication
if(request.getAuth() != null) {
// Add auth preference:
Auth auth = request.getAuth();
List<String> authPrefs = new ArrayList<>();
if(auth instanceof BasicAuth) {
authPrefs.add(AuthSchemes.BASIC);
}
else if(auth instanceof DigestAuth) {
authPrefs.add(AuthSchemes.DIGEST);
}
else if(auth instanceof NtlmAuth) {
authPrefs.add(AuthSchemes.NTLM);
}
rcBuilder.setTargetPreferredAuthSchemes(authPrefs);
// BASIC & DIGEST:
if(auth instanceof BasicAuth || auth instanceof DigestAuth) {
BasicDigestAuth a = (BasicDigestAuth) auth;
String uid = a.getUsername();
String pwd = new String(a.getPassword());
String host = StringUtil.isEmpty(a.getHost()) ? urlHost : a.getHost();
String realm = StringUtil.isEmpty(a.getRealm()) ? AuthScope.ANY_REALM : a.getRealm();
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(host, urlPort, realm),
new UsernamePasswordCredentials(uid, pwd));
hcBuilder.setDefaultCredentialsProvider(credsProvider);
// preemptive mode:
if (a.isPreemptive()) {
AuthCache authCache = new BasicAuthCache();
AuthSchemeBase authScheme = a instanceof BasicAuth?
new BasicScheme(): new DigestScheme();
authCache.put(new HttpHost(urlHost, urlPort, urlProtocol), authScheme);
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
httpContext = localContext;
}
}
// NTLM:
if(auth instanceof NtlmAuth) {
NtlmAuth a = (NtlmAuth) auth;
String uid = a.getUsername();
String pwd = new String(a.getPassword());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
AuthScope.ANY,
new NTCredentials(
uid, pwd, a.getWorkstation(), a.getDomain()));
hcBuilder.setDefaultCredentialsProvider(credsProvider);
}
// Authorization header
// Logic written in same place where Header is processed--a little down!
}
try {
{ // Authorization Header Authentication:
Auth auth = request.getAuth();
if(auth != null && auth instanceof AuthorizationHeaderAuth) {
AuthorizationHeaderAuth a = (AuthorizationHeaderAuth) auth;
final String authHeader = a.getAuthorizationHeaderValue();
if(StringUtil.isNotEmpty(authHeader)) {
Header header = new BasicHeader("Authorization", authHeader);
reqBuilder.addHeader(header);
}
}
}
// Get request headers
MultiValueMap<String, String> header_data = request.getHeaders();
for (String key : header_data.keySet()) {
for(String value: header_data.get(key)) {
Header header = new BasicHeader(key, value);
reqBuilder.addHeader(header);
}
}
// Cookies
{
// Set cookie policy:
rcBuilder.setCookieSpec(CookieSpecs.BEST_MATCH);
// Add to CookieStore:
CookieStore store = new RESTClientCookieStore();
List<HttpCookie> cookies = request.getCookies();
for(HttpCookie cookie: cookies) {
BasicClientCookie c = new BasicClientCookie(
cookie.getName(), cookie.getValue());
c.setVersion(cookie.getVersion());
c.setDomain(urlHost);
c.setPath("/");
store.addCookie(c);
}
// Attach store to client:
hcBuilder.setDefaultCookieStore(store);
}
// POST/PUT/PATCH/DELETE method specific logic
if (HttpUtil.isEntityEnclosingMethod(reqBuilder.getMethod())) {
// Create and set RequestEntity
ReqEntity bean = request.getBody();
if (bean != null) {
try {
if(bean instanceof ReqEntitySimple) {
AbstractHttpEntity e = HTTPClientUtil.getEntity((ReqEntitySimple)bean);
reqBuilder.setEntity(e);
}
else if(bean instanceof ReqEntityMultipart) {
ReqEntityMultipart multipart = (ReqEntityMultipart)bean;
MultipartEntityBuilder meb = MultipartEntityBuilder.create();
// Format:
MultipartMode mpMode = multipart.getMode();
switch(mpMode) {
case BROWSER_COMPATIBLE:
meb.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
break;
case RFC_6532:
meb.setMode(HttpMultipartMode.RFC6532);
break;
case STRICT:
meb.setMode(HttpMultipartMode.STRICT);
break;
}
// Parts:
for(ReqEntityPart part: multipart.getBody()) {
if(part instanceof ReqEntityStringPart) {
ReqEntityStringPart p = (ReqEntityStringPart)part;
String body = p.getPart();
ContentType ct = p.getContentType();
final StringBody sb;
if(ct != null) {
sb = new StringBody(body, HTTPClientUtil.getContentType(ct));
}
else {
sb = new StringBody(body, org.apache.http.entity.ContentType.DEFAULT_TEXT);
}
meb.addPart(part.getName(), sb);
}
else if(part instanceof ReqEntityFilePart) {
ReqEntityFilePart p = (ReqEntityFilePart)part;
File body = p.getPart();
ContentType ct = p.getContentType();
final FileBody fb;
if(ct != null) {
fb = new FileBody(body, HTTPClientUtil.getContentType(ct), p.getFilename());
}
else {
fb = new FileBody(body, org.apache.http.entity.ContentType.DEFAULT_BINARY, p.getFilename());
}
meb.addPart(p.getName(), fb);
}
}
reqBuilder.setEntity(meb.build());
}
}
catch (UnsupportedEncodingException ex) {
for(View view: views){
view.doError(Util.getStackTrace(ex));
view.doEnd();
}
return;
}
}
}
// SSL
// Set the hostname verifier:
final SSLReq sslReq = request.getSslReq();
if(sslReq != null) {
SSLHostnameVerifier verifier = sslReq.getHostNameVerifier();
final X509HostnameVerifier hcVerifier;
switch(verifier){
case STRICT:
hcVerifier = SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER;
break;
case BROWSER_COMPATIBLE:
hcVerifier = SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER;
break;
case ALLOW_ALL:
hcVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
break;
default:
hcVerifier = SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER;
break;
}
// Register the SSL Scheme:
final KeyStore trustStore = sslReq.getTrustStore() == null?
null:
sslReq.getTrustStore().getKeyStore();
final KeyStore keyStore = sslReq.getKeyStore() == null?
null:
sslReq.getKeyStore().getKeyStore();
final TrustStrategy trustStrategy = sslReq.isTrustSelfSignedCert()
? new TrustSelfSignedStrategy(): null;
SSLContext ctx = new SSLContextBuilder()
.loadKeyMaterial(keyStore, sslReq.getKeyStore()!=null? sslReq.getKeyStore().getPassword(): null)
.loadTrustMaterial(trustStore, trustStrategy)
.setSecureRandom(null)
.useTLS()
.build();
SSLConnectionSocketFactory sf = new SSLConnectionSocketFactory(ctx, hcVerifier);
hcBuilder.setSSLSocketFactory(sf);
}
// How to handle redirects:
rcBuilder.setRedirectsEnabled(request.isFollowRedirect());
// Now Execute:
long startTime = System.currentTimeMillis();
RequestConfig rc = rcBuilder.build();
reqBuilder.setConfig(rc);
HttpUriRequest req = reqBuilder.build();
httpClient = hcBuilder.build();
HttpResponse http_res = httpClient.execute(req, httpContext);
long endTime = System.currentTimeMillis();