text:
"""
Tests for database migrations. This test case reads the configuration
file test_migrations.conf for database connection settings
to use in the tests. For each connection found in the config file,
the test case runs a series of test cases to ensure that migrations work
properly both upgrading and downgrading, and that no data loss occurs
if possible.
"""

import os

import fixtures
from migrate.versioning import api as migration_api
from migrate.versioning import repository
from oslo_db.sqlalchemy import test_migrations
from oslo_db.sqlalchemy import utils as db_utils
# from oslo_db.tests.sqlalchemy import base as test_base
import sqlalchemy
from sqlalchemy.engine import reflection

from freezer_api.db.sqlalchemy import driver
import freezer_api.db.sqlalchemy.migrate_repo

class MigrationsMixin(test_migrations.WalkVersionsMixin):
    """Test sqlalchemy-migrate migrations."""

    BOOL_TYPE = sqlalchemy.types.BOOLEAN
    TIME_TYPE = sqlalchemy.types.DATETIME
    INTEGER_TYPE = sqlalchemy.types.INTEGER
    VARCHAR_TYPE = sqlalchemy.types.VARCHAR
    TEXT_TYPE = sqlalchemy.types.Text

    @property
    def INIT_VERSION(self):
        return driver.INIT_VERSION

    @property
    def REPOSITORY(self):
        migrate_file = freezer_api.db.sqlalchemy.migrate_repo.__file__
        return repository.Repository(
            os.path.abspath(os.path.dirname(migrate_file)))

    @property
    def migration_api(self):
        return migration_api

    @property
    def migrate_engine(self):
        return self.engine

    def get_table_ref(self, engine, name, metadata):
        metadata.bind = engine
        return sqlalchemy.Table(name, metadata, autoload=True)

    class BannedDBSchemaOperations(fixtures.Fixture):
        """Ban some operations for migrations"""
        def __init__(self, banned_resources=None):
            super(MigrationsMixin.BannedDBSchemaOperations, self).__init__()
            self._banned_resources = banned_resources or []

        @staticmethod
        def _explode(resource, op):
            print('%s.%s()' % (resource, op))  # noqa
            raise Exception(
                'Operation %s.%s() is not allowed in a database migration' % (
                    resource, op))

        def setUp(self):
            super(MigrationsMixin.BannedDBSchemaOperations, self).setUp()
            for thing in self._banned_resources:
                self.useFixture(fixtures.MonkeyPatch(
                    'sqlalchemy.%s.drop' % thing,
                    lambda *a, **k: self._explode(thing, 'drop')))
                self.useFixture(fixtures.MonkeyPatch(
                    'sqlalchemy.%s.alter' % thing,
                    lambda *a, **k: self._explode(thing, 'alter')))

    def migrate_up(self, version, with_data=False):
        # NOTE(dulek): This is a list of migrations where we allow dropping
        # things. The rules for adding things here are very very specific.
        # Insight on how to drop things from the DB in a backward-compatible
        # manner is provided in Cinder's developer documentation.
        # Reviewers: DO NOT ALLOW THINGS TO BE ADDED HERE WITHOUT CARE
        exceptions = [3]

        if version not in exceptions:
            banned = ['Table', 'Column']
        else:
            banned = None
        with MigrationsMixin.BannedDBSchemaOperations(banned):
            super(MigrationsMixin, self).migrate_up(version, with_data)

    def assertColumnExists(self, engine, table, column):
        t = db_utils.get_table(engine, table)
        self.assertIn(column, t.c)

    def assertColumnsExist(self, engine, table, columns):
        for column in columns:
            self.assertColumnExists(engine, table, column)

    def assertColumnType(self, engine, table, column, column_type):
        t = db_utils.get_table(engine, table)
        column_ref_type = str(t.c[column].type)
        self.assertEqual(column_ref_type, column_type)

    def assertColumnCount(self, engine, table, columns):
        t = db_utils.get_table(engine, table)
        self.assertEqual(len(columns), len(t.columns))

    def assertColumnNotExists(self, engine, table, column):
        t = db_utils.get_table(engine, table)
        self.assertNotIn(column, t.c)

    def assertIndexExists(self, engine, table, index):
        t = db_utils.get_table(engine, table)
        index_names = [idx.name for idx in t.indexes]
        self.assertIn(index, index_names)

    def __check_cinderbase_fields(self, columns):
        """Check fields inherited from CinderBase ORM class."""
        self.assertIsInstance(columns.created_at.type, self.TIME_TYPE)
        self.assertIsInstance(columns.updated_at.type, self.TIME_TYPE)
        self.assertIsInstance(columns.deleted_at.type, self.TIME_TYPE)
        self.assertIsInstance(columns.deleted.type, self.BOOL_TYPE)

    def _check_001(self, engine, data):
        clients_columns = [
            'created_at',
            'updated_at',
            'deleted_at',
            'deleted',
            'user_id',
            'id',
            'project_id',
            'client_id',
            'hostname',
            'description',
            'uuid',
        ]
        self.assertColumnsExist(
            engine, 'clients', clients_columns)
        self.assertColumnCount(
            engine, 'clients', clients_columns)

        sessions_columns = [
            'created_at',
            'updated_at',
            'deleted_at',
            'deleted',
            'id',
            'session_tag',
            'description',
            'hold_off',
            'schedule',
            'job',
            'project_id',
            'user_id',
            'time_start',
            'time_end',
            'time_started',
            'time_ended',
            'status',
            'result',
        ]
        self.assertColumnsExist(
            engine, 'sessions', sessions_columns)
        self.assertColumnCount(
            engine, 'sessions', sessions_columns)

        jobs_columns = [
            'created_at',
            'updated_at',
            'deleted_at',
            'deleted',
            'id',
            'project_id',
            'user_id',
            'schedule',
            'client_id',
            'session_id',
            'session_tag',
            'description',
            'job_actions',
        ]
        self.assertColumnsExist(
            engine, 'jobs', jobs_columns)
        self.assertColumnCount(
            engine, 'jobs', jobs_columns)

        actions_columns = [
            'created_at',
            'updated_at',
            'deleted_at',
            'deleted',
            'id',
            'action',
            'project_id',
            'user_id',
            'actionmode',
            'src_file',
            'backup_name',
            'container',
            'timeout',
            'priority',
            'max_retries_interval',
            'max_retries',
            'mandatory',
            'log_file',
            'backup_metadata',
        ]
        self.assertColumnsExist(
            engine, 'actions', actions_columns)
        self.assertColumnCount(
            engine, 'actions', actions_columns)

        action_reports_columns = [
            'created_at',
            'updated_at',
            'deleted_at',
            'deleted',
            'id',
            'project_id',
            'user_id',
            'result',
            'time_elapsed',
            'report_date',
            'log',
        ]
        self.assertColumnsExist(
            engine, 'action_reports', action_reports_columns)
        self.assertColumnCount(
            engine, 'action_reports', action_reports_columns)

        backups_columns = [
            'created_at',
            'updated_at',
            'deleted_at',
            'deleted',
            'id',
            'job_id',
            'project_id',
            'user_id',
            'user_name',
            'backup_metadata',
        ]
        self.assertColumnsExist(
            engine, 'backups', backups_columns)
        self.assertColumnCount(
            engine, 'backups', backups_columns)

    def _check_002(self, engine, data):
        actions_columns = [
            'created_at',
            'updated_at',
            'deleted_at',
            'deleted',
            'id',
            'action',
            'project_id',
            'user_id',
            'actionmode',
            'src_file',
            'backup_name',
            'container',
            'timeout',
            'priority',
            'max_retries_interval',
            'max_retries',
            'mandatory',
            'log_file',
            'backup_metadata',
        ]
        self.assertColumnsExist(
            engine, 'actions', actions_columns)
        self.assertColumnCount(
            engine, 'actions', actions_columns)

    def _check_003(self, engine, data):
        actions_columns = [
            'created_at',
            'updated_at',
            'deleted_at',
            'deleted',
            'id',
            'action',
            'project_id',
            'user_id',
            'actionmode',
            'path_to_backup',
            'backup_name',
            'container',
            'timeout',
            'priority',
            'max_retries_interval',
            'max_retries',
            'mandatory',
            'log_file',
            'backup_metadata',
        ]
        self.assertColumnsExist(
            engine, 'actions', actions_columns)
        self.assertColumnCount(
            engine, 'actions', actions_columns)

    def get_table_names(self, engine):
        inspector = reflection.Inspector.from_engine(engine)
        return inspector.get_table_names()

    def get_foreign_key_columns(self, engine, table_name):
        foreign_keys = set()
        table = db_utils.get_table(engine, table_name)
        inspector = reflection.Inspector.from_engine(engine)
        for column_dict in inspector.get_columns(table_name):
            column_name = column_dict['name']
            column = getattr(table.c, column_name)
            if column.foreign_keys:
                foreign_keys.add(column_name)
        return foreign_keys

    def get_indexed_columns(self, engine, table_name):
        indexed_columns = set()
        for index in db_utils.get_indexes(engine, table_name):
            for column_name in index['column_names']:
                indexed_columns.add(column_name)
        return indexed_columns

    def assert_each_foreign_key_is_part_of_an_index(self):
        engine = self.migrate_engine

        non_indexed_foreign_keys = set()

        for table_name in self.get_table_names(engine):
            indexed_columns = self.get_indexed_columns(engine, table_name)
            foreign_key_columns = self.get_foreign_key_columns(
                engine, table_name
            )
            for column_name in foreign_key_columns - indexed_columns:
                non_indexed_foreign_keys.add(table_name + '.' + column_name)

        self.assertSetEqual(set(), non_indexed_foreign_keys)

    def test_walk_versions(self):
        self.walk_versions(False, False)
        self.assert_each_foreign_key_is_part_of_an_index()

# class TestSqliteMigrations(test_base.DbTestCase,
#                            MigrationsMixin):
#     def assert_each_foreign_key_is_part_of_an_index(self):
        # Skip the test for SQLite because SQLite does not list
        # UniqueConstraints as indexes, which makes this test fail.
        # Given that SQLite is only for testing purposes, it is safe to skip
    # pass


text:
using System;
using System.Linq;
using Deedle;
using Python.Runtime;
using QuantConnect.Packets;

