text:
"""Models for storing the classification data models."""

from __future__ import annotations

import datetime

from core import feconf
from core import utils
from core.platform import models

from typing import Any, Dict, List, Optional, Sequence, Tuple, Union

MYPY = False
if MYPY: # pragma: no cover
    from mypy_imports import base_models
    from mypy_imports import datastore_services

(base_models,) = models.Registry.import_models([models.NAMES.base_model])
datastore_services = models.Registry.import_datastore_services()

NEW_AND_PENDING_TRAINING_JOBS_FETCH_LIMIT = 100

TrainingDataUnionType = Union[
    Dict[str, Union[int, List[str]]],
    List[Dict[str, Union[int, List[str]]]]
]

class ClassifierTrainingJobModel(base_models.BaseModel):
    """Model for storing classifier training jobs.

    The id of instances of this class has the form
    '[exp_id].[random hash of 12 chars]'.
    """

    # The ID of the algorithm used to create the model.
    algorithm_id = datastore_services.StringProperty(
        required=True, indexed=True)
    # The ID of the interaction to which the algorithm belongs.
    interaction_id = (
        datastore_services.StringProperty(required=True, indexed=True))
    # The exploration_id of the exploration to whose state the model belongs.
    exp_id = datastore_services.StringProperty(required=True, indexed=True)
    # The exploration version at the time this training job was created.
    exp_version = (
        datastore_services.IntegerProperty(required=True, indexed=True))
    # The name of the state to which the model belongs.
    state_name = datastore_services.StringProperty(required=True, indexed=True)
    # The status of the training job. It can be either NEW, COMPLETE or PENDING.
    status = datastore_services.StringProperty(
        required=True, choices=feconf.ALLOWED_TRAINING_JOB_STATUSES,
        default=feconf.TRAINING_JOB_STATUS_PENDING, indexed=True)
    # The training data which is to be populated when retrieving the job.
    # The list contains dicts where each dict represents a single training
    # data group. The training data are computed from answers that have been
    # anonymized and that are not connected to any existing or deleted users.
    training_data = datastore_services.JsonProperty(default=None)
    # The time when the job's status should next be checked.
    # It is incremented by TTL when a job with status NEW is picked up by VM.
    next_scheduled_check_time = datastore_services.DateTimeProperty(
        required=True, indexed=True)
    # The algorithm version for the classifier. Algorithm version identifies
    # the format of the classifier_data as well as the prediction API to be
    # used.
    algorithm_version = datastore_services.IntegerProperty(
        required=True, indexed=True)

    @staticmethod
    def get_deletion_policy() -> base_models.DELETION_POLICY:
        """Model doesn't contain any data directly corresponding to a user."""
        return base_models.DELETION_POLICY.NOT_APPLICABLE

    @staticmethod
    def get_model_association_to_user(
    ) -> base_models.MODEL_ASSOCIATION_TO_USER:
        """Model does not contain user data."""
        return base_models.MODEL_ASSOCIATION_TO_USER.NOT_CORRESPONDING_TO_USER

    @classmethod
    def get_export_policy(cls) -> Dict[str, base_models.EXPORT_POLICY]:
        """Model doesn't contain any data directly corresponding to a user."""
        return dict(super(cls, cls).get_export_policy(), **{
            'algorithm_id': base_models.EXPORT_POLICY.NOT_APPLICABLE,
            'interaction_id': base_models.EXPORT_POLICY.NOT_APPLICABLE,
            'exp_id': base_models.EXPORT_POLICY.NOT_APPLICABLE,
            'exp_version': base_models.EXPORT_POLICY.NOT_APPLICABLE,
            'state_name': base_models.EXPORT_POLICY.NOT_APPLICABLE,
            'status': base_models.EXPORT_POLICY.NOT_APPLICABLE,
            'training_data': base_models.EXPORT_POLICY.NOT_APPLICABLE,
            'next_scheduled_check_time':
                base_models.EXPORT_POLICY.NOT_APPLICABLE,
            'algorithm_version': base_models.EXPORT_POLICY.NOT_APPLICABLE
        })

    @classmethod
    def _generate_id(cls, exp_id: str) -> str:
        """Generates a unique id for the training job of the form
        '[exp_id].[random hash of 16 chars]'.

        Args:
            exp_id: str. ID of the exploration.

        Returns:
            str. ID of the new ClassifierTrainingJobModel instance.

        Raises:
            Exception. The id generator for ClassifierTrainingJobModel is
                producing too many collisions.
        """

        for _ in range(base_models.MAX_RETRIES):
            new_id = '%s.%s' % (
                exp_id,
                utils.convert_to_hash(
                    str(utils.get_random_int(base_models.RAND_RANGE)),
                    base_models.ID_LENGTH))
            if not cls.get_by_id(new_id):
                return new_id

        raise Exception(
            'The id generator for ClassifierTrainingJobModel is producing '
            'too many collisions.')

    @classmethod
    def create(
            cls,
            algorithm_id: str,
            interaction_id: str,
            exp_id: str,
            exp_version: int,
            next_scheduled_check_time: datetime.datetime,
            training_data: TrainingDataUnionType,
            state_name: str,
            status: str,
            algorithm_version: int
    ) -> str:
        """Creates a new ClassifierTrainingJobModel entry.

        Args:
            algorithm_id: str. ID of the algorithm used to generate the model.
            interaction_id: str. ID of the interaction to which the algorithm
                belongs.
            exp_id: str. ID of the exploration.
            exp_version: int. The exploration version at the time
                this training job was created.
            next_scheduled_check_time: datetime.datetime. The next scheduled
                time to check the job.
            training_data: dict. The data used in training phase.
            state_name: str. The name of the state to which the classifier
                belongs.
            status: str. The status of the training job.
            algorithm_version: int. The version of the classifier model to be
                trained.

        Returns:
            str. ID of the new ClassifierModel entry.

        Raises:
            Exception. A model with the same ID already exists.
        """

        instance_id = cls._generate_id(exp_id)
        training_job_instance = cls(
            id=instance_id, algorithm_id=algorithm_id,
            interaction_id=interaction_id,
            exp_id=exp_id,
            exp_version=exp_version,
            next_scheduled_check_time=next_scheduled_check_time,
            state_name=state_name, status=status,
            training_data=training_data,
            algorithm_version=algorithm_version
        )

        training_job_instance.update_timestamps()
        training_job_instance.put()
        return instance_id

    @classmethod
    def query_new_and_pending_training_jobs(
            cls, offset: int
    ) -> Tuple[Sequence[ClassifierTrainingJobModel], int]:
        """Gets the next 10 jobs which are either in status "new" or "pending",
        ordered by their next_scheduled_check_time attribute.

        Args:
            offset: int. Number of query results to skip.

        Returns:
            tuple(list(ClassifierTrainingJobModel), int).
            A tuple containing the list of the ClassifierTrainingJobModels
            with status new or pending and the offset value.
        """
        query = (
            cls.get_all()
            .filter(
                datastore_services.all_of(
                    cls.status.IN([
                        feconf.TRAINING_JOB_STATUS_NEW,
                        feconf.TRAINING_JOB_STATUS_PENDING
                    ]),
                    cls.next_scheduled_check_time <= datetime.datetime.utcnow()
                )
            )
            .order(cls.next_scheduled_check_time)
        )

        classifier_job_models: Sequence[ClassifierTrainingJobModel] = (
            query.fetch(
                NEW_AND_PENDING_TRAINING_JOBS_FETCH_LIMIT, offset=offset))
        offset = offset + len(classifier_job_models)
        return classifier_job_models, offset

    # TODO(#13523): Change 'job_dict' to domain object/TypedDict to
    # remove Any from type-annotation below.
    @classmethod
    def create_multi(cls, job_dicts_list: List[Dict[str, Any]]) -> List[str]:
        """Creates multiple new  ClassifierTrainingJobModel entries.

        Args:
            job_dicts_list: list(dict). The list of dicts where each dict
                represents the attributes of one ClassifierTrainingJobModel.

        Returns:
            list(str). List of job IDs.
        """
        job_models = []
        job_ids = []
        for job_dict in job_dicts_list:
            instance_id = cls._generate_id(job_dict['exp_id'])
            training_job_instance = cls(
                id=instance_id, algorithm_id=job_dict['algorithm_id'],
                interaction_id=job_dict['interaction_id'],
                exp_id=job_dict['exp_id'],
                exp_version=job_dict['exp_version'],
                next_scheduled_check_time=job_dict['next_scheduled_check_time'],
                state_name=job_dict['state_name'], status=job_dict['status'],
                training_data=job_dict['training_data'],
                algorithm_version=job_dict['algorithm_version'])

            job_models.append(training_job_instance)
            job_ids.append(instance_id)
        cls.update_timestamps_multi(job_models)
        cls.put_multi(job_models)
        return job_ids

