Search in sources :

Example 1 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project fineract by apache.

the class ThitsaWorksCreditBureauIntegrationWritePlatformServiceImpl method okHttpConnectionMethod.

@Transactional
@Override
@SuppressWarnings("deprecation")
public String okHttpConnectionMethod(String userName, String password, String subscriptionKey, String subscriptionId, String url, String token, File file, FormDataContentDisposition fileData, Long uniqueId, String nrcId, String process) {
    String reponseMessage = null;
    RequestBody requestBody = null;
    OkHttpClient client = new OkHttpClient();
    if (process.equals("UploadCreditReport")) {
        String fileName = fileData.getFileName();
        requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
        requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", fileName, requestBody).addFormDataPart("BODY", "formdata").addFormDataPart("userName", userName).build();
    } else if (process.equals("token")) {
        final MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        String jsonBody = "" + "BODY=x-www-form-urlencoded&\r" + "grant_type=password&\r" + "userName=" + userName + "&\r" + "password=" + password + "&\r";
        requestBody = RequestBody.create(jsonBody, mediaType);
    } else if (process.equals("NRC")) {
        final MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        String jsonBody = "BODY=x-www-form-urlencoded&nrc=" + nrcId + "&";
        requestBody = RequestBody.create(jsonBody, mediaType);
    }
    HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
    String urlokhttp = urlBuilder.build().toString();
    Request request = null;
    if (token == null) {
        request = new Request.Builder().header("mcix-subscription-key", subscriptionKey).header("mcix-subscription-id", subscriptionId).header("Content-Type", "application/x-www-form-urlencoded").url(urlokhttp).post(requestBody).build();
    }
    if (token != null) {
        if (process.equals("CreditReport")) {
            // GET method for fetching credit report
            request = new Request.Builder().header("mcix-subscription-key", subscriptionKey).header("mcix-subscription-id", subscriptionId).header("Content-Type", "application/x-www-form-urlencoded").header("Authorization", "Bearer " + token).url(urlokhttp).get().build();
        } else if (process.equals("UploadCreditReport")) {
            // POST for uploading Credit-Report(multipart/form-data)
            // To ThitsaWork
            request = new Request.Builder().header("mcix-subscription-key", subscriptionKey).header("mcix-subscription-id", subscriptionId).header("Content-Type", "multipart/form-data").header("Authorization", "Bearer " + token).url(urlokhttp).post(requestBody).build();
        } else {
            // POST method for application/x-www-form-urlencoded
            request = new Request.Builder().header("mcix-subscription-key", subscriptionKey).header("mcix-subscription-id", subscriptionId).header("Content-Type", "application/x-www-form-urlencoded").header("Authorization", "Bearer " + token).url(urlokhttp).post(requestBody).build();
        }
    }
    Response response;
    Integer responseCode = 0;
    try {
        response = client.newCall(request).execute();
        responseCode = response.code();
        reponseMessage = response.body().string();
    } catch (IOException e) {
        LOG.error("error occured in HTTP request-response method.", e);
    }
    if (responseCode != HttpURLConnection.HTTP_OK) {
        this.httpResponse(responseCode, reponseMessage);
    }
    if (process.equals("UploadCreditReport")) {
        // to show the Response on frontEnd
        JsonObject reportObject = JsonParser.parseString(reponseMessage).getAsJsonObject();
        String ResponseMessageJson = reportObject.get("ResponseMessage").getAsString();
        this.handleAPIIntegrityIssues(ResponseMessageJson);
    }
    return reponseMessage;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) CommandWrapperBuilder(org.apache.fineract.commands.service.CommandWrapperBuilder) DataValidatorBuilder(org.apache.fineract.infrastructure.core.data.DataValidatorBuilder) Request(okhttp3.Request) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project Shitter by nuclearfog.

the class Twitter method downloadImage.

/**
 * download image from twitter
 *
 * @param link link to the image
 * @return image bitmap
 */
public MediaStream downloadImage(String link) throws TwitterException {
    try {
        // this type of link requires authentication
        if (link.startsWith(DOWNLOAD)) {
            Response response = get(link, new ArrayList<>(0));
            if (response.code() == 200 && response.body() != null) {
                MediaType type = response.body().contentType();
                if (type != null) {
                    String mime = type.toString();
                    InputStream stream = response.body().byteStream();
                    return new MediaStream(stream, mime);
                }
            }
            throw new TwitterException(response);
        } else // public link, no authentication required
        {
            Request request = new Request.Builder().url(link).get().build();
            Response response = client.newCall(request).execute();
            if (response.code() == 200 && response.body() != null) {
                MediaType type = response.body().contentType();
                if (type != null) {
                    String mime = type.toString();
                    InputStream stream = response.body().byteStream();
                    return new MediaStream(stream, mime);
                }
            }
            throw new TwitterException(response);
        }
    } catch (IOException e) {
        throw new TwitterException(e);
    }
}
Also used : Response(okhttp3.Response) MediaStream(org.nuclearfog.twidda.backend.api.holder.MediaStream) InputStream(java.io.InputStream) Request(okhttp3.Request) MediaType(okhttp3.MediaType) IOException(java.io.IOException)