namespace QuantConnect.Report.ReportElements
{
    internal sealed class RollingPortfolioBetaReportElement : ChartReportElement
    {
        private LiveResult _live;
        private BacktestResult _backtest;

        /// <summary>
        /// Create a new plot of the rolling portfolio beta to equities
        /// </summary>
        /// <param name="name">Name of the widget</param>
        /// <param name="key">Location of injection</param>
        /// <param name="backtest">Backtest result object</param>
        /// <param name="live">Live result object</param>
        public RollingPortfolioBetaReportElement(string name, string key, BacktestResult backtest, LiveResult live)
        {
            _live = live;
            _backtest = backtest;
            Name = name;
            Key = key;
        }

        /// <summary>
        /// Generate the rolling portfolio beta to equities plot using the python libraries.
        /// </summary>
        public override string Render()
        {
            var backtestPoints = ResultsUtil.EquityPoints(_backtest);
            var backtestBenchmarkPoints = ResultsUtil.BenchmarkPoints(_backtest);
            var livePoints = ResultsUtil.EquityPoints(_live);
            var liveBenchmarkPoints = ResultsUtil.BenchmarkPoints(_live);

            var backtestSeries = new Series<DateTime, double>(backtestPoints);
            var backtestBenchmarkSeries = new Series<DateTime, double>(backtestBenchmarkPoints);
            var liveSeries = new Series<DateTime, double>(livePoints);
            var liveBenchmarkSeries = new Series<DateTime, double>(liveBenchmarkPoints);

            var base64 = "";
            using (Py.GIL())
            {
                var backtestList = new PyList();
                var liveList = new PyList();

                var backtestRollingBetaSixMonths = Rolling.Beta(backtestSeries, backtestBenchmarkSeries, windowSize: 22 * 6);
                var backtestRollingBetaTwelveMonths = Rolling.Beta(backtestSeries, backtestBenchmarkSeries, windowSize: 252);

                backtestList.Append(backtestRollingBetaSixMonths.Keys.ToList().ToPython());
                backtestList.Append(backtestRollingBetaSixMonths.Values.ToList().ToPython());
                backtestList.Append(backtestRollingBetaTwelveMonths.Keys.ToList().ToPython());
                backtestList.Append(backtestRollingBetaTwelveMonths.Values.ToList().ToPython());

                var liveRollingBetaSixMonths = Rolling.Beta(liveSeries, liveBenchmarkSeries, windowSize: 22 * 6);
                var liveRollingBetaTwelveMonths = Rolling.Beta(liveSeries, liveBenchmarkSeries, windowSize: 252);

                liveList.Append(liveRollingBetaSixMonths.Keys.ToList().ToPython());
                liveList.Append(liveRollingBetaSixMonths.Values.ToList().ToPython());
                liveList.Append(liveRollingBetaTwelveMonths.Keys.ToList().ToPython());
                liveList.Append(liveRollingBetaTwelveMonths.Values.ToList().ToPython());

                base64 = Charting.GetRollingBeta(backtestList, liveList);
            }

            return base64;
        }
    }
}

redpajama_set_name: RedPajamaGithub


text:
namespace AGN
{
	class IRenderAPI
	{
	public:
		virtual ~IRenderAPI() {}

		virtual bool init() = 0;
		virtual void handleEvents(bool& a_doQuit) = 0;
		virtual void updateWindowState() = 0;
		virtual void logLiveObjects() {};

		// setters / getters
		virtual void enableVSync(bool a_value) = 0;
		virtual void enableNormalMapping(bool a_value) = 0;
		virtual void enableSpecularMapping(bool a_value) = 0;
		virtual class IWindow& getWindow() = 0;
		virtual class IDevice& getDevice() = 0;
		virtual class IRenderer& getRenderer() = 0;
		virtual class IImGui& getImGui() = 0;
		virtual bool getVSync() = 0;
	};
}


text:
//
// TechDemo/main.cpp
// Copyright (C) 2012 Lexi Robinson
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

#include <iostream>
#include <stdexcept>
// GL
#include "gl.h"
// GLFW
#include <GL/glfw.h>
// GLM libs
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// Personal libs
#include "PhysFS.h"
#include "ResourceManager.h"
#include "InputManager.h"
#include "ObjLoader.h"

// Various hardcoded options
//#define TRIANGLE_DEBUGGING

// loadin
void initphysfs(const char *argv0);
void loadshit();
void loadobj(const std::string& name);
// renderin
void opencontext();
void render();
void errchck(const char* str);
bool checkwindow();
// movin
void processmove(const InputData& in, const double deltatime);
// closedown
void shutdowneverything();

// woo globals
GLuint program;
GLuint viewLocation;
GLsizei numpoints;

OpenGL::ResourceManager *mgr;

bool viewUpdated;

glm::mat4      modelMatrix;
glm::mat4       viewMatrix;
glm::mat4 projectionMatrix;

std::vector<RenderData> chunks;

int main(int argc, const char * argv[])
{
    try {
        initphysfs(argv[0]);
        opencontext();
        loadshit();
        InputManager mgr;
        double ltime, ctime, dtime;
        while (checkwindow()) {
            // Time since last frame
            ctime = glfwGetTime();
            dtime = ctime - ltime;
            ltime = ctime;
            const auto& in = mgr.GetInput();
            if (in.Exit)
                break;
            processmove(in, dtime);
            render();
            usleep(1000);
        }
    } catch (std::exception& e) {
        // Place breakpoint here!
        std::cerr << "Uncaught exception!\n" << e.what() << std::endl;
    }
    shutdowneverything();
    return 0;
}

void opencontext() {
    // Initialise GLFW
    if(!glfwInit()) {
        throw std::runtime_error("Failed to initialise GLFW!");
    }
    // Make sure we de-GLFW.
    std::atexit(glfwTerminate);
    // Setup the context
    glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
    glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE);
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
    glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    // TODO: Some kind of proper size detectione
    const int windowWidth  = 1280; // 1 standard resolution step smaller than my mbp's screen.
    const int windowHeight = 800;
    // Open a window
    if(!glfwOpenWindow(windowWidth, windowHeight, 0, 0, 0, 0, 32, 0, GLFW_WINDOW)) {
        throw std::runtime_error("Failed to open GLFW window");
    }
    // Name it
    glfwSetWindowTitle("Gaudy Tech Demo");
    glfwDisable(GLFW_MOUSE_CURSOR);
#ifndef __APPLE__
#error TODO: GLEW!
#endif

    /*
     * OpenGL
     */

    // Heh heh heh cornflowers.
    glClearColor(100.0f / 255.0f, 149.0f / 255.0f, 237.0f / 255.0f, 0.0f);

    // Depth
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    //glEnable(GL_CULL_FACE);

    // Alpha
    /*
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    */
#ifdef TRIANGLE_DEBUGGING
    // Debuging
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
#endif

    // Points
    glPointSize(20);

    // resource mgmnt
    mgr = new OpenGL::ResourceManager();

    // Projectione
    projectionMatrix = glm::perspective(75.0f,  // FoV
        static_cast<GLfloat> (windowWidth) /    // Aspect ratio
        static_cast<GLfloat> (windowHeight),
        0.1f, 100.0f                            // nearZ, farZ
    );
    //modelMatrix = glm::scale(glm::mat4(1), glm::vec3(0.01));
    //viewMatrix  = glm::lookAt(glm::vec3(10, 0, 0), glm::vec3(10,0,1), glm::vec3(0,1,0));
    viewUpdated = true;
}