class StateTrainingJobsMappingModel(base_models.BaseModel):
    """Model for mapping exploration attributes to a ClassifierTrainingJob.

    The ID of instances of this class has the form
    [exp_id].[exp_version].[state_name].
    """

    # The exploration_id of the exploration to whose state the model belongs.
    exp_id = datastore_services.StringProperty(required=True, indexed=True)
    # The exploration version at the time the corresponding classifier's
    # training job was created.
    exp_version = (
        datastore_services.IntegerProperty(required=True, indexed=True))
    # The name of the state to which the model belongs.
    state_name = datastore_services.StringProperty(required=True, indexed=True)
    # The IDs of the training jobs corresponding to the exploration state. Each
    # algorithm_id corresponding to the interaction of the exploration state is
    # mapped to its unique job_id.
    algorithm_ids_to_job_ids = datastore_services.JsonProperty(
        required=True, indexed=True)

    @staticmethod
    def get_deletion_policy() -> base_models.DELETION_POLICY:
        """Model doesn't contain any data directly corresponding to a user."""
        return base_models.DELETION_POLICY.NOT_APPLICABLE

    @staticmethod
    def get_model_association_to_user(
    ) -> base_models.MODEL_ASSOCIATION_TO_USER:
        """Model does not contain user data."""
        return base_models.MODEL_ASSOCIATION_TO_USER.NOT_CORRESPONDING_TO_USER

    @classmethod
    def get_export_policy(cls) -> Dict[str, base_models.EXPORT_POLICY]:
        """Model doesn't contain any data directly corresponding to a user."""
        return dict(super(cls, cls).get_export_policy(), **{
            'exp_id': base_models.EXPORT_POLICY.NOT_APPLICABLE,
            'exp_version': base_models.EXPORT_POLICY.NOT_APPLICABLE,
            'state_name': base_models.EXPORT_POLICY.NOT_APPLICABLE,
            'algorithm_ids_to_job_ids': base_models.EXPORT_POLICY.NOT_APPLICABLE
        })

    @classmethod
    def _generate_id(
            cls,
            exp_id: str,
            exp_version: int,
            state_name: str
    ) -> str:
        """Generates a unique ID for the Classifier Exploration Mapping of the
        form [exp_id].[exp_version].[state_name].

        Args:
            exp_id: str. ID of the exploration.
            exp_version: int. The exploration version at the time
                this training job was created.
            state_name: unicode. The name of the state to which the classifier
                belongs.

        Returns:
            str. ID of the new Classifier Exploration Mapping instance.
        """
        return '%s.%s.%s' % (exp_id, exp_version, state_name)

    @classmethod
    def get_models(
        cls, exp_id: str, exp_version: int, state_names: List[str]
    ) -> List[Optional[StateTrainingJobsMappingModel]]:
        """Retrieves the Classifier Exploration Mapping models given Exploration
        attributes.

        Args:
            exp_id: str. ID of the exploration.
            exp_version: int. The exploration version at the time
                this training job was created.
            state_names: list(unicode). The state names for which we retrieve
                the mapping models.

        Returns:
            list(ClassifierExplorationMappingModel|None). The model instances
            for the classifier exploration mapping.
        """
        mapping_ids = []
        for state_name in state_names:
            mapping_id = cls._generate_id(exp_id, exp_version, state_name)
            mapping_ids.append(mapping_id)
        mapping_instances = cls.get_multi(mapping_ids)
        return mapping_instances

    @classmethod
    def get_model(
        cls, exp_id: str, exp_version: int, state_name: str
    ) -> Optional[StateTrainingJobsMappingModel]:
        """Retrieves the Classifier Exploration Mapping model for given
        exploration.

        Args:
            exp_id: str. ID of the exploration.
            exp_version: int. The exploration version at the time
                this training job was created.
            state_name: unicode. The state name for which we retrieve
                the mapping model.

        Returns:
            ClassifierExplorationMappingModel|None. The model instance
            for the classifier exploration mapping. It returns None if the no
            entry for given <exp_id, exp_version, state_name> is found.
        """
        mapping_id = cls._generate_id(exp_id, exp_version, state_name)
        model = cls.get_by_id(mapping_id)
        return model

    @classmethod
    def create(
            cls,
            exp_id: str,
            exp_version: int,
            state_name: str,
            algorithm_ids_to_job_ids: Dict[str, str]
    ) -> str:
        """Creates a new ClassifierExplorationMappingModel entry.

        Args:
            exp_id: str. ID of the exploration.
            exp_version: int. The exploration version at the time
                this training job was created.
            state_name: unicode. The name of the state to which the classifier
                belongs.
            algorithm_ids_to_job_ids: dict(str, str). The mapping from
                algorithm IDs to the IDs of their corresponding classifier
                training jobs.

        Returns:
            str. ID of the new ClassifierExplorationMappingModel entry.

        Raises:
            Exception. A model with the same ID already exists.
        """

        instance_id = cls._generate_id(exp_id, exp_version, state_name)
        if not cls.get_by_id(instance_id):
            mapping_instance = cls(
                id=instance_id, exp_id=exp_id, exp_version=exp_version,
                state_name=state_name,
                algorithm_ids_to_job_ids=algorithm_ids_to_job_ids)

            mapping_instance.update_timestamps()
            mapping_instance.put()
            return instance_id
        raise Exception('A model with the same ID already exists.')

    @classmethod
    def create_multi(
            cls,
            state_training_jobs_mappings: List[StateTrainingJobsMappingModel]
    ) -> List[str]:
        """Creates multiple new StateTrainingJobsMappingModel entries.

        Args:
            state_training_jobs_mappings: list(StateTrainingJobsMapping). The
                list of StateTrainingJobsMapping domain objects.

        Returns:
            list(int). The list of mapping IDs.
        """
        mapping_models = []
        mapping_ids = []
        for state_training_job_mapping in state_training_jobs_mappings:
            instance_id = cls._generate_id(
                state_training_job_mapping.exp_id,
                state_training_job_mapping.exp_version,
                state_training_job_mapping.state_name)
            mapping_instance = cls(
                id=instance_id, exp_id=state_training_job_mapping.exp_id,
                exp_version=state_training_job_mapping.exp_version,
                state_name=state_training_job_mapping.state_name,
                algorithm_ids_to_job_ids=(
                    state_training_job_mapping.algorithm_ids_to_job_ids
                ))

            mapping_models.append(mapping_instance)
            mapping_ids.append(instance_id)
        cls.update_timestamps_multi(mapping_models)
        cls.put_multi(mapping_models)
        return mapping_ids


text:
package com.winterwell.es.client;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.winterwell.es.client.agg.AggregationResults;
import com.winterwell.es.fail.ESBulkException;
import com.winterwell.es.fail.ESException;
import com.winterwell.gson.Gson;
import com.winterwell.gson.GsonBuilder;
import com.winterwell.utils.Dep;
import com.winterwell.utils.Printer;
import com.winterwell.utils.StrUtils;
import com.winterwell.utils.Utils;
import com.winterwell.utils.containers.Containers;
import com.winterwell.utils.log.Log;
import com.winterwell.utils.web.IHasJson;

/**
 * Imitates {@link GetResponse}
 * @author daniel
 *
 */
public class ESHttpResponse implements IESResponse, SearchResponse, BulkResponse, GetResponse,
// allow response to be sent as json over http
IHasJson
{

	@Override
	public Long getVersion() {
		Map<String, Object> jmap = getJsonMap();
		Number v = (Number) jmap.get("_version");
		return v==null? null : v.longValue();
	}

	private final String json;
	private final RuntimeException error;
	private final transient ESHttpRequest req;
	private Map parsed;
	private boolean sourceOnly;

	/* (non-Javadoc)
	 * @see com.winterwell.es.client.IESResponse#toString()
	 */
	@Override
	public String toString() {
		return "ESHttpResponse["+StrUtils.ellipsize(Utils.or(json, ""+error), 120)+"]";
	}

	/**
	 * For use by wrapper sub-classes
	 * @param response
	 */
	protected ESHttpResponse(ESHttpResponse response) {
		this(response.req, response.json, response.error);
	}

	public ESHttpResponse(ESHttpRequest req, String json) {
		this(req, json, null);
	}

	/**
	 *
	 * @param req
	 * @param ex Should we standardise on {@link ESException}??
	 */
	public ESHttpResponse(ESHttpRequest req, RuntimeException ex) {
		this(req, null, ex);
	}

	ESHttpResponse(ESHttpRequest req, String json, RuntimeException ex) {
		this.req = req;
		this.json = json;

//		// HACK - ESv7 doesnt throw errors from bulk requests
//		if (ex==null && json!=null && req instanceof BulkRequestBuilder) {
//			Map<String, Object> jobj = getParsedJson();
//			Object errors = jobj.get("errors");
//			if (Utils.yes(errors)) {
//				List<Map> items = (List) jobj.get("items");
//				List<Object> itemErrors = Containers.filterNulls(Containers.apply(items, i -> i.get("error")));
//
//				ex = new ESBulkException(itemExs);
//				Printer.out(items);
//			}
//		}
		this.error = ex;
		// source only?
		if (req instanceof GetRequestBuilder && ((GetRequestBuilder) req).sourceOnly) {
			sourceOnly = true;
		}
	}

	/* (non-Javadoc)
	 * @see com.winterwell.es.client.IESResponse#isSuccess()
	 */
	@Override
	public boolean isSuccess() {
		return error==null;
	}

	@Override
	public Map<String, Object> getSourceAsMap() {
		check();
		Map<String, Object> map = getParsedJson();
		// is it just the source?
		if (sourceOnly) {
			return map;
		}
		Object source = map.get("_source");
		return (Map) source;
	}

	public Object getFromResultFields(String input) {
		Map<String, Object> map = getParsedJson();
		// is it just the source?
		Object get = map.get("get");
		Object fields = ((Map) get).get("fields");
		return ((Map) fields).get(input);
	}

	/**
	 * @return Could contain POJOs depending on gson setup.
	 *
	 * @see #getJsonMap() which uses a "plain gson" for Map objects
	 */
	public Map<String, Object> getParsedJson() {
		if (parsed!=null) return parsed;
		parsed = gson().fromJson(json, Map.class);
		return parsed;
	}

	/**
	 * Uses a "plain" (inflexible) Gson, so nothing gets converted into "fancy" POJOs.
	 * @return
	 */
	public Map<String, Object> getJsonMap() {
		Map map = plainGson().fromJson(json, Map.class);
		return map;
	}

	private Gson plainGson() {
		Gson gb = new GsonBuilder()
						.setClassProperty(null)
						.create();
		return gb;
	}

	/**
	 * The raw json as returned by ES.
	 */
	public String getJson() {
		return json;
	}

	private Gson gson() {
		// req should never be null -- unless its been serialised and back
		if (req==null || req.hClient==null) {
			// A deserialised response (hence with no request).
			// ...fallback to Dep for Gson
			return Dep.get(Gson.class);
		}
		return req.hClient.config.getGson();
	}

	/* (non-Javadoc)
	 * @see com.winterwell.es.client.IESResponse#isAcknowledged()
	 */
	@Override
	public boolean isAcknowledged() {
		return isSuccess();
	}

	@Override
	public List getField(String name) {
		Map<String, Object> map = getFieldsFromGet();
		Object output = map.get(name);
		return (List) output;
	}

	@Override
	public String getSourceAsString() {
		check();
		// is it just the source?
		if (sourceOnly) {
			return json;
		}
		Map<String, Object> map = getParsedJson();
		Object source = map.get("_source");
		return gson().toJson(source);
	}

	// From BulkResponse
	@Override
	public boolean hasErrors() {
		if ( ! isSuccess()) {
			return true;
		}
		Map<String, Object> map = getParsedJson();
		Object fails = map.get("errors"); // NB: boolean in ESv7, was the errors in ESv5
		if (Utils.yes(fails)) {
			return true;
		}
		return false;
	}

	/**
	 * error or null
	 *
	 * TODO handle bulk-request errors, which are different
	 * @see #getBulkErrors()
	 */
	public RuntimeException getError() {
		// HACK! unreliable if deserialising cos req is transient
		if (req instanceof BulkRequestBuilder && error==null) {
			return getBulkErrors();
		}
		return error;
	}

	/**
	 * TODO handle bulk-request errors nicely
	 * @return
	 */
	RuntimeException getBulkErrors() {
		List<Exception> errors = new ArrayList<>();
		Map<String, Object> parsedJson = getParsedJson();
		List<Map<String, Map<String, Object>>> items = (List) parsedJson.get("items");
		if (items == null) return null;
		for(Map<String, Map<String, Object>> item : items) {
			for (Map.Entry<String, Map<String, Object>> entry : item.entrySet()) {
				Map<String, Object> values = entry.getValue();
				Map err = (Map) values.get("error");
				if (err == null) continue;
				String errs = err.toString();
				ESException ex = new ESException((String)err.get("reason"));
				errors.add(ex);
			}
		}
		if ( ! errors.isEmpty()) {
			return new ESBulkException(errors);
		}
		return null;
	}

	@Override
	public ESHttpResponse check() {
		if (error!=null) {
			Log.d("fail.check", "throw from "+req);
			throw error;
		}
		return this;
	}

	@Override
	public List<Map> getHits() {
		if ( ! isSuccess()) throw error;
		Map<String, Object> map = getParsedJson();
		Map hits = (Map) map.get("hits");
		Object hitsList = hits.get("hits");
		return (List<Map>) hitsList;
	}

	@Override
	public List<Map<String, Object>> getSearchResults() {
		if ( ! isSuccess()) throw error;
		Map<String, Object> map = getJsonMap();
		Map hits = (Map) map.get("hits");
		List<Map<String,Object>> hitsList = (List) hits.get("hits");
		List results = Containers.apply(hitsList, hit -> hit.get("_source"));
		return results;
	}

	/**
	 * {@inheritDoc}
	 *
	 * Uses {@link #gson()} for the convertor.
	 */
	@Override
	public <X> List<X> getSearchResults(Class<? extends X> klass) {
		check();
		Map<String, Object> jobj = getJsonMap();
		List<Map> hits = (List<Map>) ((Map)jobj.get("hits")).get("hits");
		List<X> results = Containers.apply(hits, map -> gson().convert((Map)map.get("_source"), klass));
		return results;
	}

	@Override
	public Map getAggregations() {
		if ( ! isSuccess()) throw error;
		Map<String, Object> map = getParsedJson();
		Map hits = (Map) map.get("aggregations");
		return hits;
	}

	@Override
	public AggregationResults getAggregationResults(String aggName) {
		Map rs = (Map) getAggregations().get(aggName);
		return new AggregationResults(aggName, rs);
	}

	/**
	 * @return Never null - can throw exceptions
	 */
	@Override
	public long getTotal() throws IllegalArgumentException {
		if ( ! isSuccess()) throw error;
		Map<String, Object> map = getParsedJson();
		Map hits = (Map) map.get("hits");
		if (hits == null){
			throw new IllegalArgumentException("hits field cannot be null");
		}
		// c.f. https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-request-track-total-hits.html
		Map hitTotal = (Map) hits.get("total");
		if (hitTotal == null){
			throw new IllegalArgumentException("hitTotal field cannot be null");
		}
		Number hitTotalValue = (Number) hitTotal.get("value");
		return hitTotalValue.longValue();
	}

	@Override
	public Map getFacets() {
		if ( ! isSuccess()) throw error;
		Map<String, Object> map = getParsedJson();
		Object hits = map.get("facets");
		return (Map) hits;
	}

	@Override
	public Map getFieldsFromGet() {
		if (error!=null) throw error;
		Map<String, Object> map = getParsedJson();
		Map<String, Object> get = (Map<String, Object>) map.get("get");
		Map<String, Object> hits = (Map<String, Object>) (Map<String, Object>) get.get("fields");
		return hits;
	}

	@Override
	public String getScrollId() {
		if ( ! isSuccess()) throw error;
		Map<String, Object> map = getParsedJson();
		Object sid = map.get("_scroll_id");
		return (String) sid;
	}

	@Override
	public List<Map> getSuggesterHits(String name) {
		if ( ! isSuccess()) throw error;
		Map<String, Object> map = getParsedJson();
		Map suggesters = (Map) map.get("suggest");
		List<Map> res = (List<Map>) suggesters.get(name);
		//  Do num -> result -> options -> num -> _source to get a doc
		List hits = Containers.flatten(Containers.apply(res, r -> r.get("options")));
		return hits;
	}

	@Override
	public Object toJson2() throws UnsupportedOperationException {
		return Containers.objectAsMap(this);
	}

}

