SpcLoginAction.java

package com.mycim.webapp.actions;

import com.fa.sesa.exception.Errors;
import com.fa.sesa.exception.SystemIllegalArgumentException;
import com.fa.sesa.i18n.I18nUtils;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.logging.Logger;
import com.mycim.framework.logging.LoggerFactory;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.valueobject.security.User;
import com.mycim.valueobject.sys.ReferenceFileDetail;
import org.apache.commons.codec.binary.Base64;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zhi.cai
 * @version 6.0.0
 * @date 2020-3-10
 **/
public class SpcLoginAction extends WipSetupAction {

    private static final Logger logger = LoggerFactory.getLogger(SpcLoginAction.class);

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        Long facilityRrn = LocalContext.getFacilityRrn();
        String userId = LocalContext.getUserId();
        Long userRrn = LocalContext.getUserRrn();

        User user = securityService.getUser(userRrn);
        String language = I18nUtils.getCurrentLanguage().toString();

        if (StringUtils.equals("CN", language)) {
            language = "zh-cn";
        } else if (StringUtils.equals("EN", language)) {
            language = "en-us";
        }

        String facilityId = this.getInstanceId(facilityRrn);

        String pwd = user.getPassword();

        // get the spc login url from reference detail: $$SPC_LOGIN_URL;
        String url = StringUtils.EMPTY;
        ReferenceFileDetail refDetail = this.getReferenceFileDetail("$$SPC_LOGIN_URL", "1", facilityRrn);
        if (refDetail != null) {
            StringBuffer urlBuffer = new StringBuffer();
            urlBuffer.append(refDetail.getData1Value()).append("://");
            urlBuffer.append(refDetail.getData2Value());
            url = urlBuffer.toString();
        } else {
            throw new SystemIllegalArgumentException(
                    Errors.create().content("REFERENCE DETAIL: $$SPC_LOGIN_URL HASN'T BEEN " + "SPECIFIED!!").build());
        }

        // get the current time as send time;
        String sendTime = StringUtils.EMPTY;
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sendTime = sf.format(new Date());

        // encode the detail result with base64;
        StringBuffer detailBuffer = new StringBuffer();
        detailBuffer.append("FLAG1").append('=').append(userId).append(',');
        detailBuffer.append("FLAG2").append('=').append(pwd).append(',');
        detailBuffer.append("FAB").append('=').append(facilityId).append(',');
        detailBuffer.append("LANGUAGE").append('=').append(language).append(',');
        detailBuffer.append("SENDTIME").append('=').append(sendTime);
        String detail = new String(Base64.encodeBase64(detailBuffer.toString().getBytes()));

        logger.info("The detail buffer is -----> " + detailBuffer);
        logger.info("The Encoded detail is -----> " + detail);
        logger.info("The url is -------> " + url);

        request.setAttribute("detail", detail);
        request.setAttribute("url", url);

        return mapping.getInputForward();
    }

}