text:
package org.hisp.dhis.webapi.controller;

import com.google.common.base.Strings;
import com.google.common.io.ByteSource;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.hisp.dhis.calendar.CalendarService;
import org.hisp.dhis.common.DhisApiVersion;
import org.hisp.dhis.common.IdentifiableObjectManager;
import org.hisp.dhis.common.ValueType;
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.category.CategoryOption;
import org.hisp.dhis.category.CategoryOptionCombo;
import org.hisp.dhis.category.CategoryService;
import org.hisp.dhis.dataset.DataSet;
import org.hisp.dhis.dataset.DataSetService;
import org.hisp.dhis.datavalue.AggregateAccessManager;
import org.hisp.dhis.datavalue.DataValue;
import org.hisp.dhis.datavalue.DataValueService;
import org.hisp.dhis.dxf2.utils.InputUtils;
import org.hisp.dhis.dxf2.webmessage.WebMessage;
import org.hisp.dhis.dxf2.webmessage.WebMessageException;
import org.hisp.dhis.dxf2.webmessage.WebMessageUtils;
import org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse;
import org.hisp.dhis.fileresource.*;
import org.hisp.dhis.i18n.I18nManager;
import org.hisp.dhis.option.OptionSet;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.organisationunit.OrganisationUnitService;
import org.hisp.dhis.period.Period;
import org.hisp.dhis.period.PeriodType;
import org.hisp.dhis.setting.SettingKey;
import org.hisp.dhis.setting.SystemSettingManager;
import org.hisp.dhis.system.util.ValidationUtils;
import org.hisp.dhis.user.CurrentUserService;
import org.hisp.dhis.user.User;
import org.hisp.dhis.webapi.mvc.annotation.ApiVersion;
import org.jclouds.rest.AuthorizationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.hisp.dhis.webapi.utils.ContextUtils.setNoStore;

/**
 * @author Lars Helge Overland
 */
@Controller
@RequestMapping( value = DataValueController.RESOURCE_PATH )
@ApiVersion( { DhisApiVersion.DEFAULT, DhisApiVersion.ALL } )
public class DataValueController
{
    public static final String RESOURCE_PATH = "/dataValues";

    // ---------------------------------------------------------------------
    // Dependencies
    // ---------------------------------------------------------------------

    @Autowired
    private CurrentUserService currentUserService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private OrganisationUnitService organisationUnitService;

    @Autowired
    private DataValueService dataValueService;

    @Autowired
    private DataSetService dataSetService;

    @Autowired
    private IdentifiableObjectManager idObjectManager;

    @Autowired
    private SystemSettingManager systemSettingManager;

    @Autowired
    private InputUtils inputUtils;

    @Autowired
    private FileResourceService fileResourceService;

    @Autowired
    private I18nManager i18nManager;

    @Autowired
    private CalendarService calendarService;

    @Autowired
    private AggregateAccessManager accessManager;

    // ---------------------------------------------------------------------
    // POST
    // ---------------------------------------------------------------------

    @PreAuthorize( "hasRole('ALL') or hasRole('F_DATAVALUE_ADD')" )
    @RequestMapping( method = RequestMethod.POST )
    @ResponseStatus( HttpStatus.CREATED )
    public void saveDataValue(
        @RequestParam String de,
        @RequestParam( required = false ) String co,
        @RequestParam( required = false ) String cc,
        @RequestParam( required = false ) String cp,
        @RequestParam String pe,
        @RequestParam String ou,
        @RequestParam( required = false ) String ds,
        @RequestParam( required = false ) String value,
        @RequestParam( required = false ) String comment,
        @RequestParam( required = false ) boolean followUp,
        @RequestParam( required = false ) boolean force, HttpServletResponse response )
        throws WebMessageException
    {
        boolean strictPeriods = (Boolean) systemSettingManager.getSystemSetting( SettingKey.DATA_IMPORT_STRICT_PERIODS );
        boolean strictCategoryOptionCombos = (Boolean) systemSettingManager.getSystemSetting( SettingKey.DATA_IMPORT_STRICT_CATEGORY_OPTION_COMBOS );
        boolean strictOrgUnits = (Boolean) systemSettingManager.getSystemSetting( SettingKey.DATA_IMPORT_STRICT_ORGANISATION_UNITS );
        boolean requireCategoryOptionCombo = (Boolean) systemSettingManager.getSystemSetting( SettingKey.DATA_IMPORT_REQUIRE_CATEGORY_OPTION_COMBO );
        FileResourceRetentionStrategy retentionStrategy = (FileResourceRetentionStrategy) systemSettingManager.getSystemSetting( SettingKey.FILE_RESOURCE_RETENTION_STRATEGY );
        User currentUser = currentUserService.getCurrentUser();

        // ---------------------------------------------------------------------
        // Input validation
        // ---------------------------------------------------------------------

        DataElement dataElement = getAndValidateDataElement( currentUser, de );

        CategoryOptionCombo categoryOptionCombo = getAndValidateCategoryOptionCombo( co, requireCategoryOptionCombo );

        CategoryOptionCombo attributeOptionCombo = getAndValidateAttributeOptionCombo( cc, cp );

        Period period = getAndValidatePeriod( pe );

        OrganisationUnit organisationUnit = getAndValidateOrganisationUnit( ou );

        DataSet dataSet = getAndValidateOptionalDataSet( ds, dataElement );

        validateInvalidFuturePeriod( period, dataElement );

        validateAttributeOptionComboWithOrgUnitAndPeriod( attributeOptionCombo, organisationUnit, period );

        String valueValid = ValidationUtils.dataValueIsValid( value, dataElement );

        if ( valueValid != null )
        {
            throw new WebMessageException( WebMessageUtils.conflict( "Invalid value: " + value + ", must match data element type: " + dataElement.getValueType() ) );
        }

        String commentValid = ValidationUtils.commentIsValid( comment );

        if ( commentValid != null )
        {
            throw new WebMessageException( WebMessageUtils.conflict( "Invalid comment: " + comment ) );
        }

        OptionSet optionSet = dataElement.getOptionSet();

        if ( !Strings.isNullOrEmpty( value ) && optionSet != null && !optionSet.getOptionCodesAsSet().contains( value ) )
        {
            throw new WebMessageException( WebMessageUtils.conflict( "Data value is not a valid option of the data element option set: " + dataElement.getUid() ) );
        }

        List<String> categoryOptionComboErrors = accessManager.canWriteCached( currentUser, categoryOptionCombo );

        if ( !categoryOptionComboErrors.isEmpty() )
        {
            throw new WebMessageException( WebMessageUtils.conflict( "User does not have write access to category option combo: " + co + ", errors: " + categoryOptionComboErrors ) );
        }

        List<String> attributeOptionComboErrors = accessManager.canWriteCached( currentUser, attributeOptionCombo );

        if ( !attributeOptionComboErrors.isEmpty() )
        {
            throw new WebMessageException( WebMessageUtils.conflict( "User does not have write access to attribute option combo: " + co + ", errors: " + attributeOptionComboErrors ) );
        }

        // ---------------------------------------------------------------------
        // Optional constraints
        // ---------------------------------------------------------------------

        if ( strictPeriods && !dataElement.getPeriodTypes().contains( period.getPeriodType() ) )
        {
            throw new WebMessageException( WebMessageUtils.conflict(
                "Period type of period: " + period.getIsoDate() + " not valid for data element: " + dataElement.getUid() ) );
        }

        if ( strictCategoryOptionCombos && !dataElement.getCategoryOptionCombos().contains( categoryOptionCombo ) )
        {
            throw new WebMessageException( WebMessageUtils.conflict(
                "Category option combo: " + categoryOptionCombo.getUid() + " must be part of category combo of data element: " + dataElement.getUid() ) );
        }

        if ( strictOrgUnits && !organisationUnit.hasDataElement( dataElement ) )
        {
            throw new WebMessageException( WebMessageUtils.conflict(
                "Data element: " + dataElement.getUid() + " must be assigned through data sets to organisation unit: " + organisationUnit.getUid() ) );
        }

        // ---------------------------------------------------------------------
        // Locking validation
        // ---------------------------------------------------------------------

        if ( !inputUtils.canForceDataInput( currentUser, force ) )
        {
            validateDataSetNotLocked( dataElement, period, dataSet, organisationUnit, attributeOptionCombo );
        }

        // ---------------------------------------------------------------------
        // Period validation
        // ---------------------------------------------------------------------

        validateDataInputPeriodForDataElementAndPeriod( dataElement, period, dataSet );

        // ---------------------------------------------------------------------
        // Assemble and save data value
        // ---------------------------------------------------------------------

        String storedBy = currentUserService.getCurrentUsername();

        Date now = new Date();

        DataValue dataValue = dataValueService.getDataValue( dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo );

        FileResource fileResource = null;

        if ( dataValue == null )
        {
            // ---------------------------------------------------------------------
            // Deal with file resource
            // ---------------------------------------------------------------------

            if ( dataElement.getValueType().isFile() )
            {
                fileResource = validateAndSetAssigned( value );
            }

            dataValue = new DataValue( dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo,
                StringUtils.trimToNull( value ), storedBy, now, StringUtils.trimToNull( comment ) );

            dataValueService.addDataValue( dataValue );
        }
        else
        {
            if ( value == null && ValueType.TRUE_ONLY.equals( dataElement.getValueType() ) )
            {
                if ( comment == null )
                {
                    dataValueService.deleteDataValue( dataValue );
                    return;
                }
                else
                {
                    value = DataValue.FALSE;
                }
            }

            // ---------------------------------------------------------------------
            // Deal with file resource
            // ---------------------------------------------------------------------

            if ( dataElement.getValueType().isFile() )
            {
                fileResource = validateAndSetAssigned( value );
            }

            if ( dataElement.isFileType() && retentionStrategy == FileResourceRetentionStrategy.NONE )
            {
                try
                {
                    fileResourceService.deleteFileResource( dataValue.getValue() );
                }
                catch ( AuthorizationException exception )
                {
                    // If we fail to delete the fileResource now, mark it as unassigned for removal later
                    fileResourceService.getFileResource( dataValue.getValue() ).setAssigned( false );
                }
                dataValue.setValue( StringUtils.EMPTY );
            }

            // -----------------------------------------------------------------
            // Value and comment are sent individually, so null checks must be
            // made for each. Empty string is sent for clearing a value.
            // -----------------------------------------------------------------

            if ( value != null )
            {
                dataValue.setValue( StringUtils.trimToNull( value ) );
            }

            if ( comment != null )
            {
                dataValue.setComment( StringUtils.trimToNull( comment ) );
            }

            if ( followUp )
            {
                dataValue.toggleFollowUp();
            }

            dataValue.setLastUpdated( now );
            dataValue.setStoredBy( storedBy );

            dataValueService.updateDataValue( dataValue );
        }

        if ( fileResource != null )
        {
            fileResourceService.updateFileResource( fileResource );
        }
    }

    // ---------------------------------------------------------------------
    // DELETE
    // ---------------------------------------------------------------------

