Search in sources :

Example 16 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project staf by simpleworks-gmbh.

the class HttpClient method buildResponseBody.

/**
 * @brief method to transform a {@code Response} into a convinient
 *        {@code HttpResponse} response
 *
 * @param {@code ResponseBody} body
 * @return {@code HttpResponse} response, respecting the method argument,
 *         ReponseBody can be empty!
 * @throws IOException
 */
private static HttpResponse buildResponseBody(final ResponseBody body) throws Exception {
    if (body == null) {
        throw new IllegalArgumentException("body can't be null.");
    }
    final HttpResponse result;
    final MediaType contenttype = body.contentType();
    if (contenttype == null) {
        return new HttpResponse();
    }
    final String type = contenttype.type();
    if (HttpClient.logger.isDebugEnabled()) {
        HttpClient.logger.debug(String.format("body: %s, %s.", UtilsFormat.format("contenttype", contenttype), UtilsFormat.format("type", type)));
    }
    switch(type) {
        // https://square.github.io/okhttp/4.x/okhttp/okhttp3/-media-type/type/
        case "text":
            result = HttpClient.getResponseText(body);
            break;
        case "application":
            result = HttpClient.getResponseApplication(body);
            break;
        case "image":
            result = HttpClient.getResponseImage(body);
            break;
        default:
            throw new IllegalArgumentException(String.format("Content Type '%s' is not implemented yet.", type));
    }
    return result;
}
Also used : HttpResponse(de.simpleworks.staf.commons.api.HttpResponse) MediaType(okhttp3.MediaType)

Example 17 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project staf by simpleworks-gmbh.

the class HttpClient method getResponseImage.

private static HttpResponse getResponseImage(final ResponseBody body) throws Exception {
    final byte[] bytes = HttpClient.readBody(body);
    if ((bytes == null) || (bytes.length == 0)) {
        throw new RuntimeException("Response Body is empty.");
    }
    final HttpResponse result = new HttpResponse();
    final MediaType contenttype = body.contentType();
    final byte[] encodedImage = Base64.getEncoder().encode(bytes);
    final String pdf = new String(encodedImage);
    result.setBase64Body(pdf);
    final ContentTypeEnum type;
    switch(contenttype.subtype()) {
        case "png":
            type = ContentTypeEnum.PNG;
            break;
        case "jpg":
            type = ContentTypeEnum.JPG;
            break;
        case "jpeg":
            type = ContentTypeEnum.JPEG;
            break;
        default:
            type = ContentTypeEnum.UNKNOWN;
    }
    result.setContentType(type);
    return result;
}
Also used : ContentTypeEnum(de.simpleworks.staf.commons.enums.ContentTypeEnum) HttpResponse(de.simpleworks.staf.commons.api.HttpResponse) MediaType(okhttp3.MediaType)

Example 18 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project staf by simpleworks-gmbh.

the class HttpClient method getContentType.

private static ContentTypeEnum getContentType(final ResponseBody body) {
    final MediaType contentType = body.contentType();
    final String ct = String.format("%s/%s", contentType.type(), contentType.subtype());
    final ContentTypeEnum result = UtilsEnum.getEnumByValue(ContentTypeEnum.JSON, ct);
    if (result == null) {
        throw new IllegalArgumentException(String.format("Content Type '%s' is not implemented yet.", ct));
    }
    return result;
}
Also used : ContentTypeEnum(de.simpleworks.staf.commons.enums.ContentTypeEnum) MediaType(okhttp3.MediaType)

Example 19 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project kubernetes-client by fabric8io.

the class OkHttpClientImpl method parseMediaType.

static MediaType parseMediaType(String contentType) {
    MediaType result = MediaType.parse(contentType);
    MEDIA_TYPES.put(contentType, result);
    return result;
}
Also used : MediaType(okhttp3.MediaType)

Example 20 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project spring-cloud-square by spring-projects-experimental.

the class WebClientCallAdapterFactory method processRequestBody.

private void processRequestBody(WebClient.RequestBodySpec spec, RequestBody requestBody) {
    Publisher<byte[]> requestBodyPublisher = Flux.create(sink -> {
        try {
            Sink fluxSink = new Sink() {

                @Override
                public void write(Buffer source, long byteCount) throws IOException {
                    sink.next(source.readByteArray(byteCount));
                }

                @Override
                public void flush() {
                    sink.complete();
                }

                @Override
                public Timeout timeout() {
                    return Timeout.NONE;
                }

                @Override
                public void close() {
                    sink.complete();
                }
            };
            BufferedSink bufferedSink = Okio.buffer(fluxSink);
            requestBody.writeTo(bufferedSink);
            bufferedSink.flush();
        } catch (IOException e) {
            sink.error(e);
        }
    });
    spec.body(requestBodyPublisher, byte[].class);
    MediaType requestContentType = requestBody.contentType();
    if (requestContentType != null) {
        spec.contentType(org.springframework.http.MediaType.parseMediaType(requestContentType.toString()));
    }
}
Also used : Buffer(okio.Buffer) Sink(okio.Sink) BufferedSink(okio.BufferedSink) MediaType(okhttp3.MediaType) BufferedSink(okio.BufferedSink) IOException(java.io.IOException)

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