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;
}
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;
}
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;
}
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;
}
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()));
}
}
Aggregations