Example usage for org.apache.commons.httpclient Header getElements

List of usage examples for org.apache.commons.httpclient Header getElements

Introduction

In this page you can find the example usage for org.apache.commons.httpclient Header getElements.

Prototype

public HeaderElement[] getElements() 

Source Link

Usage

From source file:com.intellij.tasks.impl.TaskUtil.java

public static void prettyFormatResponseToLog(@Nonnull Logger logger, @Nonnull HttpMethod response) {
    if (logger.isDebugEnabled() && response.hasBeenUsed()) {
        try {/*from  w  w  w. ja va2s .  c  om*/
            String content = ResponseUtil.getResponseContentAsString(response);
            Header header = response.getRequestHeader(HTTP.CONTENT_TYPE);
            String contentType = header == null ? "text/plain"
                    : header.getElements()[0].getName().toLowerCase(Locale.ENGLISH);
            if (contentType.contains("xml")) {
                prettyFormatXmlToLog(logger, content);
            } else if (contentType.contains("json")) {
                prettyFormatJsonToLog(logger, content);
            } else {
                logger.debug(content);
            }
        } catch (IOException e) {
            logger.error(e);
        }
    }
}

From source file:com.intellij.tasks.impl.TaskUtil.java

public static void prettyFormatResponseToLog(@Nonnull Logger logger, @Nonnull HttpResponse response) {
    if (logger.isDebugEnabled()) {
        try {/*from  ww  w .  ja v a  2 s  . c o  m*/
            String content = ResponseUtil.getResponseContentAsString(response);
            org.apache.http.Header header = response.getEntity().getContentType();
            String contentType = header == null ? "text/plain"
                    : header.getElements()[0].getName().toLowerCase(Locale.ENGLISH);
            if (contentType.contains("xml")) {
                prettyFormatXmlToLog(logger, content);
            } else if (contentType.contains("json")) {
                prettyFormatJsonToLog(logger, content);
            } else {
                logger.debug(content);
            }
        } catch (IOException e) {
            logger.error(e);
        }
    }
}

From source file:com.kodokux.github.api.GithubApiUtil.java