    @PreAuthorize( "hasRole('ALL') or hasRole('F_DATAVALUE_DELETE')" )
    @RequestMapping( method = RequestMethod.DELETE )
    @ResponseStatus( HttpStatus.NO_CONTENT )
    public void deleteDataValue(
        @RequestParam String de,
        @RequestParam( required = false ) String co,
        @RequestParam( required = false ) String cc,
        @RequestParam( required = false ) String cp,
        @RequestParam String pe,
        @RequestParam String ou,
        @RequestParam( required = false ) String ds,
        @RequestParam( required = false ) boolean force, HttpServletResponse response )
        throws WebMessageException
    {
        FileResourceRetentionStrategy retentionStrategy = (FileResourceRetentionStrategy) systemSettingManager.getSystemSetting( SettingKey.FILE_RESOURCE_RETENTION_STRATEGY );

        User currentUser = currentUserService.getCurrentUser();

        // ---------------------------------------------------------------------
        // Input validation
        // ---------------------------------------------------------------------

        DataElement dataElement = getAndValidateDataElement( currentUserService.getCurrentUser(), de );

        CategoryOptionCombo categoryOptionCombo = getAndValidateCategoryOptionCombo( co, false );

        CategoryOptionCombo attributeOptionCombo = getAndValidateAttributeOptionCombo( cc, cp );

        Period period = getAndValidatePeriod( pe );

        OrganisationUnit organisationUnit = getAndValidateOrganisationUnit( ou );

        DataSet dataSet = getAndValidateOptionalDataSet( ds, dataElement );

        // ---------------------------------------------------------------------
        // Locking validation
        // ---------------------------------------------------------------------

        if ( !inputUtils.canForceDataInput( currentUser, force ) )
        {
            validateDataSetNotLocked( dataElement, period, dataSet, organisationUnit, attributeOptionCombo );
        }

        // ---------------------------------------------------------------------
        // Period validation
        // ---------------------------------------------------------------------

        validateDataInputPeriodForDataElementAndPeriod( dataElement, period, dataSet );

        // ---------------------------------------------------------------------
        // Delete data value
        // ---------------------------------------------------------------------

        DataValue dataValue = dataValueService.getDataValue( dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo );

        if ( dataValue == null )
        {
            throw new WebMessageException( WebMessageUtils.conflict( "Data value cannot be deleted because it does not exist" ) );
        }

        if ( dataValue.getDataElement().isFileType() && retentionStrategy == FileResourceRetentionStrategy.NONE )
        {
            fileResourceService.deleteFileResource( dataValue.getValue() );
        }

        dataValueService.deleteDataValue( dataValue );
    }

    // ---------------------------------------------------------------------
    // GET
    // ---------------------------------------------------------------------

    @RequestMapping( method = RequestMethod.GET )
    public @ResponseBody List<String> getDataValue(
        @RequestParam String de,
        @RequestParam( required = false ) String co,
        @RequestParam( required = false ) String cc,
        @RequestParam( required = false ) String cp,
        @RequestParam String pe,
        @RequestParam String ou,
        Model model, HttpServletResponse response )
        throws WebMessageException
    {
        // ---------------------------------------------------------------------
        // Input validation
        // ---------------------------------------------------------------------

        User currentUser = currentUserService.getCurrentUser();

        DataElement dataElement = getAndValidateDataElement( currentUser, de );

        CategoryOptionCombo categoryOptionCombo = getAndValidateCategoryOptionCombo( co, false );

        CategoryOptionCombo attributeOptionCombo = getAndValidateAttributeOptionCombo( cc, cp );

        Period period = getAndValidatePeriod( pe );

        OrganisationUnit organisationUnit = getAndValidateOrganisationUnit( ou );

        // ---------------------------------------------------------------------
        // Get data value
        // ---------------------------------------------------------------------

        DataValue dataValue = dataValueService.getDataValue( dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo );

        if ( dataValue == null )
        {
            throw new WebMessageException( WebMessageUtils.conflict( "Data value does not exist" ) );
        }

        // ---------------------------------------------------------------------
        // Data Sharing check
        // ---------------------------------------------------------------------

        List<String> errors = accessManager.canRead( currentUser, dataValue );

        if ( !errors.isEmpty() )
        {
            throw new WebMessageException( WebMessageUtils.forbidden( errors.toString() ) );
        }

        List<String> value = new ArrayList<>();
        value.add( dataValue.getValue() );

        setNoStore( response );
        return value;
    }

    // ---------------------------------------------------------------------
    // GET file
    // ---------------------------------------------------------------------

    @RequestMapping( value = "/files", method = RequestMethod.GET )
    public void getDataValueFile(
        @RequestParam String de,
        @RequestParam( required = false ) String co,
        @RequestParam( required = false ) String cc,
        @RequestParam( required = false ) String cp,
        @RequestParam String pe,
        @RequestParam String ou, HttpServletResponse response, HttpServletRequest request )
        throws WebMessageException
    {
        // ---------------------------------------------------------------------
        // Input validation
        // ---------------------------------------------------------------------

        DataElement dataElement = getAndValidateDataElement( currentUserService.getCurrentUser(), de );

        if ( !dataElement.isFileType() )
        {
            throw new WebMessageException( WebMessageUtils.conflict( "DataElement must be of type file" ) );
        }

        CategoryOptionCombo categoryOptionCombo = getAndValidateCategoryOptionCombo( co, false );

        CategoryOptionCombo attributeOptionCombo = getAndValidateAttributeOptionCombo( cc, cp );

        Period period = getAndValidatePeriod( pe );

        OrganisationUnit organisationUnit = getAndValidateOrganisationUnit( ou );

        // ---------------------------------------------------------------------
        // Get data value
        // ---------------------------------------------------------------------

        DataValue dataValue = dataValueService.getDataValue( dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo );

        if ( dataValue == null )
        {
            throw new WebMessageException( WebMessageUtils.conflict( "Data value does not exist" ) );
        }

        // ---------------------------------------------------------------------
        // Get file resource
        // ---------------------------------------------------------------------

        String uid = dataValue.getValue();

        FileResource fileResource = fileResourceService.getFileResource( uid );

        if ( fileResource == null || fileResource.getDomain() != FileResourceDomain.DATA_VALUE )
        {
            throw new WebMessageException( WebMessageUtils.notFound( "A data value file resource with id " + uid + " does not exist." ) );
        }

        FileResourceStorageStatus storageStatus = fileResource.getStorageStatus();

        if ( storageStatus != FileResourceStorageStatus.STORED )
        {
            // Special case:
            // The FileResource exists and has been tied to this DataValue, however, the underlying file
            // content is still not stored to the (most likely external) file store provider.

            // HTTP 409, for lack of a more suitable status code
            WebMessage webMessage = WebMessageUtils.conflict( "The content is being processed and is not available yet. Try again later.",
                "The content requested is in transit to the file store and will be available at a later time." );
            webMessage.setResponse( new FileResourceWebMessageResponse( fileResource ) );

            throw new WebMessageException( webMessage );
        }

        ByteSource content = fileResourceService.getFileResourceContent( fileResource );

        if ( content == null )
        {
            throw new WebMessageException( WebMessageUtils.notFound( "The referenced file could not be found" ) );
        }

        // ---------------------------------------------------------------------
        // Attempt to build signed URL request for content and redirect
        // ---------------------------------------------------------------------

        URI signedGetUri = fileResourceService.getSignedGetFileResourceContentUri( uid );

        if ( signedGetUri != null )
        {
            response.setStatus( HttpServletResponse.SC_TEMPORARY_REDIRECT );
            response.setHeader( HttpHeaders.LOCATION, signedGetUri.toASCIIString() );

            return;
        }

        // ---------------------------------------------------------------------
        // Build response and return
        // ---------------------------------------------------------------------

        response.setContentType( fileResource.getContentType() );
        response.setContentLength( new Long( fileResource.getContentLength() ).intValue() );
        response.setHeader( HttpHeaders.CONTENT_DISPOSITION, "filename=" + fileResource.getName() );
        setNoStore( response );

        // ---------------------------------------------------------------------
        // Request signing is not available, stream content back to client
        // ---------------------------------------------------------------------

        try ( InputStream inputStream = content.openStream() )
        {
            IOUtils.copy( inputStream, response.getOutputStream() );
        }
        catch ( IOException e )
        {
            throw new WebMessageException( WebMessageUtils.error( "Failed fetching the file from storage",
                "There was an exception when trying to fetch the file from the storage backend. " +
                    "Depending on the provider the root cause could be network or file system related." ) );
        }
    }

    // ---------------------------------------------------------------------
    // Supportive methods
    // ---------------------------------------------------------------------

    private DataElement getAndValidateDataElement( User user, String de )
        throws WebMessageException
    {
        DataElement dataElement = idObjectManager.get( DataElement.class, de );

        if ( dataElement == null )
        {
            throw new WebMessageException( WebMessageUtils.conflict( "Data element not found or not accessible: " + de ) );
        }

        return dataElement;
    }

    private CategoryOptionCombo getAndValidateCategoryOptionCombo( String co, boolean requireCategoryOptionCombo )
        throws WebMessageException
    {
        CategoryOptionCombo categoryOptionCombo = categoryService.getCategoryOptionCombo( co );

        if ( categoryOptionCombo == null )
        {
            if ( requireCategoryOptionCombo )
            {
                throw new WebMessageException( WebMessageUtils.conflict( "Category option combo is required but is not specified" ) );
            }
            else if ( co != null )
            {
                throw new WebMessageException( WebMessageUtils.conflict( "Category option combo not found or not accessible: " + co ) );
            }
            else
            {
                categoryOptionCombo = categoryService.getDefaultCategoryOptionCombo();
            }
        }

        return categoryOptionCombo;
    }

    private CategoryOptionCombo getAndValidateAttributeOptionCombo( String cc, String cp )
        throws WebMessageException
    {
        CategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo( cc, cp, false );

        if ( attributeOptionCombo == null )
        {
            throw new WebMessageException( WebMessageUtils.conflict( "Attribute option combo not found or not accessible: " + cc + " " + cp ) );
        }

        return attributeOptionCombo;
    }

    private Period getAndValidatePeriod( String pe )
        throws WebMessageException
    {
        Period period = PeriodType.getPeriodFromIsoString( pe );

        if ( period == null )
        {
            throw new WebMessageException( WebMessageUtils.conflict( "Illegal period identifier: " + pe ) );
        }

        return period;
    }

    private OrganisationUnit getAndValidateOrganisationUnit( String ou )
        throws WebMessageException
    {
        OrganisationUnit organisationUnit = idObjectManager.get( OrganisationUnit.class, ou );

        if ( organisationUnit == null )
        {
            throw new WebMessageException( WebMessageUtils.conflict( "Organisation unit not found or not accessible: " + ou ) );
        }

        boolean isInHierarchy = organisationUnitService.isInUserHierarchyCached( organisationUnit );

        if ( !isInHierarchy )
        {
            throw new WebMessageException( WebMessageUtils.conflict( "Organisation unit is not in the hierarchy of the current user: " + ou ) );
        }

        return organisationUnit;
    }

    private DataSet getAndValidateOptionalDataSet( String ds, DataElement dataElement )
        throws WebMessageException
    {
        if ( ds == null )
        {
            return null;
        }

        DataSet dataSet = dataSetService.getDataSet( ds );

        if ( dataSet == null )
        {
            throw new WebMessageException( WebMessageUtils.conflict( "Data set not found or not accessible: " + ds ) );
        }

        if ( !dataSet.getDataElements().contains( dataElement ) )
        {
            throw new WebMessageException( WebMessageUtils.conflict( "Data set: " + ds + " does not contain data element: " + dataElement.getUid() ) );
        }

        return dataSet;
    }