redpajama_set_name: RedPajamaGithub


text:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE module PUBLIC
    "-//Puppy Crawl//DTD Check Configuration 1.1//EN"
    "http://www.puppycrawl.com/dtds/configuration_1_1.dtd" [
  <!ENTITY includedConfig SYSTEM "../included.xml">
]>

<module name="Checker">
    <property name="tabWidth" value="4" />
    &includedConfig;
</module>


text:
#ifdef USE_TI_UIEMAILDIALOG

#import "TiUIEmailDialogProxy.h"
#import <TitaniumKit/Mimetypes.h>
#import <TitaniumKit/TiApp.h>
#import <TitaniumKit/TiBase.h>
#import <TitaniumKit/TiBlob.h>
#import <TitaniumKit/TiColor.h>
#import <TitaniumKit/TiFile.h>
#import <TitaniumKit/TiUtils.h>

@implementation TiUIEmailDialogProxy

- (void)dealloc
{
  RELEASE_TO_NIL(attachments);
  [super dealloc];
}

- (void)_destroy
{
  RELEASE_TO_NIL(attachments);
  [super _destroy];
}

- (NSString *)apiName
{
  return @"Ti.UI.EmailDialog";
}

- (NSArray *)attachments
{
  return attachments;
}

- (void)addAttachment:(id)ourAttachment
{
  ENSURE_SINGLE_ARG(ourAttachment, NSObject);

  if (attachments == nil) {
    attachments = [[NSMutableArray alloc] initWithObjects:ourAttachment, nil];
  } else {
    [attachments addObject:ourAttachment];
  }
}

- (id)isSupported:(id)args
{
#if TARGET_IPHONE_SIMULATOR
  DebugLog(@"[INFO] iOS Simulator does not support sending emails. Use a device instead.");
  return NUMBOOL(NO);
#endif
  return NUMBOOL([MFMailComposeViewController canSendMail]);
}

- (void)open:(id)args
{
#if TARGET_IPHONE_SIMULATOR
  DebugLog(@"[INFO] iOS Simulator does not support sending emails. Use a device instead.");
  NSDictionary *event = [NSDictionary dictionaryWithObject:NUMINT(MFMailComposeResultFailed) forKey:@"result"];
  [self fireEvent:@"complete" withObject:event errorCode:MFMailComposeResultFailed message:@"iOS Simulator does not support sending emails. Use a device instead."];
#else

  [self rememberSelf];
  NSDictionary *properties = nil;
  ENSURE_ARG_OR_NIL_AT_INDEX(properties, args, 0, NSDictionary);
  Class arrayClass = [NSArray class];
  NSArray *toArray = [self valueForUndefinedKey:@"toRecipients"];
  ENSURE_CLASS_OR_NIL(toArray, arrayClass);
  NSArray *bccArray = [self valueForUndefinedKey:@"bccRecipients"];
  ENSURE_CLASS_OR_NIL(bccArray, arrayClass);
  NSArray *ccArray = [self valueForUndefinedKey:@"ccRecipients"];
  ENSURE_CLASS_OR_NIL(ccArray, arrayClass);

  ENSURE_UI_THREAD(open, args);

  NSString *subject = [TiUtils stringValue:[self valueForUndefinedKey:@"subject"]];
  NSString *message = [TiUtils stringValue:[self valueForUndefinedKey:@"messageBody"]];

  if (![MFMailComposeViewController canSendMail]) {
    NSDictionary *event = [NSDictionary dictionaryWithObject:NUMINT(MFMailComposeResultFailed) forKey:@"result"];
    [self fireEvent:@"complete" withObject:event errorCode:MFMailComposeResultFailed message:@"system can't send email"];
    return;
  }

  UIColor *barColor = [[TiUtils colorValue:[self valueForUndefinedKey:@"barColor"]] _color];

  MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
  [composer setMailComposeDelegate:self];
  if (barColor != nil) {
    [[composer navigationBar] setBarTintColor:barColor];
  }

  [composer setSubject:subject];
  [composer setToRecipients:toArray];
  [composer setBccRecipients:bccArray];
  [composer setCcRecipients:ccArray];
  [composer setMessageBody:message isHTML:[TiUtils boolValue:[self valueForUndefinedKey:@"html"] def:NO]];

  if (attachments != nil) {
    for (id attachment in attachments) {
      if ([attachment isKindOfClass:[TiBlob class]]) {
        NSString *path = [(TiBlob *)attachment path];
        if (path == nil) {
          path = @"attachment";
        } else {
          path = [path lastPathComponent];
        }
        NSString *mimetype = [attachment mimeType];
        if (mimetype == nil) {
          mimetype = [Mimetypes mimeTypeForExtension:path];
        }
        [composer addAttachmentData:[attachment data]
                           mimeType:mimetype
                           fileName:path];
      } else if ([attachment isKindOfClass:[TiFile class]]) {
        TiFile *file = (TiFile *)attachment;
        NSString *path = [file path];
        NSData *data = [NSData dataWithContentsOfFile:path];
        NSString *mimetype = [Mimetypes mimeTypeForExtension:path];
        [composer addAttachmentData:data mimeType:mimetype fileName:[path lastPathComponent]];
      }
    }
  }

  BOOL animated = [TiUtils boolValue:@"animated" properties:properties def:YES];
  [self retain];
  [[TiApp app] showModalController:composer animated:animated];
#endif
}

MAKE_SYSTEM_PROP(SENT, MFMailComposeResultSent);
MAKE_SYSTEM_PROP(SAVED, MFMailComposeResultSaved);
MAKE_SYSTEM_PROP(CANCELLED, MFMailComposeResultCancelled);
MAKE_SYSTEM_PROP(FAILED, MFMailComposeResultFailed);

#pragma mark Delegate

- (void)mailComposeController:(MFMailComposeViewController *)composer didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
  if (error != nil) {
    NSLog(@"[ERROR] Unexpected composing error: %@", error);
  }

  BOOL animated = YES;

  [[TiApp app] hideModalController:composer animated:animated];
  [composer autorelease];
  if ([self _hasListeners:@"complete"]) {
    NSDictionary *event = [NSDictionary dictionaryWithObject:NUMINT(result) forKey:@"result"];
    [self fireEvent:@"complete" withObject:event errorCode:[error code] message:[TiUtils messageFromError:error]];
  }
  [self forgetSelf];
  [self autorelease];
}

@end

#endif


text:
function(test)

  mock_package_source("mock"
    A B
    "A=>B"

    )
  ans(package_source)

  function(test_package_dependency_configuration_update)
    set(args ${ARGN})

    map_new()
    ans(package_handle)
    map_set(${package_handle} uri "project:root")

    while(true)
      list_extract_labelled_value(args --init)
      ans(init)
      if(NOT init)
        break()
      endif()
      package_dependency_configuration_update(
        ${package_source}
        ${package_handle}
        ${init}
      )
    endwhile()

    package_dependency_configuration_update(
      ${package_source}
      ${package_handle}
      ${args}
      )
    ans(result)
    json_print(${result})
    return_ref(result)
  endfunction()

  define_test_function(test_uut test_package_dependency_configuration_update)

  test_uut("{'mock:A':'true', 'mock:B':'true'}" A)
  test_uut("{'mock:A':'true', 'mock:B':'true'}" "A {asd:'123'}")
endfunction()


text:
"use strict";

require("./src/js/lib/querystring-coding");
require("./src/js/lib/querystring-coding-middleware");
require("./src/js/routable");
require("./src/js/express");
require("./src/js/middleware");
require("./src/js/headerMiddleware");
require("./src/js/redirectMiddleware");
require("./src/js/wrappedMiddleware");
require("./src/js/errorMiddleware");
require("./src/js/router");
require("./src/js/static");
require("./src/js/serve-index");
require("./src/js/handler");
require("./src/js/requestAwareMiddleware");
require("./src/js/contentAwareMiddleware");


text:
import {defaultGlobals, defaultHeaders, defaultIdKeys} from '../defaults';
import {Context, FetchOptions, ContentRange} from '../typings';
import {
  encodeUriQuery,
  encodeUriSegment,
  replaceQueryStringParamFromUrl,
  replaceUrlParamFromUrl,
  splitUrlByProtocolAndDomain
} from './url';
import {endsWith, isObject, isString, startsWith, toString} from './util';

export class HttpError extends Error {
  statusCode: number;
  status: number;
  body: unknown;
  headers: Record<string, string> = {};
  constructor(
    statusCode = 500,
    {body, message = 'HttpError', headers}: {body?: unknown; message?: string; headers?: Response['headers']}
  ) {
    super(message);
    this.name = this.constructor.name;
    this.message = message;
    if (typeof Error.captureStackTrace === 'function') {
      Error.captureStackTrace(this, this.constructor);
    } else {
      this.stack = new Error(message).stack;
    }
    // Http
    this.statusCode = statusCode;
    this.status = statusCode;
    this.body = body;
    if (headers) {
      this.headers = Object.fromEntries(headers.entries());
    }
  }
}

