Example usage for io.netty.handler.codec.http HttpHeaderNames ACCEPT

List of usage examples for io.netty.handler.codec.http HttpHeaderNames ACCEPT

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpHeaderNames ACCEPT.

Prototype

AsciiString ACCEPT

To view the source code for io.netty.handler.codec.http HttpHeaderNames ACCEPT.

Click Source Link

Document

"accept"

Usage

From source file:ccwihr.client.t1.HttpUploadClient.java

License:Apache License

/**
 * Standard usage of HTTP API in Netty without file Upload (get is not able
 * to achieve File upload due to limitation on request size).
 *
 * @return the list of headers that will be used in every example after
 **//*  www  .j av  a2s  .  co  m*/
private static List<Entry<String, String>> formget(Bootstrap bootstrap, String host, int port, String get,
        URI uriSimple) throws Exception {
    // XXX /formget
    // No use of HttpPostRequestEncoder since not a POST
    Channel channel = bootstrap.connect(host, port).sync().channel();

    // Prepare the HTTP request.
    QueryStringEncoder encoder = new QueryStringEncoder(get);
    // add Form attribute
    encoder.addParam("getform", "GET");
    encoder.addParam("info", "first value");
    encoder.addParam("secondinfo", "secondvalue &");
    // not the big one since it is not compatible with GET size
    // encoder.addParam("thirdinfo", textArea);
    encoder.addParam("thirdinfo", "third value\r\ntest second line\r\n\r\nnew line\r\n");
    encoder.addParam("Send", "Send");

    URI uriGet = new URI(encoder.toString());
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uriGet.toASCIIString());
    HttpHeaders headers = request.headers();
    headers.set(HttpHeaderNames.HOST, host);
    headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
    headers.set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP + "," + HttpHeaderValues.DEFLATE);

    headers.set(HttpHeaderNames.ACCEPT_CHARSET, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    headers.set(HttpHeaderNames.ACCEPT_LANGUAGE, "fr");
    headers.set(HttpHeaderNames.REFERER, uriSimple.toString());
    headers.set(HttpHeaderNames.USER_AGENT, "Netty Simple Http Client side");
    headers.set(HttpHeaderNames.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

    // connection will not close but needed
    // headers.set("Connection","keep-alive");
    // headers.set("Keep-Alive","300");

    headers.set(HttpHeaderNames.COOKIE, ClientCookieEncoder.STRICT.encode(new DefaultCookie("my-cookie", "foo"),
            new DefaultCookie("another-cookie", "bar")));

    // send request
    channel.writeAndFlush(request);

    // Wait for the server to close the connection.
    channel.closeFuture().sync();

    // convert headers to list
    return headers.entries();
}

From source file:cn.wcl.test.netty.HttpUploadClient.java

License:Apache License

/**
 * Standard usage of HTTP API in Netty without file Upload (get is not able to achieve File upload
 * due to limitation on request size).//from ww w.jav  a  2 s .  co  m
 *
 * @return the list of headers that will be used in every example after
 **/
private static List<Entry<String, String>> formget(Bootstrap bootstrap, String host, int port, String get,
        URI uriSimple) throws Exception {
    // XXX /formget
    // No use of HttpPostRequestEncoder since not a POST
    Channel channel = bootstrap.connect(host, port).sync().channel();

    // Prepare the HTTP request.
    QueryStringEncoder encoder = new QueryStringEncoder(get);
    // add Form attribute
    encoder.addParam("getform", "GET");
    encoder.addParam("info", "first value");
    encoder.addParam("secondinfo", "secondvalue &");
    // not the big one since it is not compatible with GET size
    // encoder.addParam("thirdinfo", textArea);
    encoder.addParam("thirdinfo", "third value\r\ntest second line\r\n\r\nnew line\r\n");
    encoder.addParam("Send", "Send");

    URI uriGet = new URI(encoder.toString());
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uriGet.toASCIIString());
    HttpHeaders headers = request.headers();
    headers.set(HttpHeaderNames.HOST, host);
    headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
    headers.set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP + "," + HttpHeaderValues.DEFLATE);

    headers.set(HttpHeaderNames.ACCEPT_CHARSET, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    headers.set(HttpHeaderNames.ACCEPT_LANGUAGE, "fr");
    headers.set(HttpHeaderNames.REFERER, uriSimple.toString());
    headers.set(HttpHeaderNames.USER_AGENT, "Netty Simple Http Client side");
    headers.set(HttpHeaderNames.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

    //connection will not close but needed
    // headers.set("Connection","keep-alive");
    // headers.set("Keep-Alive","300");

    headers.set(HttpHeaderNames.COOKIE, ClientCookieEncoder.STRICT.encode(new DefaultCookie("my-cookie", "foo"),
            new DefaultCookie("another-cookie", "bar")));

    // send request
    channel.writeAndFlush(request);

    // Wait for the server to close the connection.
    channel.closeFuture().sync();

    // convert headers to list
    return headers.entries();
}

From source file:com.google.devtools.build.lib.remote.blobstore.http.HttpDownloadHandler.java

License:Open Source License

private HttpRequest buildRequest(DownloadCommand request) {
    HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
            constructPath(request.uri(), request.hash(), request.casDownload()));
    httpRequest.headers().set(HttpHeaderNames.HOST, constructHost(request.uri()));
    httpRequest.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    httpRequest.headers().set(HttpHeaderNames.ACCEPT, "*/*");
    return httpRequest;
}

