Example usage for org.apache.http.client.methods HttpRequestBase getFirstHeader

List of usage examples for org.apache.http.client.methods HttpRequestBase getFirstHeader

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpRequestBase getFirstHeader.

Prototype

public Header getFirstHeader(String str) 

Source Link

Usage

From source file:Main.java

public static Charset getCharsetFromHttpRequest(final HttpRequestBase request) {
    if (request == null)
        return null;
    String charsetName = null;/*from   w  w w  .  j  a v  a 2  s . com*/
    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;
}

From source file:com.ycj.android.common.utils.OtherUtils.java

public static Charset getCharsetFromHttpRequest(final HttpRequestBase request) {
    if (request == null) return null;
    String charsetName = null;/*from ww  w  .  j av a  2s  . co m*/
    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;
}

From source file:com.kolich.havalo.client.service.HavaloClientSigner.java

private static final String getStringToSign(final HttpRequestBase request) {
    final StringBuilder sb = new StringBuilder();
    // HTTP-Verb (GET, PUT, POST, or DELETE) + "\n"
    sb.append(request.getMethod().toUpperCase()).append(LINE_SEPARATOR_UNIX);
    // RFC822 formatted Date (from 'Date' header on request) + "\n"      
    sb.append(request.getFirstHeader(DATE).getValue()).append(LINE_SEPARATOR_UNIX);
    // Content-Type (from 'Content-Type' request header, optional) + "\n"
    final Header contentType;
    if ((contentType = request.getFirstHeader(CONTENT_TYPE)) != null) {
        sb.append(contentType.getValue());
    }//from w  w w .  j av  a2 s . c  o m
    sb.append(LINE_SEPARATOR_UNIX);
    // CanonicalizedResource
    sb.append(request.getURI().getRawPath());
    return sb.toString();
}

From source file:com.addbean.autils.tools.OtherUtils.java

public static Charset getCharsetFromHttpRequest(final HttpRequestBase request) {
    if (request == null)
        return null;
    String charsetName = null;//from  w ww.java 2  s  .  c  o  m
    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) {
            Log.e("OtherUtils", e.getMessage());
        }
    }

    return isSupportedCharset ? Charset.forName(charsetName) : null;
}

From source file:com.ycl.framework.utils.util.OtherUtils.java

public static Charset getCharsetFromHttpRequest(final HttpRequestBase request) {
    if (request == null)
        return null;
    String charsetName = null;//from ww w  .j  av  a2 s  .c  o m
    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 ignored) {
        }
    }

    return isSupportedCharset ? Charset.forName(charsetName) : null;
}

From source file:com.lxh.util.image.OtherUtils.java

/**
 * ?HTTP?/*from  w  w w  . j  a  va2  s  .c o m*/
 * @param request HTTP{@link org.apache.http.client.methods.HttpRequestBase}
 * @return ?{@link java.nio.charset.Charset}
 * @see java.nio.charset.Charset
 */
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;
}

From source file:org.apache.camel.component.http4.HttpProducer.java

/**
 * Extracts the response from the method as a InputStream.
 *
 * @param httpRequest the method that was executed
 * @return the response either as a stream, or as a deserialized java object
 * @throws IOException can be thrown// www . j a va  2s  .co  m
 */
protected static Object extractResponseBody(HttpRequestBase httpRequest, HttpResponse httpResponse,
        Exchange exchange) throws IOException, ClassNotFoundException {
    HttpEntity entity = httpResponse.getEntity();
    if (entity == null) {
        return null;
    }

    InputStream is = entity.getContent();
    if (is == null) {
        return null;
    }

    Header header = httpResponse.getFirstHeader(Exchange.CONTENT_ENCODING);
    String contentEncoding = header != null ? header.getValue() : null;

    if (!exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
        is = GZIPHelper.uncompressGzip(contentEncoding, is);
    }
    // Honor the character encoding
    String contentType = null;
    header = httpRequest.getFirstHeader("content-type");
    if (header != null) {
        contentType = header.getValue();
        // find the charset and set it to the Exchange
        HttpHelper.setCharsetFromContentType(contentType, exchange);
    }
    InputStream response = doExtractResponseBodyAsStream(is, exchange);
    // if content type is a serialized java object then de-serialize it back to a Java object
    if (contentType != null && contentType.equals(HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT)) {
        return HttpHelper.deserializeJavaObjectFromStream(response);
    } else {
        return response;
    }
}

From source file:org.fcrepo.client.MoveBuilderTest.java

@Test
public void testMove() throws Exception {
    testBuilder.perform();//from w  w w .  j a va  2s  .  co  m

    final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
    verify(client).executeRequest(eq(uri), requestCaptor.capture());

    final HttpRequestBase request = requestCaptor.getValue();
    assertEquals("MOVE", request.getMethod());
    assertEquals(destUrl, request.getFirstHeader(DESTINATION).getValue());
}

From source file:org.fcrepo.client.GetBuilderTest.java

@Test
public void testModificationHeaders() throws Exception {
    final String etag = "123456";
    final String lastModified = "Mon, 19 May 2014 19:44:59 GMT";
    testBuilder.ifNoneMatch(etag).ifModifiedSince(lastModified).perform();

    final HttpRequestBase request = getRequest();
    assertEquals(etag, request.getFirstHeader(IF_NONE_MATCH).getValue());
    assertEquals(lastModified, request.getFirstHeader(IF_MODIFIED_SINCE).getValue());
}

From source file:org.fcrepo.client.GetBuilderTest.java

@Test
public void testStartRange() throws Exception {
    testBuilder.range(5L, null).perform();

    final HttpRequestBase request = getRequest();
    assertEquals("bytes=5-", request.getFirstHeader(RANGE).getValue());
}