Search in sources :

Example 1 with Request

use of core.framework.web.Request in project core-ng-project by neowu.

the class WebServiceControllerBuilder method buildMethod.

private String buildMethod() {
    CodeBuilder builder = new CodeBuilder();
    builder.append("public {} execute({} request) throws Exception {\n", type(Response.class), type(Request.class));
    List<String> params = Lists.newArrayList();
    Annotation[][] annotations = method.getParameterAnnotations();
    Type[] paramTypes = method.getGenericParameterTypes();
    for (int i = 0; i < annotations.length; i++) {
        Type paramType = paramTypes[i];
        String paramTypeLiteral = type(paramType);
        PathParam pathParam = Params.annotation(annotations[i], PathParam.class);
        if (pathParam != null) {
            params.add(pathParam.value());
            builder.indent(1).append("{} {} = ({}) request.pathParam(\"{}\", {});\n", paramTypeLiteral, pathParam.value(), paramTypeLiteral, pathParam.value(), variable(paramType));
        } else {
            params.add("bean");
            builder.indent(1).append("{} bean = ({}) request.bean({});\n", paramTypeLiteral, paramTypeLiteral, variable(paramType));
        }
    }
    if (void.class == method.getReturnType()) {
        builder.indent(1).append("delegate.{}(", method.getName());
    } else {
        builder.indent(1).append("{} response = delegate.{}(", type(method.getReturnType()), method.getName());
    }
    builder.appendCommaSeparatedValues(params).append(");\n");
    if (void.class.equals(method.getReturnType())) {
        builder.indent(1).append("return {}.empty().status({});\n", Response.class.getCanonicalName(), variable(responseStatus));
    } else {
        builder.indent(1).append("return {}.bean(response).status({});\n", Response.class.getCanonicalName(), variable(responseStatus));
    }
    builder.append("}");
    return builder.build();
}
Also used : Response(core.framework.web.Response) Type(java.lang.reflect.Type) Request(core.framework.web.Request) PathParam(core.framework.api.web.service.PathParam) CodeBuilder(core.framework.impl.asm.CodeBuilder)

Example 2 with Request

use of core.framework.web.Request in project core-ng-project by neowu.

the class WebSecurityInterceptor method intercept.

@Override
public Response intercept(Invocation invocation) throws Exception {
    Request request = invocation.context().request();
    if (!"https".equals(request.scheme())) {
        return Response.redirect(redirectURL(request), HTTPStatus.MOVED_PERMANENTLY);
    } else {
        Response response = invocation.proceed();
        response.header("Strict-Transport-Security", "max-age=31536000");
        response.contentType().ifPresent(contentType -> {
            if (ContentType.TEXT_HTML.mediaType().equals(contentType.mediaType())) {
                response.header("X-Frame-Options", "DENY");
                response.header("X-XSS-Protection", "1; mode=block");
            }
            response.header("X-Content-Type-Options", "nosniff");
        });
        return response;
    }
}
Also used : Response(core.framework.web.Response) Request(core.framework.web.Request)

Aggregations

Request (core.framework.web.Request)2 Response (core.framework.web.Response)2 PathParam (core.framework.api.web.service.PathParam)1 CodeBuilder (core.framework.impl.asm.CodeBuilder)1 Type (java.lang.reflect.Type)1