Search in sources :

Example 31 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project CipherTrust_Application_Protection by thalescpl-io.

the class CipherTrustManagerHelper method getTokenFromRefresh.

/**
 * Returns an String that will be a JWT token to be used for REST calls
 * based on the refresh token.
 * <p>
 * Note: This is using a Java KeyStore for authentication.
 *
 * @param keystorepwd
 *            password to the java keystore
 * @param keystorelocation
 *            location of javakeystore that contains certificates
 * @return string JWT token
 */
public String getTokenFromRefresh(String keystorepwd, String keystorelocation) throws IOException {
    OkHttpClient client = getOkHttpClient(keystorepwd, this.cmipaddress, keystorelocation);
    MediaType mediaType = MediaType.parse("application/json");
    String grant_typetag = "{\"grant_type\":";
    String grant_type = "refresh_token";
    String refreshtokentag = "\"refresh_token\":";
    String authcall = grant_typetag + quote + grant_type + quote + comma + refreshtokentag + quote + this.refreshtoken + quote + " }";
    RequestBody body = RequestBody.create(authcall, mediaType);
    Request request = new Request.Builder().url("https://" + this.cmipaddress + "/api/v1/auth/tokens").method("POST", body).addHeader("Content-Type", "application/json").build();
    Response response = client.newCall(request).execute();
    String returnvalue = response.body().string();
    String jwt = JsonPath.read(returnvalue.toString(), "$.jwt").toString();
    return jwt;
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Example 32 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project CipherTrust_Application_Protection by thalescpl-io.

the class CipherTrustManagerHelper method getTokenFromRefresh.

/**
 * Returns an String that will be a JWT token to be used for REST calls
 * based on the refresh token.
 * <p>
 * Note: This is not using a Java KeyStore for authentication.
 *
 * @return string JWT token
 */
public String getTokenFromRefresh() throws IOException {
    OkHttpClient client = getUnsafeOkHttpClient();
    MediaType mediaType = MediaType.parse("application/json");
    String grant_typetag = "{\"grant_type\":";
    String grant_type = "refresh_token";
    String refreshtokentag = "\"refresh_token\":";
    String authcall = grant_typetag + quote + grant_type + quote + comma + refreshtokentag + quote + this.refreshtoken + quote + " }";
    RequestBody body = RequestBody.create(authcall, mediaType);
    Request request = new Request.Builder().url("https://" + this.cmipaddress + "/api/v1/auth/tokens").method("POST", body).addHeader("Content-Type", "application/json").build();
    Response response = client.newCall(request).execute();
    String returnvalue = response.body().string();
    String jwt = JsonPath.read(returnvalue.toString(), "$.jwt").toString();
    return jwt;
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Example 33 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project CipherTrust_Application_Protection by thalescpl-io.

the class CipherTrustManagerHelper method getToken.

/**
 * Returns an String that will be a JWT token to be used for REST calls.
 * <p>
 * Note: This is using a Java KeyStore for authentication.
 *
 * @param keystorepwd
 *            password to the java keystore
 * @param keystorelocation
 *            location of javakeystore that contains certificates
 * @return string JWT token
 */
public String getToken(String keystorepwd, String keystorelocation) throws IOException {
    this.keystorepwd = keystorepwd;
    OkHttpClient client = getOkHttpClient(keystorepwd, this.cmipaddress, keystorelocation);
    MediaType mediaType = MediaType.parse("application/json");
    String grant_typetag = "{\"grant_type\":";
    String grant_type = "password";
    String passwordtag = "\"password\":";
    String usernametag = "\"username\":";
    String labels = "\"labels\": [\"myapp\",\"cli\"]}";
    String authcall = grant_typetag + quote + grant_type + quote + comma + usernametag + quote + this.username + quote + comma + passwordtag + quote + this.password + quote + comma + labels;
    RequestBody body = RequestBody.create(authcall, mediaType);
    Request request = new Request.Builder().url("https://" + this.cmipaddress + "/api/v1/auth/tokens").method("POST", body).addHeader("Content-Type", "application/json").build();
    Response response = client.newCall(request).execute();
    String returnvalue = response.body().string();
    String jwt = JsonPath.read(returnvalue.toString(), "$.jwt").toString();
    this.refreshtoken = JsonPath.read(returnvalue.toString(), "$.refresh_token").toString();
    this.token = jwt;
    return jwt;
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Example 34 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project spacecraft-android by JamesfChen.

the class ResponseTest method error.

@Test
public void error() {
    MediaType plainText = MediaType.get("text/plain; charset=utf-8");
    ResponseBody errorBody = ResponseBody.create(plainText, "Broken!");
    Response<?> response = Response.error(400, errorBody);
    assertThat(response.raw()).isNotNull();
    assertThat(response.raw().body().contentType()).isEqualTo(plainText);
    assertThat(response.raw().body().contentLength()).isEqualTo(7);
    try {
        response.raw().body().source();
        fail();
    } catch (IllegalStateException expected) {
    }
    assertThat(response.code()).isEqualTo(400);
    assertThat(response.message()).isEqualTo("Response.error()");
    assertThat(response.headers().size()).isZero();
    assertThat(response.isSuccessful()).isFalse();
    assertThat(response.body()).isNull();
    assertThat(response.errorBody()).isSameAs(errorBody);
}
Also used : MediaType(okhttp3.MediaType) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 35 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project nylas-java by nylas.

the class HttpLoggingInterceptor method logRequest.

private void logRequest(Request request) throws IOException {
    RequestBody requestBody = request.body();
    boolean hasBody = requestBody != null;
    // Summary
    if (requestLogs.isDebugEnabled()) {
        requestLogs.debug("=> " + request.method() + " " + request.url() + " reqBodySize=" + (hasBody ? requestBody.contentLength() : 0));
    }
    logHeaders("=>", request.headers());
    if (bodyLogs.isDebugEnabled()) {
        String message;
        if (!hasBody) {
            message = " No request body";
        } else {
            MediaType contentType = requestBody.contentType();
            if (!isPrintableMediaType(contentType)) {
                message = " Skipped logging request body of type that may not be printable: " + contentType;
            } else {
                Buffer buf = new Buffer();
                requestBody.writeTo(buf);
                message = bodyBufferToString("", buf, contentType);
            }
        }
        bodyLogs.debug("=>" + message);
    }
}
Also used : Buffer(okio.Buffer) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Aggregations

MediaType (okhttp3.MediaType)297 RequestBody (okhttp3.RequestBody)186 Request (okhttp3.Request)179 Response (okhttp3.Response)158 IOException (java.io.IOException)137 ResponseBody (okhttp3.ResponseBody)71 OkHttpClient (okhttp3.OkHttpClient)68 Charset (java.nio.charset.Charset)50 Buffer (okio.Buffer)50 Headers (okhttp3.Headers)48 JSONObject (org.json.JSONObject)38 BufferedSource (okio.BufferedSource)36 MultipartBody (okhttp3.MultipartBody)34 HttpUrl (okhttp3.HttpUrl)30 Map (java.util.Map)24 BufferedSink (okio.BufferedSink)23 File (java.io.File)22 InputStream (java.io.InputStream)21 ArrayList (java.util.ArrayList)21 Test (org.junit.Test)21