    private void validateInvalidFuturePeriod( Period period, DataElement dataElement )
        throws WebMessageException
    {
        Period latestFuturePeriod = dataElement.getLatestOpenFuturePeriod();

        if ( period.isAfter( latestFuturePeriod ) && calendarService.getSystemCalendar().isIso8601() )
        {
            throw new WebMessageException( WebMessageUtils.conflict( "Period: " +
                period.getIsoDate() + " is after latest open future period: " + latestFuturePeriod.getIsoDate() + " for data element: " + dataElement.getUid() ) );
        }
    }

    private void validateAttributeOptionComboWithOrgUnitAndPeriod( CategoryOptionCombo attributeOptionCombo,
        OrganisationUnit organisationUnit, Period period )
        throws WebMessageException
    {
        for ( CategoryOption option : attributeOptionCombo.getCategoryOptions() )
        {
            if ( option.getStartDate() != null && period.getEndDate().compareTo( option.getStartDate() ) < 0 )
            {
                throw new WebMessageException( WebMessageUtils.conflict( "Period " + period.getIsoDate()
                    + " is before start date " + i18nManager.getI18nFormat().formatDate( option.getStartDate() )
                    + " for attributeOption '" + option.getName() + "'" ) );
            }

            if ( option.getEndDate() != null && period.getStartDate().compareTo( option.getEndDate() ) > 0 )
            {
                throw new WebMessageException( WebMessageUtils.conflict( "Period " + period.getIsoDate()
                    + " is after end date " + i18nManager.getI18nFormat().formatDate( option.getEndDate() )
                    + " for attributeOption '" + option.getName() + "'" ) );
            }
        }
    }

    private void validateDataSetNotLocked( DataElement dataElement, Period period, DataSet dataSet,
        OrganisationUnit organisationUnit, CategoryOptionCombo attributeOptionCombo )
        throws WebMessageException
    {
        User user = currentUserService.getCurrentUser();

        if ( dataSet == null ? dataSetService.isLocked( user, dataElement, period, organisationUnit, attributeOptionCombo, null )
            : dataSetService.isLocked( user, dataSet, period, organisationUnit, attributeOptionCombo, null) )
        {
            throw new WebMessageException( WebMessageUtils.conflict( "Data set is locked" ) );
        }
    }

    private void validateDataInputPeriodForDataElementAndPeriod( DataElement dataElement, Period period, DataSet dataSet )
        throws WebMessageException
    {
        if ( !( dataSet == null ? dataElement.isDataInputAllowedForPeriodAndDate( period, new Date() )
            : dataSet.isDataInputPeriodAndDateAllowed( period, new Date() ) ) )
        {
            throw new WebMessageException( WebMessageUtils.conflict( "Period reported is not open in data set" ) );
        }
    }

    private FileResource validateAndSetAssigned( String uid )
        throws WebMessageException
    {
        FileResource fileResource = null;

        if ( uid != null )
        {
            fileResource = fileResourceService.getFileResource( uid );

            if ( fileResource == null || fileResource.getDomain() != FileResourceDomain.DATA_VALUE )
            {
                throw new WebMessageException( WebMessageUtils.notFound( FileResource.class, uid ) );
            }

            if ( fileResource.isAssigned() )
            {
                throw new WebMessageException( WebMessageUtils.conflict( "File resource already assigned or linked to another data value" ) );
            }

            fileResource.setAssigned( true );
        }
        else
        {
            throw new WebMessageException( WebMessageUtils.conflict( "Missing parameter 'value'" ) );
        }

        return fileResource;
    }
}


text:
namespace chromeos {

KioskExternalUpdateValidator::KioskExternalUpdateValidator(
    const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner,
    const std::string& app_id,
    const base::FilePath& crx_dir,
    const base::FilePath& crx_unpack_dir,
    const base::WeakPtr<KioskExternalUpdateValidatorDelegate>& delegate)
    : backend_task_runner_(backend_task_runner),
      app_id_(app_id),
      crx_dir_(crx_dir),
      crx_unpack_dir_(crx_unpack_dir),
      delegate_(delegate) {
}

KioskExternalUpdateValidator::~KioskExternalUpdateValidator() {
}

void KioskExternalUpdateValidator::Start() {
  scoped_refptr<extensions::SandboxedUnpacker> unpacker(
      new extensions::SandboxedUnpacker(crx_dir_,
                                        extensions::Manifest::EXTERNAL_PREF,
                                        extensions::Extension::NO_FLAGS,
                                        crx_unpack_dir_,
                                        backend_task_runner_.get(),
                                        this));
  if (!backend_task_runner_->PostTask(
          FROM_HERE,
          base::Bind(&extensions::SandboxedUnpacker::Start, unpacker.get()))) {
    NOTREACHED();
  }
}

void KioskExternalUpdateValidator::OnUnpackFailure(
    const base::string16& error_message) {
  LOG(ERROR) << "Failed to unpack external kiosk crx file: " << app_id_ << " "
             << error_message;
  content::BrowserThread::PostTask(
      content::BrowserThread::UI,
      FROM_HERE,
      base::Bind(
          &KioskExternalUpdateValidatorDelegate::OnExternalUpdateUnpackFailure,
          delegate_,
          app_id_));
}

void KioskExternalUpdateValidator::OnUnpackSuccess(
    const base::FilePath& temp_dir,
    const base::FilePath& extension_dir,
    const base::DictionaryValue* original_manifest,
    const extensions::Extension* extension,
    const SkBitmap& install_icon) {
  DCHECK(app_id_ == extension->id());

  std::string minimum_browser_version;
  if (!extension->manifest()->GetString(
          extensions::manifest_keys::kMinimumChromeVersion,
          &minimum_browser_version)) {
    LOG(ERROR) << "Can't find minimum browser version for app " << app_id_;
    minimum_browser_version.clear();
  }

  content::BrowserThread::PostTask(
      content::BrowserThread::UI,
      FROM_HERE,
      base::Bind(
          &KioskExternalUpdateValidatorDelegate::OnExtenalUpdateUnpackSuccess,
          delegate_,
          app_id_,
          extension->VersionString(),
          minimum_browser_version,
          temp_dir));
}

}  // namespace chromeos

redpajama_set_name: RedPajamaGithub


text:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Flushing the database cache</title>
    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
    <link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" />
    <link rel="up" href="am.html" title="Chapter 3.  Access Method Operations" />
    <link rel="prev" href="am_verify.html" title="Database verification and salvage" />
    <link rel="next" href="am_close.html" title="Database close" />

Library Version 11.2.5.3

          Flushing the database cache

          Prev
          Chapter 3.
		Access Method Operations

           Next

Flushing the database cache

The DB->sync() method flushes all modified records from the database
cache to disk.

          It is important to understand that flushing cached information
to disk only minimizes the window of opportunity for corrupted data, it
does not eliminate the possibility.

While unlikely, it is possible for database corruption to happen if a
system or application crash occurs while writing data to the database. To
ensure that database corruption never occurs, applications must either:

Use transactions and logging with automatic recovery.

Use logging and application-specific recovery.

Edit a copy of the database, and, once all applications
using the database have successfully called DB->close(), use
system operations (for example, the POSIX rename system call) to
atomically replace the original database with the updated copy.

          Prev

            Up

           Next

          Database verification and salvage

            Home

           Database close


text:
using System;
using System.ComponentModel;

namespace Azure.ResourceManager.Synapse.Models
{
    /// <summary> The name of blob storage event type to process. </summary>
    public readonly partial struct BlobStorageEventType : IEquatable<BlobStorageEventType>
    {
        private readonly string _value;

        /// <summary> Initializes a new instance of <see cref="BlobStorageEventType"/>. </summary>
        /// <exception cref="ArgumentNullException"> <paramref name="value"/> is null. </exception>
        public BlobStorageEventType(string value)
        {
            _value = value ?? throw new ArgumentNullException(nameof(value));
        }

        private const string MicrosoftStorageBlobCreatedValue = "Microsoft.Storage.BlobCreated";
        private const string MicrosoftStorageBlobRenamedValue = "Microsoft.Storage.BlobRenamed";

        /// <summary> Microsoft.Storage.BlobCreated. </summary>
        public static BlobStorageEventType MicrosoftStorageBlobCreated { get; } = new BlobStorageEventType(MicrosoftStorageBlobCreatedValue);
        /// <summary> Microsoft.Storage.BlobRenamed. </summary>
        public static BlobStorageEventType MicrosoftStorageBlobRenamed { get; } = new BlobStorageEventType(MicrosoftStorageBlobRenamedValue);
        /// <summary> Determines if two <see cref="BlobStorageEventType"/> values are the same. </summary>
        public static bool operator ==(BlobStorageEventType left, BlobStorageEventType right) => left.Equals(right);
        /// <summary> Determines if two <see cref="BlobStorageEventType"/> values are not the same. </summary>
        public static bool operator !=(BlobStorageEventType left, BlobStorageEventType right) => !left.Equals(right);
        /// <summary> Converts a string to a <see cref="BlobStorageEventType"/>. </summary>
        public static implicit operator BlobStorageEventType(string value) => new BlobStorageEventType(value);

        /// <inheritdoc />
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override bool Equals(object obj) => obj is BlobStorageEventType other && Equals(other);
        /// <inheritdoc />
        public bool Equals(BlobStorageEventType other) => string.Equals(_value, other._value, StringComparison.InvariantCultureIgnoreCase);

        /// <inheritdoc />
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override int GetHashCode() => _value?.GetHashCode() ?? 0;
        /// <inheritdoc />
        public override string ToString() => _value;
    }
}


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

namespace Destrier.Test.Postgres
{
    public class DatabaseContext : IDisposable
    {
        public DatabaseContext()
        {
            SchemaName = "public";
            DatabaseName = "tempdb";
            ConnectionName = "default";
            ConnectionString = "User ID=tempdb_user; Password=passw0rd; Server=localhost; Port=5432; Database=tempdb";
            ProviderName = "Npgsql";
            SetupDatabaseContext();
        }

        public DatabaseContext(String connectionName, String connectionString, String providerName)
        {
            this.ConnectionName = connectionName;
            this.ConnectionString = connectionString;
            this.ProviderName = providerName;
            this.SetupDatabaseContext();
        }
        public String DatabaseName { get; set; }
        public String ConnectionName { get; set; }
        public String ConnectionString { get; set; }
        public String ProviderName { get; set; }
        public String SchemaName { get; set; }

        public void SetupDatabaseContext()
        {
            if (!Destrier.DatabaseConfigurationContext.ConnectionStrings.ContainsKey(ConnectionName))
            {
                if (String.IsNullOrEmpty(ProviderName))
                {
                    Destrier.DatabaseConfigurationContext.ConnectionStrings.Add(ConnectionName, ConnectionString);
                }
                else
                {
                    Destrier.DatabaseConfigurationContext.AddConnectionString(ConnectionName, ConnectionString, ProviderName);
                }
            }

            if (!String.IsNullOrEmpty(this.SchemaName))
                DatabaseConfigurationContext.DefaultSchemaName = SchemaName;

            if (!String.IsNullOrEmpty(this.DatabaseName))
                DatabaseConfigurationContext.DefaultDatabaseName = DatabaseName;

            if (!TestIfSchemaExists())
            {
                EnsureInitDataStore();
            }
        }

        public virtual Boolean TestIfSchemaExists() { return false; }
        public virtual void EnsureInitDataStore() { }
        public virtual void EnsureDestroyDataStore() { }

        public void Dispose()
        {
            EnsureDestroyDataStore();
        }
    }

    public class TestObjectContext : DatabaseContext, IDisposable
    {
        public TestObjectContext() : base() { }

        public TestObjectContext(String connectionName, String connectionString, String providerName)
            : base(connectionName, connectionString, providerName) { }

