use of core.framework.http.ContentType in project core-ng-project by neowu.
the class RequestParser method logRequestBody.
private void logRequestBody(RequestImpl request) {
ContentType contentType = request.contentType;
if (contentType == null)
return;
Object bodyParam = null;
if (ContentType.APPLICATION_JSON.mediaType().equals(contentType.mediaType())) {
bodyParam = new JSONParam(request.body, contentType.charset().orElse(Charsets.UTF_8));
} else if (ContentType.TEXT_XML.mediaType().equals(contentType.mediaType())) {
bodyParam = new BytesParam(request.body, contentType.charset().orElse(Charsets.UTF_8));
}
if (bodyParam != null)
logger.debug("[request] body={}", bodyParam);
}
use of core.framework.http.ContentType in project core-ng-project by neowu.
the class HTTPClientImpl method httpRequest.
HttpUriRequest httpRequest(HTTPRequest request) {
HTTPMethod method = request.method();
String uri = request.uri();
logger.debug("[request] method={}, uri={}", method, uri);
RequestBuilder builder = RequestBuilder.create(method.name());
try {
builder.setUri(uri);
} catch (IllegalArgumentException e) {
throw new HTTPClientException("uri is invalid, uri=" + uri, "INVALID_URL", e);
}
request.headers().forEach((name, value) -> {
logger.debug("[request:header] {}={}", name, new FieldParam(name, value));
builder.setHeader(name, value);
});
request.params().forEach((name, value) -> {
logger.debug("[request:param] {}={}", name, value);
builder.addParameter(name, value);
});
byte[] body = request.body();
if (body != null) {
ContentType contentType = request.contentType();
logRequestBody(request, contentType);
org.apache.http.entity.ContentType type = org.apache.http.entity.ContentType.create(contentType.mediaType(), contentType.charset().orElse(null));
builder.setEntity(new ByteArrayEntity(request.body(), type));
}
return builder.build();
}
use of core.framework.http.ContentType in project core-ng-project by neowu.
the class MimeTypesTest method get.
@Test
void get() {
assertNull(MimeTypes.get("file"));
ContentType contentType = MimeTypes.get("favicon.ico");
assertNotNull(contentType);
assertEquals("image/x-icon", contentType.mediaType());
}
use of core.framework.http.ContentType in project core-ng-project by neowu.
the class StaticDirectoryController method execute.
@Override
public Response execute(Request request) {
String path = request.pathParam("path");
Path filePath = contentDirectory.resolve(path);
logger.debug("requestFile={}", filePath);
if (!Files.isRegularFile(filePath, LinkOption.NOFOLLOW_LINKS) || !filePath.startsWith(contentDirectory))
throw new NotFoundException("not found, path=" + request.path());
Response response = Response.file(filePath);
ContentType contentType = MimeTypes.get(filePath.getFileName().toString());
if (contentType != null)
response.contentType(contentType);
if (cacheHeader != null)
response.header(HTTPHeaders.CACHE_CONTROL, cacheHeader);
return response;
}
Aggregations