use of org.apache.http.HeaderElement in project tutorials by eugenp.
the class HttpClientConnectionManagementLiveTest method whenCustomizingKeepAliveStrategy_thenNoExceptions.
// 5
@Test
public final // 5.1
void whenCustomizingKeepAliveStrategy_thenNoExceptions() throws ClientProtocolException, IOException {
final ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(final HttpResponse myResponse, final HttpContext myContext) {
final HeaderElementIterator it = new BasicHeaderElementIterator(myResponse.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
final HeaderElement he = it.nextElement();
final String param = he.getName();
final String value = he.getValue();
if ((value != null) && param.equalsIgnoreCase("timeout")) {
return Long.parseLong(value) * 1000;
}
}
final HttpHost target = (HttpHost) myContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
if ("localhost".equalsIgnoreCase(target.getHostName())) {
return 10 * 1000;
} else {
return 5 * 1000;
}
}
};
client = HttpClients.custom().setKeepAliveStrategy(myStrategy).setConnectionManager(poolingConnManager).build();
client.execute(get1);
client.execute(get2);
}
use of org.apache.http.HeaderElement in project ovirt-engine by oVirt.
the class CSRFProtectionFilter method isProtectionRequested.
/**
* Checks if the headers contained in the given request indicate that the user wants to enable protection. This
* means checking if the {@code Prefer} header exists and has at least one {@code csrf-protection} element. For
* example:
*
* <pre>
* GET /ovirt-engine/api HTTP/1.1
* Host: ovirt.example.com
* Prefer: persistent-auth, csrf-protection
* </pre>
*
* @param request the HTTP request to check
* @return {@code true} if the request contains headers that indicate that protection should be enabled,
* {@code false} otherwise
*/
private boolean isProtectionRequested(HttpServletRequest request) {
Enumeration<String> headerValues = request.getHeaders(PREFER_HEADER);
while (headerValues.hasMoreElements()) {
String headerValue = headerValues.nextElement();
HeaderElement[] headerElements = BasicHeaderValueParser.parseElements(headerValue, null);
for (HeaderElement headerElement : headerElements) {
String elementName = headerElement.getName();
if (PREFER_ELEMENT.equalsIgnoreCase(elementName)) {
return true;
}
}
}
return false;
}
use of org.apache.http.HeaderElement in project ovirt-engine by oVirt.
the class FiltersHelper method getPrefer.
public static int getPrefer(HttpServletRequest req) {
int ret = 0;
Enumeration<String> headerValues = req.getHeaders(Constants.HEADER_PREFER);
while (headerValues.hasMoreElements()) {
String headerValue = headerValues.nextElement();
HeaderElement[] headerElements = BasicHeaderValueParser.parseElements(headerValue, null);
if (headerElements != null) {
for (HeaderElement headerElement : headerElements) {
String elementName = headerElement.getName();
if ("new-auth".equalsIgnoreCase(elementName)) {
ret |= PREFER_NEW_AUTH;
}
if ("persistent-auth".equalsIgnoreCase(elementName)) {
ret |= PREFER_PERSISTENCE_AUTH;
}
}
}
}
return ret;
}
use of org.apache.http.HeaderElement in project vespa by vespa-engine.
the class ApacheGatewayConnectionTest method addMockedHeader.
private void addMockedHeader(final HttpResponse httpResponseMock, final String name, final String value, HeaderElement[] elements) {
final Header header = new Header() {
@Override
public String getName() {
return name;
}
@Override
public String getValue() {
return value;
}
@Override
public HeaderElement[] getElements() throws ParseException {
return elements;
}
};
when(httpResponseMock.getFirstHeader(name)).thenReturn(header);
}
use of org.apache.http.HeaderElement in project jmeter by apache.
the class MultipartUrlConfig method parseArguments.
/**
* This method allows a proxy server to send over the raw text from a
* browser's output stream to be parsed and stored correctly into the
* UrlConfig object.
*
* @param queryString text to parse
*/
public void parseArguments(String queryString) {
// $NON-NLS-1$
String[] parts = JOrphanUtils.split(queryString, "--" + getBoundary());
for (String part : parts) {
// $NON-NLS-1$
String contentDisposition = getHeaderValue("Content-disposition", part);
// $NON-NLS-1$
String contentType = getHeaderValue("Content-type", part);
// Check if it is form data
if (contentDisposition != null && contentDisposition.contains("form-data")) {
// $NON-NLS-1$
// Get the form field name
HeaderElement[] headerElements = null;
try {
headerElements = BasicHeaderValueParser.parseElements(contentDisposition, BasicHeaderValueParser.INSTANCE);
} catch (ParseException e) {
log.info("Can't parse header {}", contentDisposition, e);
}
String name = "";
String path = null;
if (headerElements != null) {
for (HeaderElement element : headerElements) {
name = getParameterValue(element, "name", "");
path = getParameterValue(element, "filename", null);
}
}
if (path != null && !path.isEmpty()) {
// Set the values retrieved for the file upload
files.addHTTPFileArg(path, name, contentType);
} else {
// Find the first empty line of the multipart, it signals end of headers for multipart
// Agents are supposed to terminate lines in CRLF:
// $NON-NLS-1$
int indexEmptyCrLfCrLfLinePos = part.indexOf(CRLFCRLF);
// $NON-NLS-1$
int indexEmptyLfLfLinePos = part.indexOf(LFLF);
String value = null;
if (indexEmptyCrLfCrLfLinePos > -1) {
// CRLF blank line found
value = part.substring(indexEmptyCrLfCrLfLinePos + CRLFCRLF.length(), part.lastIndexOf(CRLF));
} else if (indexEmptyLfLfLinePos > -1) {
// LF blank line found
value = part.substring(indexEmptyLfLfLinePos + LFLF.length(), part.lastIndexOf(LF));
}
this.addNonEncodedArgument(name, value, contentType);
}
}
}
}
Aggregations