<commit_before>/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you 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 king.flow.test.net;

import com.github.jsonj.JsonElement;
import com.github.jsonj.JsonObject;
import com.github.jsonj.tools.JsonParser;
import com.github.jsonj.tools.JsonSerializer;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Set;
import java.util.TreeSet;

import java.util.logging.Level;
import java.util.logging.Logger;
import king.flow.common.CommonConstants;
import static king.flow.common.CommonConstants.UTF8;
import static king.flow.common.CommonConstants.XML_NODE_PREFIX;

/**
 * Handler implementation for the echo server.
 */
@Sharable
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
    
    private static final Logger logger = Logger.getLogger(
            EchoServerHandler.class.getName());
    private static String dataPath = "./mock-data/server.json";
    private JsonObject jsonRoot;
    private Set<String> invalidAccount;
    private static final String DOT = ".";
    private static final String DOT_SPLIT_PATTERN = "\\.";
    
    public EchoServerHandler() throws FileNotFoundException, IOException {
        initialData();
    }
    
    private void initialData() throws IOException {
        JsonParser jsonParser = new JsonParser();
        jsonRoot = jsonParser.parse(new InputStreamReader(new FileInputStream(dataPath), UTF8)).asObject();
//        jsonRoot = jsonParser.parse(new FileReader(dataPath)).asObject();
        invalidAccount = new TreeSet<>();
    }
    
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf bb = (ByteBuf) msg;
        int code = bb.readInt();
        logger.log(Level.INFO, "retcode : {0}", code);
        int length = bb.readInt();
        logger.log(Level.INFO, "packet length : {0}", length);
        final String request = bb.readBytes(length).toString(Charset.forName("utf-8"));
        logger.log(Level.INFO, "packet content : \n{0}", request);
        
        String terminal = readTerminalId(request);
        String terminalPrefix = new StringBuilder("terminal").append(DOT)
                .append(terminal).append(DOT).toString();
        
        String prsCode = readPrsCode(request);
        String prsCodePrefix = new StringBuilder(terminalPrefix)
                .append("prscode").append(DOT)
                .append(prsCode).append(DOT).toString();
        
        String pwdPrefix = new StringBuilder(terminalPrefix)
                .append("pwdVerify").append(DOT).toString();
        
        String operationPrefix = new StringBuilder(prsCodePrefix)
                .append("operation").append(DOT).toString();

        //whether operation need to account privilege
        for (String accountName : invalidAccount) {
            String actNameMark = ">" + accountName + "</";
            if (request.contains(actNameMark)) {
                StringBuilder actHaltMsgCondition = new StringBuilder(terminalPrefix)
                        .append("accountHalt").append(DOT).append("errmsg");
                String actHaltErrMsg = readJsonElement(actHaltMsgCondition).asPrimitive().asString();
                ctx.write(Unpooled.copiedBuffer(MockServerResp.mockFailedGeneralResp(terminal, actHaltErrMsg).getBytes(UTF8)));
                return;
            }
        }

        //whether operation need to verify password
        StringBuilder verifyPwdQuery = new StringBuilder(operationPrefix).append("verifyPwd");
        boolean needPwdVerification = needPwdVerification(verifyPwdQuery);
        if (needPwdVerification) {
            StringBuilder pwdCondition = new StringBuilder(pwdPrefix).append("password");
            String password = readJsonElement(pwdCondition).asPrimitive().asString();
            String pwdMark = ">" + password + "</";
            if (!request.contains(pwdMark)) {
                StringBuilder pwdErrMsgCondition = new StringBuilder(pwdPrefix).append("errmsg");
                String pwdErrMsg = readJsonElement(pwdErrMsgCondition).asPrimitive().asString();
                ctx.write(Unpooled.copiedBuffer(MockServerResp.mockFailedGeneralResp(terminal, pwdErrMsg).getBytes(UTF8)));<commit_msg>update mock server implementation<commit_after>/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you 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 king.flow.test.net;

import com.github.jsonj.JsonElement;
import com.github.jsonj.JsonObject;
import com.github.jsonj.tools.JsonParser;
import com.github.jsonj.tools.JsonSerializer;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Set;
import java.util.TreeSet;

import java.util.logging.Level;
import java.util.logging.Logger;
import king.flow.common.CommonConstants;
import static king.flow.common.CommonConstants.UTF8;
import static king.flow.common.CommonConstants.XML_NODE_PREFIX;

/**
 * Handler implementation for the echo server.
 */
@Sharable
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
    
    private static final Logger logger = Logger.getLogger(
            EchoServerHandler.class.getName());
    private static String dataPath = "./mock-data/server.json";
    private JsonObject jsonRoot;
    private Set<String> invalidAccount;
    private static final String DOT = ".";
    private static final String DOT_SPLIT_PATTERN = "\\.";
    
    public EchoServerHandler() throws FileNotFoundException, IOException {
        initialData();
    }
    
    private void initialData() throws IOException {
        JsonParser jsonParser = new JsonParser();
        jsonRoot = jsonParser.parse(new InputStreamReader(new FileInputStream(dataPath), UTF8)).asObject();
//        jsonRoot = jsonParser.parse(new FileReader(dataPath)).asObject();
        invalidAccount = new TreeSet<>();
    }
    
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf bb = (ByteBuf) msg;
        int code = bb.readInt();
        logger.log(Level.INFO, "retcode : {0}", code);
        int length = bb.readInt();
        logger.log(Level.INFO, "packet length : {0}", length);
        final String request = bb.readBytes(length).toString(Charset.forName("utf-8"));
        logger.log(Level.INFO, "packet content : \n{0}", request);
        
        String terminal = readTerminalId(request);
        String terminalPrefix = new StringBuilder("terminal").append(DOT)
                .append(terminal).append(DOT).toString();
        
        String prsCode = readPrsCode(request);
        String prsCodePrefix = new StringBuilder(terminalPrefix)
                .append("prscode").append(DOT)
                .append(prsCode).append(DOT).toString();
        
        String pwdPrefix = new StringBuilder(terminalPrefix)
                .append("pwdVerify").append(DOT).toString();
        
        String operationPrefix = new StringBuilder(prsCodePrefix)
                .append("operation").append(DOT).toString();

        //whether operation need to account privilege
        if (!prsCode.equalsIgnoreCase("accoutGuashi")) {
            for (String accountName : invalidAccount) {
                String actNameMark = ">" + accountName + "</";
                if (request.contains(actNameMark)) {
                    StringBuilder actHaltMsgCondition = new StringBuilder(terminalPrefix)
                            .append("accountHalt").append(DOT).append("errmsg");
                    String actHaltErrMsg = readJsonElement(actHaltMsgCondition).asPrimitive().asString();
                    ctx.write(Unpooled.copiedBuffer(MockServerResp.mockFailedGeneralResp(terminal, actHaltErrMsg).getBytes(UTF8)));
                    return;
                }
            }
        }

        //whether operation need to verify password
        StringBuilder verifyPwdQuery = new StringBuilder(operationPrefix).append("verifyPwd");
        boolean needPwdVerification = needPwdVerification(verifyPwdQuery);
        if (needPwdVerification) {
            StringBuilder pwdCondition = new StringBuilder(pwdPrefix).append("password");
            String password = readJsonElement(pwdCondition).asPrimitive().asString();
            String pwdMark = ">" + password + "</";
            if (!request.contains(pwdMark)) {
                StringBuilder pwdErrMsgCondition = new StringBuilder(pwdPrefix).append("errmsg");
                String pwdErrMsg = readJsonElement(pwdErrMsgCondition).asPrimitive().asString();
                ctx.write(Unpooled.copiedBuffer(MockServerResp.mockFailedGeneralResp(terminal, pwdErrMsg).getBytes(UTF8)));
{'fraction_non_alphanumeric': 0.10223209971977969, 'fraction_numerical': 0.0030920861919025992, 'mean_word_length': 3.2193232776192415, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6888216', 'n_tokens_mistral': 2732, 'n_tokens_neox': 2667, 'n_words': 662}