From source file:com.google.devtools.build.lib.remote.blobstore.http.HttpUploadHandler.java

License:Open Source License

private HttpRequest buildRequest(UploadCommand msg) {
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT,
            constructPath(msg.uri(), msg.hash(), msg.casUpload()));
    request.headers().set(HttpHeaderNames.HOST, constructHost(msg.uri()));
    request.headers().set(HttpHeaderNames.ACCEPT, "*/*");
    request.headers().set(HttpHeaderNames.CONTENT_LENGTH, msg.contentLength());
    request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    return request;
}

From source file:com.linecorp.armeria.client.http.SimpleHttpClientIntegrationTest.java

License:Apache License

@Test
public void testRequestNoBody() throws Exception {
    SimpleHttpClient client = Clients.newClient(remoteInvokerFactory, "none+http://127.0.0.1:" + httpPort,
            SimpleHttpClient.class);
    SimpleHttpRequest request = SimpleHttpRequestBuilder.forGet("/httptestbody")
            .header(HttpHeaderNames.ACCEPT, "utf-8").build();
    SimpleHttpResponse response = client.execute(request).get();
    assertEquals(HttpResponseStatus.OK, response.status());
    assertEquals("alwayscache", response.headers().get(HttpHeaderNames.CACHE_CONTROL));
    assertEquals("METHOD: GET|ACCEPT: utf-8|BODY: ", new String(response.content(), StandardCharsets.UTF_8));
}

From source file:com.linecorp.armeria.client.http.SimpleHttpClientIntegrationTest.java

License:Apache License

@Test
public void testRequestWithBody() throws Exception {
    SimpleHttpClient client = Clients.newClient(remoteInvokerFactory, "none+http://127.0.0.1:" + httpPort,
            SimpleHttpClient.class);
    SimpleHttpRequest request = SimpleHttpRequestBuilder.forPost("/httptestbody")
            .header(HttpHeaderNames.ACCEPT, "utf-8").content("requestbody", StandardCharsets.UTF_8)
            .build();//from  www  .  j a  va2 s .  co  m
    SimpleHttpResponse response = client.execute(request).get();
    assertEquals(HttpResponseStatus.OK, response.status());
    assertEquals("alwayscache", response.headers().get(HttpHeaderNames.CACHE_CONTROL));
    assertEquals("METHOD: POST|ACCEPT: utf-8|BODY: requestbody",
            new String(response.content(), StandardCharsets.UTF_8));
}

From source file:com.linecorp.armeria.client.http.SimpleHttpRequestBuilderTest.java

License:Apache License

