use of org.apache.http.HeaderElement in project xUtils by wyouflf.
the class OtherUtils method getCharsetFromHttpRequest.
public static Charset getCharsetFromHttpRequest(final HttpRequestBase request) {
if (request == null)
return null;
String charsetName = null;
Header header = request.getFirstHeader("Content-Type");
if (header != null) {
for (HeaderElement element : header.getElements()) {
NameValuePair charsetPair = element.getParameterByName("charset");
if (charsetPair != null) {
charsetName = charsetPair.getValue();
break;
}
}
}
boolean isSupportedCharset = false;
if (!TextUtils.isEmpty(charsetName)) {
try {
isSupportedCharset = Charset.isSupported(charsetName);
} catch (Throwable e) {
}
}
return isSupportedCharset ? Charset.forName(charsetName) : null;
}
use of org.apache.http.HeaderElement in project linuxtools by eclipse.
the class OSIORestClient method getTaskData.
public void getTaskData(Set<String> taskIds, TaskRepository taskRepository, TaskDataCollector collector, IOperationMonitor monitor) throws OSIORestException {
OSIORestConfiguration config;
try {
config = connector.getRepositoryConfiguration(taskRepository);
} catch (CoreException e1) {
throw new OSIORestException(e1);
}
for (String taskId : taskIds) {
if (taskId.isEmpty()) {
continue;
}
String user = userName;
// $NON-NLS-1$
String[] tokens = taskId.split("#");
String spaceName = tokens[0];
// check for workitem in space not owned by this user
// in which case it is prefixed by username
// $NON-NLS-1$
String[] spaceTokens = spaceName.split("/");
if (spaceTokens.length > 1) {
spaceName = spaceTokens[1];
user = spaceTokens[0];
}
String wiNumber = tokens[1];
try {
// We need to translate from the space's workitem number to the real id
// The easiest way is to use a namedspaces request that we know will give
// us a "ResourceMovedPermanently" error which will contain the URL of the
// real location of the workitem which contains the workitem uuid.
user = URLQueryEncoder.transform(user);
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
String query = "/namedspaces/" + user + "/" + spaceName + "/workitems/" + wiNumber;
// $NON-NLS-1$
String wid = "";
try {
wid = restRequestProvider.getWID(monitor, client, query, taskRepository);
} catch (OSIORestResourceMovedPermanentlyException e) {
Header h = e.getHeader();
HeaderElement[] elements = h.getElements();
for (HeaderElement element : elements) {
if ("Location".equals(element.getName())) {
// $NON-NLS-1$
// $NON-NLS-1$
int index = element.getValue().indexOf("workitem/");
wid = element.getValue().substring(index + 9);
}
}
}
// $NON-NLS-1$
String workitemquery = "/workitems/" + wid;
TaskData taskData = restRequestProvider.getSingleTaskData(monitor, client, connector, workitemquery, taskRepository);
Space space = null;
String spaceId = taskData.getRoot().getAttribute(OSIORestTaskSchema.getDefault().SPACE_ID.getKey()).getValue();
space = getSpaceById(spaceId, taskRepository);
restRequestProvider.getTaskComments(monitor, client, space, taskData);
restRequestProvider.getTaskCreator(monitor, client, taskData);
restRequestProvider.getTaskLabels(monitor, client, space, taskData);
restRequestProvider.getTaskLinks(monitor, client, this, space, taskData, config);
setTaskAssignees(taskData);
config.updateSpaceOptions(taskData);
config.addValidOperations(taskData);
collector.accept(taskData);
} catch (RuntimeException | CoreException e) {
// if the Throwable was wrapped in a RuntimeException in
// OSIORestGetTaskData.JSonTaskDataDeserializer.deserialize()
// we now remove the wrapper and throw an OSIORestException
e.printStackTrace();
Throwable cause = e.getCause();
if (cause instanceof CoreException) {
throw new OSIORestException(cause);
}
}
}
}
use of org.apache.http.HeaderElement in project instructure-android by instructure.
the class HttpHelpers method parseLinkHeaderResponse.
/**
* parseLinkHeaderResponse is the old way of parsing the pagination URLs out of the response.
* @param response
* @return
*/
private static APIHttpResponse parseLinkHeaderResponse(HttpResponse response) {
APIHttpResponse httpResponse = new APIHttpResponse();
// Get status code.
httpResponse.responseCode = response.getStatusLine().getStatusCode();
// Check if response is supposed to have a body
if (httpResponse.responseCode != 204) {
try {
httpResponse.responseBody = EntityUtils.toString(response.getEntity());
} catch (Exception E) {
}
}
Header[] linkHeader = response.getHeaders("Link");
for (int j = 0; j < linkHeader.length; j++) {
HeaderElement[] elements = linkHeader[j].getElements();
for (int i = 0; i < elements.length; i++) {
String first = elements[i].getName();
String last = elements[i].getValue();
// Seems to strip out the equals between name and value
String url = first + "=" + last;
if (url.startsWith("<") && url.endsWith(">")) {
url = url.substring(1, url.length() - 1);
} else {
continue;
}
for (int k = 0; k < elements[i].getParameterCount(); k++) {
NameValuePair nvp = elements[i].getParameter(k);
if (nvp.getName().equals("rel")) {
if (nvp.getValue().equals("prev")) {
httpResponse.prevURL = url;
} else if (nvp.getValue().equals("next")) {
httpResponse.nextURL = url;
} else if (nvp.getValue().equals("first")) {
httpResponse.firstURL = url;
} else if (nvp.getValue().equals("last")) {
httpResponse.lastURL = url;
}
}
}
}
}
return httpResponse;
}
use of org.apache.http.HeaderElement in project csb-sdk by aliyun.
the class HttpClientFactory method createKeepAliveStrategy.
private static ConnectionKeepAliveStrategy createKeepAliveStrategy() {
return new ConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
return Long.parseLong(value) * 1000;
}
}
return 5 * 1000;
}
};
}
use of org.apache.http.HeaderElement in project xian by happyyangyuan.
the class ConnKeepAliveStrategy method create.
public static ConnectionKeepAliveStrategy create(long keepTimeMill) {
if (keepTimeMill < 0)
throw new IllegalArgumentException("apache_httpclient连接持久时间不能小于0");
return new ConnectionKeepAliveStrategy() {
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
// Honor 'keep-alive' header
HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
try {
return Long.parseLong(value) * 1000;
} catch (NumberFormatException ignore) {
}
}
}
return keepTimeMill;
}
};
}
Aggregations