Search in sources :

Example 11 with HeaderElement

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;
}
Also used : NameValuePair(org.apache.http.NameValuePair) Header(org.apache.http.Header) HeaderElement(org.apache.http.HeaderElement)

Example 12 with HeaderElement

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);
            }
        }
    }
}
Also used : Space(org.eclipse.linuxtools.internal.mylyn.osio.rest.core.response.data.Space) HeaderElement(org.apache.http.HeaderElement) TaskData(org.eclipse.mylyn.tasks.core.data.TaskData) CoreException(org.eclipse.core.runtime.CoreException) Header(org.apache.http.Header)

Example 13 with HeaderElement

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;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) Header(org.apache.http.Header) HeaderElement(org.apache.http.HeaderElement) IOException(java.io.IOException)

Example 14 with HeaderElement

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;
        }
    };
}
Also used : BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator) HeaderElementIterator(org.apache.http.HeaderElementIterator) ConnectionKeepAliveStrategy(org.apache.http.conn.ConnectionKeepAliveStrategy) HeaderElement(org.apache.http.HeaderElement) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator)

Example 15 with HeaderElement

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;
        }
    };
}
Also used : BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator) HeaderElementIterator(org.apache.http.HeaderElementIterator) ConnectionKeepAliveStrategy(org.apache.http.conn.ConnectionKeepAliveStrategy) HeaderElement(org.apache.http.HeaderElement) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator)

Aggregations

HeaderElement (org.apache.http.HeaderElement)58 Header (org.apache.http.Header)17 NameValuePair (org.apache.http.NameValuePair)14 HeaderElementIterator (org.apache.http.HeaderElementIterator)10 BasicHeaderElementIterator (org.apache.http.message.BasicHeaderElementIterator)10 HttpResponse (org.apache.http.HttpResponse)9 MalformedCookieException (org.apache.http.cookie.MalformedCookieException)9 ParserCursor (org.apache.http.message.ParserCursor)9 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 Cookie (org.apache.http.cookie.Cookie)5 CookieAttributeHandler (org.apache.http.cookie.CookieAttributeHandler)5 HttpContext (org.apache.http.protocol.HttpContext)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ConnectionKeepAliveStrategy (org.apache.http.conn.ConnectionKeepAliveStrategy)4 HashSet (java.util.HashSet)3 NoSuchElementException (java.util.NoSuchElementException)3 ParseException (org.apache.http.ParseException)3 ProtocolException (org.apache.http.ProtocolException)3