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:keywhiz.service.resources.SessionLogoutResourceIntegrationTest.java

License:Apache License

@Test
public void sendsExpiredCookie() throws Exception {
    Request request = new Request.Builder().post(RequestBody.create(MediaType.parse("text/plain"), ""))
            .url(testUrl("/admin/logout")).build();

    Response response = client.newCall(request).execute();
    assertThat(response.code()).isEqualTo(303);

    List<String> cookies = response.headers(HttpHeaders.SET_COOKIE);
    assertThat(cookies).hasSize(1);/*w w w. jav a  2  s.c o  m*/

    NewCookie cookie = NewCookie.valueOf(cookies.get(0));
    assertThat(cookie.getName()).isEqualTo("session");
    assertThat(cookie.getValue()).isEqualTo("expired");
    assertThat(cookie.getVersion()).isEqualTo(1);
    assertThat(cookie.getPath()).isEqualTo("/admin");
    assertThat(cookie.isSecure()).isTrue();
    assertThat(cookie.isHttpOnly()).isTrue();
    assertThat(cookie.getExpiry()).isEqualTo(new Date(0));
}

From source file:library.util.OkHttpStack.java

License:Apache License

@SuppressWarnings("deprecation")
private static void setConnectionParametersForRequest(Builder builder, Request<?> request)
        throws IOException, AuthFailureError {
    switch (request.getMethod()) {
    case Request.Method.DEPRECATED_GET_OR_POST:
        // Ensure backwards compatibility.  Volley assumes a request with a null body is a GET.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            builder.post(RequestBody.create(MediaType.parse(request.getPostBodyContentType()), postBody));
        }//from w  w  w. ja  va2  s.com
        break;
    case Request.Method.GET:
        builder.get();
        break;
    case Request.Method.DELETE:
        builder.delete();
        break;
    case Request.Method.POST:
        builder.post(createRequestBody(request));
        break;
    case Request.Method.PUT:
        builder.put(createRequestBody(request));
        break;
    case Request.Method.HEAD:
        builder.head();
        break;
    case Request.Method.OPTIONS:
        builder.method("OPTIONS", null);
        break;
    case Request.Method.TRACE:
        builder.method("TRACE", null);
        break;
    case Request.Method.PATCH:
        builder.patch(createRequestBody(request));
        break;
    default:
        throw new IllegalStateException("Unknown method type.");
    }
}

From source file:net.codestory.rest.misc.PostBody.java

License:Apache License

public static RequestBody json(String body) {
    return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), body);
}

From source file:net.ltgt.resteasy.client.okhttp.OkHttpClientEngine.java

License:Apache License

private RequestBody createRequestBody(final ClientInvocation request) {
    if (request.getEntity() == null) {
        return null;
    }/*from  w w w  .ja  v  a  2s. co m*/

    // NOTE: this will invoke WriterInterceptors which can possibly change the request,
    // so it must be done first, before reading any header.
    final Buffer buffer = new Buffer();
    try {
        request.writeRequestBody(buffer.outputStream());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    javax.ws.rs.core.MediaType mediaType = request.getHeaders().getMediaType();
    final MediaType contentType = (mediaType == null) ? null : MediaType.parse(mediaType.toString());

    return new RequestBody() {
        @Override
        public long contentLength() throws IOException {
            return buffer.size();
        }

        @Override
        public MediaType contentType() {
            return contentType;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.write(buffer, buffer.size());
        }
    };
}

From source file:net.omnypay.sdk.exampleapp.network.HttpOps.java

License:Apache License

public static void doPost(final String url, final Object postBody, final Listener callback) {
    new Thread(new Runnable() {
        @Override/*from  ww  w  .j  a  v a  2  s .  co  m*/
        public void run() {
            Gson gson = new Gson();
            MediaType JSON = MediaType.parse("application/json; charset=utf-8");
            RequestBody body = RequestBody.create(JSON, gson.toJson(postBody));
            String timestamp = "" + ((new Date().getTime()) / 1000);
            String localVarPath = "/api/identity/authentication".replaceAll("\\{format\\}", "json");
            String signature = generateSignature("POST", timestamp, Constants.API_KEY, Constants.API_SECRET,
                    localVarPath, Constants.CORRELATION_ID, postBody);
            Request request = new Request.Builder().header("X-api-key", Constants.API_KEY)
                    .header("X-correlation-id", Constants.CORRELATION_ID).header("X-timestamp", timestamp)
                    .header("X-signature", signature).url(url).post(body).build();
            try {
                Response response = client.newCall(request).execute();
                callback.onResult(response.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }).start();
}

From source file:net.qiujuer.common.okhttp.impl.RequestCallBuilder.java

License:Apache License

protected RequestBody createMultipartBody(StrParam[] stringStrParams, IOParam[] IOParams) {
    MultipartBuilder builder = new MultipartBuilder();
    builder.type(MultipartBuilder.FORM);
    builder = buildMultipartBody(builder);

    if (stringStrParams != null && stringStrParams.length > 0) {
        for (StrParam strParam : stringStrParams) {
            if (strParam.key != null && strParam.value != null) {
                builder.addFormDataPart(strParam.key, strParam.value);
                log("buildMultiStringParam: key: " + strParam.key + " value: " + strParam.value);
            } else {
                log("buildMultiStringParam: key: " + (strParam.key != null ? strParam.key : "null") + " value: "
                        + (strParam.value != null ? strParam.value : "null"));
            }//from   www . j  a  va 2 s  .  c om
        }
    }

    if (IOParams != null && IOParams.length > 0) {
        for (IOParam param : IOParams) {
            if (param.key != null && param.file != null) {
                String fileName = param.file.getName();
                RequestBody fileBody = RequestBody.create(MediaType.parse(Util.getFileMimeType(fileName)),
                        param.file);
                builder.addFormDataPart(param.key, fileName, fileBody);
                log("buildMultiFileParam: key: " + param.key + " value: " + fileName);
            } else {
                log("buildMultiFileParam: key: " + (param.key != null ? param.key : "null") + " file: "
                        + (param.file != null ? param.file.getName() : "null"));
            }
        }
    }
    return builder.build();
}

From source file:net.qiujuer.common.okhttp.impl.RequestCallBuilder.java

License:Apache License

@Override
public Request.Builder builderPost(String url, String string) {
    RequestBody body = RequestBody//from   w  ww . j  a va2s.  c  o  m
            .create(MediaType.parse(String.format("text/plain; charset=%s", mProtocolCharset)), string);

    return builderPost(url, body);
}

From source file:net.qiujuer.common.okhttp.impl.RequestCallBuilder.java

License:Apache License

@Override
public Request.Builder builderPost(String url, byte[] bytes) {
    RequestBody body = RequestBody.create(
            MediaType.parse(String.format("application/octet-stream; charset=%s", mProtocolCharset)), bytes);

    return builderPost(url, body);
}

From source file:net.qiujuer.common.okhttp.impl.RequestCallBuilder.java

License:Apache License

@Override
public Request.Builder builderPost(String url, File file) {
    RequestBody body = RequestBody.create(
            MediaType.parse(String.format("application/octet-stream; charset=%s", mProtocolCharset)), file);

    return builderPost(url, body);
}

From source file:net.qiujuer.common.okhttp.impl.RequestCallBuilder.java

License:Apache License

@Override
public Request.Builder builderPost(String url, JSONObject jsonObject) {
    RequestBody body = RequestBody.create(
            MediaType.parse(String.format("application/json; charset=%s", mProtocolCharset)),
            jsonObject.toString());// w  ww .  j a  v a 2s . c  o m

    return builderPost(url, body);
}