<commit_before>/**
 * Copyright (c) 2014 Netflix, Inc.  All rights reserved.
 * 
 * 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 com.netflix.msl.server.common;

import com.netflix.msl.MslConstants;
import com.netflix.msl.MslCryptoException;
import com.netflix.msl.MslEncodingException;
import com.netflix.msl.MslKeyExchangeException;
import com.netflix.msl.entityauth.EntityAuthenticationScheme;
import com.netflix.msl.keyx.KeyExchangeScheme;
import com.netflix.msl.msg.ConsoleFilterStreamFactory;
import com.netflix.msl.msg.MessageInputStream;
import com.netflix.msl.msg.MslControl;
import com.netflix.msl.server.configuration.msg.ServerMessageContext;
import com.netflix.msl.server.configuration.tokens.TokenFactoryType;
import com.netflix.msl.server.configuration.util.ServerMslContext;
import com.netflix.msl.userauth.UserAuthenticationScheme;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Future;

/**
 * User: skommidi
 * Date: 7/21/14
 */
public class BaseServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    protected static final String payload = "Hello";
    protected static final String error = "Error";
    private static final int TIMEOUT = 25000;
    private boolean isNullCryptoContext;
    private boolean setConsoleFilterStreamFactory;
    private EntityAuthenticationScheme entityAuthScheme;
    private int numThreads;
    private TokenFactoryType tokenFactoryType;
    private long initialSequenceNum;
    private boolean isMessageEncrypted;
    private boolean isIntegrityProtected;
    private final List<EntityAuthenticationScheme> unSupportedEntityAuthFactories;
    private final List<UserAuthenticationScheme> unSupportedUserAuthFactories;
    private final List<KeyExchangeScheme> unSupportedKeyxFactories;
    protected ServerMslContext mslCtx;
    protected ServerMessageContext msgCtx;
    private MslControl mslCtrl;

    public BaseServlet(final int numThreads, final EntityAuthenticationScheme entityAuthScheme, final TokenFactoryType tokenFactoryType,
                       final long initialSequenceNum, final boolean isMessageEncrypted, final boolean isIntegrityProtected,
                       final List<EntityAuthenticationScheme> unSupportedEntityAuthFactories,
                       final List<UserAuthenticationScheme> unSupportedUserAuthFactories, final List<KeyExchangeScheme> unSupportedKeyxFactories,
                       final boolean isNullCryptoContext, final boolean setConsoleFilterStreamFactory) throws MslCryptoException, MslEncodingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, MslKeyExchangeException {
        this.numThreads = numThreads;
        this.entityAuthScheme = entityAuthScheme;
        this.tokenFactoryType = tokenFactoryType;
        this.initialSequenceNum = initialSequenceNum;
        this.isMessageEncrypted = isMessageEncrypted;
        this.isIntegrityProtected = isIntegrityProtected;
        this.unSupportedEntityAuthFactories = unSupportedEntityAuthFactories;
        this.unSupportedUserAuthFactories = unSupportedUserAuthFactories;
        this.unSupportedKeyxFactories = unSupportedKeyxFactories;
        this.isNullCryptoContext = isNullCryptoContext;
        this.setConsoleFilterStreamFactory = setConsoleFilterStreamFactory;
        configure();
    }

    private void configure() throws MslCryptoException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, MslKeyExchangeException, MslEncodingException {
        mslCtrl = new MslControl(numThreads);
        if(setConsoleFilterStreamFactory) {
            mslCtrl.setFilterFactory(new ConsoleFilterStreamFactory());
        }
        /**
         * Msl Context Configuration
         */
        mslCtx = new ServerMslContext(entityAuthScheme, false, tokenFactoryType, initialSequenceNum, unSupportedEntityAuthFactories,
                unSupportedUserAuthFactories, unSupportedKeyxFactories, isNullCryptoContext);

        /**
         * Message Context Configuration
         */
        msgCtx = new ServerMessageContext(mslCtx, payload.getBytes(MslConstants.DEFAULT_CHARSET), isMessageEncrypted);
        msgCtx.setIntegrityProtected(isIntegrityProtected);
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type");

        super.service(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();

        Map<String, String[]> params = request.getParameterMap();
        for (Entry<String,String[]> entry : params.entrySet()) {
            try {
                String key = entry.getKey();
                String[] value = entry.getValue();
                setPrivateVariable(out, key, value);
            } catch (Exception e) {
                e.printStackTrace();
                out.println(e.getMessage());
            }
        }
        try {
            configure();
        } catch (Exception e) {
            e.printStackTrace();
            out.println(e.getMessage());
        }
        out.println(request.getServletPath());
        out.close();
    }

    @Override<commit_msg>Suppress warnings on parameter map type.<commit_after>/**
 * Copyright (c) 2014 Netflix, Inc.  All rights reserved.
 * 
 * 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 com.netflix.msl.server.common;

import com.netflix.msl.MslConstants;
import com.netflix.msl.MslCryptoException;
import com.netflix.msl.MslEncodingException;
import com.netflix.msl.MslKeyExchangeException;
import com.netflix.msl.entityauth.EntityAuthenticationScheme;
import com.netflix.msl.keyx.KeyExchangeScheme;
import com.netflix.msl.msg.ConsoleFilterStreamFactory;
import com.netflix.msl.msg.MessageInputStream;
import com.netflix.msl.msg.MslControl;
import com.netflix.msl.server.configuration.msg.ServerMessageContext;
import com.netflix.msl.server.configuration.tokens.TokenFactoryType;
import com.netflix.msl.server.configuration.util.ServerMslContext;
import com.netflix.msl.userauth.UserAuthenticationScheme;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Future;

/**
 * User: skommidi
 * Date: 7/21/14
 */
public class BaseServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    protected static final String payload = "Hello";
    protected static final String error = "Error";
    private static final int TIMEOUT = 25000;
    private boolean isNullCryptoContext;
    private boolean setConsoleFilterStreamFactory;
    private EntityAuthenticationScheme entityAuthScheme;
    private int numThreads;
    private TokenFactoryType tokenFactoryType;
    private long initialSequenceNum;
    private boolean isMessageEncrypted;
    private boolean isIntegrityProtected;
    private final List<EntityAuthenticationScheme> unSupportedEntityAuthFactories;
    private final List<UserAuthenticationScheme> unSupportedUserAuthFactories;
    private final List<KeyExchangeScheme> unSupportedKeyxFactories;
    protected ServerMslContext mslCtx;
    protected ServerMessageContext msgCtx;
    private MslControl mslCtrl;

    public BaseServlet(final int numThreads, final EntityAuthenticationScheme entityAuthScheme, final TokenFactoryType tokenFactoryType,
                       final long initialSequenceNum, final boolean isMessageEncrypted, final boolean isIntegrityProtected,
                       final List<EntityAuthenticationScheme> unSupportedEntityAuthFactories,
                       final List<UserAuthenticationScheme> unSupportedUserAuthFactories, final List<KeyExchangeScheme> unSupportedKeyxFactories,
                       final boolean isNullCryptoContext, final boolean setConsoleFilterStreamFactory) throws MslCryptoException, MslEncodingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, MslKeyExchangeException {
        this.numThreads = numThreads;
        this.entityAuthScheme = entityAuthScheme;
        this.tokenFactoryType = tokenFactoryType;
        this.initialSequenceNum = initialSequenceNum;
        this.isMessageEncrypted = isMessageEncrypted;
        this.isIntegrityProtected = isIntegrityProtected;
        this.unSupportedEntityAuthFactories = unSupportedEntityAuthFactories;
        this.unSupportedUserAuthFactories = unSupportedUserAuthFactories;
        this.unSupportedKeyxFactories = unSupportedKeyxFactories;
        this.isNullCryptoContext = isNullCryptoContext;
        this.setConsoleFilterStreamFactory = setConsoleFilterStreamFactory;
        configure();
    }

    private void configure() throws MslCryptoException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, MslKeyExchangeException, MslEncodingException {
        mslCtrl = new MslControl(numThreads);
        if(setConsoleFilterStreamFactory) {
            mslCtrl.setFilterFactory(new ConsoleFilterStreamFactory());
        }
        /**
         * Msl Context Configuration
         */
        mslCtx = new ServerMslContext(entityAuthScheme, false, tokenFactoryType, initialSequenceNum, unSupportedEntityAuthFactories,
                unSupportedUserAuthFactories, unSupportedKeyxFactories, isNullCryptoContext);

        /**
         * Message Context Configuration
         */
        msgCtx = new ServerMessageContext(mslCtx, payload.getBytes(MslConstants.DEFAULT_CHARSET), isMessageEncrypted);
        msgCtx.setIntegrityProtected(isIntegrityProtected);
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type");

        super.service(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();

        @SuppressWarnings("unchecked")
        Map<String, String[]> params = request.getParameterMap();
        for (Entry<String,String[]> entry : params.entrySet()) {
            try {
                String key = entry.getKey();
                String[] value = entry.getValue();
                setPrivateVariable(out, key, value);
            } catch (Exception e) {
                e.printStackTrace();
                out.println(e.getMessage());
            }
        }
        try {
            configure();
        } catch (Exception e) {
            e.printStackTrace();
            out.println(e.getMessage());
        }
        out.println(request.getServletPath());
        out.close();
    }

    @Override
{'fraction_non_alphanumeric': 0.07453703703703704, 'fraction_numerical': 0.002932098765432099, 'mean_word_length': 3.8398058252427183, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '420141', 'n_tokens_mistral': 3277, 'n_tokens_neox': 3231, 'n_words': 781}