type BuildFetchUrlOptions = {
  url: string;
  urlParams: Record<string, {isQueryParamValue?: boolean}>;
  stripTrailingSlashes?: boolean;
  method?: string;
  params?: Record<string, string>;
  isArray?: boolean;
};
export const buildFetchUrl = (
  context: Context,
  {url, method = 'get', urlParams, params = {}, isArray = false, stripTrailingSlashes = true}: BuildFetchUrlOptions
): string => {
  const [protocolAndDomain = '', remainderUrl] = splitUrlByProtocolAndDomain(url);
  const contextAsObject = !isObject(context)
    ? {
        [defaultIdKeys.singular]: context
      }
    : context;
  // Replace urlParams with values from context
  let builtUrl = Object.keys(urlParams).reduce((wipUrl, urlParam) => {
    const urlParamInfo = urlParams[urlParam];
    const value = params[urlParam] || contextAsObject[urlParam] || ''; // self.defaults[urlParam];
    if (value) {
      const encodedValue = urlParamInfo.isQueryParamValue
        ? encodeUriQuery(toString(value), true)
        : encodeUriSegment(toString(value));
      return replaceUrlParamFromUrl(wipUrl, urlParam, encodedValue);
    } else if (!isArray && urlParam === defaultIdKeys.singular && !['post'].includes(method.toLowerCase())) {
      throw new Error(`Failed to resolve required "${urlParam}" from context=${JSON.stringify(context)}`);
    }
    return replaceUrlParamFromUrl(wipUrl, urlParam);
  }, remainderUrl);
  // Strip trailing slashes and set the url (unless this behavior is specifically disabled)
  if (stripTrailingSlashes) {
    builtUrl = builtUrl.replace(/\/+$/, '') || '/';
  }
  return protocolAndDomain + builtUrl;
};

export const buildFetchOpts = (
  context: Context,
  {method, headers, credentials, query, body}: FetchOptions
): FetchOptions => {
  const opts: FetchOptions = {
    method: 'GET',
    headers: defaultHeaders
  };
  if (method) {
    opts.method = method;
  }
  if (headers) {
    opts.headers = {
      ...opts.headers,
      ...headers
    };
  }
  if (credentials) {
    opts.credentials = credentials;
  }
  if (query) {
    opts.query = query;
  }
  const hasBody = /^(POST|PUT|PATCH|DELETE)$/i.test(opts.method as string);
  if (body) {
    opts.body = isString(body) ? body : JSON.stringify(body);
  } else if (hasBody && context) {
    const contextAsObject = !isObject(context)
      ? {
          [defaultIdKeys.singular]: context
        }
      : context;
    opts.body = JSON.stringify(contextAsObject);
  }
  return opts;
};

export const parseResponseBody = <T = unknown>(res: Response): Promise<T> => {
  const contentType = res.headers.get('Content-Type');
  // @NOTE parses 'application/problem+json; charset=utf-8' for example
  // see https://tools.ietf.org/html/rfc6839
  const isJson =
    contentType && (startsWith(contentType, 'application/json') || endsWith(contentType.split(';')[0], '+json'));
  return res[isJson ? 'json' : 'text']();
};

export interface SerializableResponse<T = unknown> {
  body: T;
  code: Response['status']; // @deprecated
  status: Response['status'];
  headers: Record<string, string>;
  ok: Response['ok'];
  contentRange?: ContentRange | null;
  receivedAt: number;
  [s: string]: unknown;
}

export const serializeResponse = async <T = unknown>(res: Response): Promise<SerializableResponse<T>> => ({
  body: await parseResponseBody<T>(res),
  code: res.status, // @deprecated,
  status: res.status,
  headers: Object.fromEntries(res.headers.entries()),
  receivedAt: Date.now(),
  ok: res.ok
});

const fetch = async (url: string, options: FetchOptions = {}): Promise<Response> => {
  // Support options.query
  const query = isObject(options.query) ? options.query : {};
  const builtUrl = Object.keys(query).reduce((wipUrl, queryParam) => {
    const queryParamValue: string = isString(query[queryParam])
      ? (query[queryParam] as string)
      : JSON.stringify(query[queryParam]);
    return replaceQueryStringParamFromUrl(wipUrl, queryParam, queryParamValue);
  }, url);
  return (options.Promise || defaultGlobals.Promise)
    .resolve((defaultGlobals.fetch || fetch)(builtUrl, options as RequestInit))
    .then(async (res) => {
      if (!res.ok) {
        const body = await parseResponseBody(res);
        throw new HttpError(res.status, {
          body,
          message: res.statusText,
          headers: res.headers
        });
      }
      return res;
    });
};

export default fetch;


text:
import { ArgumentsEqualityComparer } from "./arguments.equality-comparer";
import { MethodExpression } from "../reflector/expressions";

/**
 * @hidden
 */
export class InstanceMethodEqualityComparer {

    constructor(
        private readonly argumentsMatcher: ArgumentsEqualityComparer) {

    }

    public equals(left: MethodExpression, right: MethodExpression): boolean {
        return left.name === right.name && this.argumentsMatcher.equals(left.args, right.args);
    }
}


text:
layout: post
title:  "DVWA"
author: Degan
categories: white-hat
tags:	white_hat hacker dvwa
comments: true
---
# DVWA

>Damn Vulnerable Web Application (DVWA) is a PHP/MySQL web application that is damn vulnerable. Its main goal is to be an aid for security professionals to test their skills and tools in a legal environment, help web developers better understand the processes of securing web applications and to aid both students & teachers to learn about web application security in a controlled class room environment.

>The aim of DVWA is to practice some of the most common web vulnerability, with various difficultly levels, with a simple straightforward interface

