com.blade.mvc.http.Request.header() - java examples

Here are the examples of the java api com.blade.mvc.http.Request.header() taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

5 Examples 7

19 View Complete Implementation : WebKit.java
Copyright Apache License 2.0
Author : lets-blade
/**
 * Get the client IP address by request
 *
 * @param request Request instance
 * @return return ip address
 */
public static String ipAddress(Request request) {
    String ipAddress = request.header("x-forwarded-for");
    if (StringKit.isBlank(ipAddress) || UNKNOWN_MAGIC.equalsIgnoreCase(ipAddress)) {
        ipAddress = request.header("Proxy-Client-IP");
    }
    if (StringKit.isBlank(ipAddress) || UNKNOWN_MAGIC.equalsIgnoreCase(ipAddress)) {
        ipAddress = request.header("WL-Proxy-Client-IP");
    }
    if (StringKit.isBlank(ipAddress) || UNKNOWN_MAGIC.equalsIgnoreCase(ipAddress)) {
        ipAddress = request.header("X-Real-IP");
    }
    if (StringKit.isBlank(ipAddress) || UNKNOWN_MAGIC.equalsIgnoreCase(ipAddress)) {
        ipAddress = request.header("HTTP_CLIENT_IP");
    }
    if (StringKit.isBlank(ipAddress) || UNKNOWN_MAGIC.equalsIgnoreCase(ipAddress)) {
        ipAddress = request.header("HTTP_X_FORWARDED_FOR");
    }
    return ipAddress;
}

18 View Complete Implementation : ExceptionHandlerTest.java
Copyright Apache License 2.0
Author : lets-blade
@Before
public void before() {
    request = mock(Request.clreplaced);
    when(request.header("Accept")).thenReturn("text/html");
    response = mock(Response.clreplaced);
    WebContext.init(Blade.me(), "/");
    WebContext.set(new WebContext(request, response, null));
}

18 View Complete Implementation : HttpRequestTest.java
Copyright Apache License 2.0
Author : lets-blade
@Test
public void testHeaders() {
    Request mockRequest = mockHttpRequest("GET");
    Map<String, String> headers = new HashMap<>();
    headers.put("h1", "a1");
    headers.put("h2", "a2");
    when(mockRequest.headers()).thenReturn(headers);
    Request request = new HttpRequest(mockRequest);
    replacedertEquals("a1", request.header("h1"));
    replacedertEquals("a2", request.header("h2"));
}

14 View Complete Implementation : RouteActionArguments.java
Copyright Apache License 2.0
Author : lets-blade
private static Object getHeader(ParamStruct paramStruct) throws BladeException {
    Type argType = paramStruct.argType;
    HeaderParam headerParam = paramStruct.headerParam;
    String paramName = paramStruct.paramName;
    Request request = paramStruct.request;
    String key = StringKit.isEmpty(headerParam.value()) ? paramName : headerParam.value();
    String val = request.header(key);
    if (StringKit.isBlank(val)) {
        val = headerParam.defaultValue();
    }
    return ReflectKit.convert(argType, val);
}

14 View Complete Implementation : StaticFileHandler.java
Copyright Apache License 2.0
Author : lets-blade
private boolean isHttp304(ChannelHandlerContext ctx, Request request, long size, long lastModified) {
    String ifModifiedSince = request.header(HttpConst.IF_MODIFIED_SINCE);
    if (StringKit.isNotEmpty(ifModifiedSince) && httpCacheSeconds > 0) {
        Date ifModifiedSinceDate = format(ifModifiedSince, Const.HTTP_DATE_FORMAT);
        // Only compare up to the second because the datetime format we send to the client
        // does not have milliseconds
        long ifModifiedSinceDateSeconds = ifModifiedSinceDate.getTime() / 1000;
        if (ifModifiedSinceDateSeconds == lastModified / 1000) {
            FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_MODIFIED);
            String contentType = StringKit.mimeType(request.uri());
            if (null != contentType) {
                response.headers().set(HttpConst.CONTENT_TYPE, contentType);
            }
            response.headers().set(HttpConst.DATE, DateKit.gmtDate());
            response.headers().set(HttpConst.CONTENT_LENGTH, size);
            if (request.keepAlive()) {
                response.headers().set(HttpConst.CONNECTION, HttpConst.KEEP_ALIVE);
            }
            // Close the connection as soon as the error message is sent.
            ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
            return true;
        }
    }
    return false;
}