@Test
public void headerByName() {
    SimpleHttpRequest request = SimpleHttpRequestBuilder.forGet("/path").header(HttpHeaderNames.ACCEPT, "utf-8")
            .header(HttpHeaderNames.COOKIE, "monster").header(HttpHeaderNames.ACCEPT, "shift-jis").build();
    assertEquals(3, request.headers().size());
    assertEquals(Arrays.asList("utf-8", "shift-jis"), request.headers().getAll(HttpHeaderNames.ACCEPT));
    assertEquals("monster", request.headers().get(HttpHeaderNames.COOKIE));
}

From source file:com.linecorp.armeria.client.http.SimpleHttpRequestBuilderTest.java

License:Apache License

@Test
public void headersObject() {
    HttpHeaders headers = new DefaultHttpHeaders();
    headers.set(HttpHeaderNames.CACHE_CONTROL, "alwayscache");
    headers.set(HttpHeaderNames.ACCEPT_ENCODING, "gzip");
    SimpleHttpRequest request = SimpleHttpRequestBuilder.forGet("/path").header(HttpHeaderNames.ACCEPT, "utf-8")
            .headers(headers).header(HttpHeaderNames.ORIGIN, "localhost").build();
    assertEquals(4, request.headers().size());
    assertEquals("alwayscache", request.headers().get(HttpHeaderNames.CACHE_CONTROL));
    assertEquals("gzip", request.headers().get(HttpHeaderNames.ACCEPT_ENCODING));
    assertEquals("localhost", request.headers().get(HttpHeaderNames.ORIGIN));
    assertEquals("utf-8", request.headers().get(HttpHeaderNames.ACCEPT));
}

From source file:com.linecorp.armeria.client.http.SimpleHttpResponseTest.java

License:Apache License

@Test
public void normal() {
    HttpHeaders headers = new DefaultHttpHeaders();
    headers.set(HttpHeaderNames.ACCEPT, "utf-8");
    byte[] body = "content".getBytes(StandardCharsets.UTF_8);
    SimpleHttpResponse response = new SimpleHttpResponse(HttpResponseStatus.NOT_FOUND, headers, body);
    assertEquals(HttpResponseStatus.NOT_FOUND, response.status());
    assertEquals(headers, response.headers());
    assertArrayEquals(body, response.content());
}

From source file:com.linecorp.armeria.server.thrift.ThriftServiceCodec.java

License:Apache License

private SerializationFormat validateRequestAndDetermineSerializationFormat(Object originalRequest)
        throws InvalidHttpRequestException {
    if (!(originalRequest instanceof HttpRequest)) {
        return defaultSerializationFormat;
    }/*  ww  w .j  a v  a  2s  .co  m*/
    final SerializationFormat serializationFormat;
    HttpRequest httpRequest = (HttpRequest) originalRequest;
    if (httpRequest.method() != HttpMethod.POST) {
        throw new InvalidHttpRequestException(HttpResponseStatus.METHOD_NOT_ALLOWED,
                HTTP_METHOD_NOT_ALLOWED_EXCEPTION);
    }

    final String contentTypeHeader = httpRequest.headers().get(HttpHeaderNames.CONTENT_TYPE);
    if (contentTypeHeader != null) {
        serializationFormat = SerializationFormat.fromMimeType(contentTypeHeader)
                .orElse(defaultSerializationFormat);
        if (!allowedSerializationFormats.contains(serializationFormat)) {
            throw new InvalidHttpRequestException(HttpResponseStatus.UNSUPPORTED_MEDIA_TYPE,
                    THRIFT_PROTOCOL_NOT_SUPPORTED);
        }
    } else {
        serializationFormat = defaultSerializationFormat;
    }

    final String acceptHeader = httpRequest.headers().get(HttpHeaderNames.ACCEPT);
    if (acceptHeader != null) {
        // If accept header is present, make sure it is sane. Currently, we do not support accept
        // headers with a different format than the content type header.
        SerializationFormat outputSerializationFormat = SerializationFormat.fromMimeType(acceptHeader)
                .orElse(serializationFormat);
        if (outputSerializationFormat != serializationFormat) {
            throw new InvalidHttpRequestException(HttpResponseStatus.NOT_ACCEPTABLE,
                    ACCEPT_THRIFT_PROTOCOL_MUST_MATCH_CONTENT_TYPE);
        }
    }
    return serializationFormat;
}