![VirtualBox_install_1](https://degan85.github.io/assets/dvwa.png)

- web  해킹을 실습 할 수 있도록 제작

- 초기 id : admin
- password : password

- `view source`는 현재 페이지 소스
- `view help`에서는 공격 중 도움말
- DVWA security : 보안 레벨 설정 가능
	- Low
	- medium
	- high
	- Impossible

---

## Reference

[화이트해커가 되기 위한 8가지 웹 해킹 기술](https://www.udemy.com/everything-about-white-hat-hacker/learn/v4/overview)


text:
import React from 'react';

class Loader extends React.Component {
  render() {
    return (

    );
  }
}

export default Loader;


text:
using System.IO.Abstractions;
using Raven.Assure.Log;

namespace Raven.Assure.Fluent
{
   public class AssureFileSystem : IAssureFileSystem, ISetupAssure<AssureFileSystem>
   {
      protected IFileSystem fileSystem;
      protected ILogger logger;

      public string BackupLocation { get; protected set; }
      public string FileSystemName { get; protected set; }
      public string ServerUrl { get; protected set; }

      public AssureFileSystem()
      {
         this.fileSystem = new FileSystem();
         this.logger = new ConsoleLogger();
      }

      public AssureFileSystem LogWith(ILogger logger)
      {
         this.logger = logger;

         return this;
      }

      public AssureFileSystem On(IFileSystem fileSystem)
      {
         this.fileSystem = fileSystem;

         return this;
      }
   }
}


text:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>cattle-iaas-bootstrap</artifactId>
    <parent>
        <artifactId>cattle-parent</artifactId>
        <groupId>io.cattle</groupId>
        <version>0.5.0-SNAPSHOT</version>
        <relativePath>../../parent/pom.xml</relativePath>
    </parent>
    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>cattle-iaas-api-logic</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <scope>provided</scope>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>


text:
GuiSettings::GuiSettings(Window* window, const char* title) : GuiComponent(window), mMenu(window, title)
{
	addChild(&mMenu);

	mMenu.addButton("BACK", "go back", [this] { delete this; });

	setSize((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight());
	mMenu.setPosition((mSize.x() - mMenu.getSize().x()) / 2, Renderer::getScreenHeight() * 0.15f);
}

GuiSettings::~GuiSettings()
{
	save();
}

void GuiSettings::save()
{
	if(!mSaveFuncs.size())
		return;

	for(auto it = mSaveFuncs.cbegin(); it != mSaveFuncs.cend(); it++)
		(*it)();

	Settings::getInstance()->saveFile();
}

bool GuiSettings::input(InputConfig* config, Input input)
{
	if(config->isMappedTo("b", input) && input.value != 0)
	{
		delete this;
		return true;
	}

	if(config->isMappedTo("start", input) && input.value != 0)
	{
		// close everything
		Window* window = mWindow;
		while(window->peekGui() && window->peekGui() != ViewController::get())
			delete window->peekGui();
		return true;
	}

	return GuiComponent::input(config, input);
}

HelpStyle GuiSettings::getHelpStyle()
{
	HelpStyle style = HelpStyle();
	style.applyTheme(ViewController::get()->getState().getSystem()->getTheme(), "system");
	return style;
}

std::vector<HelpPrompt> GuiSettings::getHelpPrompts()
{
	std::vector<HelpPrompt> prompts = mMenu.getHelpPrompts();

	prompts.push_back(HelpPrompt("b", "back"));
	prompts.push_back(HelpPrompt("start", "close"));

	return prompts;
}


text:
==============================
Worldmap XBlock Adapter Design
==============================
  - Robert Light
    light@alum.mit.edu

The Worldmap XBlock adapter integrates Harvard University's Worldmap_ GIS facility with the edX XBlock_ architecture.
The main piece of "glue" that allows these two disperate systems to talk to one another is the *Messaging* system that allows
the XBlock to send commands to the Worldmap client and receive messages in return.  The javascript files that implement this messaging
are xBlockCom-master.js and xBlockCom-slave.js

This facility is built on top of HTML5's cross-document messaging via Window.postMessage_ capabilities which lets a main page communicate with a page
loaded within an IFrame and vice verse.  This technology is working on all current versions of all major browsers
as documented here_.

The xBlockCom-master.js is loaded by the XBlock-based page which is either loaded via the XBlock workbench, LMS or Studio.
The worldmap itself is loaded in an IFrame within the XBlock's page-space. The xBlockCom-slave.js is loaded by the embed.html mainpage which wraps the worldmap.

There are only 2 restrictions with this architecture:

1. If the master page is running off an *http* server, the slave page (ie: worldmap) must also be served on an *http* server (likewise with https).  This is a restriction imposed by the basic javascript sandbox security rules.
2. The data contained in the message payload must not have pointers to protected resources - like the browser's sandbox resources etc.
3. Since a single edX "page" may host more than one worldmap IFrames, each slave instance must be addressed uniquely.  Hence with the xBlockCom-master.js, all communications require a uniqueId that
distinguishes one slave instance from another (we use the XBlock's unique value for *scope_ids.usage_id* to disambiguate between multiple IFrames.

.. figure:: images/worldmap-xblock-basic-layout.png
   :scale: 60

.. _Worldmap: http://worldmap.harvard.edu/
.. _XBlock: https://xblock.readthedocs.org/en/latest/
.. _Window.postMessage: http://blog.teamtreehouse.com/cross-domain-messaging-with-postmessage
.. _here: http://caniuse.com/#feat=x-doc-messaging

From the point of view of the master, to send a message to a particular slave, you use::

    MESSAGING.getInstance().send(slaveUniqId, new Message(messageType, dataFromMaster));

to setup a function to field messages from a particular slave, you use::

    MESSAGING.getInstance().addHandler(slaveUniqId, messageType, function(msg) {
        alert("the slave said: "+msg.message);
        //generally we use: dataFromSlave = JSON.parse(msg.message) to get to the data.
    });

In the slave, to setup a handler function to field messages of *messageType* from the master, you use::

    MESSAGING.getInstance().registerHandler(messageType, function(msg) {
        dataFromMaster = msg.getMessage();
    });

The slave, of course can send a message at anytime back to the master via::

    MESSAGING.getInstance().send( new Message( messageType, dataFromSlave));

The xBlockCom-master.js and xBlockCom-slave.js are written to be completely divorced from worldmap and can be used in any other
situation where communication must occur between a master page and an embeded page loaded in an IFrame.

embed.html
----------

The embed.html was extended from the original version of embed.html whose job it was to set up the configuration for worldmap and then instantiates it.
It was expanded to include all the setup of the various messaging handlers (discussed above) and then sends a message back to the master
saying "portalReady".  At that point, the master sets the position and zoom level to whatever it was the last time the student viewed this map
and initializes the visibility of the layers to their previous (or initial) values.

All message passing between master and slave is asyncronous, the calls return immediately and it is the responsibility of the slave to return
enough information to the master to understand what the slave is responding to.

TODO: we need a way to start everything out with particular layers visible.


text:
/**
 * @file
 * @brief Defines the simplest operations in scheduler. Implementation depends
 * on the scheduling algorithm. Part of method is used in other functions.
 * All of policy must implement its interface.
 *
 * @date 22.04.10
 * @author Dmitry Avdyukhin
 * @author Bulychev Anton
 *          - Description of functions
 */

#ifndef KERNEL_SCHED_SCHED_STRATEGY_H_
#define KERNEL_SCHED_SCHED_STRATEGY_H_

#include <kernel/sched/runq.h>
#include <kernel/spinlock.h>

struct runq {
	runq_t queue;
	spinlock_t lock;
};

#endif /* KERNEL_SCHED_SCHED_STRATEGY_H_ */


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<title>org.apache.nutch.urlfilter.suffix (apache-nutch 1.10 API)</title>
<meta name="date" content="2015-04-29">
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">

JavaScript is disabled on your browser.

Overview

Package

Class

Use

Tree

Deprecated

Index

Help

Prev Package

Next Package

Frames

No Frames

All Classes

Package org.apache.nutch.urlfilter.suffix

URL filter plugin to either exclude or include only URLs which match
 one of the given (path) suffixes.

See: Description

Class Summary

Class
Description

SuffixURLFilter

Filters URLs based on a file of URL suffixes.

Package org.apache.nutch.urlfilter.suffix Description

URL filter plugin to either exclude or include only URLs which match
 one of the given (path) suffixes.

Overview

Package

Class

Use

Tree

Deprecated

Index

Help

Prev Package

Next Package

Frames

No Frames

All Classes

Copyright © 2015 The Apache Software Foundation


text:
class Devise::MailerPreview < ActionMailer::Preview
  # hit http://localhost:3000/rails/mailers/devise/mailer/confirmation_instructions
  def confirmation_instructions
    Devise::Mailer.confirmation_instructions(User.first, {})
  end

  # hit http://localhost:3000/rails/mailers/devise/mailer/reset_password_instructions
  def reset_password_instructions
    Devise::Mailer.reset_password_instructions(User.first, {})
  end

  # hit http://localhost:3000/rails/mailers/devise/mailer/unlock_instructions
  def unlock_instructions
    Devise::Mailer.unlock_instructions(User.first, {})
  end
end


text:
<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Storage;
use App\Models\Batch;
use App\Http\Requests\UploadRequest;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class UploadController extends Controller
{
    /**
     * @var Batch
     */
    protected $batch;

    /**
     * Constructor.
     *
     * @param Batch $batch
     */
    public function __construct(Batch $batch)
    {
        $this->batch = $batch;
    }

    /**
     * Uploads files to the specified batch.
     *
     * @param UploadRequest $request
     * @param string $uuid
     */
    public function perform(UploadRequest $request, $uuid)
    {
        // Locate the batch
        $batch = $this->batch->locate($uuid);

        // Retrieve the file from the request
        $file = $request->file('file');

        // Double check the file instance
        if($file instanceof UploadedFile) {

            // Validate file name length
            if(strlen($file->getClientOriginalName()) > 70) {
                abort(422, 'File name is too large');
            }

            // Generate a file name with UUID and its extension
            $name = uuid() . "." . $file->getClientOriginalExtension();

            // Get the storage path
            $path = $batch->uuid . DIRECTORY_SEPARATOR . $name;

            // Move the file into storage
            Storage::put(
                $path,
                file_get_contents($file->getRealPath())
            );

            // Add the file to the batch
            $batch->addFile($file->getClientOriginalName(), $file->getClientMimeType(), $file->getClientSize(), $path);
        } else {
            abort(404);
        }
    }
}


text:
<?php

namespace core;

use libraries\utils\UnicodeHandler;
use \libraries\utils\YAMLParser;
use Monolog\Logger;

/**
 * PHP REST Client
 * https://github.com/tcdent/php-restclient
 * (c) 2013 Travis Dent <tcdent@gmail.com>
 */
class RestClientException extends \Exception {

}

class RestClient implements \Iterator, \ArrayAccess {

    public $options;
    public $handle; // cURL resource handle.
    // Populated after execution:
    public $response; // Response body.
    public $headers; // Parsed response header object.
    public $info; // Response info object.
    public $error; // Response error string.
    // Populated as-needed.
    public $decoded_response; // Decoded response body.
    private $iterator_positon;
    private $logger = null;
    private $lastUrl = '';

    public function __construct($options = array()) {

        $default_options = array(
            'headers' => array(),
            'parameters' => array(),
            'curl_options' => array(),
            'user_agent' => "PHP RestClient/0.1.4",
            'base_url' => NULL,
            'format' => NULL,
            'format_regex' => "/(\w+)\/(\w+)(;[.+])?/",
            'decoders' => array(
                'json' => 'json_decode',
                'php' => 'unserialize'
            ),
            'username' => NULL,
            'password' => NULL
        );

        $this->options = array_merge($default_options, $options);
        if (array_key_exists('decoders', $options))
            $this->options['decoders'] = array_merge(
                    $default_options['decoders'], $options['decoders']);
    }

    public function set_option($key, $value) {
        $this->options[$key] = $value;
    }

    public function register_decoder($format, $method) {
        // Decoder callbacks must adhere to the following pattern:
        //   array my_decoder(string $data)
        $this->options['decoders'][$format] = $method;
    }

    // Iterable methods:
    public function rewind() {
        $this->decode_response();
        return reset($this->decoded_response);
    }

    public function current() {
        return current($this->decoded_response);
    }

    public function key() {
        return key($this->decoded_response);
    }

    public function next() {
        return next($this->decoded_response);
    }

    public function valid() {
        return is_array($this->decoded_response) && (key($this->decoded_response) !== NULL);
    }

    // ArrayAccess methods:
    public function offsetExists($key) {
        $this->decode_response();
        return is_array($this->decoded_response) ?
                isset($this->decoded_response[$key]) : isset($this->decoded_response->{$key});
    }

    public function offsetGet($key) {
        $this->decode_response();
        if (!$this->offsetExists($key))
            return NULL;

        return is_array($this->decoded_response) ?
                $this->decoded_response[$key] : $this->decoded_response->{$key};
    }

    public function offsetSet($key, $value) {
        throw new RestClientException("Decoded response data is immutable.");
    }

    public function offsetUnset($key) {
        throw new RestClientException("Decoded response data is immutable.");
    }

    // Request methods:
    public function get($url, $parameters = array(), $headers = array()) {
        return $this->execute($url, 'GET', $parameters, $headers);
    }

    public function post($url, $parameters = array(), $headers = array()) {
        return $this->execute($url, 'POST', $parameters, $headers);
    }

    public function put($url, $parameters = array(), $headers = array()) {
        return $this->execute($url, 'PUT', $parameters, $headers);
    }

    public function delete($url, $parameters = array(), $headers = array()) {
        return $this->execute($url, 'DELETE', $parameters, $headers);
    }

    public function execute($url, $method = 'GET', $parameters = array(), $headers = array()) {

        $client = clone $this;
       // $client = $this;
        $client->url = $url;
        $client->handle = curl_init();
        $curlopt = array(
            CURLOPT_HEADER => TRUE,
            CURLOPT_RETURNTRANSFER => TRUE,
            CURLOPT_USERAGENT => $client->options['user_agent'],
            CURLOPT_ENCODING => ''
        );

        if ($client->options['username'] && $client->options['password'])
            $curlopt[CURLOPT_USERPWD] = sprintf("%s:%s", $client->options['username'], $client->options['password']);

        if (count($client->options['headers']) || count($headers)) {
            $curlopt[CURLOPT_HTTPHEADER] = array();
            $headers = array_merge($client->options['headers'], $headers);
            foreach ($headers as $key => $value) {
                $curlopt[CURLOPT_HTTPHEADER][] = sprintf("%s:%s", $key, $value);
            }
        }

//        if ($client->options['format'])
//            $client->url .= '.' . $client->options['format'];
//
        // Allow passing parameters as a pre-encoded string (or something that
        // allows casting to a string). Parameters passed as strings will not be
        // merged with parameters specified in the default options.
        if (is_array($parameters)) {
            $parameters = array_merge($client->options['parameters'], $parameters);
            $parameters_string = $client->format_query($parameters);
        } else
            $parameters_string = (string) $parameters;

        if (strtoupper($method) == 'POST') {
            $curlopt[CURLOPT_POST] = TRUE;
            $curlopt[CURLOPT_POSTFIELDS] = $client->format_query($parameters);
        } elseif (strtoupper($method) != 'GET') {
            $curlopt[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
            $curlopt[CURLOPT_POSTFIELDS] = $client->format_query($parameters_string);
        } elseif ($parameters_string) {
            $client->url .= strpos($client->url, '?') ? '&' : '?';

            $client->url .= $this->buildGetParameters($parameters);
        }

        if ($client->options['base_url']) {
            if ($client->url[0] != '/' && substr($client->options['base_url'], -1) != '/')
                $client->url = '/' . $client->url;
            $client->url = $client->options['base_url'] . $client->url;
        }
        $curlopt[CURLOPT_URL] = $client->url;

        if ($client->options['curl_options']) {
            // array_merge would reset our numeric keys.
            foreach ($client->options['curl_options'] as $key => $value) {
                $curlopt[$key] = $value;
            }
        }

        $this->lastUrl = $client->url;

        curl_setopt_array($client->handle, $curlopt);

        $client->parse_response(curl_exec($client->handle));

        $client->info = (object) curl_getinfo($client->handle);
        $client->error = curl_error($client->handle);

        curl_close($client->handle);

       // pr($client);
        return $client;
    }

    public function parse_response($response) {
        $headers = array();
        $http_ver = strtok($response, "\n");

        while ($line = strtok("\n")) {
            if (strlen(trim($line)) == 0)
                break;

            list($key, $value) = explode(':', $line, 2);
            $key = trim(strtolower(str_replace('-', '_', $key)));
            $value = trim($value);
            if (empty($headers[$key]))
                $headers[$key] = $value;
            elseif (is_array($headers[$key]))
                $headers[$key][] = $value;
            else
                $headers[$key] = array($headers[$key], $value);
        }

        $this->headers = (object) $headers;
        $this->response = strtok("");
    }

    public function get_response_format() {
        if (!$this->response)
            throw new RestClientException(
            "A response must exist before it can be decoded.");

        // User-defined format.
        if (!empty($this->options['format']))
            return $this->options['format'];

        // Extract format from response content-type header.
        if (!empty($this->headers->content_type))
            if (preg_match($this->options['format_regex'], $this->headers->content_type, $matches))
                return $matches[2];

        throw new RestClientException(
        "Response format could not be determined.");
    }

    public function decode_response() {

        //fix for odd bug where header info gets cast into response data
        if (strpos($this->response, '{') !== false) {

            $tmp = explode('{', $this->response, 2);
            $this->response = '{' . $tmp[1];
        }

        if (empty($this->decoded_response)) {
            $format = $this->get_response_format();
            if (!array_key_exists($format, $this->options['decoders']))
                throw new RestClientException("'${format}' is not a supported " .
                "format, register a decoder to handle this response.");

            $this->decoded_response = call_user_func(
                    $this->options['decoders'][$format], $this->response);
        }

        $uh = new UnicodeHandler($this->logger, $this->loadEncodingConfiguration());

        $this->decoded_response = $uh->decode($this->decoded_response);

        //$this->decoded_response = $uh->decode($this->decoded_response);
        unset($uh);
        $result = json_decode(json_encode($this->decoded_response), true);

        return $result;
    }

    // additions
    // to curl upload an image:
//$ch = curl_init();
//$data = array('name' => 'Foo', 'file' => '@/path/to/image.jpeg');
//curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
//curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//curl_exec($ch);

    public function setLogger(Logger $logger) {
        $this->logger = $logger;
    }

    private function buildGetParameters($parameters) {
        $uh = new UnicodeHandler($this->logger, $this->loadEncodingConfiguration());

        $parameters = $uh->encode($parameters); // $this->formatToHexForSending($parameters);
        unset($uh);
        $retval = '';
        foreach ($parameters as $key => $value) {
            if (is_array($value)) {
                foreach ($value as $subkey => $subvalue) {
                    //for now, lets just assume no one sends an array as get params deeper than 1 level
                    $retval .= "&" . trim($subkey) . "=" . urlencode($subvalue);
                }
            } else {
                $retval .= "&" . trim($key) . "=" . urlencode($value);
            }
        }

        return substr($retval, 1);
    }

    public function format_query($parameters, $primary = '=', $secondary = '&') {
        $query = "";
        $uh = new UnicodeHandler($this->logger, $this->loadEncodingConfiguration());

        if (is_array($parameters)) {
            $parameters = $uh->encode($parameters); // $this->formatToHexForSending($parameters);
        }

        unset($uh);

        foreach ($parameters as $key => $value) {
            if (is_array($value)) {
                $value = json_encode($value);
            }

            $pair = array(($key), ($value));
            $query .= implode($primary, $pair) . $secondary;
        }

        return rtrim($query, $secondary);
    }

    private function loadEncodingConfiguration() {

        $loader = new YAMLParser($this->logger);
        //check to see if it's a core component, then add 'core' to the path if yes
        $loader->setFilePath(__SITE_PATH . DIRECTORY_SEPARATOR . __NAMESPACE . DIRECTORY_SEPARATOR . ((strpos(__NAMESPACE, 'framework') !== false) ? 'core' . DIRECTORY_SEPARATOR : '') .
                __COMPONENT_FOLDER . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'encodings.yml');
        $config = $loader->loadConfig();

        unset($loader);

        return $config;
    }

    private function text2bin($parameters) {
        $retval = array();
        $bin = '';
        foreach ($parameters as $key => $value) {
            if (is_array($value)) {
                $retval[$key] = $this->text2bin($value);
            } else {
                $len = strlen($value);

                for ($i = 0; $i < $len; $i++) {
                    $bin .= strlen(decbin(ord($value[$i]))) < 8 ? str_pad(decbin(ord($value[$i])), 8, 0, STR_PAD_LEFT) : decbin(ord($value[$i]));
                }
                $retval[$key] = $bin;
            }
        }

        return $retval;
    }

    private function convertObjectToArray($object) {
        $retval = array();
        if (!is_object($object) && !is_array($object)) {
            return $object;
        }
        if (is_object($object)) {
            $object = get_object_vars($object);
        }
        foreach ($object as $key => $value) {
            if (is_object($value) || is_array($value)) {
                $retval[$key] = $this->convertObjectToArray($value);
            } else {
                $retval[$key] = $value;
            }
        }

        return $retval;
    }

    private function formatToAsciiAfterReceiving($parameters) {
        $retval = array();

        if (!is_array($parameters)) {
            return $this->hex2ascii($parameters);
        }
        foreach ($parameters as $key => $value) {
            if (is_array($value)) {
                $retval[$key] = $this->formatToAsciiAfterReceiving($value);
            } else {
                $retval[$key] = $this->hex2ascii($value);
            }
        }

        return $retval;
    }

    private function formatToHexForSending($parameters) {
        $retval = array();
        if (!is_array($parameters)) {
            return $this->ascii2hex($parameters);
        }
        foreach ($parameters as $key => $value) {
            if (is_array($parameters)) {
                $retval[$key] = $this->formatToHexForSending($value);
            } else {
                $retval[$key] = $this->ascii2hex($value);
            }
        }

        return $retval;
    }

    private function ascii2hex($ascii) {
        //add quotes to cast as string from int value otherwise this cheap-oh method falls over
        $ascii = "" . $ascii;
        $hex = '';
        for ($i = 0; $i < strlen($ascii); $i++) {
            $byte = strtoupper(dechex(ord($ascii{$i})));
            $byte = str_repeat('0', 2 - strlen($byte)) . $byte;
            $hex .= $byte . " ";
        }

        return $hex;
    }

    private function hex2ascii($hex) {
        if (is_object($hex)) {
            return $hex;
        }
        $ascii = '';
        $hex = str_replace(" ", "", $hex);
        for ($i = 0; $i < strlen($hex); $i = $i + 2) {
            $ascii.=chr(hexdec(substr($hex, $i, 2)));
        }
        return($ascii);
    }

}


text:
// Autogenerated from vk-api-schema. Please don't edit it manually.
package com.vk.api.sdk.objects.ads;

import com.google.gson.Gson;
import com.google.gson.JsonPrimitive;
import com.google.gson.annotations.SerializedName;
import com.vk.api.sdk.objects.Validable;
import com.vk.api.sdk.objects.annotations.Required;
import com.vk.api.sdk.objects.base.BoolInt;
import java.util.Objects;

/**
 * Ad object
 */
public class Ad implements Validable {
    /**
     * Ad format
     */
    @SerializedName("ad_format")
    private Integer adFormat;

    /**
     * Ad platform
     */
    @SerializedName("ad_platform")
    private JsonPrimitive adPlatform;

    /**
     * Total limit
     */
    @SerializedName("all_limit")
    private Integer allLimit;

    @SerializedName("approved")
    @Required
    private AdApproved approved;

    /**
     * Campaign ID
     */
    @SerializedName("campaign_id")
    private Integer campaignId;

    /**
     * Category ID
     */
    @SerializedName("category1_id")
    private Integer category1Id;

    /**
     * Additional category ID
     */
    @SerializedName("category2_id")
    private Integer category2Id;

    @SerializedName("cost_type")
    private AdCostType costType;

    /**
     * Cost of a click, kopecks
     */
    @SerializedName("cpc")
    private Integer cpc;

    /**
     * Cost of 1000 impressions, kopecks
     */
    @SerializedName("cpm")
    private Integer cpm;

    /**
     * Cost of an action, kopecks
     */
    @SerializedName("cpa")
    private Integer cpa;

    /**
     * Cost of 1000 impressions optimized, kopecks
     */
    @SerializedName("ocpm")
    private Integer ocpm;

    /**
     * Max cost of target actions for autobidding, kopecks
     */
    @SerializedName("autobidding_max_cost")
    private Integer autobiddingMaxCost;

    /**
     * Information whether disclaimer is enabled
     */
    @SerializedName("disclaimer_medical")
    private BoolInt disclaimerMedical;

    /**
     * Information whether disclaimer is enabled
     */
    @SerializedName("disclaimer_specialist")
    private BoolInt disclaimerSpecialist;

    /**
     * Information whether disclaimer is enabled
     */
    @SerializedName("disclaimer_supplements")
    private BoolInt disclaimerSupplements;

    /**
     * Ad ID
     */
    @SerializedName("id")
    @Required
    private Integer id;

    /**
     * Impressions limit
     */
    @SerializedName("impressions_limit")
    private Integer impressionsLimit;

    /**
     * Information whether impressions are limited
     */
    @SerializedName("impressions_limited")
    private BoolInt impressionsLimited;

    /**
     * Ad title
     */
    @SerializedName("name")
    @Required
    private String name;

    @SerializedName("status")
    @Required
    private AdStatus status;

    /**
     * Information whether the ad is a video
     */
    @SerializedName("video")
    private BoolInt video;

    public Integer getAdFormat() {
        return adFormat;
    }

    public Ad setAdFormat(Integer adFormat) {
        this.adFormat = adFormat;
        return this;
    }

    public JsonPrimitive getAdPlatform() {
        return adPlatform;
    }

    public Ad setAdPlatform(JsonPrimitive adPlatform) {
        this.adPlatform = adPlatform;
        return this;
    }

    public Integer getAllLimit() {
        return allLimit;
    }

    public Ad setAllLimit(Integer allLimit) {
        this.allLimit = allLimit;
        return this;
    }

    public AdApproved getApproved() {
        return approved;
    }

    public Ad setApproved(AdApproved approved) {
        this.approved = approved;
        return this;
    }

    public Integer getCampaignId() {
        return campaignId;
    }

    public Ad setCampaignId(Integer campaignId) {
        this.campaignId = campaignId;
        return this;
    }

    public Integer getCategory1Id() {
        return category1Id;
    }

    public Ad setCategory1Id(Integer category1Id) {
        this.category1Id = category1Id;
        return this;
    }

    public Integer getCategory2Id() {
        return category2Id;
    }

    public Ad setCategory2Id(Integer category2Id) {
        this.category2Id = category2Id;
        return this;
    }

    public AdCostType getCostType() {
        return costType;
    }

    public Ad setCostType(AdCostType costType) {
        this.costType = costType;
        return this;
    }

    public Integer getCpc() {
        return cpc;
    }

    public Ad setCpc(Integer cpc) {
        this.cpc = cpc;
        return this;
    }

    public Integer getCpm() {
        return cpm;
    }

    public Ad setCpm(Integer cpm) {
        this.cpm = cpm;
        return this;
    }

    public Integer getCpa() {
        return cpa;
    }

    public Ad setCpa(Integer cpa) {
        this.cpa = cpa;
        return this;
    }

    public Integer getOcpm() {
        return ocpm;
    }

    public Ad setOcpm(Integer ocpm) {
        this.ocpm = ocpm;
        return this;
    }

    public Integer getAutobiddingMaxCost() {
        return autobiddingMaxCost;
    }

    public Ad setAutobiddingMaxCost(Integer autobiddingMaxCost) {
        this.autobiddingMaxCost = autobiddingMaxCost;
        return this;
    }

    public boolean isDisclaimerMedical() {
        return disclaimerMedical == BoolInt.YES;
    }

    public BoolInt getDisclaimerMedical() {
        return disclaimerMedical;
    }

    public boolean isDisclaimerSpecialist() {
        return disclaimerSpecialist == BoolInt.YES;
    }

    public BoolInt getDisclaimerSpecialist() {
        return disclaimerSpecialist;
    }

    public boolean isDisclaimerSupplements() {
        return disclaimerSupplements == BoolInt.YES;
    }

    public BoolInt getDisclaimerSupplements() {
        return disclaimerSupplements;
    }

    public Integer getId() {
        return id;
    }

    public Ad setId(Integer id) {
        this.id = id;
        return this;
    }

    public Integer getImpressionsLimit() {
        return impressionsLimit;
    }

    public Ad setImpressionsLimit(Integer impressionsLimit) {
        this.impressionsLimit = impressionsLimit;
        return this;
    }

    public boolean isImpressionsLimited() {
        return impressionsLimited == BoolInt.YES;
    }

    public BoolInt getImpressionsLimited() {
        return impressionsLimited;
    }

    public String getName() {
        return name;
    }

    public Ad setName(String name) {
        this.name = name;
        return this;
    }

    public AdStatus getStatus() {
        return status;
    }

    public Ad setStatus(AdStatus status) {
        this.status = status;
        return this;
    }

    public boolean isVideo() {
        return video == BoolInt.YES;
    }

    public BoolInt getVideo() {
        return video;
    }

    @Override
    public int hashCode() {
        return Objects.hash(autobiddingMaxCost, cpm, impressionsLimit, ocpm, allLimit, campaignId, category2Id, disclaimerMedical, category1Id, disclaimerSpecialist, video, disclaimerSupplements, adPlatform, approved, impressionsLimited, cpa, costType, cpc, name, id, adFormat, status);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Ad ad = (Ad) o;
        return Objects.equals(adFormat, ad.adFormat) &&
                Objects.equals(cpm, ad.cpm) &&
                Objects.equals(ocpm, ad.ocpm) &&
                Objects.equals(autobiddingMaxCost, ad.autobiddingMaxCost) &&
                Objects.equals(costType, ad.costType) &&
                Objects.equals(video, ad.video) &&
                Objects.equals(allLimit, ad.allLimit) &&
                Objects.equals(category1Id, ad.category1Id) &&
                Objects.equals(impressionsLimited, ad.impressionsLimited) &&
                Objects.equals(approved, ad.approved) &&
                Objects.equals(disclaimerSpecialist, ad.disclaimerSpecialist) &&
                Objects.equals(adPlatform, ad.adPlatform) &&
                Objects.equals(cpa, ad.cpa) &&
                Objects.equals(cpc, ad.cpc) &&
                Objects.equals(impressionsLimit, ad.impressionsLimit) &&
                Objects.equals(name, ad.name) &&
                Objects.equals(id, ad.id) &&
                Objects.equals(disclaimerSupplements, ad.disclaimerSupplements) &&
                Objects.equals(campaignId, ad.campaignId) &&
                Objects.equals(category2Id, ad.category2Id) &&
                Objects.equals(disclaimerMedical, ad.disclaimerMedical) &&
                Objects.equals(status, ad.status);
    }

    @Override
    public String toString() {
        final Gson gson = new Gson();
        return gson.toJson(this);
    }

    public String toPrettyString() {
        final StringBuilder sb = new StringBuilder("Ad{");
        sb.append("adFormat=").append(adFormat);
        sb.append(", cpm=").append(cpm);
        sb.append(", ocpm=").append(ocpm);
        sb.append(", autobiddingMaxCost=").append(autobiddingMaxCost);
        sb.append(", costType=").append(costType);
        sb.append(", video=").append(video);
        sb.append(", allLimit=").append(allLimit);
        sb.append(", category1Id=").append(category1Id);
        sb.append(", impressionsLimited=").append(impressionsLimited);
        sb.append(", approved=").append(approved);
        sb.append(", disclaimerSpecialist=").append(disclaimerSpecialist);
        sb.append(", adPlatform=").append(adPlatform);
        sb.append(", cpa=").append(cpa);
        sb.append(", cpc=").append(cpc);
        sb.append(", impressionsLimit=").append(impressionsLimit);
        sb.append(", name='").append(name).append("'");
        sb.append(", id=").append(id);
        sb.append(", disclaimerSupplements=").append(disclaimerSupplements);
        sb.append(", campaignId=").append(campaignId);
        sb.append(", category2Id=").append(category2Id);
        sb.append(", disclaimerMedical=").append(disclaimerMedical);
        sb.append(", status=").append(status);
        sb.append('}');
        return sb.toString();
    }
}


text:
<?php

use Anomaly\Streams\Platform\Database\Migration\Migration;

class FannanModuleLotteryCreateLotteryStream extends Migration
{

    /**
     * The stream definition.
     *
     * @var array
     */
    protected $stream = [
        'slug' => 'lottery',
         'title_column' => 'lottery_title',
         'translatable' => true,
         'trashable' => false,
         'searchable' => false,
         'sortable' => false,
    ];

    /**
     * The stream assignments.
     *
     * @var array
     */
    protected $assignments = [
        'lottery_title' => [
            'required' => true,
        ],
        'lottery_start' => [
            'required' => true,
        ],
        'lottery_end' => [
            'required' => true,
        ],
        'lottery_times' => [
            'required' => true,
        ],
        'lottery_is_ticket',
        'lottery_is_open',
        'lottery_description',
        'lottery_rule'
    ];

}


text:
 Edit accommodation type

<form #f = "ngForm" (ngSubmit)="onSubmit()" ngNativeValidate>

     Id:
    <input type="text" id="Id" name="Id" [(ngModel)]="Id" disabled>

     Name:
    <input type="text" id="Name" name="Name" [(ngModel)]="Name" required>

    <input type="submit" value="Edit">

</form>

 {{Message}}


text:
import React from 'react'

class Footer extends React.PureComponent {
  render () {
    return

        Copyright 2017 Junyoung Choi. Source code of Carbonstack is published under MIT.

  }
}

export default Footer


text: This is a semester project for the Yelp Dataset challenge (http://www.yelp.com/dataset_challenge).


text:
package com.stratio.explorer.testsAT.specs;

public class ThenSpec extends BaseSpec {

    public ThenSpec(Common spec) {
        this.commonspec = spec;
    }
}


text:
layout    : post
title     :  "Meu primeiro post"
date      :   2014-06-24 13:00:00
categories: desenvolvimento php javascript
tags      : [php, javascript]
author    : williamurbano
---

# Lorem ipsum

Lorem ipsum Ut labore dolor incididunt tempor nisi amet fugiat fugiat quis anim et nulla esse laborum ad Ut anim aliqua anim ullamco sint aliqua anim et sunt ut anim nisi eiusmod elit aute laboris fugiat non consequat in laboris veniam ullamco velit in nostrud non aliquip pariatur fugiat non esse minim aliqua ullamco dolore est dolore cillum commodo aliqua commodo mollit enim ad Duis dolor Excepteur esse nulla veniam do ea magna occaecat veniam et labore occaecat adipisicing sunt commodo magna consequat est fugiat sit laborum laboris do qui consectetur sunt anim Excepteur aliquip amet amet in commodo labore nulla cillum dolor in do irure in dolore laboris Ut eiusmod eu sed nostrud dolor sint nisi pariatur elit Duis quis ea adipisicing consequat dolore nisi dolor labore esse eiusmod dolore do culpa dolor et ut et aliquip ad aliquip quis in consectetur ea magna voluptate laborum.

:smile:
:apple:

```javascript
;(function($, document, window) {
  $.fn.customCss = function(options) {
    var settings,
        defaults = {
          option1: 'value1',
          option2: 'value2',
          option3: {
            suboption31: 'value31'
          }
        };

    settings = $.extend({}, true, defaults, options);

    return this.each(function() {
      $(this).css(settings);
    });
  }
})(jQuery, document, window, undefined);
```


text:
include $(srctree)/drivers/misc/mediatek/Makefile.custom

obj-$(CONFIG_MTK_VIBRATOR) := vibrator_drv.o

obj-y += $(subst ",,$(CONFIG_MTK_PLATFORM))/

#ccflags-y := -Idrivers/staging/android/


text:
using System;
using System.Web.Mvc;

namespace MvcController.Extensions
{
  [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
  public class CheckOrderAttribute : ActionFilterAttribute
  {

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
      filterContext.HttpContext.Response.Write(
          String.Format("
OnActionExecuting{0}
", this.Order));
      //if (this.Order == 2)
      //{
      //  throw new Exception("例外発生");
      //}
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
      filterContext.HttpContext.Response.Write(
          String.Format("
OnActionExecuted{0}
", this.Order));
      //filterContext.ExceptionHandled = true;
    }

    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
      filterContext.HttpContext.Response.Write(
          String.Format("
OnResultExecuting{0}
", this.Order));
    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
      filterContext.HttpContext.Response.Write(
          String.Format("
OnResultExecuted{0}
", this.Order));
    }
  }
}


text:
<?php

namespace spec\Crummy\Phlack\Bot\Mainframe\Plugin;

use Crummy\Phlack\Bot\Mainframe\Packet;
use Crummy\Phlack\Common\Formatter\FormatterInterface;
use Crummy\Phlack\WebHook\Reply\Reply;
use PhpSpec\ObjectBehavior;

class EncoderPluginSpec extends ObjectBehavior
{
    function let(FormatterInterface $formatter)
    {
        $this->beConstructedWith($formatter);
    }

    function it_is_a_Mainframe_Plugin()
    {
        $this->shouldHaveType('Crummy\Phlack\Bot\Mainframe\Plugin\EncoderPlugin');
        $this->shouldImplement('\Crummy\Phlack\Bot\Mainframe\Plugin\PluginInterface');
    }

    function it_fires_after_the_command_is_executed(Packet $packet)
    {
        $this->onAfterExecute($packet)->shouldReturn($packet);
    }

    function it_encodes_the_output(FormatterInterface $formatter, Packet $packet, Reply $reply)
    {
        $unencoded = '<foo!> <#C01010|botgarage>';
        $encoded = '<foo!> <#C01010|botgarage>';

        $packet->offsetExists('output')->willReturn(true);
        $packet->offsetGet('output')->willReturn($reply);

        $reply->offsetExists('text')->willReturn(true);
        $reply->offsetGet('text')->willReturn($unencoded);

        $formatter->format($unencoded)->willReturn($encoded);
        $reply->offsetSet('text', $encoded)->shouldBeCalled();

        $this->onAfterExecute($packet);
    }
}


text:
title: TechCamp Memphis 2014 Tickets on Sale
tags:
    - memphis
    - techcamp
categories:
    - events
author:
    - Joe
---
Memphis Technology Foundation is proud to be a Platinum Sponsor of TechCamp Memphis 2014.

Tickets are now on sale for [TechCamp Memphis 2014](http://techcampmemphis.com/)

Sell Tickets through Eventbrite


text:
<?php

namespace NilPortugues\Tests\Api\Dummy\ComplexObject;

use NilPortugues\Tests\Api\Dummy\ComplexObject\ValueObject\PostId;
use NilPortugues\Tests\Api\Dummy\ComplexObject\ValueObject\UserId;

class PostLite
{
    /**
     * @var PostId
     */
    protected $postId;
    /**
     * @var
     */
    protected $title;
    /**
     * @var
     */
    protected $content;
    /**
     * @var User
     */
    protected $author;

    /**
     * @param PostId $id
     * @param $title
     * @param $content
     * @param User $user
     */
    public function __construct(PostId $id, $title, $content, User $user)
    {
        $this->postId = $id;
        $this->title = $title;
        $this->content = $content;
        $this->author = $user;
    }

    /**
     * @return mixed
     */
    public function getContent()
    {
        return $this->content;
    }

    /**
     * @return PostId
     */
    public function getPostId()
    {
        return $this->postId;
    }

    /**
     * @return mixed
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * @return UserId
     */
    public function getUserId()
    {
        return $this->author;
    }
}


text:
//
// TBUntappableCell.h
// TableViewBuddy
//
// Copyright (c) 2014-2016 Hironori Ichimiya <hiron@hironytic.com>
//
// 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.
//

#import "TBTableViewCell.h"

/**
 `TBUntappableCell` represents a cell that does not respond to tap.
 */
@interface TBUntappableCell : TBTableViewCell

@end


text:
package com.github.xdcrafts.flower.spring.impl;

import com.github.xdcrafts.flower.core.Extension;
import com.github.xdcrafts.flower.core.Selector;
import com.github.xdcrafts.flower.core.impl.DefaultFeature;
import org.springframework.beans.factory.annotation.Required;

import java.util.Map;

/**
 * Spring factory bean for default feature that uses bean name as it's name..
 */
public class DefaultFeatureFactory extends AbstractNameAwareFactoryBean<DefaultFeature> {

    private Map<Extension, Selector> extensions;

    @Required
    public void setExtensions(Map<Extension, Selector> extensions) {
        this.extensions = extensions;
    }

    @Override
    public Class<?> getObjectType() {
        return DefaultFeature.class;
    }

    @Override
    protected DefaultFeature createInstance() throws Exception {
        return new DefaultFeature(getBeanName(), this.extensions);
    }
}


text:
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>fr.romainmoreau.epaper</groupId>
		<artifactId>e-paper</artifactId>
		<version>0.1.0-SNAPSHOT</version>
	</parent>
	<artifactId>e-paper-jaxb-api</artifactId>
	<dependencies>
		<dependency>
			<groupId>fr.romainmoreau.epaper</groupId>
			<artifactId>e-paper-client-api</artifactId>
		</dependency>
		<dependency>
			<groupId>jakarta.xml.bind</groupId>
			<artifactId>jakarta.xml.bind-api</artifactId>
		</dependency>
		<dependency>
			<groupId>org.glassfish.jaxb</groupId>
			<artifactId>jaxb-runtime</artifactId>
		</dependency>
	</dependencies>
</project>


text:
> The canonical implementation of node-task's [Record specification](https://github.com/node-task/spec/wiki/Record)

## Usage

```js
var Record = require('record');

var record = new Record({
  path: 'path/to/record',
  encoding: 'utf8',
  contents: 'i will be turned into a buffer'
});
```

### Record

#### record.path

Path to record.

Type: `String`
Default: `null`

#### record.contents

Record contents.

Type: `Buffer, Stream, or null`
Default: `null`

#### record.encoding

Default encoding to be used with `toString()`.

## API

### type()

Return the type of Record. (Null, Buffer, ReadableStream, etc).

### isNull()

Return true if the record contents are null.

### isBuffer()

Return true if the record contents are a Buffer.

### isStream()

Return true if the record contents are a Stream.

### clone()

Return a clone of the record.

### toString()

Calls `toString()` on `record.contents`.

- _`record.contents` must already be a buffer, or an error is thrown._
- If no encoding is provided, the Record's optional encoding property will be used.
- If neither is available, will default to `utf8` encoding.

### pipe(stream[, opt])

- If the record contents are a Buffer, it will be written to the provided stream.
- If the record contents are a Stream, pipe them to the provided stream.
- If the record contents are null, this will do nothing.
- If `opt.end` is true, [the destination stream will not be ended](http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options).

Returns the provided stream.


text:
package certificate

import (
	"crypto/x509"
	"crypto/x509/pkix"
	"fmt"
	"net"
	"sort"

	"github.com/prometheus/client_golang/prometheus"

	certificates "k8s.io/api/certificates/v1beta1"
	"k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/types"
	clientset "k8s.io/client-go/kubernetes"
	clientcertificates "k8s.io/client-go/kubernetes/typed/certificates/v1beta1"
	"k8s.io/client-go/util/certificate"
	kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
	"k8s.io/kubernetes/pkg/kubelet/metrics"
)

// NewKubeletServerCertificateManager creates a certificate manager for the kubelet when retrieving a server certificate
// or returns an error.
func NewKubeletServerCertificateManager(kubeClient clientset.Interface, kubeCfg *kubeletconfig.KubeletConfiguration, nodeName types.NodeName, getAddresses func() []v1.NodeAddress, certDirectory string) (certificate.Manager, error) {
	var certSigningRequestClient clientcertificates.CertificateSigningRequestInterface
	if kubeClient != nil && kubeClient.CertificatesV1beta1() != nil {
		certSigningRequestClient = kubeClient.CertificatesV1beta1().CertificateSigningRequests()
	}
	certificateStore, err := certificate.NewFileStore(
		"kubelet-server",
		certDirectory,
		certDirectory,
		kubeCfg.TLSCertFile,
		kubeCfg.TLSPrivateKeyFile)
	if err != nil {
		return nil, fmt.Errorf("failed to initialize server certificate store: %v", err)
	}
	var certificateExpiration = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Namespace: metrics.KubeletSubsystem,
			Subsystem: "certificate_manager",
			Name:      "server_expiration_seconds",
			Help:      "Gauge of the lifetime of a certificate. The value is the date the certificate will expire in seconds since January 1, 1970 UTC.",
		},
	)
	prometheus.MustRegister(certificateExpiration)

	getTemplate := func() *x509.CertificateRequest {
		hostnames, ips := addressesToHostnamesAndIPs(getAddresses())
		// don't return a template if we have no addresses to request for
		if len(hostnames) == 0 && len(ips) == 0 {
			return nil
		}
		return &x509.CertificateRequest{
			Subject: pkix.Name{
				CommonName:   fmt.Sprintf("system:node:%s", nodeName),
				Organization: []string{"system:nodes"},
			},
			DNSNames:    hostnames,
			IPAddresses: ips,
		}
	}

	m, err := certificate.NewManager(&certificate.Config{
		CertificateSigningRequestClient: certSigningRequestClient,
		GetTemplate:                     getTemplate,
		Usages: []certificates.KeyUsage{
			// https://tools.ietf.org/html/rfc5280#section-4.2.1.3
			//
			// Digital signature allows the certificate to be used to verify
			// digital signatures used during TLS negotiation.
			certificates.UsageDigitalSignature,
			// KeyEncipherment allows the cert/key pair to be used to encrypt
			// keys, including the symmetric keys negotiated during TLS setup
			// and used for data transfer.
			certificates.UsageKeyEncipherment,
			// ServerAuth allows the cert to be used by a TLS server to
			// authenticate itself to a TLS client.
			certificates.UsageServerAuth,
		},
		CertificateStore:      certificateStore,
		CertificateExpiration: certificateExpiration,
	})
	if err != nil {
		return nil, fmt.Errorf("failed to initialize server certificate manager: %v", err)
	}
	return m, nil
}

func addressesToHostnamesAndIPs(addresses []v1.NodeAddress) (dnsNames []string, ips []net.IP) {
	seenDNSNames := map[string]bool{}
	seenIPs := map[string]bool{}
	for _, address := range addresses {
		if len(address.Address) == 0 {
			continue
		}

		switch address.Type {
		case v1.NodeHostName:
			if ip := net.ParseIP(address.Address); ip != nil {
				seenIPs[address.Address] = true
			} else {
				seenDNSNames[address.Address] = true
			}
		case v1.NodeExternalIP, v1.NodeInternalIP:
			if ip := net.ParseIP(address.Address); ip != nil {
				seenIPs[address.Address] = true
			}
		case v1.NodeExternalDNS, v1.NodeInternalDNS:
			seenDNSNames[address.Address] = true
		}
	}

	for dnsName := range seenDNSNames {
		dnsNames = append(dnsNames, dnsName)
	}
	for ip := range seenIPs {
		ips = append(ips, net.ParseIP(ip))
	}

	// return in stable order
	sort.Strings(dnsNames)
	sort.Slice(ips, func(i, j int) bool { return ips[i].String() < ips[j].String() })

	return dnsNames, ips
}

// NewKubeletClientCertificateManager sets up a certificate manager without a
// client that can be used to sign new certificates (or rotate). It answers with
// whatever certificate it is initialized with. If a CSR client is set later, it
// may begin rotating/renewing the client cert
func NewKubeletClientCertificateManager(certDirectory string, nodeName types.NodeName, certData []byte, keyData []byte, certFile string, keyFile string) (certificate.Manager, error) {
	certificateStore, err := certificate.NewFileStore(
		"kubelet-client",
		certDirectory,
		certDirectory,
		certFile,
		keyFile)
	if err != nil {
		return nil, fmt.Errorf("failed to initialize client certificate store: %v", err)
	}
	var certificateExpiration = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Namespace: metrics.KubeletSubsystem,
			Subsystem: "certificate_manager",
			Name:      "client_expiration_seconds",
			Help:      "Gauge of the lifetime of a certificate. The value is the date the certificate will expire in seconds since January 1, 1970 UTC.",
		},
	)
	prometheus.MustRegister(certificateExpiration)

	m, err := certificate.NewManager(&certificate.Config{
		Template: &x509.CertificateRequest{
			Subject: pkix.Name{
				CommonName:   fmt.Sprintf("system:node:%s", nodeName),
				Organization: []string{"system:nodes"},
			},
		},
		Usages: []certificates.KeyUsage{
			// https://tools.ietf.org/html/rfc5280#section-4.2.1.3
			//
			// DigitalSignature allows the certificate to be used to verify
			// digital signatures including signatures used during TLS
			// negotiation.
			certificates.UsageDigitalSignature,
			// KeyEncipherment allows the cert/key pair to be used to encrypt
			// keys, including the symmetric keys negotiated during TLS setup
			// and used for data transfer..
			certificates.UsageKeyEncipherment,
			// ClientAuth allows the cert to be used by a TLS client to
			// authenticate itself to the TLS server.
			certificates.UsageClientAuth,
		},
		CertificateStore:        certificateStore,
		BootstrapCertificatePEM: certData,
		BootstrapKeyPEM:         keyData,
		CertificateExpiration:   certificateExpiration,
	})
	if err != nil {
		return nil, fmt.Errorf("failed to initialize client certificate manager: %v", err)
	}
	return m, nil
}


text:
local function AddPlayerModel( name, model )

	list.Set( "PlayerOptionsModel", name, model )
	player_manager.AddValidModel( name, model )

end

AddPlayerModel( "Juggernaut",		              "Models/mw2guy/riot/juggernaut.mdl" )
AddPlayerModel( "Riot Soldier-RU",			"Models/mw2guy/riot/riot_ru.mdl" )
AddPlayerModel( "Riot Soldier-US",			"Models/mw2guy/riot/riot_us.mdl" )


text:
<?php

namespace ALttP\Region\Standard;

use ALttP\Item;
use ALttP\Location;
use ALttP\Region;
use ALttP\Support\LocationCollection;
use ALttP\World;

/**
 * Region to hold the Fairy Fountain prizes in the world
 */
class Fountains extends Region
{
    protected $name = 'Special';

    /**
     * Create a new Fountains to hold fountain prizes
     *
     * @param World $world World this Region is part of
     *
     * @return void
     */
    public function __construct(World $world)
    {
        parent::__construct($world);

        $this->locations = new LocationCollection([
            new Location\Fountain("Waterfall Bottle", [0x348FF], null, $this),
            new Location\Fountain("Pyramid Bottle", [0x3493B], null, $this),
        ]);
    }
}


text:
.. _setup-gradle:

Setting up a project with Gradle
================================

You might want to use the following  ``build.gradle`` as a starting point if you set up a new project:

.. parsed-literal::

    apply plugin: 'java'
    apply plugin: 'idea'
    apply plugin: 'eclipse'

    repositories {
        mavenCentral()
    }

    configurations {
        javaAgent
    }

    dependencies {
        ['armeria',
         'armeria-grpc',
         'armeria-jetty',
         'armeria-kafka',
         'armeria-logback',
         'armeria-retrofit2',
         'armeria-thrift',
         'armeria-tomcat',
         'armeria-zipkin',
         'armeria-zookeeper'].each {
            compile "com.linecorp.armeria:${it}:\ |release|\ "
        }

        // Logging
        runtime 'ch.qos.logback:logback-classic:\ |ch.qos.logback:logback-classic:version|\ '
        runtime 'org.slf4j:log4j-over-slf4j:\ |org.slf4j:log4j-over-slf4j:version|\ '
    }

    // Require Java 8 to build the project.
    tasks.withType(JavaCompile) {
        sourceCompatibility '1.8'
        targetCompatibility '1.8'
    }

.. include:: setup-common.rst