Example 3 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project files-sdk-java by Files-com.

the class FilesOkHttpApi method putBufferedInputStream.

@Override
public long putBufferedInputStream(String url, HttpMethods.RequestMethods requestType, String name, BufferedInputStream inputStream) throws IOException {
    String uri = ModelUtils.forceMandatoryUriEncode(url);
    MediaType type = MediaType.parse("application/octet-stream");
    Request.Builder request = new Request.Builder();
    request.addHeader("Content-type", "application/octet-stream");
    request.url(uri);
    RequestBody body = create(type, inputStream);
    updateRequestWithHttpMethod(request, body, requestType);
    Response response = FilesHttpClient.getHttpClient().newCall(request.build()).execute();
    return 0;
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Example 4 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project che-server by eclipse-che.

the class DirectKubernetesAPIAccessHelper method convertResponseBody.

private static void convertResponseBody(Response.ResponseBuilder responseBuilder, okhttp3.Response response) {
    ResponseBody responseBody = response.body();
    if (responseBody != null) {
        responseBuilder.entity(responseBody.byteStream());
        MediaType contentType = responseBody.contentType();
        if (contentType != null) {
            responseBuilder.type(contentType.toString());
        }
    }
}
Also used : MediaType(okhttp3.MediaType) ResponseBody(okhttp3.ResponseBody)

Example 5 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project xray-maven-plugin by Xray-App.

the class XrayResultsImporter method submitStandardCloud.

public String submitStandardCloud(String format, String reportFile) throws Exception {
    OkHttpClient client = CommonUtils.getHttpClient(useInternalTestProxy, ignoreSslErrors, this.timeout);
    String authenticationPayload = "{ \"client_id\": \"" + clientId + "\", \"client_secret\": \"" + clientSecret + "\" }";
    RequestBody body = RequestBody.create(authenticationPayload, MEDIA_TYPE_JSON);
    Request request = new Request.Builder().url(xrayCloudAuthenticateUrl).post(body).build();
    Response response = null;
    String authToken = null;
    try {
        response = client.newCall(request).execute();
        String responseBody = response.body().string();
        if (response.isSuccessful()) {
            authToken = responseBody.replace("\"", "");
        } else {
            throw new IOException("failed to authenticate " + response);
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    }
    String credentials = "Bearer " + authToken;
    String[] supportedFormats = new String[] { XRAY_FORMAT, JUNIT_FORMAT, TESTNG_FORMAT, ROBOT_FORMAT, NUNIT_FORMAT, XUNIT_FORMAT, CUCUMBER_FORMAT };
    if (!Arrays.asList(supportedFormats).contains(format)) {
        throw new Exception("unsupported report format: " + format);
    }
    MediaType mediaType;
    String[] xmlBasedFormats = new String[] { JUNIT_FORMAT, TESTNG_FORMAT, ROBOT_FORMAT, NUNIT_FORMAT, XUNIT_FORMAT };
    if (Arrays.asList(xmlBasedFormats).contains(format)) {
        mediaType = MEDIA_TYPE_XML;
    } else {
        mediaType = MEDIA_TYPE_JSON;
    }
    String endpointUrl;
    if (XRAY_FORMAT.equals(format)) {
        endpointUrl = xrayCloudApiBaseUrl + "/import/execution";
    } else {
        endpointUrl = xrayCloudApiBaseUrl + "/import/execution/" + format;
    }
    RequestBody requestBody = null;
    try {
        String reportContent = new String(Files.readAllBytes(Paths.get(reportFile)));
        requestBody = RequestBody.create(reportContent, mediaType);
    } catch (Exception e1) {
        e1.printStackTrace();
        throw e1;
    }
    HttpUrl url = HttpUrl.get(endpointUrl);
    HttpUrl.Builder builder = url.newBuilder();
    if (projectKey != null) {
        builder.addQueryParameter("projectKey", this.projectKey);
    }
    if (fixVersion != null) {
        builder.addQueryParameter("fixVersion", this.fixVersion);
    }
    if (revision != null) {
        builder.addQueryParameter("revision", this.revision);
    }
    if (testPlanKey != null) {
        builder.addQueryParameter("testPlanKey", this.testPlanKey);
    }
    if (testExecKey != null) {
        builder.addQueryParameter("testExecKey", this.testExecKey);
    }
    if (testEnvironment != null) {
        builder.addQueryParameter("testEnvironment", this.testEnvironment);
    }
    request = new Request.Builder().url(builder.build()).post(requestBody).addHeader("Authorization", credentials).build();
    try {
        response = client.newCall(request).execute();
        String responseBody = response.body().string();
        if (response.isSuccessful()) {
            JSONObject responseObj = new JSONObject(responseBody);
            // System.out.println("Test Execution: "+((JSONObject)(responseObj.get("testExecIssue"))).get("key"));
            return (responseBody);
        } else {
            // System.err.println(responseBody);
            throw new IOException("Unexpected HTTP code " + response);
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) JSONObject(org.json.JSONObject) 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