<commit_before>/*
 * Copyright 2015-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.lastaflute.web;

import javax.annotation.Resource;

import org.dbflute.optional.OptionalThing;
import org.lastaflute.core.json.JsonManager;
import org.lastaflute.core.magic.async.AsyncManager;
import org.lastaflute.core.message.MessageManager;
import org.lastaflute.core.message.UserMessages;
import org.lastaflute.core.time.TimeManager;
import org.lastaflute.db.jta.stage.TransactionStage;
import org.lastaflute.web.api.ApiFailureResource;
import org.lastaflute.web.api.ApiManager;
import org.lastaflute.web.path.ActionPathResolver;
import org.lastaflute.web.response.ApiResponse;
import org.lastaflute.web.response.HtmlResponse;
import org.lastaflute.web.response.JsonResponse;
import org.lastaflute.web.response.StreamResponse;
import org.lastaflute.web.response.XmlResponse;
import org.lastaflute.web.response.next.ForwardNext;
import org.lastaflute.web.ruts.process.ActionRuntime;
import org.lastaflute.web.servlet.request.RequestManager;
import org.lastaflute.web.servlet.request.ResponseManager;
import org.lastaflute.web.servlet.session.SessionManager;
import org.lastaflute.web.util.LaActionRuntimeUtil;
import org.lastaflute.web.validation.ActionValidator;

/**
 * @author jflute
 */
public abstract class LastaAction {

    // ===================================================================================
    //                                                                          Definition
    //                                                                          ==========
    private static final UrlChain EMPTY_URL_CHAIN = new UrlChain(null);

    // ===================================================================================
    //                                                                           Attribute
    //                                                                           =========
    /** The manager of time. (NotNull) */
    @Resource
    private TimeManager timeManager;

    /** The manager of JSON. (NotNull) */
    @Resource
    private JsonManager jsonManager;

    /** The manager of message. (NotNull) */
    @Resource
    private MessageManager messageManager;

    /** The manager of asynchronous. (NotNull) */
    @Resource
    private AsyncManager asycnManager;

    /** The stage of transaction. (NotNull) */
    @Resource
    private TransactionStage transactionStage;

    /** The manager of request. (NotNull) */
    @Resource
    private RequestManager requestManager;

    /** The manager of response. (NotNull) */
    @Resource
    private ResponseManager responseManager;

    /** The manager of session. (NotNull) */
    @Resource
    private SessionManager sessionManager;

    /** The manager of API. (NotNull) */
    @Resource
    private ApiManager apiManager;

    /** The resolver of action, e.g. it can convert action type to action path. (NotNull) */
    @Resource
    private ActionPathResolver actionPathResolver;

    // ===================================================================================
    //                                                                          Validation
    //                                                                          ==========
    protected <MESSAGES extends UserMessages> ActionValidator<MESSAGES> createValidator() { // overridden as type-safe
        return createValidator(myValidationGroups());
    }

    protected Class<?>[] myValidationGroups() { // you can override
        return ActionValidator.DEFAULT_GROUPS; // means default group
    }

    @SuppressWarnings("unchecked")
    protected <MESSAGES extends UserMessages> ActionValidator<MESSAGES> createValidator(Class<?>... groups) { // for explicit groups
        return new ActionValidator<MESSAGES>(messageManager // to get validation message
                , () -> requestManager.getUserLocale() // used with messageManager
                , () -> (MESSAGES) createMessages() // for new user messages
                , () -> handleApiValidationError() // apiFailureHook
                , groups);
    }<commit_msg>rename createValidator(groups) to doCreateValidator(groups) as internal<commit_after>/*
 * Copyright 2015-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.lastaflute.web;

import javax.annotation.Resource;

import org.dbflute.optional.OptionalThing;
import org.lastaflute.core.json.JsonManager;
import org.lastaflute.core.magic.async.AsyncManager;
import org.lastaflute.core.message.MessageManager;
import org.lastaflute.core.message.UserMessages;
import org.lastaflute.core.time.TimeManager;
import org.lastaflute.db.jta.stage.TransactionStage;
import org.lastaflute.web.api.ApiFailureResource;
import org.lastaflute.web.api.ApiManager;
import org.lastaflute.web.path.ActionPathResolver;
import org.lastaflute.web.response.ApiResponse;
import org.lastaflute.web.response.HtmlResponse;
import org.lastaflute.web.response.JsonResponse;
import org.lastaflute.web.response.StreamResponse;
import org.lastaflute.web.response.XmlResponse;
import org.lastaflute.web.response.next.ForwardNext;
import org.lastaflute.web.ruts.process.ActionRuntime;
import org.lastaflute.web.servlet.request.RequestManager;
import org.lastaflute.web.servlet.request.ResponseManager;
import org.lastaflute.web.servlet.session.SessionManager;
import org.lastaflute.web.util.LaActionRuntimeUtil;
import org.lastaflute.web.validation.ActionValidator;

/**
 * @author jflute
 */
public abstract class LastaAction {

    // ===================================================================================
    //                                                                          Definition
    //                                                                          ==========
    private static final UrlChain EMPTY_URL_CHAIN = new UrlChain(null);

    // ===================================================================================
    //                                                                           Attribute
    //                                                                           =========
    /** The manager of time. (NotNull) */
    @Resource
    private TimeManager timeManager;

    /** The manager of JSON. (NotNull) */
    @Resource
    private JsonManager jsonManager;

    /** The manager of message. (NotNull) */
    @Resource
    private MessageManager messageManager;

    /** The manager of asynchronous. (NotNull) */
    @Resource
    private AsyncManager asycnManager;

    /** The stage of transaction. (NotNull) */
    @Resource
    private TransactionStage transactionStage;

    /** The manager of request. (NotNull) */
    @Resource
    private RequestManager requestManager;

    /** The manager of response. (NotNull) */
    @Resource
    private ResponseManager responseManager;

    /** The manager of session. (NotNull) */
    @Resource
    private SessionManager sessionManager;

    /** The manager of API. (NotNull) */
    @Resource
    private ApiManager apiManager;

    /** The resolver of action, e.g. it can convert action type to action path. (NotNull) */
    @Resource
    private ActionPathResolver actionPathResolver;

    // ===================================================================================
    //                                                                          Validation
    //                                                                          ==========
    protected <MESSAGES extends UserMessages> ActionValidator<MESSAGES> createValidator() { // overridden as type-safe
        return doCreateValidator(myValidationGroups());
    }

    protected Class<?>[] myValidationGroups() { // you can override
        return ActionValidator.DEFAULT_GROUPS; // means default group
    }

    @SuppressWarnings("unchecked")
    protected <MESSAGES extends UserMessages> ActionValidator<MESSAGES> doCreateValidator(Class<?>... groups) { // for explicit groups
        return new ActionValidator<MESSAGES>(messageManager // to get validation message
                , () -> requestManager.getUserLocale() // used with messageManager
                , () -> (MESSAGES) createMessages() // for new user messages
                , () -> handleApiValidationError() // apiFailureHook
                , groups);
    }
{'fraction_non_alphanumeric': 0.14207071776413296, 'fraction_numerical': 0.0025407579928011857, 'mean_word_length': 2.5595327807083645, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6640472', 'n_tokens_mistral': 2273, 'n_tokens_neox': 2174, 'n_words': 609}