        public override Boolean TestIfSchemaExists()
        {
            var exists = false;
            Execute.StatementReader("select * from pg_tables where schemaname='public' and tablename ilike 'testobjects';", (dr) =>
            {
                while (dr.Read())
                    exists = true;
            });
            return exists;
        }

        public override void EnsureInitDataStore()
        {
            Destrier.DatabaseConfigurationContext.DefaultDatabaseName = "tempdb";
			var initDbScript = @"
CREATE TABLE TestObjects
(
    id serial primary key,
    name varchar(255) not null,
    nullname varchar(255),
    mockobjecttypeid smallint not null,
    active boolean not null,
    created timestamp not null,
    modified timestamp,
    nullableid int,
    referencedobjectid int,
    type int,
    nullabletype int,
    singlechar char,
    single float,
    double float,
    nullabledouble float,
    guid uuid not null,
    nullableguid uuid
);

CREATE OR REPLACE FUNCTION GenerateData() RETURNS SETOF TestObjects AS
$BODY$
DECLARE
	id int;
	i int := 0;
	subId smallint :=1 ;
	typeId smallint := 1 ;
	nullableTypeId smallint;
	nullableGuid uuid;
BEGIN
	WHILE i < 10001
	LOOP
	    INSERT INTO TestObjects
	    (Name, mockObjectTypeId, active, created, modified, nullableId, referencedObjectId, type, nullableType, singleChar, single, double, nullableDouble, guid, nullableGuid)
	    VALUES
	    ( 'name' || cast(i as varchar), @typeId, true, now(), null, null, subId, 1, nullableTypeId, 'c', 1, 1, nullableTypeId, uuid_generate_v1(), nullableGuid);

		IF (nullableTypeId is null)
		THEN
			nullableTypeId = 1;
		ELSIF(nullableTypeId is not null) THEN
			nullableTypeId = null;
		END IF;

		IF (nullableGuid is null) THEN
			nullableGuid = uuid_generate_v1();
		ELSIF(nullableGuid is not null)
		THEN
			nullableGuid = null;
		END IF;

	    IF (subId = 100) THEN subId = 1; END IF;
	    IF (typeId = 10) THEN typeId = 1; END IF;

	    subId = subId + 1;
	    typeId = typeId + 1;
	    i = i + 1;
	END LOOP;
END;
$BODY$
LANGUAGE 'plpgsql';

SELECT GenerateData();
";

            if (!TestIfSchemaExists())
            {
                Destrier.Execute.NonQuery(initDbScript);
            }
        }

        public override void EnsureDestroyDataStore()
        {
            var statement = @"DROP TABLE IF EXISTS tempdb.public.TestObjects CASCADE;";
            Execute.NonQuery(statement);
        }
    }

    public class LibraryContext : DatabaseContext
    {
        public LibraryContext()
            : base()
        {
            if (!TestIfSchemaExists())
            {
                EnsureInitDataStore();
            }
        }

        public LibraryContext(String connectionName, String connectionString, String providerName)
            : base(connectionName, connectionString, providerName)
        {
            if (!TestIfSchemaExists())
            {
                EnsureInitDataStore();
            }
        }

        public override Boolean TestIfSchemaExists()
        {
            var exists = false;
            Execute.StatementReader("select * from pg_tables where schemaname='public' and tablename ilike 'books';", (dr) =>
            {
                while (dr.Read())
                    exists = true;
            });
            return exists;
        }

        public override void EnsureInitDataStore()
        {
            Destrier.DatabaseConfigurationContext.DefaultDatabaseName = "tempdb";
            var initDbScript = @"
CREATE TABLE People
(
id serial primary key,
name varchar(255) not null
);

CREATE TABLE Books
(
id serial primary key,
title varchar(255) not null,
year smallint not null,
authorId int not null references People(Id),
notes varchar(255)
);

CREATE TABLE Chapters
(
id serial primary key,
title varchar(255) not null,
number int not null,
bookId int not null
);

CREATE TABLE Pages
(
id serial primary key,
number int not null,
bookId int not null references Books(Id),
chapterId int not null references Chapters(Id),
text varchar(1024) not null
);

INSERT INTO People (name) VALUES ('Ernest Hemingway'); --1
INSERT INTO People (name) VALUES ('T. S. Eliot'); --2
INSERT INTO People (name) VALUES ('William Shakespeare'); --3
INSERT INTO People (name) VALUES ('F. Scott Fitzgerald'); --4
INSERT INTO People (name) VALUES ('Henry David Thoreau'); --5

INSERT INTO Books (title, year, authorId, notes) VALUES ('The Old Man and the Sea', 1952, 1, null); --1
INSERT INTO Books (title, year, authorId, notes) VALUES ('The Waste Land', 1922, 2, null); --2
INSERT INTO Books (title, year, authorId, notes) VALUES ('Hamlet', 1603, 3, null); --3
INSERT INTO Books (title, year, authorId, notes) VALUES ('Macbeth', 1611, 3, null); --4
INSERT INTO Books (title, year, authorId, notes) VALUES ('The Great Gatsby', 1925, 4, null); --5
INSERT INTO Books (title, year, authorId, notes) VALUES ('Walden', 1854, 5, null); --6

INSERT INTO Chapters (title, number, bookid) VALUES ('The Old Man and the Sea', 1, 1);
INSERT INTO Chapters (title, number, bookid) VALUES ('The Burial Of The Dead', 1, 2);
INSERT INTO Chapters (title, number, bookid) VALUES ('A Game of Chess', 2, 2);
INSERT INTO Chapters (title, number, bookid) VALUES ('Death By Water', 3, 2);
INSERT INTO Chapters (title, number, bookid) VALUES ('What The Thunder Said', 4, 2);
INSERT INTO Chapters (title, number, bookid) VALUES ('Elsinore. A platform before the castle.', 1, 3);
INSERT INTO Chapters (title, number, bookid) VALUES ('A room of state in the castle.', 2, 3);
INSERT INTO Chapters (title, number, bookid) VALUES ('A room in Polonius house.', 3, 3);
INSERT INTO Chapters (title, number, bookid) VALUES ('The platform.', 4, 3);
INSERT INTO Chapters (title, number, bookid) VALUES ('Another part of the platform.', 5, 3);
INSERT INTO Chapters (title, number, bookid) VALUES ('A desert place.', 1, 4);
INSERT INTO Chapters (title, number, bookid) VALUES ('A camp near Forres.', 2, 4);
INSERT INTO Chapters (title, number, bookid) VALUES ('A heath near Forres.', 3, 4);
INSERT INTO Chapters (title, number, bookid) VALUES ('Forres. The palace.', 4, 4);
INSERT INTO Chapters (title, number, bookid) VALUES ('Inverness. Macbeths castle.', 5, 4);
INSERT INTO Chapters (title, number, bookid) VALUES ('Before Macbeths castle.', 6, 4);
INSERT INTO Chapters (title, number, bookid) VALUES ('Macbeths castle.', 7, 4);
INSERT INTO Chapters (title, number, bookid) VALUES ('Chapter 1', 1, 5);
INSERT INTO Chapters (title, number, bookid) VALUES ('Chapter 2', 2, 5);
INSERT INTO Chapters (title, number, bookid) VALUES ('Chapter 3', 3, 5);
INSERT INTO Chapters (title, number, bookid) VALUES ('Chapter 4', 4, 5);
INSERT INTO Chapters (title, number, bookid) VALUES ('Chapter 5', 5, 5);
INSERT INTO Chapters (title, number, bookid) VALUES ('Chapter 6', 6, 5);
INSERT INTO Chapters (title, number, bookid) VALUES ('Chapter 7', 7, 5);
INSERT INTO Chapters (title, number, bookid) VALUES ('Chapter 8', 8, 5);
INSERT INTO Chapters (title, number, bookid) VALUES ('Chapter 9', 9, 5);
INSERT INTO Chapters (title, number, bookid) VALUES ('Economy', 1, 6);
INSERT INTO Chapters (title, number, bookid) VALUES ('Where I Lived. & What I Lived for', 2, 6);
INSERT INTO Chapters (title, number, bookid) VALUES ('Reading', 3, 6);
INSERT INTO Chapters (title, number, bookid) VALUES ('Sounds', 4, 6);
INSERT INTO Chapters (title, number, bookid) VALUES ('Solitude', 5, 6);
INSERT INTO Chapters (title, number, bookid) VALUES ('Visitors', 6, 6);
INSERT INTO Chapters (title, number, bookid) VALUES ('The Bean-Field', 7, 6);
INSERT INTO Chapters (title, number, bookid) VALUES ('The Village', 8, 6);
INSERT INTO Chapters (title, number, bookid) VALUES ('The Ponds', 9, 6);
INSERT INTO Chapters (title, number, bookid) VALUES ('Baker Farm', 10, 6);
INSERT INTO Chapters (title, number, bookid) VALUES ('Higer Laws', 11, 6);
INSERT INTO Chapters (title, number, bookid) VALUES ('Brute Neighbors', 12, 6);
INSERT INTO Chapters (title, number, bookid) VALUES ('House-Warming', 13, 6);
INSERT INTO Chapters (title, number, bookid) VALUES ('Former Inhabitants: & Winter Visitors', 14, 6);
INSERT INTO Chapters (title, number, bookid) VALUES ('Winter Animals', 15, 6);
INSERT INTO Chapters (title, number, bookid) VALUES ('The Pond in Winter', 16, 6);
INSERT INTO Chapters (title, number, bookid) VALUES ('Spring', 17, 6);
INSERT INTO Chapters (title, number, bookid) VALUES ('Conclusion', 18, 6);

INSERT INTO Pages (number, bookId, chapterId, text) VALUES (1, 1, 1, 'He was an old man who fished alone in a skiff in the Gulf Stream and he had gone eighty-four days now without taking a fish.');
INSERT INTO Pages (number, bookId, chapterId, text) VALUES (2, 1, 1, 'Five and you nearly were killed when I brought the fish in too green and he nearly tore the boat to pieces. Can you remember?');
INSERT INTO Pages (number, bookId, chapterId, text) VALUES (3, 1, 1, 'They picked up the gear from the boat. The old man carried the mast on his shoulder and the boy carried the wooden boat with the coiled, hard-braided brown lines, the gaff and the harpoon with its shaft.');
";

            if (!TestIfSchemaExists())
            {
                Destrier.Execute.NonQuery(initDbScript);
            }
        }

        public override void EnsureDestroyDataStore()
        {
            var statement = @"
DROP TABLE IF EXISTS tempdb.public.Pages;
DROP TABLE IF EXISTS tempdb.public.Chapters;
DROP TABLE IF EXISTS tempdb.public.Books;
DROP TABLE IF EXISTS tempdb.public.People;
";
            Execute.NonQuery(statement);
        }
    }
}


text:
"""
  gp_era.py
"""

import sys, os, stat, re
import hashlib

ERA_RE = re.compile(r"era\s*=\s*(\w+)")

def INFO(msg):
    self = sys._getframe(1).f_locals['self']
    if self.logger: self.logger.info(msg)

def DEBUG(msg):
    self = sys._getframe(1).f_locals['self']
    if self.logger: self.logger.debug(msg)

