use of com.google.common.net.MediaType in project guava by hceylan.
the class MediaTypeTest method testGetCharset_unsupportedCharset.
public void testGetCharset_unsupportedCharset() {
MediaType mediaType = MediaType.parse("text/plain; charset=utf-wtf");
try {
mediaType.charset();
fail();
} catch (UnsupportedCharsetException expected) {
}
}
use of com.google.common.net.MediaType in project airlift by airlift.
the class StringResponseHandler method handle.
@Override
public StringResponse handle(Request request, Response response) {
byte[] bytes = readResponseBytes(request, response);
Charset charset = Optional.ofNullable(response.getHeader(CONTENT_TYPE)).map(MediaType::parse).flatMap(mediaType -> mediaType.charset().toJavaUtil()).orElse(UTF_8);
return new StringResponse(response.getStatusCode(), response.getHeaders(), new String(bytes, charset));
}
use of com.google.common.net.MediaType in project providence by morimekta.
the class ProvidenceServlet method doPost.
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
long startTime = System.nanoTime();
AtomicReference<PServiceCall> callRef = new AtomicReference<>();
AtomicReference<PServiceCall> responseRef = new AtomicReference<>();
PProcessor processor = new WrappedProcessor(processorProvider.processorForRequest(req), (c, r) -> {
callRef.set(c);
responseRef.set(r.handleCall(c));
return responseRef.get();
});
try {
Serializer requestSerializer = serializerProvider.getDefault();
if (req.getContentType() != null) {
try {
MediaType mediaType = MediaType.parse(req.getContentType());
requestSerializer = serializerProvider.getSerializer(mediaType.withoutParameters().toString());
} catch (IllegalArgumentException e) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown content-type: " + req.getContentType());
LOGGER.warn("Unknown content type in request", e);
return;
}
} else {
LOGGER.debug("Request is missing content type.");
}
Serializer responseSerializer = requestSerializer;
String acceptHeader = req.getHeader("Accept");
if (acceptHeader != null) {
String[] entries = acceptHeader.split("[,]");
for (String entry : entries) {
entry = entry.trim();
if (entry.isEmpty()) {
continue;
}
if ("*/*".equals(entry)) {
// Then responding same as request is good.
break;
}
try {
MediaType mediaType = MediaType.parse(entry);
responseSerializer = serializerProvider.getSerializer(mediaType.withoutParameters().toString());
break;
} catch (IllegalArgumentException ignore) {
// Ignore. Bad header input is pretty common.
}
}
}
MessageReader reader = new IOMessageReader(req.getInputStream(), requestSerializer);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MessageWriter writer = new IOMessageWriter(baos, responseSerializer);
getHandler(processor).process(reader, writer);
resp.setStatus(HttpServletResponse.SC_OK);
if (baos.size() > 0) {
resp.setContentType(responseSerializer.mediaType());
resp.setContentLength(baos.size());
resp.getOutputStream().write(baos.toByteArray());
resp.getOutputStream().flush();
}
long endTime = System.nanoTime();
double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS;
try {
instrumentation.onComplete(duration, callRef.get(), responseRef.get());
} catch (Throwable th) {
LOGGER.error("Exception in service instrumentation", th);
}
} catch (EOFException e) {
// output stream closed before write is complete.
// So we cannot even try to respond.
long endTime = System.nanoTime();
double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS;
try {
instrumentation.onTransportException(e, duration, callRef.get(), responseRef.get());
} catch (Throwable th) {
LOGGER.error("Exception in service instrumentation", th);
}
} catch (Exception e) {
try {
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Internal server error: " + e.getMessage());
} catch (IOException ioEx) {
e.addSuppressed(ioEx);
}
long endTime = System.nanoTime();
double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS;
try {
instrumentation.onTransportException(e, duration, callRef.get(), responseRef.get());
} catch (Throwable th) {
LOGGER.error("Exception in service instrumentation", th);
}
}
}
use of com.google.common.net.MediaType in project spf4j by zolyfarkas.
the class Configs method read.
@Nullable
public static <T> T read(final Class<T> type, final SchemaResolver schemaResolver, final Reader reader) throws IOException {
SpecificData sd = SpecificData.get();
Schema rSchema = sd.getSchema(type);
ConfigHeader header = parseHeader(reader, rSchema, schemaResolver);
Schema wSchema = header.getwSchema();
Reader content = header.getReader();
MediaType mt = header.getMediaType();
DatumReader<T> dr = new SpecificDatumReader<>(wSchema, rSchema);
Decoder decoder;
Adapter adapter = AvroCompatUtils.getAdapter();
if ("application".equals(mt.type()) && "json".equals(mt.subtype())) {
decoder = adapter.getJsonDecoder(wSchema, content);
} else if ("text".equals(mt.type()) && "yaml".equals(mt.subtype())) {
decoder = adapter.getYamlDecoder(wSchema, content);
} else {
throw new IllegalArgumentException("Unsupported media type " + mt);
}
return dr.read(null, decoder);
}
Aggregations