void loadshit() {
    std::string basedir, objfile;
    PhysFS::FileStream in("load.txt", PhysFS::OM_READ);
    std::getline(in, basedir);
    std::getline(in, objfile);
    float scale;
    in >> scale;
    in.close();

    if (objfile == "")
        objfile = basedir + ".obj";
    PhysFS::mount("objs/" + basedir, "data");
    modelMatrix = glm::scale(glm::mat4(1), glm::vec3(scale));
    chunks = LoadObj("data/" + objfile, *mgr);

    program = mgr->LoadShaders("BasicVertex", "BasicFragment");
    glUseProgram(program);

    viewLocation = glGetUniformLocation(program, "View");
    glUniformMatrix4fv(viewLocation, 1, GL_FALSE, glm::value_ptr(viewMatrix));

    glUniformMatrix4fv(glGetUniformLocation(program, "Projection"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
    glUniformMatrix4fv(glGetUniformLocation(program, "Model"     ), 1, GL_FALSE, glm::value_ptr(     modelMatrix));

    glUseProgram(0);
}

bool checkwindow() {
	return glfwGetWindowParam(GLFW_OPENED) == GL_TRUE;
}

void render() {
    //static GLuint colourpos = glGetUniformLocation(program, "colour");
    if (!viewUpdated)
        return;
    viewUpdated = false;
    glUseProgram(program);
    glUniformMatrix4fv(viewLocation, 1, GL_FALSE, glm::value_ptr(viewMatrix));
    for (RenderData obj : chunks) {
        glBindVertexArray(obj.VAO);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // debug
        glDrawArrays(GL_POINTS, 0, numpoints);

        // hum
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, obj.EAB);

        // drawink
        glDrawElements(GL_TRIANGLES, obj.IndexCount, obj.IndexType, 0);
    }
    //glDrawElements(GL_POINTS, numpoints, GL_UNSIGNED_INT, 0);
    glfwSwapBuffers();
}
void shutdowneverything() {
    delete mgr;
}

void processmove(const InputData& input, const double dtime) {
    static const float fMouseSpeed = 0.005f;
    static const float fKeySpeed   = 6.0f;
	//static const float clampUp = 3.14f * 2.2f;
	//static const float clampDn = 3.14f * 1.8f;

    static       float viewAngleH, viewAngleV;
    static       glm::vec3 viewPos;

	const float ftime = static_cast<float>(dtime);

    if (input.DeltaViewX == 0 && input.DeltaViewY == 0 && !(
        input.Forwards || input.Backwards || input.Left || input.Right))
        return;

	// Viewage
	viewAngleH += fMouseSpeed * input.DeltaViewX;
	viewAngleV -= fMouseSpeed * input.DeltaViewY;
	//viewAngleV = (viewAngleV > clampUp) ? clampUp : viewAngleV;
	//viewAngleV = (viewAngleV < clampDn) ? clampDn : viewAngleV;

    glm::vec3 dir(
                 sin(viewAngleH) * cos(viewAngleV),
                 sin(viewAngleV),
                 cos(viewAngleH) * cos(viewAngleV)
                 );
	float fhvang = viewAngleH - 3.14f / 2.0f;
	glm::vec3 right (
                     sin(fhvang),
                     0,
                     cos(fhvang)
                     );
    glm::vec3 up = glm::cross(dir, right);
	glm::vec3 vel(0,0,0);
	float amt = ftime * fKeySpeed;
	if (input.Forwards)
		vel += dir * amt;
	if (input.Backwards)
		vel -= dir * amt;
	if (input.Left)
		vel += right * amt;
	if (input.Right)
		vel -= right * amt;
	viewPos += vel;
	viewMatrix = glm::lookAt(viewPos, viewPos + dir, up);
    viewUpdated = true;
}

/*
 * PhysFS
 */

void killphysfs() {
	try {
		PhysFS::deinit();
	} catch(PhysFS::Exception) {}
}
void initphysfs(const char *argv0) {
    try {
		PhysFS::init(argv0);
        // ewww hacky!!
		PhysFS::mount("/Users/lexi/techdemo", "", false);
        std::atexit(killphysfs);
	} catch (PhysFS::Exception& e) {
        std::cerr << "Could not start PhysFS! " << e.what() << std::endl;
        throw;
	}
}

void errchck(const char* str) {
	GLenum err;
	while ((err = glGetError()) != GL_NO_ERROR) {
		std::cerr << "fuckup in " << str << std::endl;
		if (err == GL_INVALID_ENUM)
			std::cerr << "INVALID ENUM!" << std::endl;
		else if (err == GL_INVALID_VALUE)
			std::cerr << "Invalid value!" << std::endl;
		else if (err == GL_INVALID_OPERATION)
			std::cerr << "Invalid opreation!" << std::endl;
		else if (err == GL_INVALID_FRAMEBUFFER_OPERATION)
			std::cerr << "whoops, there goes the framebuffer" << std::endl;
		else if (err == GL_OUT_OF_MEMORY)
			std::cerr << "ABORT ABORT" << std::endl;
	}
}


text:
namespace tensorflow {
namespace protobuf = ::google::protobuf;
}  // namespace tensorflow

#endif  // THIRD_PARTY_TENSORFLOW_CORE_PLATFORM_DEFAULT_PROTOBUF_H_


text:
using System.Collections.Generic;

namespace Abp.Webhooks
{
    public class WebhookHeader
    {
        /// <summary>
        /// If true, webhook will only contain given headers. If false given headers will be added to predefined headers in subscription.
        /// Default is false
        /// </summary>
        public bool UseOnlyGivenHeaders { get; set; }

        /// <summary>
        /// That headers will be sent with the webhook.
        /// </summary>
        public IDictionary<string, string> Headers { get; set; }
    }
}


text:
require File.dirname(__FILE__) + '/../test_helper'
require_dependency 'user_controller'

# Raise errors beyond the default web-based presentation
class UserController; def rescue_action(e) raise e end; end

class UserControllerTest < Test::Unit::TestCase

  fixtures :users, :accounts, :payments, :squares

  def setup
    @controller = UserController.new
    @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
    @request.host = "localhost"
    users(:bob).update_expiry(0)
  end

  def test_auth_bob
    @request.session['return-to'] = "/bogus/location"

    post :login, "user" => { "login" => "bob", "password" => "atest" }
    assert_session_has :user
    assert_equal users(:bob), @response.session[:user]
    assert_redirect_url "http://#{@request.host}/bogus/location"
  end

  def do_test_signup(bad_password, bad_email)
    ActionMailer::Base.deliveries = []

    @request.session['return-to'] = "/bogus/location"

    if not bad_password and not bad_email
      post :sneaky, "user" => { "firstname" => "joseph", "lastname" => "mcTest", "name" => "jojo", "login" => "newbob", "password" => "newpassword", "password_confirmation" => "newpassword", "email" => "newbob@test.com" }
      assert_nil session[:user]
      assert_response :redirect
      assert_equal 'http://localhost/login', redirect_to_url
      assert_equal 1, ActionMailer::Base.deliveries.size
      mail = ActionMailer::Base.deliveries[0]
      assert_equal "newbob@test.com", mail.to_addrs[0].to_s
      assert_match /login:\s+\w+\n/, mail.encoded
      assert_match /password:\s+\w+\n/, mail.encoded
      mail.encoded =~ /key=(.*?)"/
      key = $1

      user = User.find_by_email("newbob@test.com")
      assert_not_nil user
      assert_equal false, user.verified?

      # First past the expiration.
      Time.advance_by_days = 1
      get :welcome, "user"=> { "id" => "#{user.id}" }, "key" => "#{key}"
      Time.advance_by_days = 0
      user = User.find_by_email("newbob@test.com")
      assert_equal false, user.verified?

      # Then a bogus key.
      get :welcome, "user"=> { "id" => "#{user.id}" }, "key" => "boguskey"
      user = User.find_by_email("newbob@test.com")
      assert_equal false, user.verified?

      # Now the real one.
      get :welcome, "user"=> { "id" => "#{user.id}" }, "key" => "#{key}"
      user = User.find_by_email("newbob@test.com")
      assert_equal true, user.verified?

      post :login, "user" => { "login" => "newbob", "password" => "newpassword" }
      assert_session_has :user
      get :logout
    elsif bad_password
      post :sneaky, "user" => {  "firstname" => "joseph", "lastname" => "mcTest", "name" => "jojo", "login" => "newbob", "password" => "bad", "password_confirmation" => "bad", "email" => "newbob@test.com" }
      assert_session_has_no :user
      assert_invalid_column_on_record "user", "password"
      assert_success
      assert_equal 0, ActionMailer::Base.deliveries.size
    elsif bad_email
      ActionMailer::Base.inject_one_error = true
      post :signup, "sneaky" => "yes", "user" => {  "firstname" => "joseph", "lastname" => "mcTest", "name" => "jojo", "login" => "newbob", "password" => "newpassword", "password_confirmation" => "newpassword", "email" => "newbob@test.com" }
      assert_session_has_no "user"
      assert_equal 0, ActionMailer::Base.deliveries.size
    else
      # Invalid test case
      assert false
    end
  end

  def test_edit
    post :logout
    post :login, "user" => { "login" => "bob", "password" => "atest" }
    assert_session_has :user

    post :edit, "user" => { "firstname" => "Bob", "form" => "edit" }
    assert_equal @response.session[:user].firstname, "Bob"

    post :edit, "user" => { "firstname" => "", "form" => "edit" }
    assert_equal @response.session[:user].firstname, ""

    get :logout
  end

  def test_delete
    ActionMailer::Base.deliveries = []

    # Immediate delete
    post :login, "user" => { "login" => "deletebob1", "password" => "alongtest" }
    assert_session_has :user

    UserSystem::CONFIG[:delayed_delete] = false
    post :edit, "user" => { "form" => "delete" }
    assert_equal 1, ActionMailer::Base.deliveries.size

    assert_session_has_no :user
    post :login, "user" => { "login" => "deletebob1", "password" => "alongtest" }
    assert_session_has_no :user

    # Now try delayed delete
    ActionMailer::Base.deliveries = []

    post :login, "user" => { "login" => "deletebob2", "password" => "alongtest" }
    assert_session_has :user

    UserSystem::CONFIG[:delayed_delete] = true
    post :edit, "user" => { "form" => "delete" }
    assert_equal 1, ActionMailer::Base.deliveries.size
    mail = ActionMailer::Base.deliveries[0]
    mail.encoded =~ /user\[id\]=(.*?)&key=(.*?)"/
    id = $1
    key = $2
    post :restore_deleted, "user" => { "id" => "#{id}" }, "key" => "badkey"
    assert_session_has_no :user

    # Advance the time past the delete date
    Time.advance_by_days = UserSystem::CONFIG[:delayed_delete_days]
    post :restore_deleted, "user" => { "id" => "#{id}" }, "key" => "#{key}"
    assert_session_has_no :user
    Time.advance_by_days = 0

    post :restore_deleted, "user" => { "id" => "#{id}" }, "key" => "#{key}"
    assert_session_has :user
    get :logout
  end

  def test_signup
    do_test_signup(true, false)
    do_test_signup(false, true)
    do_test_signup(false, false)
  end

  def do_change_password(bad_password, bad_email)
    ActionMailer::Base.deliveries = []

    post :login, "user" => { "login" => "josh", "password" => "atest" }
    assert_session_has :user, "We should be able to initially log-in for the purposes of this test"

    if not bad_password and not bad_email
      post :change_password, "user" => { "password" => "changed_password", "password_confirmation" => "changed_password" }
      assert_equal 1, ActionMailer::Base.deliveries.size
      mail = ActionMailer::Base.deliveries[0]
      assert_equal "josh@topsecret.gov", mail.to_addrs[0].to_s
      assert_match /login:\s+\w+\n/, mail.encoded
      assert_match /password:\s+\w+\n/, mail.encoded
    elsif bad_password
      post :change_password, "user" => { "password" => "bad", "password_confirmation" => "bad" }
      assert_invalid_column_on_record "user", "password"
      assert_success
      assert_equal 0, ActionMailer::Base.deliveries.size
    elsif bad_email
      ActionMailer::Base.inject_one_error = true
      post :change_password, "user" => { "password" => "changed_password", "password_confirmation" => "changed_password" }
      assert_equal 0, ActionMailer::Base.deliveries.size
    else
      # Invalid test case
      assert false
    end

    get :logout
    assert_session_has_no :user

# removed while we talk about the user controller backend
#    if not bad_password and not bad_email
#      post :login, "user" => { "login" => "josh", "password" => "changed_password" }
#      assert_session_has :user, "Should be able to log-in the user"
#      post :change_password, "user" => { "password" => "atest", "password_confirmation" => "atest" }
#      get :logout
#    end

#    post :login, "user" => { "login" => "josh", "password" => "atest" }
#    assert_session_has :user

    get :logout
  end

  def test_change_password
    do_change_password(false, false)
    do_change_password(true, false)
    do_change_password(false, true)
  end

  def do_forgot_password(bad_address, bad_email, logged_in)
    ActionMailer::Base.deliveries = []

    if logged_in
      post :login, "user" => { "login" => "bob", "password" => "atest" }
      assert_session_has :user, "Should be able to log in the user initially"
    end

    @request.session['return-to'] = "/bogus/location"
    if not bad_address and not bad_email
      post :forgot_password, "user" => { "email" => "bob@test.com" }
      password = "anewpassword"
      if logged_in
        assert_equal 0, ActionMailer::Base.deliveries.size
        assert_redirect_url(@controller.url_for(:action => "change_password"))
        post :change_password, "user" => { "password" => "#{password}", "password_confirmation" => "#{password}" }
      else
        assert_equal 1, ActionMailer::Base.deliveries.size
        mail = ActionMailer::Base.deliveries[0]
        assert_equal "bob@test.com", mail.to_addrs[0].to_s
        mail.encoded =~ /user\[id\]=(.*?)&key=(.*?)"/
        id = $1
        key = $2
        post :change_password, "user" => { "password" => "#{password}", "password_confirmation" => "#{password}", "id" => "#{id}" }, "key" => "#{key}"
        assert_equal "poo", @response.flash['notice'], 'this is a fake test.'
        assert_session_has :user
        get :logout
      end
    elsif bad_address
      post :forgot_password, "user" => { "email" => "bademail@test.com" }
      assert_equal 0, ActionMailer::Base.deliveries.size
    elsif bad_email
      ActionMailer::Base.inject_one_error = true
      post :forgot_password, "user" => { "email" => "bob@test.com" }
      assert_equal 0, ActionMailer::Base.deliveries.size
    else
      # Invalid test case
      assert false
    end

    if not bad_address and not bad_email
      if logged_in
        get :logout
      else
        assert_redirect_url(@controller.url_for(:action => "login"))
      end
      post :login, "user" => { "login" => "bob", "password" => "#{password}" }
    else
      # Okay, make sure the database did not get changed
      if logged_in
        get :logout
      end
      post :login, "user" => { "login" => "bob", "password" => "atest" }
    end

    assert_session_has :user

    # Put the old settings back
    if not bad_address and not bad_email
      post :change_password, "user" => { "password" => "atest", "password_confirmation" => "atest" }
    end

    get :logout
  end

  def test_forgot_password
    do_forgot_password(false, false, false)
    do_forgot_password(false, false, true)
    do_forgot_password(true, false, false)
    do_forgot_password(false, true, false)
  end

  def test_bad_signup
    @request.session['return-to'] = "/bogus/location"

    post :signup, "user" => { "login" => "newbob", "password" => "newpassword", "password_confirmation" => "wrong" }
    assert_invalid_column_on_record "user", "password"
    assert_success

    post :signup, "user" => { "login" => "yo", "password" => "newpassword", "password_confirmation" => "newpassword" }
    assert_invalid_column_on_record "user", "login"
    assert_success

    post :signup, "user" => { "login" => "yo", "password" => "newpassword", "password_confirmation" => "wrong" }
    assert_invalid_column_on_record "user", ["login", "password"]
    assert_success
  end

  def test_invalid_login
    post :login, "user" => { "login" => "bob", "password" => "not_correct" }
    assert_session_has_no :user
    assert assigns(:login)
  end

  def test_login_logoff
    post :login, "user" => { "login" => "bob", "password" => "atest" }
    assert_session_has :user
    get :logout
    assert_session_has_no :user
  end

end


text:
namespace annalisa
{
    template<class Node, class Input>
    class kohonen
    {
    public:
        // returns how much a node influences a neighbouring node
        using radius_function = std::function<float(unsigned int, // current iteration
                                                    const Node&, // winner node
                                                    const Node&)>; // other node

        // returns the distance between an input and a node
        using distance_function = std::function<float(unsigned int, // current iteration
                                                      const Node&,
                                                      const Input&)>;

        // moves a node towards an input, returns a scalar of how much it moved
        // the output scalar is compared with min_diff in the update
        using attract_function = std::function<float(unsigned int, // current iteration
                                                     float, // influence from radius
                                                            // and learning_rate
                                                     Node&, // node to be moved
                                                     const Input&)>; // input to move towards

        kohonen(std::vector<Node> nodes,
                radius_function radius,
                distance_function distance,
                attract_function attract,
                float learning_rate,
                float learning_decay)
            : nodes(nodes),
              radius(radius),
              distance(distance),
              attract(attract),
              learning_rate(learning_rate),
              learning_decay(learning_decay)
        { };

        void update(const std::vector<Input>& inputs,
                    unsigned int num_iterations, float min_diff)
        {
            std::uniform_int_distribution<size_t> random(0, inputs.size() - 1);
            for (unsigned int i = 0; i < num_iterations; i++)
            {
                auto diff_sum = 0.0f;

                auto& chosen_input = inputs[random(gen)];

                auto winner = nodes[0];
                {
                    auto winner_distance = distance(i, winner, chosen_input);

                    for (size_t j = 1; j < nodes.size(); i++)
                    {
                        auto dist = distance(i, nodes[j], chosen_input);
                        if (dist < winner_distance)
                        {
                            winner = nodes[j];
                            winner_distance = dist;
                        }
                    }
                }

                for (size_t k = 0; k < nodes.size(); k++)
                {
                    auto influence = radius(i, winner, nodes[k]);
                    if (influence == 0)
                        continue;

                    diff_sum += attract(i, learning_rate * influence, winner, chosen_input);
                }

                if (diff_sum < min_diff)
                {
                    return;
                }

                learning_rate *= learning_decay;
            }

        }

        const std::vector<Node>& get_nodes() const
        {
            return nodes;
        }

        size_t size() const
        {
            return nodes.size();
        }

    private:
        std::vector<Node> nodes;
        radius_function radius;
        distance_function distance;
        attract_function attract;
        float learning_rate;
        float learning_decay;
        std::mt19937 gen;
    };
}


text:
package com.bj58.spat.gaea.serializer.component.exception;

/**
 * StreamException
 *
 * @author Service Platform Architecture Team (spat@58.com)
 */
public class StreamException extends Exception {

	private static final long serialVersionUID = 8613200648317056670L;
	private String msg;

    public StreamException(String message) {
        msg = message;
    }

    public StreamException() {
        msg = "Stream error.";
    }

    @Override
    public String getMessage() {
        return msg;
    }
}


text:
%% Illustrate the robustness of the student and laplace distributions compared to the Gaussian.
%
%%

% This file is from pmtk3.googlecode.com

function robustDemo()

n = 30;
setSeed(8);
data = randn(n,1);
outliers = [8 ; 8.75 ; 9.5];
nn = length(outliers);
nbins = 7;

figure;
plotHist(data,nbins,n);
plotPDFs(data);
printPmtkFigure('robustDemoNoOutliers')
%%
figure;
plotHist(data,nbins,n+nn);
plotHist(outliers,nn,n+nn);
plotPDFs([data ; outliers]);
printPmtkFigure('robustDemoOutliers')

%% Bucket the data into nbins, divide the size of each bin by norm and plot
%% the normalized histogram.
function plotHist(data,nbins,norm)
    hold on;
    [counts, locations] = hist(data,nbins);
    sCounts = counts ./ norm;
    bar(locations,sCounts);
end
%%
function plotPDFs(data)
    Xbar = mean(data);
    sigma = std(data);
    gauss = @(X) gaussProb(X, Xbar, sigma.^2);
    model = studentFit(data);
    sT = @(X)exp(studentLogprob(model, X));
    model = laplaceFit(data);
    lap = @(X)exp(laplaceLogprob(model, X));
    hold on;
    x = (-5:0.01:10)';
    h(1) = plot(x,gauss(x),'k:','LineWidth',3);
    h(2) = plot(x,sT(x),'r-','LineWidth',3);
    h(3) = plot(x,lap(x),'b--','LineWidth',3);
    axis([-5,10,0,0.5]);
    set(gca,'YTick',0:0.1:0.5);
    legendStr = {'gaussian', 'student T', 'laplace'};
    if isOctave(),
        legend(legendStr{:})
    else
        legend(h, legendStr{:})
    end
end

end


text:
package com.planet_ink.coffee_mud.Races;
import com.planet_ink.coffee_mud.core.interfaces.*;
import com.planet_ink.coffee_mud.core.*;
import com.planet_ink.coffee_mud.core.collections.*;
import com.planet_ink.coffee_mud.Abilities.interfaces.*;
import com.planet_ink.coffee_mud.Areas.interfaces.*;
import com.planet_ink.coffee_mud.Behaviors.interfaces.*;
import com.planet_ink.coffee_mud.CharClasses.interfaces.*;
import com.planet_ink.coffee_mud.Commands.interfaces.*;
import com.planet_ink.coffee_mud.Common.interfaces.*;
import com.planet_ink.coffee_mud.Exits.interfaces.*;
import com.planet_ink.coffee_mud.Items.interfaces.*;
import com.planet_ink.coffee_mud.Libraries.interfaces.*;
import com.planet_ink.coffee_mud.Locales.interfaces.*;
import com.planet_ink.coffee_mud.MOBS.interfaces.*;
import com.planet_ink.coffee_mud.Races.interfaces.*;

import java.util.*;

public class GiantFish extends Fish
{
	@Override
	public String ID()
	{
		return "GiantFish";
	}

	private final static String localizedStaticName = CMLib.lang().L("Giant Fish");

	@Override
	public String name()
	{
		return localizedStaticName;
	}

	@Override
	public int shortestMale()
	{
		return 50;
	}

	@Override
	public int shortestFemale()
	{
		return 55;
	}

	@Override
	public int heightVariance()
	{
		return 20;
	}

	@Override
	public int lightestWeight()
	{
		return 1955;
	}

	@Override
	public int weightVariance()
	{
		return 405;
	}

	@Override
	public long forbiddenWornBits()
	{
		return ~(Wearable.WORN_EYES);
	}

	private final static String localizedStaticRacialCat = CMLib.lang().L("Fish");

	@Override
	public String racialCategory()
	{
		return localizedStaticRacialCat;
	}

	private static Vector<RawMaterial>	resources	= new Vector<RawMaterial>();

	//  							  an ey ea he ne ar ha to le fo no gi mo wa ta wi
	private static final int[] parts={0 ,2 ,0 ,1 ,0 ,0 ,0 ,1 ,0 ,0 ,0 ,2 ,1 ,0 ,1 ,0 };

	@Override
	public int[] bodyMask()
	{
		return parts;
	}

	private final int[]	agingChart	= { 0, 2, 4, 6, 8, 10, 12, 14, 16 };

	@Override
	public int[] getAgingChart()
	{
		return agingChart;
	}

	@Override
	public void affectCharStats(final MOB affectedMOB, final CharStats affectableStats)
	{
		//super.affectCharStats(affectedMOB, affectableStats);
		affectableStats.setRacialStat(CharStats.STAT_INTELLIGENCE,1);
		affectableStats.setRacialStat(CharStats.STAT_STRENGTH,10);
		affectableStats.setRacialStat(CharStats.STAT_DEXTERITY,13);
	}

	@Override
	public void unaffectCharStats(final MOB affectedMOB, final CharStats affectableStats)
	{
		super.unaffectCharStats(affectedMOB, affectableStats);
		affectableStats.setStat(CharStats.STAT_INTELLIGENCE,affectedMOB.baseCharStats().getStat(CharStats.STAT_INTELLIGENCE));
		affectableStats.setStat(CharStats.STAT_MAX_INTELLIGENCE_ADJ,affectedMOB.baseCharStats().getStat(CharStats.STAT_MAX_INTELLIGENCE_ADJ));
		affectableStats.setStat(CharStats.STAT_STRENGTH,affectedMOB.baseCharStats().getStat(CharStats.STAT_STRENGTH));
		affectableStats.setStat(CharStats.STAT_MAX_STRENGTH_ADJ,affectedMOB.baseCharStats().getStat(CharStats.STAT_MAX_STRENGTH_ADJ));
		affectableStats.setStat(CharStats.STAT_DEXTERITY,affectedMOB.baseCharStats().getStat(CharStats.STAT_DEXTERITY));
		affectableStats.setStat(CharStats.STAT_MAX_DEXTERITY_ADJ,affectedMOB.baseCharStats().getStat(CharStats.STAT_MAX_DEXTERITY_ADJ));
	}

	@Override
	public String arriveStr()
	{
		return "swims in";
	}

	@Override
	public String leaveStr()
	{
		return "swims";
	}

	@Override
	public Weapon myNaturalWeapon()
	{
		if(naturalWeapon==null)
		{
			naturalWeapon=CMClass.getWeapon("StdWeapon");
			naturalWeapon.setName(L("some sharp teeth"));
			naturalWeapon.setMaterial(RawMaterial.RESOURCE_BONE);
			naturalWeapon.setUsesRemaining(1000);
			naturalWeapon.setWeaponDamageType(Weapon.TYPE_PIERCING);
		}
		return naturalWeapon;
	}

	@Override
	public List<RawMaterial> myResources()
	{
		synchronized(resources)
		{
			if(resources.size()==0)
			{
				for(int i=0;i<25;i++)
				{
					resources.addElement(makeResource
					(L("some @x1 meat",name().toLowerCase()),RawMaterial.RESOURCE_FISH));
				}
				for(int i=0;i<15;i++)
				{
					resources.addElement(makeResource
					(L("a @x1 scaly hide",name().toLowerCase()),RawMaterial.RESOURCE_SCALES,L("@x1 scale",name().toLowerCase())));
				}
				resources.addElement(makeResource
				(L("some @x1 blood",name().toLowerCase()),RawMaterial.RESOURCE_BLOOD));
			}
		}
		return resources;
	}
}


text:
package org.apache.directory.server.kerberos.kdc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.security.PrivilegedAction;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.ModificationItem;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.Configuration;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

import org.apache.directory.api.ldap.model.constants.SchemaConstants;
import org.apache.directory.api.ldap.model.constants.SupportedSaslMechanisms;
import org.apache.directory.ldap.client.api.Krb5LoginConfiguration;
import org.apache.directory.server.annotations.CreateKdcServer;
import org.apache.directory.server.annotations.CreateLdapServer;
import org.apache.directory.server.annotations.CreateTransport;
import org.apache.directory.server.annotations.SaslMechanism;
import org.apache.directory.server.constants.ServerDNConstants;
import org.apache.directory.server.core.annotations.ContextEntry;
import org.apache.directory.server.core.annotations.CreateDS;
import org.apache.directory.server.core.annotations.CreateIndex;
import org.apache.directory.server.core.annotations.CreatePartition;
import org.apache.directory.server.core.api.CoreSession;
import org.apache.directory.server.core.api.DirectoryService;
import org.apache.directory.server.core.integ.AbstractLdapTestUnit;
import org.apache.directory.server.core.integ.FrameworkRunner;
import org.apache.directory.server.core.jndi.CoreContextFactory;
import org.apache.directory.server.core.kerberos.KeyDerivationInterceptor;
import org.apache.directory.server.i18n.I18n;
import org.apache.directory.server.ldap.handlers.sasl.cramMD5.CramMd5MechanismHandler;
import org.apache.directory.server.ldap.handlers.sasl.digestMD5.DigestMd5MechanismHandler;
import org.apache.directory.server.ldap.handlers.sasl.gssapi.GssapiMechanismHandler;
import org.apache.directory.server.ldap.handlers.sasl.ntlm.NtlmMechanismHandler;
import org.apache.directory.server.ldap.handlers.sasl.plain.PlainMechanismHandler;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * An {@link AbstractServerTest} testing SASL GSSAPI authentication
 * and security layer negotiation.  These tests require both the LDAP
 * and the Kerberos protocol.  As with any "three-headed" Kerberos
 * scenario, there are 3 principals:  1 for the test user, 1 for the
 * Kerberos ticket-granting service (TGS), and 1 for the LDAP service.
 *
 * @author Apache Directory Project
 */
@RunWith(FrameworkRunner.class)
@CreateDS(name = "SaslGssapiBindITest-class",
    partitions =
        {
            @CreatePartition(
                name = "example",
                suffix = "dc=example,dc=com",
                contextEntry = @ContextEntry(
                    entryLdif =
                    "dn: dc=example,dc=com\n" +
                        "dc: example\n" +
                        "objectClass: top\n" +
                        "objectClass: domain\n\n"),
                indexes =
                    {
                        @CreateIndex(attribute = "objectClass"),
                        @CreateIndex(attribute = "dc"),
                        @CreateIndex(attribute = "ou")
                })
    },
    additionalInterceptors =
        {
            KeyDerivationInterceptor.class
    })
@CreateLdapServer(
    transports =
        {
            @CreateTransport(protocol = "LDAP")
    },
    saslHost = "localhost",
    saslPrincipal = "ldap/localhost@EXAMPLE.COM",
    saslMechanisms =
        {
            @SaslMechanism(name = SupportedSaslMechanisms.PLAIN, implClass = PlainMechanismHandler.class),
            @SaslMechanism(name = SupportedSaslMechanisms.CRAM_MD5, implClass = CramMd5MechanismHandler.class),
            @SaslMechanism(name = SupportedSaslMechanisms.DIGEST_MD5, implClass = DigestMd5MechanismHandler.class),
            @SaslMechanism(name = SupportedSaslMechanisms.GSSAPI, implClass = GssapiMechanismHandler.class),
            @SaslMechanism(name = SupportedSaslMechanisms.NTLM, implClass = NtlmMechanismHandler.class),
            @SaslMechanism(name = SupportedSaslMechanisms.GSS_SPNEGO, implClass = NtlmMechanismHandler.class)
    })
@CreateKdcServer(
    transports =
        {
            @CreateTransport(protocol = "UDP", port = 6088),
            @CreateTransport(protocol = "TCP", port = 6088)
    })
public class SaslGssapiBindITest extends AbstractLdapTestUnit
{
    private DirContext ctx;

    /** the context root for the schema */
    protected LdapContext schemaRoot;

    /** the context root for the system partition */
    protected LdapContext sysRoot;

    /** the context root for the rootDSE */
    protected CoreSession rootDse;

    /**
     * Creates a new instance of SaslGssapiBindTest and sets JAAS system properties.
     */
    public SaslGssapiBindITest()
    {
        String krbConfPath = getClass().getClassLoader().getResource( "krb5.conf" ).getFile();
        System.setProperty( "java.security.krb5.conf", krbConfPath );
        System.setProperty( "sun.security.krb5.debug", "false" );
    }

    /**
     * Set up a partition for EXAMPLE.COM and add user and service principals to
     * test authentication with.
     */
    @Before
    public void setUp() throws Exception
    {
        String servicePrincipalName = KerberosTestUtils.fixServicePrincipalName( "ldap/localhost@EXAMPLE.COM", null, getLdapServer() );

        Attributes attrs;

        setContexts( "uid=admin,ou=system", "secret" );

        // -------------------------------------------------------------------
        // Enable the krb5kdc schema
        // -------------------------------------------------------------------

        // check if krb5kdc is disabled
        Attributes krb5kdcAttrs = schemaRoot.getAttributes( "cn=Krb5kdc" );
        boolean isKrb5KdcDisabled = false;

        if ( krb5kdcAttrs.get( "m-disabled" ) != null )
        {
            isKrb5KdcDisabled = ( ( String ) krb5kdcAttrs.get( "m-disabled" ).get() ).equalsIgnoreCase( "TRUE" );
        }

        // if krb5kdc is disabled then enable it
        if ( isKrb5KdcDisabled )
        {
            Attribute disabled = new BasicAttribute( "m-disabled" );
            ModificationItem[] mods = new ModificationItem[]
                { new ModificationItem( DirContext.REMOVE_ATTRIBUTE, disabled ) };
            schemaRoot.modifyAttributes( "cn=Krb5kdc", mods );
        }

        // Get a context, create the ou=users subcontext, then create the 3 principals.
        Hashtable<String, Object> env = new Hashtable<String, Object>();
        env.put( DirectoryService.JNDI_KEY, getService() );
        env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.directory.server.core.jndi.CoreContextFactory" );
        env.put( Context.PROVIDER_URL, "dc=example,dc=com" );
        env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
        env.put( Context.SECURITY_CREDENTIALS, "secret" );
        env.put( Context.SECURITY_AUTHENTICATION, "simple" );

        ctx = new InitialDirContext( env );

        attrs = getOrgUnitAttributes( "users" );
        DirContext users = ctx.createSubcontext( "ou=users", attrs );

        attrs = getPrincipalAttributes( "Nelson", "Horatio Nelson", "hnelson", "secret", "hnelson@EXAMPLE.COM" );
        users.createSubcontext( "uid=hnelson", attrs );

        attrs = getPrincipalAttributes( "Service", "KDC Service", "krbtgt", "secret", "krbtgt/EXAMPLE.COM@EXAMPLE.COM" );
        users.createSubcontext( "uid=krbtgt", attrs );

        attrs = getPrincipalAttributes( "Service", "LDAP Service", "ldap", "randall", servicePrincipalName );
        users.createSubcontext( "uid=ldap", attrs );
    }

    /**
     * Convenience method for creating principals.
     *
     * @param cn           the commonName of the person
     * @param principal    the kerberos principal name for the person
     * @param sn           the surName of the person
     * @param uid          the unique identifier for the person
     * @param userPassword the credentials of the person
     * @return the attributes of the person principal
     */
    protected Attributes getPrincipalAttributes( String sn, String cn, String uid, String userPassword, String principal )
    {
        Attributes attrs = new BasicAttributes( true );
        Attribute ocls = new BasicAttribute( "objectClass" );
        ocls.add( "top" );
        ocls.add( "person" ); // sn $ cn
        ocls.add( "inetOrgPerson" ); // uid
        ocls.add( "krb5principal" );
        ocls.add( "krb5kdcentry" );
        attrs.put( ocls );
        attrs.put( "cn", cn );
        attrs.put( "sn", sn );
        attrs.put( "uid", uid );
        attrs.put( "userPassword", userPassword );
        attrs.put( "krb5PrincipalName", principal );
        attrs.put( "krb5KeyVersionNumber", "0" );

        return attrs;
    }

    /**
     * Convenience method for creating an organizational unit.
     *
     * @param ou the ou of the organizationalUnit
     * @return the attributes of the organizationalUnit
     */
    protected Attributes getOrgUnitAttributes( String ou )
    {
        Attributes attrs = new BasicAttributes( true );
        Attribute ocls = new BasicAttribute( "objectClass" );
        ocls.add( "top" );
        ocls.add( "organizationalUnit" );
        attrs.put( ocls );
        attrs.put( "ou", ou );

        return attrs;
    }

    /**
     * Tests to make sure GSSAPI binds below the RootDSE work.
     */
    @Test
    public void testSaslGssapiBind()
    {
        kdcServer.getConfig().setPaEncTimestampRequired( false );
        // Use our custom configuration to avoid reliance on external config
        Configuration.setConfiguration( new Krb5LoginConfiguration() );
        // 1. Authenticate to Kerberos.
        LoginContext lc = null;
        try
        {
            lc = new LoginContext( SaslGssapiBindITest.class.getName(), new CallbackHandlerBean( "hnelson", "secret" ) );
            lc.login();
        }
        catch ( LoginException le )
        {
            // Bad username:  Client not found in Kerberos database
            // Bad password:  Integrity check on decrypted field failed
            fail( "Authentication failed:  " + le.getMessage() );
        }

        // 2. Perform JNDI work as authenticated Subject.
        Subject.doAs( lc.getSubject(), new PrivilegedAction()
        {
            public Object run()
            {
                //FIXME activate this code as soon as the GSSAPIMechanismHandler is fixed.
                //Currently GSSAPI authentication for the ldap server is broken
                try
                {
                    // Create the initial context
                    Hashtable<String, String> env = new Hashtable<String, String>();
                    env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
                    env.put( Context.PROVIDER_URL, "ldap://localhost:" + getLdapServer().getPort() );

                    // Request the use of the "GSSAPI" SASL mechanism
                    // Authenticate by using already established Kerberos credentials
                    env.put( Context.SECURITY_AUTHENTICATION, "GSSAPI" );

                    // Request privacy protection
                    env.put( "javax.security.sasl.qop", "auth-conf" );

                    // Request mutual authentication
                    env.put( "javax.security.sasl.server.authentication", "true" );

                    // Request high-strength cryptographic protection
                    env.put( "javax.security.sasl.strength", "high" );

                    DirContext ctx = new InitialDirContext( env );

                    String[] attrIDs =
                        { "uid" };

                    Attributes attrs = ctx.getAttributes( "uid=hnelson,ou=users,dc=example,dc=com", attrIDs );

                    String uid = null;

                    if ( attrs.get( "uid" ) != null )
                    {
                        uid = ( String ) attrs.get( "uid" ).get();
                    }

                    assertEquals( uid, "hnelson" );
                }
                catch ( NamingException e )
                {
                    fail( "Should not have caught exception:  " + e.getMessage() + e.getRootCause() );
                }

                return null;
            }
        } );

    }

    /**
     * Tear down.
     */
    @After
    public void tearDown() throws Exception
    {
        ctx.close();
        ctx = null;
    }

    // copied the below two methods from AbstractServerTest
    /**
     * Sets the contexts for this base class.  Values of user and password used to
     * set the respective JNDI properties.  These values can be overriden by the
     * overrides properties.
     *
     * @param user the username for authenticating as this user
     * @param passwd the password of the user
     * @throws NamingException if there is a failure of any kind
     */
    protected void setContexts( String user, String passwd ) throws Exception
    {
        Hashtable<String, Object> env = new Hashtable<String, Object>();
        env.put( DirectoryService.JNDI_KEY, getService() );
        env.put( Context.SECURITY_PRINCIPAL, user );
        env.put( Context.SECURITY_CREDENTIALS, passwd );
        env.put( Context.SECURITY_AUTHENTICATION, "simple" );
        env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
        setContexts( env );
    }

    /**
     * Sets the contexts of this class taking into account the extras and overrides
     * properties.
     *
     * @param env an environment to use while setting up the system root.
     * @throws NamingException if there is a failure of any kind
     */
    protected void setContexts( Hashtable<String, Object> env ) throws Exception
    {
        Hashtable<String, Object> envFinal = new Hashtable<String, Object>( env );
        envFinal.put( Context.PROVIDER_URL, ServerDNConstants.SYSTEM_DN );
        sysRoot = new InitialLdapContext( envFinal, null );

        envFinal.put( Context.PROVIDER_URL, "" );
        rootDse = getService().getAdminSession();

        envFinal.put( Context.PROVIDER_URL, SchemaConstants.OU_SCHEMA );
        schemaRoot = new InitialLdapContext( envFinal, null );
    }

    private class CallbackHandlerBean implements CallbackHandler
    {
        private String name;
        private String password;

        /**
         * Creates a new instance of CallbackHandlerBean.
         *
         * @param name
         * @param password
         */
        public CallbackHandlerBean( String name, String password )
        {
            this.name = name;
            this.password = password;
        }

        public void handle( Callback[] callbacks ) throws UnsupportedCallbackException, IOException
        {
            for ( int ii = 0; ii < callbacks.length; ii++ )
            {
                Callback callBack = callbacks[ii];

                // Handles username callback.
                if ( callBack instanceof NameCallback )
                {
                    NameCallback nameCallback = ( NameCallback ) callBack;
                    nameCallback.setName( name );
                    // Handles password callback.
                }
                else if ( callBack instanceof PasswordCallback )
                {
                    PasswordCallback passwordCallback = ( PasswordCallback ) callBack;
                    passwordCallback.setPassword( password.toCharArray() );
                }
                else
                {
                    throw new UnsupportedCallbackException( callBack, I18n.err( I18n.ERR_617 ) );
                }
            }
        }
    }

}


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Twilio
{
    public class CredentialList : TwilioBase
    {
        public string Sid { get; set; }
        public string FriendlyName { get; set; }
    }
}


text: <!DOCTYPE html>


text:
%APPEND_PDFS Appends/concatenates multiple PDF files
%
% Example:
%   append_pdfs(output, input1, input2, ...)
%   append_pdfs(output, input_list{:})
%   append_pdfs test.pdf temp1.pdf temp2.pdf
%
% This function appends multiple PDF files to an existing PDF file, or
% concatenates them into a PDF file if the output file doesn't yet exist.
%
% This function requires that you have ghostscript installed on your
% system. Ghostscript can be downloaded from: http://www.ghostscript.com
%
% IN:
%    output - string of output file name (including the extension, .pdf).
%             If it exists it is appended to; if not, it is created.
%    input1 - string of an input file name (including the extension, .pdf).
%             All input files are appended in order.
%    input_list - cell array list of input file name strings. All input
%                 files are appended in order.

% Copyright: Oliver Woodford, 2011

% Thanks to Reinhard Knoll for pointing out that appending multiple pdfs in
% one go is much faster than appending them one at a time.

% Thanks to Michael Teo for reporting the issue of a too long command line.
% Issue resolved on 5/5/2011, by passing gs a command file.

% Thanks to Martin Wittmann for pointing out the quality issue when
% appending multiple bitmaps.
% Issue resolved (to best of my ability) 1/6/2011, using the prepress
% setting

function append_pdfs(varargin)
% Are we appending or creating a new file
append = exist(varargin{1}, 'file') == 2;
if append
    output = [tempname '.pdf'];
else
    output = varargin{1};
    varargin = varargin(2:end);
end
% Create the command file
cmdfile = [tempname '.txt'];
fh = fopen(cmdfile, 'w');
fprintf(fh, '-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile="%s" -f', output);
fprintf(fh, ' "%s"', varargin{:});
fclose(fh);
% Call ghostscript
ghostscript(['@"' cmdfile '"']);
% Delete the command file
delete(cmdfile);
% Rename the file if needed
if append
    movefile(output, varargin{1});
end
end


text:
package org.apache.flume.source;

import org.apache.flume.Event;
import org.apache.flume.annotations.InterfaceAudience;
import org.apache.flume.annotations.InterfaceStability;
import org.apache.flume.event.EventBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Clock;
import java.time.ZoneOffset;
import java.time.temporal.ChronoField;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.netty.buffer.ByteBuf;

@InterfaceAudience.Private
@InterfaceStability.Evolving
public class SyslogUtils {
  public static final String SYSLOG_TIMESTAMP_FORMAT_RFC5424_2 = "yyyy-MM-dd'T'HH:mm:ss.SZ";
  public static final String SYSLOG_TIMESTAMP_FORMAT_RFC5424_1 = "yyyy-MM-dd'T'HH:mm:ss.S";
  public static final String SYSLOG_TIMESTAMP_FORMAT_RFC5424_3 = "yyyy-MM-dd'T'HH:mm:ssZ";
  public static final String SYSLOG_TIMESTAMP_FORMAT_RFC5424_4 = "yyyy-MM-dd'T'HH:mm:ss";
  public static final String SYSLOG_TIMESTAMP_FORMAT_RFC3164_1 = "yyyyMMM d HH:mm:ss";

  public static final String SYSLOG_MSG_RFC5424_0 =
      "(?:\\<(\\d{1,3})\\>)" + // priority
          "(?:(\\d?)\\s?)" + // version
          /* yyyy-MM-dd'T'HH:mm:ss.SZ or yyyy-MM-dd'T'HH:mm:ss.S+hh:mm or - (null stamp) */
          "(?:" +
          "(\\d{4}[-]\\d{2}[-]\\d{2}[T]\\d{2}[:]\\d{2}[:]\\d{2}" +
          "(?:\\.\\d{1,6})?(?:[+-]\\d{2}[:]\\d{2}|Z)?)|-)" + // stamp
          "\\s" + // separator
          "(?:([\\w][\\w\\d\\.@\\-]*)|-)" + // host name or - (null)
          "\\s" + // separator
          "(.*)$"; // body

  public static final String SYSLOG_MSG_RFC3164_0 =
      "(?:\\<(\\d{1,3})\\>)" +
          "(?:(\\d)?\\s?)" + // version
          // stamp MMM d HH:mm:ss, single digit date has two spaces
          "([A-Z][a-z][a-z]\\s{1,2}\\d{1,2}\\s\\d{2}[:]\\d{2}[:]\\d{2})" +
          "\\s" + // separator
          "([\\w][\\w\\d\\.@-]*)" + // host
          "\\s(.*)$";  // body

  public static final int SYSLOG_PRIORITY_POS = 1;
  public static final int SYSLOG_VERSION_POS = 2;
  public static final int SYSLOG_TIMESTAMP_POS = 3;
  public static final int SYSLOG_HOSTNAME_POS = 4;
  public static final int SYSLOG_BODY_POS = 5;

  private Mode m = Mode.START;
  private StringBuilder prio = new StringBuilder();
  private ByteArrayOutputStream baos;
  private static final Logger logger = LoggerFactory
      .getLogger(SyslogUtils.class);

  public static final String SYSLOG_FACILITY = "Facility";
  public static final String SYSLOG_SEVERITY = "Severity";
  public static final String SYSLOG_PRIORITY = "Priority";
  public static final String SYSLOG_VERSION = "Version";
  public static final String EVENT_STATUS = "flume.syslog.status";
  public static final Integer MIN_SIZE = 10;
  public static final Integer DEFAULT_SIZE = 2500;
  private final boolean isUdp;
  private Clock clock;
  private boolean isBadEvent;
  private boolean isIncompleteEvent;
  private Integer maxSize;
  private Set<String> keepFields;

  private class SyslogFormatter {
    public Pattern regexPattern;
    public ArrayList<String> searchPattern = new ArrayList<String>();
    public ArrayList<String> replacePattern = new ArrayList<String>();
    public ArrayList<SimpleDateFormat> dateFormat = new ArrayList<SimpleDateFormat>();
    public boolean addYear;
  }

  private ArrayList<SyslogFormatter> formats = new ArrayList<SyslogFormatter>();

  private String priority = null;
  private String version = null;
  private String timeStamp = null;
  private String hostName = null;
  private String msgBody = null;

  private static final String[] DEFAULT_FIELDS_TO_KEEP = {
      SyslogSourceConfigurationConstants.CONFIG_KEEP_FIELDS_PRIORITY,
      SyslogSourceConfigurationConstants.CONFIG_KEEP_FIELDS_VERSION,
      SyslogSourceConfigurationConstants.CONFIG_KEEP_FIELDS_TIMESTAMP,
      SyslogSourceConfigurationConstants.CONFIG_KEEP_FIELDS_HOSTNAME
  };
  public static final String KEEP_FIELDS_ALL = "--all--";

  public static boolean keepAllFields(Set<String> keepFields) {
    if (keepFields == null) {
      return false;
    }
    return keepFields.contains(KEEP_FIELDS_ALL);
  }

  public static Set<String> chooseFieldsToKeep(String keepFields) {
    if (keepFields == null) {
      return null;
    }

    keepFields = keepFields.trim().toLowerCase(Locale.ENGLISH);

    if (keepFields.equals("false") || keepFields.equals("none")) {
      return null;
    }

    if (keepFields.equals("true") || keepFields.equals("all")) {
      Set<String> fieldsToKeep = new HashSet<String>(1);
      fieldsToKeep.add(KEEP_FIELDS_ALL);
      return fieldsToKeep;
    }

    Set<String> fieldsToKeep = new HashSet<String>(DEFAULT_FIELDS_TO_KEEP.length);

    for (String field : DEFAULT_FIELDS_TO_KEEP) {
      if (keepFields.indexOf(field) != -1) {
        fieldsToKeep.add(field);
      }
    }

    return fieldsToKeep;
  }

  public static String addFieldsToBody(Set<String> keepFields,
                                       String body,
                                       String priority,
                                       String version,
                                       String timestamp,
                                       String hostname) {
    // Prepend fields to be kept in message body.
    if (keepFields != null) {
      if (keepFields.contains(SyslogSourceConfigurationConstants.CONFIG_KEEP_FIELDS_HOSTNAME)) {
        body = hostname + " " + body;
      }
      if (keepFields.contains(SyslogSourceConfigurationConstants.CONFIG_KEEP_FIELDS_TIMESTAMP)) {
        body = timestamp + " " + body;
      }
      if (keepFields.contains(SyslogSourceConfigurationConstants.CONFIG_KEEP_FIELDS_VERSION)
          && version != null && !version.isEmpty()) {
        body = version + " " + body;
      }
      if (keepFields.contains(SyslogSourceConfigurationConstants.CONFIG_KEEP_FIELDS_PRIORITY)) {
        body = "<" + priority + ">" + body;
      }
    }

    return body;
  }

  public static String getIP(SocketAddress socketAddress) {
    try {
      InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
      String ip = inetSocketAddress.getAddress().getHostAddress();
      if (ip != null) {
        return ip;
      } else {
        throw new NullPointerException("The returned IP is null");
      }
    } catch (Exception e) {
      logger.warn("Unable to retrieve client IP address", e);
    }
    // return a safe value instead of null
    return "";
  }

  public static String getHostname(SocketAddress socketAddress) {
    try {
      InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
      String hostname = inetSocketAddress.getHostName();
      if (hostname != null) {
        return hostname;
      } else {
        throw new NullPointerException("The returned hostname is null");
      }
    } catch (Exception e) {
      logger.warn("Unable to retrieve client hostname", e);
    }
    // return a safe value instead of null
    return "";
  }

  public SyslogUtils() {
    this(false);
  }

  public SyslogUtils(boolean isUdp) {
    this(DEFAULT_SIZE,
        new HashSet<>(Arrays.asList(SyslogSourceConfigurationConstants.DEFAULT_KEEP_FIELDS)),
        isUdp);
  }

  public SyslogUtils(Integer defaultSize, Set<String> keepFields, boolean isUdp) {
    this(defaultSize, keepFields, isUdp, Clock.system(Clock.systemDefaultZone().getZone()));
  }

  public SyslogUtils(Integer eventSize, Set<String> keepFields, boolean isUdp, Clock clock) {
    this.isUdp = isUdp;
    this.clock = clock;
    isBadEvent = false;
    isIncompleteEvent = false;
    maxSize = eventSize < MIN_SIZE ? MIN_SIZE : eventSize;
    baos = new ByteArrayOutputStream(eventSize);
    this.keepFields = keepFields;
    initHeaderFormats();
  }

  // extend the default header formatter
  public void addFormats(Map<String, String> formatProp) {
    if (formatProp.isEmpty() || !formatProp.containsKey(SyslogSourceConfigurationConstants.CONFIG_REGEX)) {
      return;
    }
    SyslogFormatter fmt1 = new SyslogFormatter();
    fmt1.regexPattern = Pattern.compile(formatProp.get(SyslogSourceConfigurationConstants.CONFIG_REGEX));
    if (formatProp.containsKey(SyslogSourceConfigurationConstants.CONFIG_SEARCH)) {
      fmt1.searchPattern.add(formatProp.get(SyslogSourceConfigurationConstants.CONFIG_SEARCH));
    }
    if (formatProp.containsKey(SyslogSourceConfigurationConstants.CONFIG_REPLACE)) {
      fmt1.replacePattern.add(formatProp.get(SyslogSourceConfigurationConstants.CONFIG_REPLACE));
    }
    if (formatProp.containsKey(SyslogSourceConfigurationConstants.CONFIG_DATEFORMAT)) {
      fmt1.dateFormat.add(new SimpleDateFormat(formatProp.get(SyslogSourceConfigurationConstants.CONFIG_DATEFORMAT)));
    }
    formats.add(0, fmt1);
  }

  // setup built-in formats
  private void initHeaderFormats() {
    // setup RFC5424 formater
    SyslogFormatter fmt1 = new SyslogFormatter();
    fmt1.regexPattern = Pattern.compile(SYSLOG_MSG_RFC5424_0);
    // 'Z' in timestamp indicates UTC zone, so replace it it with '+0000' for date formatting
    fmt1.searchPattern.add("Z");
    fmt1.replacePattern.add("+0000");
    // timezone in RFC5424 is [+-]tt:tt, so remove the ':' for java date formatting
    fmt1.searchPattern.add("([+-])(\\d{2})[:](\\d{2})");
    fmt1.replacePattern.add("$1$2$3");
    // FLUME-2497: SimpleDateFormat does not handle microseconds, Truncate after 3 digits.
    fmt1.searchPattern.add("(T\\d{2}:\\d{2}:\\d{2}\\.\\d{3})(\\d*)");
    fmt1.replacePattern.add("$1");
    fmt1.dateFormat.add(new SimpleDateFormat(SYSLOG_TIMESTAMP_FORMAT_RFC5424_1, Locale.ENGLISH));
    fmt1.dateFormat.add(new SimpleDateFormat(SYSLOG_TIMESTAMP_FORMAT_RFC5424_2, Locale.ENGLISH));
    fmt1.dateFormat.add(new SimpleDateFormat(SYSLOG_TIMESTAMP_FORMAT_RFC5424_3, Locale.ENGLISH));
    fmt1.dateFormat.add(new SimpleDateFormat(SYSLOG_TIMESTAMP_FORMAT_RFC5424_4, Locale.ENGLISH));
    fmt1.addYear = false;

    // setup RFC3164 formater
    SyslogFormatter fmt2 = new SyslogFormatter();
    fmt2.regexPattern = Pattern.compile(SYSLOG_MSG_RFC3164_0);
    // the single digit date has two spaces, so trim it
    fmt2.searchPattern.add("  ");
    fmt2.replacePattern.add(" ");
    fmt2.dateFormat.add(new SimpleDateFormat(SYSLOG_TIMESTAMP_FORMAT_RFC3164_1, Locale.ENGLISH));
    fmt2.addYear = true;

    formats.add(fmt1);
    formats.add(fmt2);
  }

  enum Mode {
    START, PRIO, DATA
  }

  public enum SyslogStatus {
    OTHER("Unknown"),
    INVALID("Invalid"),
    INCOMPLETE("Incomplete");

    private final String syslogStatus;

    SyslogStatus(String status) {
      syslogStatus = status;
    }

    public String getSyslogStatus() {
      return this.syslogStatus;
    }
  }

  // create the event from syslog data
  Event buildEvent() {
    try {
      byte[] body;
      int pri = 0;
      int sev = 0;
      int facility = 0;

      if (!isBadEvent) {
        pri = Integer.parseInt(prio.toString());
        sev = pri % 8;
        facility = pri / 8;
        formatHeaders();
      }

      Map<String, String> headers = new HashMap<String, String>();
      headers.put(SYSLOG_FACILITY, String.valueOf(facility));
      headers.put(SYSLOG_SEVERITY, String.valueOf(sev));
      if (priority != null && priority.length() > 0) {
        headers.put("priority", priority);
      }
      if (version != null && version.length() > 0) {
        headers.put("version", version);
      }
      if (timeStamp != null && timeStamp.length() > 0) {
        headers.put("timestamp", timeStamp);
      }
      if (hostName != null && hostName.length() > 0) {
        headers.put("host", hostName);
      }
      if (isBadEvent) {
        logger.warn("Event created from Invalid Syslog data.");
        headers.put(EVENT_STATUS, SyslogStatus.INVALID.getSyslogStatus());
      } else if (isIncompleteEvent) {
        logger.warn("Event size larger than specified event size: {}. You should " +
            "consider increasing your event size.", maxSize);
        headers.put(EVENT_STATUS, SyslogStatus.INCOMPLETE.getSyslogStatus());
      }

      if (!keepAllFields(keepFields)) {
        if (msgBody != null && msgBody.length() > 0) {
          body = msgBody.getBytes();
        } else {
          // Parse failed.
          body = baos.toByteArray();
        }
      } else {
        body = baos.toByteArray();
      }
      // format the message
      return EventBuilder.withBody(body, headers);
    } finally {
      reset();
    }
  }

  // Apply each known pattern to message
  private void formatHeaders() {
    String eventStr = baos.toString();
    String timeStampString = null;

    for (int p = 0; p < formats.size(); p++) {
      SyslogFormatter fmt = formats.get(p);
      Pattern pattern = fmt.regexPattern;
      Matcher matcher = pattern.matcher(eventStr);
      if (!matcher.matches()) {
        continue;
      }
      MatchResult res = matcher.toMatchResult();
      for (int grp = 1; grp <= res.groupCount(); grp++) {
        String value = res.group(grp);
        if (grp == SYSLOG_TIMESTAMP_POS) {
          timeStampString = value;

          // apply available format replacements to timestamp
          if (value != null) {
            for (int sp = 0; sp < fmt.searchPattern.size(); sp++) {
              value = value.replaceAll(fmt.searchPattern.get(sp), fmt.replacePattern.get(sp));
            }
            // Add year to timestamp if needed
            if (fmt.addYear) {
              value = clock.instant().atOffset(ZoneOffset.UTC).get(ChronoField.YEAR) + value;
            }
            // try the available time formats to timestamp
            for (int dt = 0; dt < fmt.dateFormat.size(); dt++) {
              try {
                Date parsedDate = fmt.dateFormat.get(dt).parse(value);

                /*
                 * Some code to try and add some smarts to the year insertion.
                 * Original code just added the current year which was okay-ish, but around
                 * January 1st becomes pretty naïve.
                 * The current year is added above. This code, if the year has been added does
                 * the following:
                 * 1. Compute what the computed time, but one month in the past would be.
                 * 2. Compute what the computed time, but eleven months in the future would be.
                 * If the computed time is more than one month in the future then roll it back a
                 * year. If the computed time is more than eleven months in the past then roll it
                 * forward a year. This gives us a 12 month rolling window (11 months in the past,
                 * 1 month in the future) of timestamps.
                 */
                if (fmt.addYear) {
                  Calendar calParsed = Calendar.getInstance();
                  calParsed.setTime(parsedDate);
                  Calendar calMinusOneMonth = Calendar.getInstance();
                  calMinusOneMonth.setTime(parsedDate);
                  calMinusOneMonth.add(Calendar.MONTH, -1);

                  Calendar calPlusElevenMonths = Calendar.getInstance();
                  calPlusElevenMonths.setTime(parsedDate);
                  calPlusElevenMonths.add(Calendar.MONTH, +11);

                  long currentTimeMillis = clock.millis();

                  if (calParsed.getTimeInMillis() > currentTimeMillis &&
                      calMinusOneMonth.getTimeInMillis() > currentTimeMillis) {
                    //Need to roll back a year
                    Calendar c1 = Calendar.getInstance();
                    c1.setTime(parsedDate);
                    c1.add(Calendar.YEAR, -1);
                    parsedDate = c1.getTime();
                  } else if (calParsed.getTimeInMillis() < currentTimeMillis &&
                             calPlusElevenMonths.getTimeInMillis() < currentTimeMillis) {
                    //Need to roll forward a year
                    Calendar c1 = Calendar.getInstance();
                    c1.setTime(parsedDate);
                    c1.add(Calendar.YEAR, +1);
                    parsedDate = c1.getTime();
                  }
                }
                timeStamp = String.valueOf(parsedDate.getTime());
                break; // done. formatted the time
              } catch (ParseException e) {
                // Error formatting the timeStamp, try next format
                continue;
              }
            }
          }
        } else if (grp == SYSLOG_HOSTNAME_POS) {
          hostName = value;
        } else if (grp == SYSLOG_PRIORITY_POS) {
          priority = value;
        } else if (grp == SYSLOG_VERSION_POS) {
          version = value;
        } else if (grp == SYSLOG_BODY_POS) {
          msgBody = addFieldsToBody(keepFields, value, priority, version,
                                    timeStampString, hostName);
        }
      }
      break; // we successfully parsed the message using this pattern
    }
  }

  private void reset() {
    baos.reset();
    m = Mode.START;
    prio.delete(0, prio.length());
    isBadEvent = false;
    isIncompleteEvent = false;
    hostName = null;
    timeStamp = null;
    msgBody = null;
  }

  // extract relevant syslog data needed for building Flume event
  public Event extractEvent(ByteBuf in) {

    /* for protocol debugging
    ByteBuffer bb = in.toByteBuffer();
    int remaining = bb.remaining();
    byte[] buf = new byte[remaining];
    bb.get(buf);
    HexDump.dump(buf, 0, System.out, 0);
    */

    byte b = 0;
    Event e = null;
    boolean doneReading = false;

    while (!doneReading && in.isReadable()) {
      b = in.readByte();
      switch (m) {
        case START:
          if (b == '<') {
            baos.write(b);
            m = Mode.PRIO;
          } else if (b == '\n') {
            //If the character is \n, it was because the last event was exactly
            //as long  as the maximum size allowed and
            //the only remaining character was the delimiter - '\n', or
            //multiple delimiters were sent in a row.
            //Just ignore it, and move forward, don't change the mode.
            //This is a no-op, just ignore it.
            logger.debug("Delimiter found while in START mode, ignoring..");

          } else {
            isBadEvent = true;
            baos.write(b);
            //Bad event, just dump everything as if it is data.
            m = Mode.DATA;
          }
          break;
        case PRIO:
          baos.write(b);
          if (b == '>') {
            if (prio.length() == 0) {
              isBadEvent = true;
            }
            m = Mode.DATA;
          } else {
            char ch = (char) b;
            prio.append(ch);
            // Priority is max 3 digits per both RFC 3164 and 5424
            // With this check there is basically no danger of
            // boas.size() exceeding this.maxSize before getting to the
            // DATA state where this is actually checked
            if (!Character.isDigit(ch) || prio.length() > 3) {
              isBadEvent = true;
              //If we hit a bad priority, just write as if everything is data.
              m = Mode.DATA;
            }
          }
          break;
        case DATA:
          // TCP syslog entries are separated by '\n'
          if (b == '\n') {
            e = buildEvent();
            doneReading = true;
          } else {
            baos.write(b);
          }
          if (baos.size() == this.maxSize && !doneReading) {
            isIncompleteEvent = true;
            e = buildEvent();
            doneReading = true;
          }
          break;
      }

    }

    // UDP doesn't send a newline, so just use what we received
    if (e == null && isUdp) {
      doneReading = true;
      e = buildEvent();
    }

    return e;
  }

  public Integer getEventSize() {
    return maxSize;
  }

  public void setEventSize(Integer eventSize) {
    this.maxSize = eventSize;
  }

  public void setKeepFields(Set<String> keepFields) {
    this.keepFields = keepFields;
  }
}