class GpEraFile:
    """
    Manage the gp_era file.
    """

    def __init__(self, datadir, do_read=False, logger=None):
        """
        Initialize path to gp_era file and reset values.
        Log subsequent activity using specified logger and
        if do_read is True, immediately attempt to read values.
        """
        self.datadir      = datadir
        self.filepath     = os.path.join(self.datadir, 'pg_log', 'gp_era')
        self.era          = None
        self.logger       = logger

        if do_read:
            self.read_gp_era()

    def read_gp_era(self):
        """
        Open the gp_era file and parse its contents.
        """
        DEBUG('%s - read_gp_era' % self.filepath)

        with open(self.filepath) as f:
            self.parse(f)

    def parse(self, f):
        """
        Parse f, looking for matching era ignoring all other lines.
        Assigns era to observed value, converting matched values from strings to integers.
        """
        self.era = None
        for line in f:
            line = line.strip()
            DEBUG('parse: %s' % line)

            m = re.match(ERA_RE, line)
            if m:
                self.era = m.group(1)
                DEBUG('match era: %s' % self.era)
                break

        assert self.era is not None

    def format(self, f):
        """
        Generate gp_era contents based on era value
        """
        f.write("# Greenplum Database era.\n")
        f.write("# Do not change the contents of this file.\n")
        f.write('era = %s\n' % self.era)
        INFO('wrote era: %s' % self.era)

    def write_gp_era(self):
        """
        Create or replace gp_era file with current values, changing
        permissions of the new file when done and verifying by re-reading
        the file contents and checking the values read match desired values
        """
        DEBUG('%s - write_gp_era' % self.filepath)

        if os.path.exists(self.filepath):
            DEBUG('found existing file')

            os.remove(self.filepath)
            DEBUG('removed existing file')

        DEBUG('opening new file')
        with open(self.filepath, 'w') as f:
            self.format(f)

        DEBUG('setting read only')
        os.chmod(self.filepath, stat.S_IRUSR)  # user read permissions (0400)

        DEBUG('verifying file')
        v = GpEraFile(self.datadir, do_read=True)
        assert self.era == v.era

    def new_era(self, host, port, time):
        """
        Write a new era based on the specified values
        """
        m = hashlib.md5()
        m.update(str(host))
        m.update(str(port))
        m.update(str(self.datadir))
        self.era = '%s_%s' % (m.hexdigest()[0:16], time)
        self.write_gp_era()

    def set_era(self, era):
        """
        Write a new era based on the specified value
        """
        self.era = era
        self.write_gp_era()

    def end_era(self):
        """
        Remove the era file.
        """
        DEBUG('%s - end_gp_era' % self.filepath)

        if os.path.exists(self.filepath):
            DEBUG('found existing file')

            os.remove(self.filepath)
            DEBUG('removed existing file')

def read_era(datadir, logger):
    """
    """
    erafile = GpEraFile(datadir, logger=logger)
    if os.path.exists(erafile.filepath):
        erafile.read_gp_era()

    if logger: logger.info('era is %s' % erafile.era)
    return erafile.era

#
# trivial unit test
#
if __name__ == '__main__':
    import copy, shutil
    import unittest2 as unittest

    TESTDIR = 'test_gp_era1'

    class MyTestCase(unittest.TestCase):
        def test1(self):
            d = GpEraFile(TESTDIR)
            d2 = copy.copy(d)
            d.era = 10
            d.write_gp_era()
            d2.read_gp_era()
            assert d.era == d2.era

    if os.path.exists(TESTDIR): shutil.rmtree(TESTDIR)
    os.mkdir(TESTDIR)
    os.mkdir(os.path.join(TESTDIR, 'pg_log'))
    unittest.main()
    shutil.rmtree(TESTDIR)


text:
package caddypki

import (
	"fmt"

	"github.com/caddyserver/caddy/v2"
	"go.uber.org/zap"
)

func init() {
	caddy.RegisterModule(PKI{})
}

// PKI provides Public Key Infrastructure facilities for Caddy.
//
// This app can define certificate authorities (CAs) which are capable
// of signing certificates. Other modules can be configured to use
// the CAs defined by this app for issuing certificates or getting
// key information needed for establishing trust.
type PKI struct {
	// The certificate authorities to manage. Each CA is keyed by an
	// ID that is used to uniquely identify it from other CAs.
	// At runtime, the GetCA() method should be used instead to ensure
	// the default CA is provisioned if it hadn't already been.
	// The default CA ID is "local".
	CAs map[string]*CA `json:"certificate_authorities,omitempty"`

	ctx caddy.Context
	log *zap.Logger
}

// CaddyModule returns the Caddy module information.
func (PKI) CaddyModule() caddy.ModuleInfo {
	return caddy.ModuleInfo{
		ID:  "pki",
		New: func() caddy.Module { return new(PKI) },
	}
}

// Provision sets up the configuration for the PKI app.
func (p *PKI) Provision(ctx caddy.Context) error {
	p.ctx = ctx
	p.log = ctx.Logger()

	for caID, ca := range p.CAs {
		err := ca.Provision(ctx, caID, p.log)
		if err != nil {
			return fmt.Errorf("provisioning CA '%s': %v", caID, err)
		}
	}

	// if this app is initialized at all, ensure there's at
	// least a default CA that can be used: the standard CA
	// which is used implicitly for signing local-use certs
	if len(p.CAs) == 0 {
		err := p.ProvisionDefaultCA(ctx)
		if err != nil {
			return fmt.Errorf("provisioning CA '%s': %v", DefaultCAID, err)
		}
	}

	return nil
}

// ProvisionDefaultCA sets up the default CA.
func (p *PKI) ProvisionDefaultCA(ctx caddy.Context) error {
	if p.CAs == nil {
		p.CAs = make(map[string]*CA)
	}

	p.CAs[DefaultCAID] = new(CA)
	return p.CAs[DefaultCAID].Provision(ctx, DefaultCAID, p.log)
}

// Start starts the PKI app.
func (p *PKI) Start() error {
	// install roots to trust store, if not disabled
	for _, ca := range p.CAs {
		if ca.InstallTrust != nil && !*ca.InstallTrust {
			ca.log.Info("root certificate trust store installation disabled; unconfigured clients may show warnings",
				zap.String("path", ca.rootCertPath))
			continue
		}

		if err := ca.installRoot(); err != nil {
			// could be some system dependencies that are missing;
			// shouldn't totally prevent startup, but we should log it
			ca.log.Error("failed to install root certificate",
				zap.Error(err),
				zap.String("certificate_file", ca.rootCertPath))
		}
	}

	// see if root/intermediates need renewal...
	p.renewCerts()

	// ...and keep them renewed
	go p.maintenance()

	return nil
}

// Stop stops the PKI app.
func (p *PKI) Stop() error {
	return nil
}

// GetCA retrieves a CA by ID. If the ID is the default
// CA ID, and it hasn't been provisioned yet, it will
// be provisioned.
func (p *PKI) GetCA(ctx caddy.Context, id string) (*CA, error) {
	ca, ok := p.CAs[id]
	if !ok {
		// for anything other than the default CA ID, error out if it wasn't configured
		if id != DefaultCAID {
			return nil, fmt.Errorf("no certificate authority configured with id: %s", id)
		}

		// for the default CA ID, provision it, because we want it to "just work"
		err := p.ProvisionDefaultCA(ctx)
		if err != nil {
			return nil, fmt.Errorf("failed to provision default CA: %s", err)
		}
		ca = p.CAs[id]
	}

	return ca, nil
}

// Interface guards
var (
	_ caddy.Provisioner = (*PKI)(nil)
	_ caddy.App         = (*PKI)(nil)
)


text:
var NodeGit = require("../");
var Diff = NodeGit.Diff;
var Patch = NodeGit.Patch;

var _blobToBuffer = Diff.blobToBuffer;

/**
 * Directly run a diff between a blob and a buffer.
 * @async
 * @param {Blob} old_blob Blob for old side of diff, or NULL for empty blob
 * @param {String} old_as_path Treat old blob as if it had this filename;
 * can be NULL
 * @param {String} buffer Raw data for new side of diff, or NULL for empty
 * @param {String} buffer_as_path Treat buffer as if it had this filename;
 * can be NULL
 * @param {DiffOptions} opts Options for diff, or NULL for default options
 * @param {Function} file_cb Callback for "file"; made once if there is a diff;
 * can be NULL
 * @param {Function} binary_cb Callback for binary files; can be NULL
 * @param {Function} hunk_cb Callback for each hunk in diff; can be NULL
 * @param {Function} line_cb Callback for each line in diff; can be NULL
 */
Diff.blobToBuffer= function(
    old_blob,
    old_as_path,
    buffer,
    buffer_as_path,
    opts,
    file_cb,
    binary_cb,
    hunk_cb,
    line_cb) {
  var bufferText;
  var bufferLength;
  if (buffer instanceof Buffer) {
    bufferText = buffer.toString("utf8");
    bufferLength = Buffer.byteLength(buffer, "utf8");
  } else {
    bufferText = buffer;
    bufferLength = !buffer ? 0 : Buffer.byteLength(buffer, "utf8");
  }

  return _blobToBuffer.call(
    this,
    old_blob,
    old_as_path,
    bufferText,
    bufferLength,
    buffer_as_path,
    opts,
    file_cb,
    binary_cb,
    hunk_cb,
    line_cb,
    null);
};

/**
 * Retrieve patches in this difflist
 *
 * @async
 * @return {Array<ConvenientPatch>} a promise that resolves to an array of
 *                                      ConvenientPatches
 */
Diff.prototype.patches = function(idxs) {
  return Patch.convenientFromDiff(this, idxs);
};


text:
#pragma once

#include <ortc/internal/types.h>
#include <ortc/internal/ortc_ISecureTransport.h>
#include <ortc/internal/ortc_RTPPacket.h>

#include <ortc/IDTLSTransport.h>
#include <ortc/IICETransport.h>
#include <ortc/IRTPTypes.h>
#include <ortc/IMediaStreamTrack.h>
#include <ortc/IStatsProvider.h>

#include <zsLib/MessageQueueAssociator.h>
#include <zsLib/ITimer.h>

#include <webrtc/video_frame.h>

#define ORTC_SETTING_RTP_SENDER_CHANNEL_RETAG_RTP_PACKETS_AFTER_SSRC_NOT_SENT_IN_SECONDS "ortc/rtp-sender-channel/retag-rtp-packets-after-ssrc-not-sent-in-seconds"
#define ORTC_SETTING_RTP_SENDER_CHANNEL_TAG_MID_RID_IN_RTCP_SDES "ortc/rtp-sender-channel/tag-mid-rid-in-rtcp-sdes"

namespace ortc
{
  namespace internal
  {
    ZS_DECLARE_INTERACTION_PTR(IRTPSenderChannelForRTPSender);
    ZS_DECLARE_INTERACTION_PTR(IRTPSenderChannelForRTPSenderChannelMediaBase);
    ZS_DECLARE_INTERACTION_PTR(IRTPSenderChannelForRTPSenderChannelAudio);
    ZS_DECLARE_INTERACTION_PTR(IRTPSenderChannelForRTPSenderChannelVideo);
    ZS_DECLARE_INTERACTION_PTR(IRTPSenderChannelForMediaStreamTrack);

    ZS_DECLARE_INTERACTION_PTR(IRTPSenderForRTPSenderChannel);
    ZS_DECLARE_INTERACTION_PTR(IRTPSenderChannelMediaBaseForRTPSenderChannel);
    ZS_DECLARE_INTERACTION_PTR(IRTPSenderChannelAudioForRTPSenderChannel);
    ZS_DECLARE_INTERACTION_PTR(IRTPSenderChannelVideoForRTPSenderChannel);
    ZS_DECLARE_INTERACTION_PTR(IMediaStreamTrackForRTPSenderChannel);

