Example usage for com.squareup.okhttp MediaType parse

List of usage examples for com.squareup.okhttp MediaType parse

Introduction

In this page you can find the example usage for com.squareup.okhttp MediaType parse.

Prototype

public static MediaType parse(String string) 

Source Link

Document

Returns a media type for string , or null if string is not a well-formed media type.

Usage

From source file:co.paralleluniverse.fibers.okhttp.InterceptorTest.java

License:Open Source License

@Test
public void applicationInterceptorsCanShortCircuitResponses() throws Exception {
    server.get().shutdown(); // Accept no connections.

    Request request = new Request.Builder().url("https://localhost:1/").build();

    final Response interceptorResponse = new Response.Builder().request(request).protocol(Protocol.HTTP_1_1)
            .code(200).message("Intercepted!")
            .body(ResponseBody.create(MediaType.parse("text/plain; charset=utf-8"), "abc")).build();

    client.interceptors().add(new Interceptor() {
        @Override//from ww  w.  j a  v  a 2  s .co m
        public Response intercept(Chain chain) throws IOException {
            return interceptorResponse;
        }
    });

    Response response = FiberOkHttpUtil.executeInFiber(client, request);
    assertSame(interceptorResponse, response);
}

From source file:co.paralleluniverse.fibers.okhttp.InterceptorTest.java

License:Open Source License

@Ignore
@Test//from www. ja v a  2s . c o m
public void networkInterceptorsCannotShortCircuitResponses() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(500));

    Interceptor interceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            return new Response.Builder().request(chain.request()).protocol(Protocol.HTTP_1_1).code(200)
                    .message("Intercepted!")
                    .body(ResponseBody.create(MediaType.parse("text/plain; charset=utf-8"), "abc")).build();
        }
    };
    client.networkInterceptors().add(interceptor);

    Request request = new Request.Builder().url(server.getUrl("/")).build();

    try {
        FiberOkHttpUtil.executeInFiber(client, request);
        fail();
    } catch (IllegalStateException expected) {
        assertEquals("network interceptor " + interceptor + " must call proceed() exactly once",
                expected.getMessage());
    }
}

From source file:co.paralleluniverse.fibers.okhttp.InterceptorTest.java

License:Open Source License

private void rewriteRequestToServer(List<Interceptor> interceptors) throws Exception {
    server.enqueue(new MockResponse());

    interceptors.add(new Interceptor() {
        @Override/*from  w  w w.j a va2 s . c om*/
        public Response intercept(Chain chain) throws IOException {
            Request originalRequest = chain.request();
            return chain.proceed(originalRequest.newBuilder().method("POST", uppercase(originalRequest.body()))
                    .addHeader("OkHttp-Intercepted", "yep").build());
        }
    });

    Request request = new Request.Builder().url(server.getUrl("/")).addHeader("Original-Header", "foo")
            .method("PUT", RequestBody.create(MediaType.parse("text/plain"), "abc")).build();

    FiberOkHttpUtil.executeInFiber(client, request);

    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("ABC", recordedRequest.getBody().readUtf8());
    assertEquals("foo", recordedRequest.getHeader("Original-Header"));
    assertEquals("yep", recordedRequest.getHeader("OkHttp-Intercepted"));
    assertEquals("POST", recordedRequest.getMethod());
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

@Test
public void applicationInterceptorsCanShortCircuitResponses() throws Exception {
    server.shutdown(); // Accept no connections.

    Request request = new Request.Builder().url("https://localhost:1/").build();

    final Response interceptorResponse = new Response.Builder().request(request).protocol(Protocol.HTTP_1_1)
            .code(200).message("Intercepted!")
            .body(ResponseBody.create(MediaType.parse("text/plain; charset=utf-8"), "abc")).build();

    client.interceptors().add(new Interceptor() {
        @Override//from w w w.  jav  a 2s  .co  m
        public Response intercept(Chain chain) throws IOException {
            return interceptorResponse;
        }
    });

    Response response = client.newCall(request).execute();
    assertSame(interceptorResponse, response);
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

@Ignore
@Test// w  w  w  .j  a  v  a  2  s.c om
public void networkInterceptorsCannotShortCircuitResponses() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(500));

    Interceptor interceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            return new Response.Builder().request(chain.request()).protocol(Protocol.HTTP_1_1).code(200)
                    .message("Intercepted!")
                    .body(ResponseBody.create(MediaType.parse("text/plain; charset=utf-8"), "abc")).build();
        }
    };
    client.networkInterceptors().add(interceptor);

    Request request = new Request.Builder().url(server.url("/")).build();

    try {
        client.newCall(request).execute();
        fail();
    } catch (IllegalStateException expected) {
        assertEquals("network interceptor " + interceptor + " must call proceed() exactly once",
                expected.getMessage());
    }
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