@NotNull
public static Collection<String> getTokenScopes(@NotNull GithubAuthData auth) throws IOException {
    HttpMethod method = null;/* w  w w .j  av a  2  s .  c  o  m*/
    try {
        String uri = GithubUrlUtil.getApiUrl(auth.getHost()) + "/user";
        method = doREST(auth, uri, null, Collections.<Header>emptyList(), HttpVerb.HEAD);

        checkStatusCode(method);

        Header header = method.getResponseHeader("X-OAuth-Scopes");
        if (header == null) {
            throw new HttpException("No scopes header");
        }

        Collection<String> scopes = new ArrayList<String>();
        for (HeaderElement elem : header.getElements()) {
            scopes.add(elem.getName());
        }
        return scopes;
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}

From source file:com.gnizr.core.util.GnizrDaoUtil.java

public static Integer detectMIMEType(String url) {
    try {//from   w  w  w  . ja v  a 2 s  . com
        HttpClient httpClient = new HttpClient();
        HeadMethod method = new HeadMethod(url);
        method.getParams().setIntParameter("http.socket.timeout", 5000);
        int code = httpClient.executeMethod(method);
        if (code == 200) {
            Header h = method.getResponseHeader("Content-Type");
            if (h != null) {
                HeaderElement[] headElm = h.getElements();
                if (headElm != null & headElm.length > 0) {
                    String mimeType = headElm[0].getValue();
                    if (mimeType == null) {
                        mimeType = headElm[0].getName();
                    }
                    if (mimeType != null) {
                        return getMimeTypeIdCode(mimeType);
                    }
                }
            }
        }
    } catch (Exception e) {
        // no code;
    }
    return MIMEType.UNKNOWN;
}

From source file:flex.messaging.services.http.httpclient.FlexGetMethod.java

protected String getContentCharSet(Header contentheader) {
    String charset = null;/*from  w w w. j  a  v a2s  . c om*/
    if (contentheader != null) {
        HeaderElement values[] = contentheader.getElements();
        if (values.length == 1) {
            NameValuePair param = values[0].getParameterByName("charset");
            if (param != null) {
                charset = param.getValue();
            }
        }
    }
    if (charset == null) {
        charset = "UTF-8";
    }
    return charset;
}

From source file:com.datos.vfs.provider.http.HttpFileContentInfoFactory.java

@Override
public FileContentInfo create(final FileContent fileContent) throws FileSystemException {
    final HttpFileObject httpFile = (HttpFileObject) FileObjectUtils
            .getAbstractFileObject(fileContent.getFile());

    String contentType = null;//ww w .jav a  2s  .  c  o m
    String contentEncoding = null;

    HeadMethod headMethod;
    try {
        headMethod = httpFile.getHeadMethod();
    } catch (final IOException e) {
        throw new FileSystemException(e);
    }
    final Header header = headMethod.getResponseHeader("content-type");
    if (header != null) {
        final HeaderElement[] element = header.getElements();
        if (element != null && element.length > 0) {
            contentType = element[0].getName();
        }
    }

    contentEncoding = headMethod.getResponseCharSet();

    return new DefaultFileContentInfo(contentType, contentEncoding);
}

From source file:com.eviware.soapui.impl.wsdl.submit.transports.http.support.attachments.MimeMessageResponse.java

public MimeMessageResponse(AbstractHttpRequestInterface<?> httpRequest, ExtendedHttpMethod httpMethod,
        String requestContent, PropertyExpansionContext context) {
    super(httpMethod, httpRequest, context);

    if (getRequestContent() == null || !getRequestContent().equals(requestContent))
        this.requestContent = requestContent;

    try {/*w w  w  .  j  av a2  s.  c  o m*/
        postResponseDataSource = new PostResponseDataSource(httpMethod);
        responseContentLength = postResponseDataSource.getDataSize();

        Header h = httpMethod.getResponseHeader("Content-Type");
        HeaderElement[] elements = h.getElements();

        String rootPartId = null;

        for (HeaderElement element : elements) {
            String name = element.getName().toUpperCase();
            if (name.startsWith("MULTIPART/")) {
                NameValuePair parameter = element.getParameterByName("start");
                if (parameter != null)
                    rootPartId = parameter.getValue();
            }
        }

        mmSupport = new MultipartMessageSupport(postResponseDataSource, rootPartId,
                (AbstractHttpOperation) httpRequest.getOperation(), false, httpRequest.isPrettyPrint());

        if (httpRequest.getSettings().getBoolean(HttpSettings.INCLUDE_RESPONSE_IN_TIME_TAKEN))
            this.timeTaken += httpMethod.getResponseReadTime();
    } catch (Exception e) {
        SoapUI.logError(e);
    }
}

From source file:com.eviware.soapui.impl.wsdl.submit.transports.http.MimeMessageResponse.java

public MimeMessageResponse(WsdlRequest wsdlRequest, final TimeablePostMethod postMethod,
        String requestContent) {//from   w w w. jav a 2 s .  co m
    this.wsdlRequest = wsdlRequest;
    this.requestContent = requestContent;
    this.timeTaken = postMethod.getTimeTaken();
    responseContentLength = postMethod.getResponseContentLength();

    try {
        initHeaders(postMethod);

        MimeMultipart mp = new MimeMultipart(new PostResponseDataSource(postMethod));
        message = new MimeMessage(HttpClientRequestTransport.JAVAMAIL_SESSION);
        message.setContent(mp);

        Header h = postMethod.getResponseHeader("Content-Type");
        HeaderElement[] elements = h.getElements();

        String rootPartId = null;

        for (HeaderElement element : elements) {
            if (element.getName().toUpperCase().startsWith("MULTIPART/")) {
                NameValuePair parameter = element.getParameterByName("start");
                if (parameter != null)
                    rootPartId = parameter.getValue();
            }
        }

        for (int c = 0; c < mp.getCount(); c++) {
            BodyPart bodyPart = mp.getBodyPart(c);

            if (bodyPart.getContentType().toUpperCase().startsWith("MULTIPART/")) {
                MimeMultipart mp2 = new MimeMultipart(new BodyPartDataSource(bodyPart));
                for (int i = 0; i < mp2.getCount(); i++) {
                    result.add(new BodyPartAttachment(mp2.getBodyPart(i)));
                }
            } else {
                BodyPartAttachment attachment = new BodyPartAttachment(bodyPart);

                String[] contentIdHeaders = bodyPart.getHeader("Content-ID");
                if (contentIdHeaders != null && contentIdHeaders.length > 0
                        && contentIdHeaders[0].equals(rootPartId)) {
                    rootPart = attachment;
                } else
                    result.add(attachment);
            }
        }

        // if no explicit root part has been set, use the first one in the result
        if (rootPart == null)
            rootPart = result.remove(0);

        if (wsdlRequest.getSettings().getBoolean(HttpSettings.INCLUDE_RESPONSE_IN_TIME_TAKEN))
            this.timeTaken = postMethod.getTimeTakenUntilNow();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.owncloud.android.lib.resources.files.SearchOperation.java

/** Check whether the request was successful */
private boolean checkSuccess(int status, GetMethod request) throws Exception {
    if (status != HttpStatus.SC_OK) {
        return false;
    }/*from   w w  w. j  av a  2s .co  m*/

    Header header = request.getResponseHeader("content-type");
    if (header == null) {
        throw new Exception("No content-type header");
    }

    //noinspection LoopStatementThatDoesntLoop
    do {
        HeaderElement[] elements = header.getElements();
        if (elements.length != 1) {
            break;
        }
        String type = elements[0].getName();
        if (!type.equalsIgnoreCase("application/json")) {
            break;
        }

        return true;
    } while (false);
    throw new Exception("Unsupported content type: " + header.getValue());
}

From source file:davmail.http.DavGatewayHttpClientFacade.java

/**
 * Test method header for supported authentication mode,
 * return true if Basic authentication is not available
 *
 * @param getMethod http method//from w  w  w . j  av  a2s . c  o  m
 * @return true if only NTLM is enabled
 */
public static boolean acceptsNTLMOnly(HttpMethod getMethod) {
    Header authenticateHeader = null;
    if (getMethod.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
        authenticateHeader = getMethod.getResponseHeader("WWW-Authenticate");
    } else if (getMethod.getStatusCode() == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
        authenticateHeader = getMethod.getResponseHeader("Proxy-Authenticate");
    }
    if (authenticateHeader == null) {
        return false;
    } else {
        boolean acceptBasic = false;
        boolean acceptNTLM = false;
        HeaderElement[] headerElements = authenticateHeader.getElements();
        for (HeaderElement headerElement : headerElements) {
            if ("NTLM".equalsIgnoreCase(headerElement.getName())) {
                acceptNTLM = true;
            }
            if ("Basic realm".equalsIgnoreCase(headerElement.getName())) {
                acceptBasic = true;
            }
        }
        return acceptNTLM && !acceptBasic;

    }
}