    ZS_DECLARE_INTERACTION_PROXY(IRTPSenderChannelAsyncDelegate);

    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    #pragma mark
    #pragma mark IRTPSenderChannelForRTPSender
    #pragma mark

    interaction IRTPSenderChannelForRTPSender
    {
      ZS_DECLARE_TYPEDEF_PTR(IRTPSenderChannelForRTPSender, ForRTPSender);

      ZS_DECLARE_TYPEDEF_PTR(IRTPTypes::Parameters, Parameters);
      typedef std::list<RTCPPacketPtr> RTCPPacketList;
      ZS_DECLARE_PTR(RTCPPacketList);

      ZS_DECLARE_TYPEDEF_PTR(IStatsProviderTypes::PromiseWithStatsReport, PromiseWithStatsReport);
      ZS_DECLARE_TYPEDEF_PTR(IStatsReportTypes::StatsTypeSet, StatsTypeSet)

      static ElementPtr toDebug(ForRTPSenderPtr object);

      static RTPSenderChannelPtr create(
                                        RTPSenderPtr sender,
                                        MediaStreamTrackPtr track,
                                        const Parameters &params
                                        );

      virtual PUID getID() const = 0;

      virtual void notifyTrackChanged(MediaStreamTrackPtr track) = 0;

      virtual void notifyTransportState(ISecureTransportTypes::States state) = 0;

      virtual void notifyPackets(RTCPPacketListPtr packets) = 0;

      virtual void notifyUpdate(const Parameters &params) = 0;

      virtual bool handlePacket(RTCPPacketPtr packet) = 0;

      virtual void requestStats(PromiseWithStatsReportPtr promise, const StatsTypeSet &stats) = 0;

      virtual void insertDTMF(
                              const char *tones,
                              Milliseconds duration,
                              Milliseconds interToneGap
                              ) = 0;

      virtual String toneBuffer() const = 0;
      virtual Milliseconds duration() const = 0;
      virtual Milliseconds interToneGap() const = 0;
    };

    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    #pragma mark
    #pragma mark IRTPSenderChannelForRTPSenderChannelMediaBase
    #pragma mark

    interaction IRTPSenderChannelForRTPSenderChannelMediaBase
    {
      ZS_DECLARE_TYPEDEF_PTR(IRTPSenderChannelForRTPSenderChannelMediaBase, ForRTPSenderChannelMediaBase)

      virtual PUID getID() const = 0;

      virtual bool sendPacket(RTPPacketPtr packet) = 0;

      virtual bool sendPacket(RTCPPacketPtr packet) = 0;
    };

    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    #pragma mark
    #pragma mark IRTPSenderChannelForRTPSenderChannelAudio
    #pragma mark

    interaction IRTPSenderChannelForRTPSenderChannelAudio : public IRTPSenderChannelForRTPSenderChannelMediaBase
    {
      ZS_DECLARE_TYPEDEF_PTR(IRTPSenderChannelForRTPSenderChannelAudio, ForRTPSenderChannelAudio)

      virtual void notifyDTMFSenderToneChanged(const char *tone) = 0;
    };

    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    #pragma mark
    #pragma mark IRTPSenderChannelForRTPSenderChannelVideo
    #pragma mark

    interaction IRTPSenderChannelForRTPSenderChannelVideo : public IRTPSenderChannelForRTPSenderChannelMediaBase
    {
      ZS_DECLARE_TYPEDEF_PTR(IRTPSenderChannelForRTPSenderChannelVideo, ForRTPSenderChannelVideo)
    };

    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    #pragma mark
    #pragma mark IRTPSenderChannelForMediaStreamTrack
    #pragma mark

    interaction IRTPSenderChannelForMediaStreamTrack
    {
      ZS_DECLARE_TYPEDEF_PTR(IRTPSenderChannelForMediaStreamTrack, ForMediaStreamTrack)
      ZS_DECLARE_TYPEDEF_PTR(webrtc::VideoFrame, VideoFrame);

      static ElementPtr toDebug(ForMediaStreamTrackPtr object);

      virtual PUID getID() const = 0;

      virtual int32_t sendAudioSamples(
                                       const void* audioSamples,
                                       const size_t numberOfSamples,
                                       const uint8_t numberOfChannels
                                       ) = 0;

      virtual void sendVideoFrame(VideoFramePtr videoFrame) = 0;
    };

    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    #pragma mark
    #pragma mark IRTPSenderChannelAsyncDelegate
    #pragma mark

    interaction IRTPSenderChannelAsyncDelegate
    {
      typedef std::list<RTCPPacketPtr> RTCPPacketList;
      ZS_DECLARE_PTR(RTCPPacketList);
      ZS_DECLARE_TYPEDEF_PTR(IRTPTypes::Parameters, Parameters);

      ZS_DECLARE_TYPEDEF_PTR(IMediaStreamTrackForRTPSenderChannel, UseMediaStreamTrack);

      virtual void onTrackChanged(UseMediaStreamTrackPtr track) = 0;

      virtual void onSecureTransportState(ISecureTransport::States state) = 0;

      virtual void onNotifyPackets(RTCPPacketListPtr packets) = 0;

      virtual void onUpdate(ParametersPtr params) = 0;
    };

    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    #pragma mark
    #pragma mark RTPSenderChannel
    #pragma mark

    class RTPSenderChannel : public Noop,
                             public MessageQueueAssociator,
                             public SharedRecursiveLock,
                             public IRTPSenderChannelForRTPSender,
                             public IRTPSenderChannelForRTPSenderChannelAudio,
                             public IRTPSenderChannelForRTPSenderChannelVideo,
                             public IRTPSenderChannelForMediaStreamTrack,
                             public IWakeDelegate,
                             public zsLib::ITimerDelegate,
                             public IRTPSenderChannelAsyncDelegate
    {
    protected:
      struct make_private {};

    public:
      friend interaction IRTPSenderChannel;
      friend interaction IRTPSenderChannelFactory;
      friend interaction IRTPSenderChannelForRTPSender;
      friend interaction IRTPSenderChannelForRTPSenderChannelMediaBase;
      friend interaction IRTPSenderChannelForRTPSenderChannelAudio;
      friend interaction IRTPSenderChannelForRTPSenderChannelVideo;
      friend interaction IRTPSenderChannelForMediaStreamTrack;

      ZS_DECLARE_TYPEDEF_PTR(IRTPSenderForRTPSenderChannel, UseSender);
      ZS_DECLARE_TYPEDEF_PTR(IMediaStreamTrackForRTPSenderChannel, UseMediaStreamTrack);
      ZS_DECLARE_TYPEDEF_PTR(webrtc::VideoFrame, VideoFrame);

      ZS_DECLARE_TYPEDEF_PTR(IRTPSenderChannelMediaBaseForRTPSenderChannel, UseMediaBase);
      ZS_DECLARE_TYPEDEF_PTR(IRTPSenderChannelAudioForRTPSenderChannel, UseAudio);
      ZS_DECLARE_TYPEDEF_PTR(IRTPSenderChannelVideoForRTPSenderChannel, UseVideo);

      ZS_DECLARE_TYPEDEF_PTR(IRTPTypes::Parameters, Parameters)
      typedef std::list<RTCPPacketPtr> RTCPPacketList;
      ZS_DECLARE_PTR(RTCPPacketList);

      typedef IRTPTypes::SSRCType SSRCType;
      ZS_DECLARE_TYPEDEF_PTR(IRTPTypes::HeaderExtensionParameters, HeaderExtensionParameters)

      enum States
      {
        State_Pending,
        State_Ready,
        State_ShuttingDown,
        State_Shutdown,
      };
      static const char *toString(States state);

      ZS_DECLARE_STRUCT_PTR(TaggingInfo)

      struct TaggingInfo
      {
        SSRCType mSSRC {};

        Time mLastSentPacket {};

        bool mReceiverAck {};
        WORD mSequenceNumberFirst {};
        WORD mSequenceNumberLast {};
      };

      typedef std::map<SSRCType, TaggingInfoPtr> TaggingMap;

    public:
      RTPSenderChannel(
                       const make_private &,
                       IMessageQueuePtr queue,
                       UseSenderPtr sender,
                       UseMediaStreamTrackPtr track,
                       const Parameters &params
                       );

    protected:
      RTPSenderChannel(Noop, IMessageQueuePtr queue = IMessageQueuePtr()) :
        Noop(true),
        MessageQueueAssociator(queue),
        SharedRecursiveLock(SharedRecursiveLock::create())
      {}

      void init();

    public:
      virtual ~RTPSenderChannel();

      static RTPSenderChannelPtr convert(ForRTPSenderPtr object);
      static RTPSenderChannelPtr convert(ForRTPSenderChannelMediaBasePtr object);
      static RTPSenderChannelPtr convert(ForRTPSenderChannelAudioPtr object);
      static RTPSenderChannelPtr convert(ForRTPSenderChannelVideoPtr object);
      static RTPSenderChannelPtr convert(ForMediaStreamTrackPtr object);

    protected:
      //-----------------------------------------------------------------------
      #pragma mark
      #pragma mark RTPSenderChannel => IRTPSenderChannelForRTPSender
      #pragma mark

      static ElementPtr toDebug(RTPSenderChannelPtr receiver);

      static RTPSenderChannelPtr create(
                                        RTPSenderPtr sender,
                                        MediaStreamTrackPtr track,
                                        const Parameters &params
                                        );

      virtual PUID getID() const override {return mID;}

      virtual void notifyTrackChanged(MediaStreamTrackPtr track) override;

      virtual void notifyTransportState(ISecureTransportTypes::States state) override;

      virtual void notifyPackets(RTCPPacketListPtr packets) override;

      virtual void notifyUpdate(const Parameters &params) override;

      virtual bool handlePacket(RTCPPacketPtr packet) override;

      virtual void requestStats(PromiseWithStatsReportPtr promise, const StatsTypeSet &stats) override;

      virtual void insertDTMF(
                              const char *tones,
                              Milliseconds duration,
                              Milliseconds interToneGap
                              ) override;

      virtual String toneBuffer() const override;
      virtual Milliseconds duration() const override;
      virtual Milliseconds interToneGap() const override;

      //-----------------------------------------------------------------------
      #pragma mark
      #pragma mark RTPSenderChannel => IRTPSenderChannelForRTPSenderChannelMediaBase
      #pragma mark

      // (duplicate) virtual PUID getID() const = 0;

      virtual bool sendPacket(RTPPacketPtr packet) override;

      virtual bool sendPacket(RTCPPacketPtr packet) override;

      //-----------------------------------------------------------------------
      #pragma mark
      #pragma mark RTPSenderChannel => IRTPSenderChannelForRTPSenderChannelAudio
      #pragma mark

      virtual void notifyDTMFSenderToneChanged(const char *tone) override;

      //-----------------------------------------------------------------------
      #pragma mark
      #pragma mark RTPSenderChannel => IRTPSenderChannelForRTPSenderChannelVideo
      #pragma mark

      //-----------------------------------------------------------------------
      #pragma mark
      #pragma mark RTPSenderChannel => IRTPSenderChannelForMediaStreamTrack
      #pragma mark

      // (duplicate) static ElementPtr toDebug(ForMediaStreamTrackPtr receiver);

      // (duplicate) virtual PUID getID() const = 0;

      virtual int32_t sendAudioSamples(
                                       const void* audioSamples,
                                       const size_t numberOfSamples,
                                       const uint8_t numberOfChannels
                                       ) override;

      virtual void sendVideoFrame(VideoFramePtr videoFrame) override;

      //-----------------------------------------------------------------------
      #pragma mark
      #pragma mark RTPSenderChannel => IWakeDelegate
      #pragma mark

      virtual void onWake() override;

      //-----------------------------------------------------------------------
      #pragma mark
      #pragma mark RTPSenderChannel => ITimerDelegate
      #pragma mark

      virtual void onTimer(ITimerPtr timer) override;

      //-----------------------------------------------------------------------
      #pragma mark
      #pragma mark RTPSenderChannel => IRTPSenderChannelAsyncDelegate
      #pragma mark

      virtual void onTrackChanged(UseMediaStreamTrackPtr track) override;

      virtual void onSecureTransportState(ISecureTransport::States state) override;

      virtual void onNotifyPackets(RTCPPacketListPtr packets) override;

      virtual void onUpdate(ParametersPtr params) override;

    protected:
      //-----------------------------------------------------------------------
      #pragma mark
      #pragma mark RTPSenderChannel => (internal)
      #pragma mark

      Log::Params log(const char *message) const;
      Log::Params debug(const char *message) const;
      virtual ElementPtr toDebug() const;

      bool isShuttingDown() const;
      bool isShutdown() const;

      void step();

      void cancel();

      void setState(States state);
      void setError(WORD error, const char *reason = NULL);

      void setupTagging();

    protected:
      //-----------------------------------------------------------------------
      #pragma mark
      #pragma mark RTPSenderChannel => (data)
      #pragma mark

      AutoPUID mID;
      RTPSenderChannelWeakPtr mThisWeak;
      RTPSenderChannelPtr mGracefulShutdownReference;

      States mCurrentState {State_Pending};

      WORD mLastError {};
      String mLastErrorReason;

      ISecureTransport::States mSecureTransportState {ISecureTransport::State_Pending};

      UseSenderWeakPtr mSender;

      ParametersPtr mParameters;

      // tagging options
      HeaderExtensionParametersPtr mMuxHeader;
      HeaderExtensionParametersPtr mRIDHeader;
      std::atomic<bool> mIsTagging {false};
      String mMuxID;
      String mRID;
      String mHeaderHash;

      Seconds mRetagAfterInSeconds {};
      bool mTagSDES {false};
      TaggingMap mTaggings;

      Optional<IMediaStreamTrackTypes::Kinds> mKind;
      UseMediaStreamTrackPtr mTrack;

      // NO lockk is needed:
      UseMediaBasePtr mMediaBase; // valid
      UseAudioPtr mAudio; // either
      UseVideoPtr mVideo; // or valid
    };

    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    #pragma mark
    #pragma mark IRTPSenderChannelFactory
    #pragma mark