@Test
public void networkInterceptorsCanChangeRequestMethodFromGetToPost() throws Exception {
    server.enqueue(new MockResponse());

    client.networkInterceptors().add(new Interceptor() {
        @Override/*  w ww  .  java2  s  . co m*/
        public Response intercept(Chain chain) throws IOException {
            Request originalRequest = chain.request();
            MediaType mediaType = MediaType.parse("text/plain");
            RequestBody body = RequestBody.create(mediaType, "abc");
            return chain.proceed(originalRequest.newBuilder().method("POST", body)
                    .header("Content-Type", mediaType.toString())
                    .header("Content-Length", Long.toString(body.contentLength())).build());
        }
    });

    Request request = new Request.Builder().url(server.url("/")).get().build();

    client.newCall(request).execute();

    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("POST", recordedRequest.getMethod());
    assertEquals("abc", recordedRequest.getBody().readUtf8());
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

private void rewriteRequestToServer(List<Interceptor> interceptors) throws Exception {
    server.enqueue(new MockResponse());

    interceptors.add(new Interceptor() {
        @Override/*  w w  w .  j ava  2s.  c o  m*/
        public Response intercept(Chain chain) throws IOException {
            Request originalRequest = chain.request();
            return chain.proceed(originalRequest.newBuilder().method("POST", uppercase(originalRequest.body()))
                    .addHeader("OkHttp-Intercepted", "yep").build());
        }
    });

    Request request = new Request.Builder().url(server.url("/")).addHeader("Original-Header", "foo")
            .method("PUT", RequestBody.create(MediaType.parse("text/plain"), "abc")).build();

    client.newCall(request).execute();

    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("ABC", recordedRequest.getBody().readUtf8());
    assertEquals("foo", recordedRequest.getHeader("Original-Header"));
    assertEquals("yep", recordedRequest.getHeader("OkHttp-Intercepted"));
    assertEquals("POST", recordedRequest.getMethod());
}

From source file:com.abiquo.apiclient.RestClient.java

License:Apache License

public <T extends SingleResourceTransportDto> T post(final String uri, final String accept,
        final String contentType, final SingleResourceTransportDto body, final Class<T> returnClass) {
    try {/*from   w  ww  .j a  v  a  2 s  . c o  m*/
        RequestBody requestBody = RequestBody.create(MediaType.parse(withVersion(contentType)),
                json.write(body));
        Request request = new Request.Builder().url(absolute(uri))
                .addHeader(HttpHeaders.ACCEPT, withVersion(accept)).post(requestBody).build();

        return execute(request, returnClass);
    } catch (IOException ex) {
        throw Throwables.propagate(ex);
    }
}

From source file:com.abiquo.apiclient.RestClient.java

License:Apache License

public <T extends SingleResourceTransportDto> T post(final String uri, final String accept,
        final String contentType, final SingleResourceTransportDto body, final TypeToken<T> returnType) {
    try {/*from   w  w w .j av a2s . c o  m*/
        RequestBody requestBody = RequestBody.create(MediaType.parse(withVersion(contentType)),
                json.write(body));
        Request request = new Request.Builder().url(absolute(uri))
                .addHeader(HttpHeaders.ACCEPT, withVersion(accept)).post(requestBody).build();

        return execute(request, returnType);
    } catch (IOException ex) {
        throw Throwables.propagate(ex);
    }
}

From source file:com.abiquo.apiclient.RestClient.java

License:Apache License

public <T extends SingleResourceTransportDto> T post(final String uri, final String accept,
        final String contentType, final String body, final Class<T> returnClass) {
    try {/*from  w  w w  .j  a  v a2 s .  co  m*/
        RequestBody requestBody = RequestBody.create(MediaType.parse(withVersion(contentType)), body);
        Request request = new Request.Builder().url(absolute(uri))
                .addHeader(HttpHeaders.ACCEPT, withVersion(accept)).post(requestBody).build();

        return execute(request, returnClass);
    } catch (IOException ex) {
        throw Throwables.propagate(ex);
    }
}