use of org.apache.http.message.BasicHeader in project lucene-solr by apache.
the class BasicAuthStandaloneTest method setBasicAuthHeader.
public static void setBasicAuthHeader(AbstractHttpMessage httpMsg, String user, String pwd) {
String userPass = user + ":" + pwd;
String encoded = Base64.byteArrayToBase64(userPass.getBytes(UTF_8));
httpMsg.setHeader(new BasicHeader("Authorization", "Basic " + encoded));
log.info("Added Basic Auth security Header {}", encoded);
}
use of org.apache.http.message.BasicHeader in project jena by apache.
the class RDFParserBuilder method buildHttpClient.
private HttpClient buildHttpClient() {
if (httpClient != null)
return httpClient;
if (httpHeaders.isEmpty())
// For complete compatibility, we have to let null pass through.
return // HttpOp.getDefaultHttpClient();
null;
List<Header> hdrs = new ArrayList<>();
httpHeaders.forEach((k, v) -> {
Header header = new BasicHeader(k, v);
hdrs.add(header);
});
HttpClient hc = CachingHttpClientBuilder.create().setDefaultHeaders(hdrs).build();
return hc;
}
use of org.apache.http.message.BasicHeader in project jmeter by apache.
the class HC4CookieHandler method addCookieFromHeader.
@Override
public void addCookieFromHeader(CookieManager cookieManager, boolean checkCookies, String cookieHeader, URL url) {
boolean debugEnabled = log.isDebugEnabled();
if (debugEnabled) {
log.debug("Received Cookie: " + cookieHeader + " From: " + url.toExternalForm());
}
String protocol = url.getProtocol();
String host = url.getHost();
int port = HTTPSamplerBase.getDefaultPort(protocol, url.getPort());
String path = url.getPath();
boolean isSecure = HTTPSamplerBase.isSecure(protocol);
List<org.apache.http.cookie.Cookie> cookies = null;
CookieOrigin cookieOrigin = new CookieOrigin(host, port, path, isSecure);
BasicHeader basicHeader = new BasicHeader(HTTPConstants.HEADER_SET_COOKIE, cookieHeader);
try {
cookies = cookieSpec.parse(basicHeader, cookieOrigin);
} catch (MalformedCookieException e) {
log.error("Unable to add the cookie", e);
}
if (cookies == null) {
return;
}
for (org.apache.http.cookie.Cookie cookie : cookies) {
try {
if (checkCookies) {
cookieSpec.validate(cookie, cookieOrigin);
}
Date expiryDate = cookie.getExpiryDate();
long exp = 0;
if (expiryDate != null) {
exp = expiryDate.getTime();
}
Cookie newCookie = new Cookie(cookie.getName(), cookie.getValue(), cookie.getDomain(), cookie.getPath(), cookie.isSecure(), exp / 1000, ((BasicClientCookie) cookie).containsAttribute(ClientCookie.PATH_ATTR), ((BasicClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR), cookie.getVersion());
// Store session cookies as well as unexpired ones
if (exp == 0 || exp >= System.currentTimeMillis()) {
// Has its own debug log; removes matching cookies
cookieManager.add(newCookie);
} else {
cookieManager.removeMatchingCookies(newCookie);
if (debugEnabled) {
log.info("Dropping expired Cookie: " + newCookie.toString());
}
}
} catch (MalformedCookieException e) {
// This means the cookie was wrong for the URL
log.warn("Not storing invalid cookie: <" + cookieHeader + "> for URL " + url + " (" + e.getLocalizedMessage() + ")");
} catch (IllegalArgumentException e) {
log.warn(cookieHeader + e.getLocalizedMessage());
}
}
}
use of org.apache.http.message.BasicHeader in project iosched by google.
the class HurlStack method performRequest.
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
String url = request.getUrl();
HashMap<String, String> map = new HashMap<String, String>();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
if (mUrlRewriter != null) {
String rewritten = mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
url = rewritten;
}
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl, request);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
setConnectionParametersForRequest(connection, request);
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromConnection(connection));
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
response.addHeader(h);
}
}
return response;
}
use of org.apache.http.message.BasicHeader in project XobotOS by xamarin.
the class AbstractHttpEntity method setContentEncoding.
/**
* Specifies the Content-Encoding header, as a string.
* The default implementation calls
* {@link #setContentEncoding(Header) setContentEncoding(Header)}.
*
* @param ceString the new Content-Encoding header, or
* <code>null</code> to unset
*/
public void setContentEncoding(final String ceString) {
Header h = null;
if (ceString != null) {
h = new BasicHeader(HTTP.CONTENT_ENCODING, ceString);
}
setContentEncoding(h);
}
Aggregations