    interaction IRTPSenderChannelFactory
    {
      ZS_DECLARE_TYPEDEF_PTR(IRTPTypes::Parameters, Parameters)

      static IRTPSenderChannelFactory &singleton();

      virtual RTPSenderChannelPtr create(
                                         RTPSenderPtr sender,
                                         MediaStreamTrackPtr track,
                                         const Parameters &params
                                         );
    };

    class RTPSenderChannelFactory : public IFactory<IRTPSenderChannelFactory> {};
  }
}

ZS_DECLARE_PROXY_BEGIN(ortc::internal::IRTPSenderChannelAsyncDelegate)
ZS_DECLARE_PROXY_TYPEDEF(ortc::internal::ISecureTransport::States, States)
ZS_DECLARE_PROXY_TYPEDEF(ortc::internal::IRTPSenderChannelAsyncDelegate::RTCPPacketListPtr, RTCPPacketListPtr)
ZS_DECLARE_PROXY_TYPEDEF(ortc::internal::IRTPSenderChannelAsyncDelegate::ParametersPtr, ParametersPtr)
ZS_DECLARE_PROXY_TYPEDEF(ortc::internal::IMediaStreamTrackForRTPSenderChannel, UseMediaStreamTrack)
ZS_DECLARE_PROXY_METHOD_1(onTrackChanged, UseMediaStreamTrackPtr)
ZS_DECLARE_PROXY_METHOD_1(onSecureTransportState, States)
ZS_DECLARE_PROXY_METHOD_1(onNotifyPackets, RTCPPacketListPtr)
ZS_DECLARE_PROXY_METHOD_1(onUpdate, ParametersPtr)
ZS_DECLARE_PROXY_END()


text:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<artifactId>spring-plugin-core</artifactId>

	<name>Spring Plugin - Core</name>
	<description>Core plugin infrastructure</description>

	<parent>
		<groupId>org.springframework.plugin</groupId>
		<artifactId>spring-plugin</artifactId>
		<version>3.1.0-SNAPSHOT</version>
	</parent>

	<properties>
		<java-module-name>spring.plugin.core</java-module-name>
	</properties>

	<build>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
			</resource>
			<resource>
				<directory>../src/main/resources</directory>
				<filtering>true</filtering>
				<targetPath>META-INF</targetPath>
			</resource>
		</resources>
	</build>

	<dependencies>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
			<scope>test</scope>
		</dependency>

	</dependencies>

</project>


text:
@interface FullScreenVC : UIViewController

@property (strong, nonatomic) UIImage *image;
@property (strong, nonatomic) NSString *name;

+ (UINavigationController*)returnFromStorybord;
- (instancetype)init NS_UNAVAILABLE;
- (void)setFullSizeImage:(UIImage *)image;
@end


text:
package magpie.data.utilities.filters;

import java.util.Arrays;
import java.util.List;
import magpie.data.Dataset;
import magpie.optimization.rankers.BaseEntryRanker;
import magpie.optimization.rankers.MultiObjectiveEntryRanker;
import magpie.user.CommandHandler;

/**
 * Filter entries based on ranking. Lets the user pick the number of entries to keep,
 * whether to select the best or lest fit, use the predicted or measured class, and then
 * the overall method and its options. Consequently, the "Usage" is pretty messy.
 *
 * <usage>
Usage: <number> <maximum|minimum> <predicted|measured> <method> [<options...>]
 *
<pr>number: Number of entries that pass this filter
 *
<pr>maximum|minimum: Whether entries with a max/minimum value pass the filter
 *
<pr>method: Objective function used to rank entries. Name of an {@link BaseEntryRanker} ("?" for available methods)
 *
<pr>options...: Any options for the objective function</usage>
 *
 * @author Logan Ward
 * @version 0.1
 */
public class EntryRankerFilter extends BaseDatasetFilter {
    /** Number of entries to filter */
    private int NumberToFilter = 200;
    /** Entry ranking method */
    private BaseEntryRanker Ranker;

    @Override
    public void setOptions(List<Object> OptionsObj) throws Exception {
        String Options[] = CommandHandler.convertCommandToString(OptionsObj);
        String RankingMethod;
        List<Object> MethodOptions;
		boolean measured;
		boolean maximize;
        try {
            NumberToFilter = Integer.parseInt(Options[0]);
            if (Options[1].toLowerCase().contains("max"))
                maximize = true;
            else if (Options[1].toLowerCase().contains("min"))
                maximize = false;
            else
                throw new Exception();

            if (Options[2].equalsIgnoreCase("measured"))
                measured = true;
            else if (Options[2].equalsIgnoreCase("predicted"))
                measured = false;
            else
                throw new Exception();

            RankingMethod = Options[3];
            MethodOptions = OptionsObj.subList(4, Options.length);
        } catch (Exception e) {
            throw new Exception(printUsage());
        }

        // Make the BaseEntryRanker
        Ranker = (BaseEntryRanker) CommandHandler.instantiateClass("optimization.rankers." + RankingMethod, MethodOptions);
        Ranker.setMaximizeFunction(maximize);
        Ranker.setUseMeasured(measured);
    }

    @Override
    public String printUsage() {
        return "Usage: <number> <maximum|minimum> <predicted|measured> <EntryRanker Method> <Method Options...>";
    }

    /**
     * Define how many entries will pass this filter
     * @param NumberToFilter Desired number
     */
    public void setNumberToFilter(int NumberToFilter) {
        this.NumberToFilter = NumberToFilter;
    }

	/**
	 * Define the ranker used to filter entries.
	 * @param Ranker Desired ranker
	 */
	public void setRanker(BaseEntryRanker Ranker) {
		this.Ranker = Ranker;
	}

    @Override
    public void train(Dataset TrainingSet) {
        Ranker.train(TrainingSet);
    }

    @Override
    protected boolean[] label(Dataset D) {
        if (Ranker.isUsingMeasured() && (! D.getEntry(0).hasMeasurement()))
            throw new Error("Missing measured class.");
        if (! Ranker.isUsingMeasured() && (! D.getEntry(0).hasPrediction()))
            throw new Error("Missing predicted class.");

        boolean[] passes = new boolean[D.NEntries()];
        Arrays.fill(passes, false);
        int[] ranks = Ranker.rankEntries(D);
        for (int i=0; i<Math.min(NumberToFilter, D.NEntries()); i++) passes[ranks[i]] = true;
        return passes;
    }

}


text:
package goscp

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"log"
	"os"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"
	"time"

	"github.com/cheggaaa/pb"
	"golang.org/x/crypto/ssh"
)

var (
	// SCP messages
	fileCopyRx  = regexp.MustCompile(`C(?P<mode>\d{4}) (?P<length>\d+) (?P<filename>.+)`)
	dirCopyRx   = regexp.MustCompile(`D(?P<mode>\d{4}) (?P<length>\d+) (?P<dirname>.+)`)
	timestampRx = regexp.MustCompile(`T(?P<mtime>\d+) 0 (?P<atime>\d+) 0`)
	endDir      = "E"
)

// Client wraps a ssh.Client and provides additional functionality.
type Client struct {
	SSHClient       *ssh.Client
	DestinationPath []string

	// Errors that have occurred while communicating with host
	errors []error

	// Verbose output when communicating with host
	Verbose bool

	// Stop transfer on OS error - occurs during filepath.Walk
	StopOnOSError bool

	// Show progress bar
	ShowProgressBar bool

	// Configurable progress bar
	ProgressBar *pb.ProgressBar

	// Stdin for SSH session
	scpStdinPipe io.WriteCloser

	// Stdout for SSH session
	scpStdoutPipe *readCanceller
}

// NewClient returns a ssh.Client wrapper.
// DestinationPath is set to the current directory by default.
func NewClient(c *ssh.Client) *Client {
	scpc := &Client{
		SSHClient:       c,
		DestinationPath: []string{"."},
		ShowProgressBar: true,
	}

	// Total is set before progress starts
	scpc.ProgressBar = scpc.newDefaultProgressBar(0)

	return scpc
}

// SetDestinationPath sets where content will be sent.
func (c *Client) SetDestinationPath(path string) {
	c.DestinationPath = []string{path}
}

func (c *Client) addError(err error) {
	c.errors = append(c.errors, err)
}

// GetLastError should be queried after a call to Download() or Upload().
func (c *Client) GetLastError() error {
	if len(c.errors) > 0 {
		return c.errors[len(c.errors)-1]
	}
	return nil
}

// GetErrorStack returns all errors that have occurred so far.
func (c *Client) GetErrorStack() []error {
	return c.errors
}

// Cancel an ongoing operation.
func (c *Client) Cancel() {
	if c.scpStdoutPipe != nil {
		close(c.scpStdoutPipe.cancel)
	}
}

