Search in sources :

Example 1 with ContentType

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);
}
Also used : JSONParam(core.framework.impl.log.filter.JSONParam) ContentType(core.framework.http.ContentType) BytesParam(core.framework.impl.log.filter.BytesParam)

Example 2 with ContentType

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();
}
Also used : RequestBuilder(org.apache.http.client.methods.RequestBuilder) FieldParam(core.framework.impl.log.filter.FieldParam) ContentType(core.framework.http.ContentType) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HTTPMethod(core.framework.http.HTTPMethod) HTTPClientException(core.framework.http.HTTPClientException)

Example 3 with ContentType

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());
}
Also used : ContentType(core.framework.http.ContentType) Test(org.junit.jupiter.api.Test)

Example 4 with ContentType

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;
}
Also used : Path(java.nio.file.Path) Response(core.framework.web.Response) ContentType(core.framework.http.ContentType) NotFoundException(core.framework.web.exception.NotFoundException)

Aggregations

ContentType (core.framework.http.ContentType)4 HTTPClientException (core.framework.http.HTTPClientException)1 HTTPMethod (core.framework.http.HTTPMethod)1 BytesParam (core.framework.impl.log.filter.BytesParam)1 FieldParam (core.framework.impl.log.filter.FieldParam)1 JSONParam (core.framework.impl.log.filter.JSONParam)1 Response (core.framework.web.Response)1 NotFoundException (core.framework.web.exception.NotFoundException)1 Path (java.nio.file.Path)1 RequestBuilder (org.apache.http.client.methods.RequestBuilder)1 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)1 Test (org.junit.jupiter.api.Test)1