<commit_before>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2011, Red Hat Middleware LLC, and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * 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.jboss.aerogear.unifiedpush.test;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.ssl.SslContext;
import org.littleshoot.proxy.HttpFilters;
import org.littleshoot.proxy.HttpFiltersAdapter;
import org.littleshoot.proxy.HttpFiltersSourceAdapter;
import org.littleshoot.proxy.HttpProxyServer;
import org.littleshoot.proxy.impl.DefaultHttpProxyServer;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.net.ssl.SSLException;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Class which creates a proxy when deployed and shuts it down when undeployed.
 *
 * FIXME missing proper implementation, currently only works as pass-through proxy, but it should validate the sent
 * push messages.
 */
@Startup
@Singleton
public class ProxySetup {

    private static final Logger LOGGER = Logger.getLogger(ProxySetup.class.getName());

    /**
     * First found will be used. If none found, the {@link #DEFAULT_BIND_IP} will be used.
     */
    private static final String[] POSSIBLE_BIND_IP_PROPERTIES = { "OPENSHIFT_AEROGEAR_PUSH_IP",
            "OPENSHIFT_JBOSS_UNIFIED_PUSH_IP", "MYTESTIP_1", "OPENSHIFT_UNIFIED_PUSH_IP" };
    private static final String DEFAULT_BIND_IP = "127.0.0.1";
    // TODO add a possibility to change the port using an env property
    private static final int HTTP_PROXY_PORT = 16000;

    private HttpProxyServer server;


    private Thread performOnBackgroundThread(final Runnable runnable) {
        final Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    runnable.run();
                } finally {
                }
            }
        };
        t.start();
        return t;
    }

    public Thread doWork() {
        final Runnable runnable = new Runnable() {
            public void run() {
                final int PORT = 16001;
                // Configure SSL.
                SslContext sslCtx;
                try {
                    java.util.Map<String, String> env = System.getenv();

                    String cf = env.getOrDefault("GCM_MOCK_CRT","/tmp/gcm_mock.crt");
                    File certfile = new File(cf);
                    String kf = env.getOrDefault("GCM_MOCK_KEY","/tmp/gcm_mock.key");
                    File keyfile = new File(kf);

                    if(certfile.exists()==false){
                        throw new FileNotFoundException("File "+cf+" needs to exist.");
                    }

                    if(keyfile.exists()==false){
                        throw new FileNotFoundException("File "+kf+" needs to exist.");
                    }<commit_msg>Changing proxy-cert-settings from env to java property<commit_after>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2011, Red Hat Middleware LLC, and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * 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.jboss.aerogear.unifiedpush.test;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.ssl.SslContext;
import org.littleshoot.proxy.HttpFilters;
import org.littleshoot.proxy.HttpFiltersAdapter;
import org.littleshoot.proxy.HttpFiltersSourceAdapter;
import org.littleshoot.proxy.HttpProxyServer;
import org.littleshoot.proxy.impl.DefaultHttpProxyServer;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.net.ssl.SSLException;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Class which creates a proxy when deployed and shuts it down when undeployed.
 *
 * FIXME missing proper implementation, currently only works as pass-through proxy, but it should validate the sent
 * push messages.
 */
@Startup
@Singleton
public class ProxySetup {

    private static final Logger LOGGER = Logger.getLogger(ProxySetup.class.getName());

    /**
     * First found will be used. If none found, the {@link #DEFAULT_BIND_IP} will be used.
     */
    private static final String[] POSSIBLE_BIND_IP_PROPERTIES = { "OPENSHIFT_AEROGEAR_PUSH_IP",
            "OPENSHIFT_JBOSS_UNIFIED_PUSH_IP", "MYTESTIP_1", "OPENSHIFT_UNIFIED_PUSH_IP" };
    private static final String DEFAULT_BIND_IP = "127.0.0.1";
    // TODO add a possibility to change the port using an env property
    private static final int HTTP_PROXY_PORT = 16000;

    private HttpProxyServer server;


    private Thread performOnBackgroundThread(final Runnable runnable) {
        final Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    runnable.run();
                } finally {
                }
            }
        };
        t.start();
        return t;
    }

    public Thread doWork() {
        final Runnable runnable = new Runnable() {
            public void run() {
                final int PORT = 16001;
                // Configure SSL.
                SslContext sslCtx;
                try {
                    String cf = System.getProperty("gcmMockCrt");
                    File certfile = new File(cf);
                    String kf = System.getProperty("gcmMockKey");
                    File keyfile = new File(kf);

                    if(certfile.exists()==false){
                        throw new FileNotFoundException("File "+cf+" needs to exist.");
                    }

                    if(keyfile.exists()==false){
                        throw new FileNotFoundException("File "+kf+" needs to exist.");
                    }
{'fraction_non_alphanumeric': 0.0882388787324802, 'fraction_numerical': 0.006093845216331505, 'mean_word_length': 2.8345794392523365, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, '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': '4746479', 'n_tokens_mistral': 2334, 'n_tokens_neox': 2243, 'n_words': 672}