// Download remotePath to c.DestinationPath.
func (c *Client) Download(remotePath string) {
	session, err := c.SSHClient.NewSession()
	if err != nil {
		c.addError(err)
		return
	}
	defer session.Close()

	go c.handleDownload(session)

	cmd := fmt.Sprintf("scp -rf %s", fmt.Sprintf("%q", remotePath))
	if err := session.Run(cmd); err != nil {
		c.addError(err)
		return
	}

	return
}

// handleDownload handles message parsing to and from the session.
func (c *Client) handleDownload(session *ssh.Session) {
	var err error

	c.scpStdinPipe, err = session.StdinPipe()
	if err != nil {
		c.addError(err)
		return
	}
	defer c.scpStdinPipe.Close()

	r, err := session.StdoutPipe()
	if err != nil {
		c.addError(err)
		return
	}

	// Initialize transfer
	c.sendAck(c.scpStdinPipe)

	// Wrapper to support cancellation
	c.scpStdoutPipe = &readCanceller{
		Reader: bufio.NewReader(r),
		cancel: make(chan struct{}, 1),
	}

	for {
		c.outputInfo("Reading message from source")
		msg, err := c.scpStdoutPipe.ReadString('\n')
		if err != nil {
			if err != io.EOF {
				c.addError(err)
			}
			return
		}

		// Strip nulls and new lines
		msg = strings.TrimSpace(strings.Trim(msg, "\x00"))
		c.outputInfo(fmt.Sprintf("Received: %s", msg))

		// Confirm message
		c.sendAck(c.scpStdinPipe)

		switch {
		case c.isFileCopyMsg(msg):
			// Handle incoming file
			err := c.file(msg)
			if err != nil {
				c.addError(err)
				return
			}
		case c.isDirCopyMsg(msg):
			// Handling incoming directory
			err := c.directory(msg)
			if err != nil {
				c.addError(err)
				return
			}
		case msg == endDir:
			// Directory finished, go up a directory
			c.upDirectory()
		case c.isWarningMsg(msg):
			c.addError(fmt.Errorf("Warning message: [%q]\n", msg))
			return
		case c.isErrorMsg(msg):
			c.addError(fmt.Errorf("Error message: [%q]\n", msg))
			return
		default:
			c.addError(fmt.Errorf("Unhandled message: [%q]\n", msg))
			return
		}

		// Confirm message
		c.sendAck(c.scpStdinPipe)
	}
}

// Upload localPath to c.DestinationPath.
func (c *Client) Upload(localPath string) {
	session, err := c.SSHClient.NewSession()
	if err != nil {
		c.addError(err)
		return
	}
	defer session.Close()

	go c.handleUpload(session, localPath)

	cmd := fmt.Sprintf("scp -rt %s", fmt.Sprintf("%q", filepath.Join(c.DestinationPath...)))
	if err := session.Run(cmd); err != nil {
		c.addError(err)
		return
	}

	return
}

// handleDownload handles message parsing to and from the session.
func (c *Client) handleUpload(session *ssh.Session, localPath string) {
	var err error

	c.scpStdinPipe, err = session.StdinPipe()
	if err != nil {
		c.addError(err)
		return
	}
	defer c.scpStdinPipe.Close()

	r, err := session.StdoutPipe()
	if err != nil {
		c.addError(err)
		return
	}

	// Wrapper to support cancellation
	c.scpStdoutPipe = &readCanceller{
		Reader: bufio.NewReader(r),
		cancel: make(chan struct{}, 1),
	}

	// This has already been used in the cmd call below
	// so it can be reused for 'end of directory' message handling
	c.DestinationPath = []string{}

	err = filepath.Walk(localPath, c.handleItem)
	if err != nil {
		c.addError(err)
		return
	}

	// End transfer
	paths := strings.Split(c.DestinationPath[0], "/")
	for range paths {
		c.sendEndOfDirectoryMessage(c.scpStdinPipe)
	}
}

// Send an acknowledgment message.
func (c *Client) sendAck(w io.Writer) {
	fmt.Fprint(w, "\x00")
}

// Send an error message.
func (c *Client) sendErr(w io.Writer) {
	fmt.Fprint(w, "\x02")
}

// Check if an incoming message is a file copy message.
func (c *Client) isFileCopyMsg(s string) bool {
	return strings.HasPrefix(s, "C")
}

// Check if an incoming message is a directory copy message.
func (c *Client) isDirCopyMsg(s string) bool {
	return strings.HasPrefix(s, "D")
}

// Check if an incoming message is a warning.
func (c *Client) isWarningMsg(s string) bool {
	return strings.HasPrefix(s, "\x01")
}

// Check if an incoming message is an error.
func (c *Client) isErrorMsg(s string) bool {
	return strings.HasPrefix(s, "\x02")
}

// Send a directory message while in source mode.
func (c *Client) sendDirectoryMessage(w io.Writer, mode os.FileMode, dirname string) {
	msg := fmt.Sprintf("D0%o 0 %s", mode, dirname)
	fmt.Fprintln(w, msg)
	c.outputInfo(fmt.Sprintf("Sent: %s", msg))
}

// Send a end of directory message while in source mode.
func (c *Client) sendEndOfDirectoryMessage(w io.Writer) {
	msg := endDir
	fmt.Fprintln(w, msg)
	c.outputInfo(fmt.Sprintf("Sent: %s", msg))
}

// Send a file message while in source mode.
func (c *Client) sendFileMessage(w io.Writer, mode os.FileMode, size int64, filename string) {
	msg := fmt.Sprintf("C0%o %d %s", mode, size, filename)
	fmt.Fprintln(w, msg)
	c.outputInfo(fmt.Sprintf("Sent: %s", msg))
}

// Handle directory copy message in sink mode.
func (c *Client) directory(msg string) error {
	parts, err := c.parseMessage(msg, dirCopyRx)
	if err != nil {
		return err
	}

	err = os.Mkdir(filepath.Join(c.DestinationPath...)+string(filepath.Separator)+parts["dirname"], 0755)
	if err != nil {
		return err
	}

	// Traverse into directory
	c.DestinationPath = append(c.DestinationPath, parts["dirname"])

	return nil
}

// Handle file copy message in sink mode.
func (c *Client) file(msg string) error {
	parts, err := c.parseMessage(msg, fileCopyRx)
	if err != nil {
		return err
	}

	fileLen, _ := strconv.Atoi(parts["length"])

	// Create local file
	localFile, err := os.Create(filepath.Join(c.DestinationPath...) + string(filepath.Separator) + parts["filename"])
	if err != nil {
		return err
	}
	defer localFile.Close()

	var w io.Writer
	if c.ShowProgressBar {
		bar := c.newProgressBar(fileLen)
		bar.Start()
		defer bar.Finish()

		w = io.MultiWriter(localFile, bar)
	} else {
		w = localFile
	}

	if n, err := io.CopyN(w, c.scpStdoutPipe, int64(fileLen)); err != nil || n < int64(fileLen) {
		c.sendErr(c.scpStdinPipe)
		return err
	}

	return nil
}

// Break down incoming protocol messages.
func (c *Client) parseMessage(msg string, rx *regexp.Regexp) (map[string]string, error) {
	parts := make(map[string]string)
	matches := rx.FindStringSubmatch(msg)
	if len(matches) == 0 {
		return parts, errors.New("Could not parse protocol message: " + msg)
	}

	for i, name := range rx.SubexpNames() {
		parts[name] = matches[i]
	}
	return parts, nil
}

// Go back up one directory.
func (c *Client) upDirectory() {
	if len(c.DestinationPath) > 0 {
		c.DestinationPath = c.DestinationPath[:len(c.DestinationPath)-1]
	}
}

// Handle each item coming through filepath.Walk.
func (c *Client) handleItem(path string, info os.FileInfo, err error) error {
	if err != nil {
		// OS error
		c.outputInfo(fmt.Sprintf("Item error: %s", err))

		if c.StopOnOSError {
			return err
		}
		return nil
	}

	if info.IsDir() {
		// Handle directories
		if len(c.DestinationPath) != 0 {
			// If not first directory
			currentPath := strings.Split(filepath.Join(c.DestinationPath...), "/")
			newPath := strings.Split(path, "/")

			// <= slashes = going back up
			if len(newPath) <= len(currentPath) {
				// Send EOD messages for the amount of directories we go up
				for i := len(newPath) - 1; i < len(currentPath); i++ {
					c.sendEndOfDirectoryMessage(c.scpStdinPipe)
				}
			}
		}
		c.DestinationPath = []string{path}
		c.sendDirectoryMessage(c.scpStdinPipe, 0644, filepath.Base(path))
	} else {
		// Handle regular files
		targetItem, err := os.Open(path)
		if err != nil {
			return err
		}

		c.sendFileMessage(c.scpStdinPipe, 0644, info.Size(), filepath.Base(path))

		if info.Size() > 0 {
			var w io.Writer
			if c.ShowProgressBar {
				bar := c.newProgressBar(int(info.Size()))
				bar.Start()
				defer bar.Finish()

				w = io.MultiWriter(c.scpStdinPipe, bar)
			} else {
				w = c.scpStdinPipe
			}

			c.outputInfo(fmt.Sprintf("Sending file: %s", path))
			if _, err := io.Copy(w, targetItem); err != nil {
				c.sendErr(c.scpStdinPipe)
				return err
			}

			c.sendAck(c.scpStdinPipe)
		} else {
			c.outputInfo(fmt.Sprintf("Sending empty file: %s", path))
			c.sendAck(c.scpStdinPipe)
		}
	}

	return nil
}

func (c *Client) outputInfo(s ...string) {
	if c.Verbose {
		log.Println(s)
	}
}

// Create a default progress bar.
func (c *Client) newDefaultProgressBar(fileLength int) *pb.ProgressBar {
	bar := pb.New(fileLength)
	bar.ShowSpeed = true
	bar.ShowTimeLeft = true
	bar.ShowCounters = true
	bar.Units = pb.U_BYTES
	bar.SetRefreshRate(time.Second)
	bar.SetWidth(80)
	bar.SetMaxWidth(80)

	return bar
}

// Creates a new progress bar based on the current settings.
func (c *Client) newProgressBar(fileLength int) *pb.ProgressBar {
	bar := pb.New(fileLength)
	bar.ShowPercent = c.ProgressBar.ShowPercent
	bar.ShowCounters = c.ProgressBar.ShowCounters
	bar.ShowSpeed = c.ProgressBar.ShowSpeed
	bar.ShowTimeLeft = c.ProgressBar.ShowTimeLeft
	bar.ShowBar = c.ProgressBar.ShowBar
	bar.ShowFinalTime = c.ProgressBar.ShowFinalTime
	bar.Output = c.ProgressBar.Output
	bar.Callback = c.ProgressBar.Callback
	bar.NotPrint = c.ProgressBar.NotPrint
	bar.Units = c.ProgressBar.Units
	bar.ForceWidth = c.ProgressBar.ForceWidth
	bar.ManualUpdate = c.ProgressBar.ManualUpdate
	bar.SetRefreshRate(c.ProgressBar.RefreshRate)
	bar.SetWidth(c.ProgressBar.Width)
	bar.SetMaxWidth(c.ProgressBar.Width)

	return bar
}

// Wrapper to support cancellation.
type readCanceller struct {
	*bufio.Reader

	// Cancel an ongoing transfer
	cancel chan struct{}
}

// Additional cancellation check.
func (r *readCanceller) Read(p []byte) (n int, err error) {
	select {
	case <-r.cancel:
		return 0, errors.New("Transfer cancelled")
	default:
		return r.Reader.Read(p)
	}
}