text:
"""Module to manipulate certificates in an OS X Keychain, and related functions.

Importing this will run LoginKeychain(), which tries to determine the importing
user's login keychain and sets the global variable login_keychain accordingly.
"""

import datetime
import logging
import operator
import os
import re
import shutil
import tempfile
from . import gmacpyutil
from . import getauth

CMD_OPENSSL = '/usr/bin/openssl'
CMD_SECURITY = '/usr/bin/security'
PEM_HEADER = '-----BEGIN CERTIFICATE-----'
PEM_FOOTER = '-----END CERTIFICATE-----'
OPENSSL_DATETIME_FORMAT = '%b %d %H:%M:%S %Y %Z'
SYSTEM_KEYCHAIN = '/Library/Keychains/System.keychain'
login_keychain = None

class Error(Exception):
  """Base error class."""

class CertError(Error):
  """Module specific exception class."""

class KeychainError(Error):
  """Module specific exception class."""

class Certificate(object):
  """Basic certificate object.

  The Certificate constructor accepts a string of PEM-encoded data. The object
  will populate with attributes decoded from the PEM.

  Certificate objects will have these read-only attributes:
  certhash        hash of the certificate's subject name
  subject         string representation of the certificate's full subject
  subject_cn      the CN of the subject
  serial          string representation of the certificate's serial number
  issuer          string representation of the certificate's full issuer
  issuer_cn       the CN of the issuer
  startdate       list, the notBefore date and a datettime object of the same
  enddate         list, the notAfter date and a datettime object of the same
  fingerprint     digest of the DER encoded version of the whole certificate
  osx_fingerprint fingerprint with the ':' removed

  .. and may have this read-only attribute:
  email           email addresses

  Attributes:
    pem: str, PEM-encoded data
  """

  def __init__(self, pem):
    self.pem = pem
    self._ParsePEMCertificate(pem)

  def get(self, key):  # pylint: disable=g-bad-name
    return self.__dict__.get(key, None)

  def _ParsePEMCertificate(self, pem):
    """Reads data from PEM encoded certificate.

    This method take a PEM-encoded certificate and parses it with openssl x509.
    It adds attributes to the object from the parsed output. We feed the PEM
    data though openssl x509 once, with a given order of requested attributes,
    and then parse the output to retrieve the attributes.

    For issuer and subject, the output is "<attribute>= <value>", and we set the
    attribute to <value>.
    For issuer_cn and subject_cn, we get "<attribute>= /X=ZZ/CN=VALUE" and
    set <attribute>_cn VALUE. If the CN is not where we expect, don't set the
    attribute.
    For fingerprint, the output is "SHA1 Fingerprint=fi:ng:er:pr:in:tt", and we
    set fingerprint to "fi:ng:er:pr:in:tt" and osx_fingerprint to
    "fingerprintt".
    For startdate and enddate, we get "notAfter=Apr 29 18:09:17 2036 GMT" and
    make a list. Index 0 has the full date string, index 1 is a datetime object
    parsed from the string. If the string is in an unknown format, index 1 will
    be None.
    For hash, no parsing is needed as the output is just the hash.
    For email, we join up all the remaining lines of output and add them.

    Args:
      pem: str, PEM-encoded data

    Raises:
      CertError: unable to get certificate data
    """
    command = [CMD_OPENSSL, 'x509', '-sha1', '-nameopt', 'compat', '-noout']
    attrs = ['hash', 'subject', 'issuer', 'startdate', 'enddate', 'fingerprint',
             'serial', 'email']

    for attr in attrs:
      command.extend(['-%s' % attr])
    (stdout, stderr, returncode) = gmacpyutil.RunProcess(command, pem)
    if returncode:
      raise CertError('Unable to retrieve data for certificates: %s' % stderr)
    output = stdout.splitlines()
    for index, attr in enumerate(attrs):
      if attr in ('issuer', 'subject'):
        name = output[index].split(' ', 1)[1]
        self.__dict__[attr] = name
        try:
          self.__dict__[''.join([attr, '_cn'])] = (
              name.split('/')[-1].split('=')[1])
        except IndexError:
          pass
      elif attr == 'fingerprint':
        self.__dict__['fingerprint'] = output[index].split('=', 1)[1]
        self.__dict__['osx_fingerprint'] = (
            re.sub(':', '', output[index].split('=', 1)[1]))
      elif attr in ('enddate', 'startdate'):
        datestring = output[index].split('=')[1]
        try:
          dateobject = datetime.datetime.strptime(datestring,
                                                  OPENSSL_DATETIME_FORMAT)
        except ValueError:
          dateobject = None
        self.__dict__[attr] = [datestring, dateobject]
      elif attr == 'hash':
        self.__dict__['certhash'] = output[index]
      elif attr == 'serial':
        self.__dict__[attr] = output[index].split('=')[1]
      elif attr == 'email':
        self.__dict__[attr] = ''.join(output[index:])

def LoginKeychain():
  """Gets the current user's login keychain and updates login_keychain."""
  global login_keychain  # pylint: disable=global-statement
  if os.uname()[0] == 'Linux':
    return
  if os.getuid() == 0:
    logging.debug('Root has no access to login keychain.')
    return
  stdout, stderr, rc = gmacpyutil.RunProcess([CMD_SECURITY, 'login-keychain'])
  if rc == 0:
    login_keychain = stdout.strip(' \n"')
  else:
    logging.error('Unable to determine login keychain: %s', stderr)
    login_keychain = None

def _GetCertificates(keychain=None):
  """Gets all certificates in a given keychain.

  On a newly-created keychain, searching for all certs gives a
  CSSMERR_DL_INVALID_RECORDTYPE error and sets the returncode to 9. Just
  assume there are no certs in this case.

  Args:
    keychain: str, keychain to look in

  Yields:
    Certificate objects

  Raises:
    CertError: could not search for certficates
    StopIteration: no more matching certs
  """
  cmd = [CMD_SECURITY, 'find-certificate', '-a', '-p']

  if keychain is not None:
    cmd.extend([keychain])

  (stdout, stderr, returncode) = gmacpyutil.RunProcess(cmd)
  if returncode == 0:
    allcerts = stdout.split(PEM_FOOTER)
    allcerts.pop()
    for cert in allcerts:
      pem = '%s\n%s\n%s' % (PEM_HEADER, '\n'.join(cert.split()[2:]), PEM_FOOTER)
      try:
        yield Certificate(pem)
      except CertError, e:
        logging.info('Encountered an unparseable certificate, continuing.')
        logging.debug(str(e))
        continue
  elif returncode == 9:
    raise StopIteration
  else:
    raise CertError('Unable to get all certificates. Exit code: %s, '
                    'Output: %s' % (returncode, stderr))

def DeleteCert(osx_fingerprint, keychain=None, gui=False,
               password=None):
  """Deletes a certificate by SHA1 hash.

  Args:
    osx_fingerprint: str, SHA-1 hash (uppercase, no colons)
    keychain: str, keychain to delete from; if unset the default keychain is
              used (normally the login keychain)
    gui: True if running in a gui context
    password: The user's password if already known.

  Raises:
    CertError: unable to delete certificate
  """
  sudo, sudo_pass = _GetSudoContext(keychain,
                                    gui=gui,
                                    password=password)
  cmd = [CMD_SECURITY, 'delete-certificate', '-Z', osx_fingerprint]
  if keychain:
    cmd.append(keychain)
  (unused_stdout, stderr, returncode) = (
      gmacpyutil.RunProcess(cmd, sudo=sudo, sudo_password=sudo_pass))

  if returncode:
    raise CertError('Unable to delete certificate: %s' % stderr)

def FindCertificates(subject=None, subject_cn=None, issuer=None, issuer_cn=None,
                     startdate=None, enddate=None, certhash=None,
                     fingerprint=None, email=None, keychain=None):
                     # pylint: disable=unused-argument
  """Finds certificates by attribute.

  Multiple attributes are ANDed together.

  To do this, we get a list of all matching certs with a list comprehension,
  then run a reduce multiplying all results. Since bools multiply as ints, a
  False means multiply by 0 so any False match makes the whole thing False.

  Args:
    subject: str, find certificate by full subject
    subject_cn: str, find certificate by subject CN
    issuer: str, find certificate by issuer
    issuer_cn: str, find certificate by issuer CN
    startdate: str, find certificate by notBefore date
    enddate: str, find certificate by notAfter date
    certhash: str, find certificate by SHA-1 hash of suject name
    fingerprint: str, find certificate by fingerprint
    email: str, find certificates by email address
    keychain: str, which keychain to look in
  Returns:
    List of matching Certificate objects
  Raises:
    CertError: could not search for certificates
  """

  attrs = vars().keys()[:]
  attrs.remove('keychain')
  allcerts = _GetCertificates(keychain=keychain)
  matchedcerts = []

  for cert in allcerts:
    if reduce(operator.mul,  # pylint: disable=bad-builtin
              [(cert.get(k) == eval(k))  # pylint: disable=eval-used
               for k in attrs
               if eval(k)],  # pylint: disable=eval-used
              True):
      matchedcerts.append(cert)
  return matchedcerts

def CertificateExpired(cert, expires=0):
  """Checks a given certificate for expiry.

  Args:
    cert: Certificate object
    expires: int, the number of seconds to check for expiry. 0 means now
  Returns:
    boolean, whether the certificate will expire in expires seconds
  Raises:
    CertError: cert is a mandatory argument
    CertError: cert is not a PEM encoded x509 cert
  """
  expiry = datetime.datetime.today() + datetime.timedelta(seconds=expires)
  # enddate is a list of [str, (datetime|None)], we want the datetime object
  cert_end = cert.enddate[1]
  if cert_end:
    return expiry > cert_end
  else:
    raise CertError('Certificate has a malformed enddate.')

def VerifyIdentityPreference(subject_cn, service):
  """Verify a TLS identity preference exists for a given cert.

  Args:
    subject_cn: str, subject CN of cert to verify matches preference
    service: str, Service for which identity should be verified

  Returns:
    Bool: true if identity exists and matches subject_cn
  """
  cmd = [CMD_SECURITY, 'get-identity-preference', '-s', service, '-c']
  (stdout, unused_stderr, rc) = gmacpyutil.RunProcess(cmd)

  if rc:
    return False

  search_string = '"labl"<blob>="%s"' % subject_cn
  return search_string in stdout

def ClearIdentityPreferences(sudo_password=None):
  """Deletes existing TLS identity preferences.

  There's no way to list all identity preferences without knowing the full name
  so it's necessary to dump all keychains and search for the desired
  preferences.  All of the desired preferences have a line in the output like
  the following:

      "svce"<blob>="com.apple.network.eap.user.identity.wlan.ssid.<SSID>"

      or

      "svce"<blob>="com.apple.network.eap.system.identity.profileid.B7392191"

  Args:
    sudo_password: str, optional, for removing from system keychain
  """
  service_re = re.compile(r'\s*"svce"<blob>="(com\.apple\.network\.eap\..*)"')

  cmd = [CMD_SECURITY, 'dump-keychain']
  (keychain_content, _, _) = gmacpyutil.RunProcess(cmd)

  for line in keychain_content.splitlines():
    matches = service_re.match(line)
    if matches:
      cmd = [CMD_SECURITY, 'set-identity-preference',
             '-n', '-s', matches.group(1)]
      logging.debug('Removing identity preference: %s', cmd)
      gmacpyutil.RunProcess(cmd, sudo=bool(sudo_password),
                            sudo_password=sudo_password)

def CreateIdentityPreference(issuer_cn, service, keychain=login_keychain):
  """Create a TLS Identity preference for a given cert.

  Args:
    issuer_cn: str, CN of issuer of cert to create preference for
    service: str, Service for which identity should be preferred
    keychain: str, keychain to create the preference entry in

  Raises:
    CertError: if there is more than one matching cert
    CertError: if the preference can't be created
  """
  logging.debug('Creating TLS identity preference for %s in keychain %s',
                service, keychain)

  existing_certs = FindCertificates(issuer_cn=issuer_cn, keychain=keychain)
  if len(existing_certs) != 1:
    raise CertError('More or less than one matching certificate '
                    'in the keychain.')

  command = [CMD_SECURITY, 'set-identity-preference', '-Z',
             existing_certs[0].osx_fingerprint, '-s', service, keychain]
  logging.debug('Command: %s', command)
  (stdout, stderr, status) = gmacpyutil.RunProcess(command)
  logging.debug('Identity preference creation output: %s', stdout)
  if status:
    raise CertError(stdout, stderr)

def _GetSudoContext(keychain, gui=False, password=None):
  """Determine if we need sudo and get a sudo password if necessary.

  Args:
    keychain: path to keychain
    gui: True if we are running from the gui
    password: The user's password.

  Returns:
    sudo: True if this is the system keychain and we will need sudo
    sudo_pass: sudo password if necessary, or None if not necessary or not
               available.
  """

  sudo_pass = None
  if keychain == SYSTEM_KEYCHAIN:

    # If we're given a password we should test if it works for sudo.
    if password:
      (unused_stdout, unused_stderr, return_code) = gmacpyutil.RunProcess(
          ['-v'], sudo=True, sudo_password=password)
      if return_code == 0:
        return keychain == SYSTEM_KEYCHAIN, password

    if gui:
      # If we arrive here it means the password doesn't work for sudo, if this
      # is a gui context, try and get a passwd, otherwise let sudo do the
      # prompting in the terminal
      try:
        sudo_pass = getauth.GetPassword(
            gui=gui, title='Get sudo Password', prompt='sudo password',
            text='Enter local machine password')
      except (EOFError, KeyboardInterrupt):
        logging.exception('Could not get sudo password from GUI prompt')
        raise
  return keychain == SYSTEM_KEYCHAIN, sudo_pass

def InstallPrivateKeyInKeychain(private_key, keychain=login_keychain,
                                trusted_app_path=None, passphrase=None,
                                gui=False, password=None):
  """Install the private key into the keychain.

  Args:
    private_key: str, private key in PEM format
    keychain: str, keychain to install cert and key into
    trusted_app_path: list of strings, optional applications that can
                      access private key.  If None, trust is set Open
    passphrase: str, optional passphrase the private key is encrypted with
    gui: True if running in a gui context
    password: The user's password if already known.

  Raises:
    KeychainError: if there are any errors installing
  """
  sudo, sudo_pass = _GetSudoContext(keychain,
                                    gui=gui,
                                    password=password)

  temp_dir = tempfile.mkdtemp(prefix='cert_pkey_install')
  key_file = '%s/private.key' % temp_dir

  try:
    key_handle = open(key_file, 'w')
    key_handle.write(private_key)
    key_handle.close()

    logging.info('Installing downloaded key into the %s keychain', keychain)

    command = [CMD_SECURITY, 'import', key_file, '-x', '-k', keychain]
    if passphrase:
      command.extend(['-P', passphrase])
    if trusted_app_path:
      for trusted_app in trusted_app_path:
        if os.path.exists(trusted_app):
          command.extend(['-T', trusted_app])
    else:
      command.extend(['-A'])
    logging.debug('Command: %s', command)
    (stdout, stderr, status) = gmacpyutil.RunProcess(command, sudo=sudo,
                                                     sudo_password=sudo_pass)
    logging.debug('Private key installation output: %s', stdout)
    if status:
      raise KeychainError(stdout, stderr)
  except IOError:
    raise KeychainError('Could not write to temp files in %s' % temp_dir)
  finally:
    shutil.rmtree(temp_dir)

def InstallCertInKeychain(pem, private_key, keychain=login_keychain,
                          trusted_app_path=None, passphrase=None, gui=False,
                          password=None):
  """Install the certificate and private key into the keychain.

  Args:
    pem: str, the certificate in PEM format
    private_key: str, private key in PEM format
    keychain: str, keychain to install cert and key into
    trusted_app_path: list of strings, optional applications that can
                      access private key; a single string can be used for
                      backwards compatibility.  If None, trust is set Open
    passphrase: str, optional passphrase the private key is encrypted with
    gui: True if running in a gui context
    password: The user's password if already known.

  Raises:
    KeychainError: if there are any errors installing
  """
  sudo, sudo_pass = _GetSudoContext(keychain,
                                    gui=gui,
                                    password=password)

  if type(trusted_app_path) == str:
    # Convert bare strings to a list for backwards compatibility
    trusted_app_path = [trusted_app_path]

  # TODO(user): Make this function just install the certificate and not handle
  # any key installation at all.
  InstallPrivateKeyInKeychain(private_key, keychain=keychain,
                              trusted_app_path=trusted_app_path,
                              passphrase=passphrase, gui=gui,
                              password=sudo_pass)

  temp_dir = tempfile.mkdtemp(prefix='cert_install')
  cert_file = '%s/certificate.cer' % temp_dir

  try:
    cert_handle = open(cert_file, 'w')
    cert_handle.write(pem)
    cert_handle.close()

    logging.info('Installing downloaded certificate into the %s keychain',
                 keychain)

    command = [CMD_SECURITY, 'import', cert_file, '-x', '-k', keychain]
    logging.debug('Command: %s', command)
    (stdout, stderr, status) = gmacpyutil.RunProcess(command, sudo=sudo,
                                                     sudo_password=sudo_pass)
    logging.debug('Certificate installation output: %s', stdout)
    if status:
      raise KeychainError(stdout, stderr)
  except IOError:
    raise KeychainError('Could not write to temp files in %s' % temp_dir)
  finally:
    shutil.rmtree(temp_dir)

def InstallTrustedCertInKeychain(pem, root_ca=False, policies=None, gui=False,
                                 password=None, keychain=SYSTEM_KEYCHAIN):
  """Install a trusted certificate into the keychain.

  Args:
    pem: str, the certificate in PEM format
    root_ca: bool, whether the cert is a root CA cert (trustRoot vs trustAsRoot)
    policies: list of strings, optional policy constraints eg, ssl, basic, etc.
    gui: True if running in a gui context
    password: The user's password if already known
    keychain: str, keychain to install cert and key into

  Raises:
    KeychainError: if there are any errors installing
  """
  sudo, sudo_pass = _GetSudoContext(keychain,
                                    gui=gui,
                                    password=password)

  temp_dir = tempfile.mkdtemp(prefix='trusted_cert_install')
  trusted_cert_file = '%s/trusted_certificate.pem' % temp_dir

  try:
    with open(trusted_cert_file, 'w') as trusted_cert_handle:
      trusted_cert_handle.write(pem)

    logging.info('Installing trusted certificate into keychain: %s', keychain)

    command = [CMD_SECURITY, 'add-trusted-cert', '-d']
    if root_ca:
      command.extend(['-r', 'trustRoot'])
    else:
      command.extend(['-r', 'trustAsRoot'])

    if policies:
      for policy in policies:
        command.extend(['-p', policy])
    command.extend(['-k', keychain, trusted_cert_file])

    logging.debug('Command: %s', command)
    (stdout, stderr, status) = gmacpyutil.RunProcess(command, sudo=sudo,
                                                     sudo_password=sudo_pass)
    logging.debug('Trusted certificate installation output: %s', stdout)
    if status:
      raise KeychainError(stdout, stderr)
  except IOError:
    raise KeychainError('Could not write to temp files in %s' % temp_dir)
  finally:
    shutil.rmtree(temp_dir)

def RemoveIssuerCertsFromKeychain(issuer_cn, keychain=login_keychain, gui=False,
                                  password=None):
  """Removes all certificates issued from a given CN from the keychain.

  DeleteCert tries to raise privileges to allow deletions from the System
  keychain, so we try and log if it fails.

  Args:
    issuer_cn: str, the certificate's issuer's CN
    keychain: str, the path to the keychain to remove from
    gui: True if running in a gui context
    password: The user's password if already known.

  Raises:
    KeychainError: if there are any errors removing

  Returns:
    Array of deleted serial numbers
  """
  if keychain is None:
    return []

  existing_certs = FindCertificates(issuer_cn=issuer_cn, keychain=keychain)
  deleted_certs = []
  for cert in existing_certs:
    try:
      logging.debug('Removing cert with fingerprint %s from %s',
                    cert.osx_fingerprint, keychain)
      DeleteCert(cert.osx_fingerprint, keychain=keychain, gui=gui,
                 password=password)
      deleted_certs.append(cert.serial)
    except CertError, e:
      logging.error('Cannot delete old certificate: %s', str(e))
  return deleted_certs

def GenerateCSR(subject, rsa_bits=2048, passphrase=None):
  """Generate a Certificate Signing Request.

  Args:
    subject: str, subject for csr
    rsa_bits: int, optional number of bits
    passphrase: str, optional passphrase to encrypt private key with

  Returns:
    tuple, cert and private key in PEM format and passphrase

  Raises:
    CertError: Error generating a CSR.
  """
  command = [CMD_OPENSSL, 'genrsa']
  env = {}
  if passphrase:
    command.extend(['-des3', '-passout', 'env:PASSPHRASE'])
    env['PASSPHRASE'] = passphrase
  command.extend([str(rsa_bits)])

  logging.debug('command: %s', command)
  logging.debug('environment: %s', env.keys())
  (stdout, stderr, status) = gmacpyutil.RunProcess(command, env=env)
  logging.debug('Private key generation output: %s', stdout)
  if status:
    raise CertError('Error creating private key: %s' % stderr)
  private_key = stdout

  command = [CMD_OPENSSL, 'req', '-new', '-subj', subject, '-key',
             '/dev/stdin']
  if passphrase:
    command.extend(['-passin', 'env:PASSPHRASE'])
    env['PASSPHRASE'] = passphrase

  logging.debug('command: %s', command)
  logging.debug('environment: %s', env.keys())
  logging.debug('stdinput: %s', private_key)
  (csr, stderr, status) = gmacpyutil.RunProcess(command, private_key, env=env)
  logging.debug('CSR generation output: %s', csr)
  if status:
    raise CertError('Error creating CSR: %s' % stderr)

  return (csr, private_key, passphrase)

LoginKeychain()


text:
<?php

// Include the initialization file
require_once dirname(dirname(__FILE__)) . '/init.php';

// Enter parameters required by the code example.
$videoId = 'INSERT_VIDEO_ID_HERE';

/**
 * Runs the example.
 * @param AdWordsUser $user the user to run the example with
 * @param string $videoId the video ID to add a call to action to.
 */
function AddVideoCallToActionExample(AdWordsUser $user, $videoId) {
  $videoService = $user->GetService('VideoService', ADWORDS_VERSION);

  $videoCallToAction = new VideoCallToAction();
  $videoCallToAction->id = $videoId;
  $callToAction = new CallToAction();
  $creative = new CallToActionCreative();
  $creative->headline = 'Mars cruises';
  $creative->descriptionLine1 = 'Astonishing views';
  $creative->descriptionLine2 = 'Mild climate';
  $creative->displayUrl = 'wwww.example.com/mars';
  $creative->destinationUrl = 'wwww.example.com/mars';
  $callToAction->creative = $creative;
  $videoCallToAction->callToAction = $callToAction;

  $videoCallToActionOperation = new VideoCallToActionOperation();
  $videoCallToActionOperation->operator = 'SET';
  $videoCallToActionOperation->operand = $videoCallToAction;

  $response = $videoService->mutateCallToAction(
      array($videoCallToActionOperation));

  if (!empty($response->value)) {
    foreach ($response->value as $videoCallToAction) {
      printf(
        "CallToAction overlay was added to video ID '%s', headline '%s'.\n",
        $videoCallToAction->videoId,
        $videoCallToAction->callToAction->creative->headline
      );
    }
  } else {
    echo "No call to action overlays were added.\n";
  }
}

// Don't run the example if the file is being included.
if (__FILE__ != realpath($_SERVER['PHP_SELF'])) {
  return;
}

try {
  // Get AdWordsUser from credentials in "../auth.ini"
  // relative to the AdWordsUser.php file's directory.
  $user = new AdWordsUser();

  // Log every SOAP XML request and response.
  $user->LogAll();

  // Run the example.
  AddVideoCallToActionExample($user, $videoId);
} catch (OAuth2Exception $e) {
  ExampleUtils::CheckForOAuth2Errors($e);
} catch (ValidationException $e) {
  ExampleUtils::CheckForOAuth2Errors($e);
} catch (Exception $e) {
  printf("An error has occurred: %s\n", $e->getMessage());
}

redpajama_set_name: RedPajamaGithub


text:
package org.apache.pulsar.proxy.server;

import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.google.common.collect.Lists;

import io.prometheus.client.jetty.JettyStatisticsCollector;

import java.io.IOException;
import java.net.URI;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import java.util.TimeZone;

import javax.servlet.DispatcherType;

import org.apache.commons.lang3.tuple.Pair;
import org.apache.pulsar.broker.authentication.AuthenticationService;
import org.apache.pulsar.broker.web.AuthenticationFilter;
import org.apache.pulsar.common.util.ObjectMapperFactory;
import org.apache.pulsar.common.util.SecurityUtility;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.Slf4jRequestLog;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.RequestLogHandler;
import org.eclipse.jetty.server.handler.StatisticsHandler;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.ExecutorThreadPool;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Manages web-service startup/stop on jetty server.
 *
 */
public class WebServer {
    private static final String MATCH_ALL = "/*";

    private final Server server;
    private final ExecutorThreadPool webServiceExecutor;
    private final AuthenticationService authenticationService;
    private final List<String> servletPaths = Lists.newArrayList();
    private final List<Handler> handlers = Lists.newArrayList();
    private final ProxyConfiguration config;
    protected int externalServicePort;
    private URI serviceURI = null;

    public WebServer(ProxyConfiguration config, AuthenticationService authenticationService) {
        this.webServiceExecutor = new ExecutorThreadPool();
        this.webServiceExecutor.setName("pulsar-external-web");
        this.server = new Server(webServiceExecutor);
        this.externalServicePort = config.getWebServicePort();
        this.authenticationService = authenticationService;
        this.config = config;

        List<ServerConnector> connectors = Lists.newArrayList();

        HttpConfiguration http_config = new HttpConfiguration();
        http_config.setOutputBufferSize(config.getHttpOutputBufferSize());

        ServerConnector connector = new ServerConnector(server, 1, 1, new HttpConnectionFactory(http_config));
        connector.setPort(externalServicePort);
        connectors.add(connector);

        if (config.isTlsEnabledInProxy()) {
            try {
                SslContextFactory sslCtxFactory = SecurityUtility.createSslContextFactory(
                        config.isTlsAllowInsecureConnection(),
                        config.getTlsTrustCertsFilePath(),
                        config.getTlsCertificateFilePath(),
                        config.getTlsKeyFilePath(),
                        config.getTlsRequireTrustedClientCertOnConnect());
                ServerConnector tlsConnector = new ServerConnector(server, 1, 1, sslCtxFactory);
                tlsConnector.setPort(config.getWebServicePortTls());
                connectors.add(tlsConnector);
            } catch (GeneralSecurityException e) {
                throw new RuntimeException(e);
            }
        }

        // Limit number of concurrent HTTP connections to avoid getting out of file descriptors
        connectors.stream().forEach(c -> c.setAcceptQueueSize(1024 / connectors.size()));
        server.setConnectors(connectors.toArray(new ServerConnector[connectors.size()]));
    }

    public URI getServiceUri() {
        return serviceURI;
    }

    public void addServlet(String basePath, ServletHolder servletHolder) {
        addServlet(basePath, servletHolder, Collections.emptyList());
    }

    public void addServlet(String basePath, ServletHolder servletHolder, List<Pair<String, Object>> attributes) {
        Optional<String> existingPath = servletPaths.stream().filter(p -> p.startsWith(basePath)).findFirst();
        if (existingPath.isPresent()) {
            throw new IllegalArgumentException(
                    String.format("Cannot add servlet at %s, path %s already exists", basePath, existingPath.get()));
        }
        servletPaths.add(basePath);

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath(basePath);
        context.addServlet(servletHolder, "/*");
        for (Pair<String, Object> attribute : attributes) {
            context.setAttribute(attribute.getLeft(), attribute.getRight());
        }
        if (config.isAuthenticationEnabled()) {
            FilterHolder filter = new FilterHolder(new AuthenticationFilter(authenticationService));
            context.addFilter(filter, MATCH_ALL, EnumSet.allOf(DispatcherType.class));
        }

        handlers.add(context);
    }

    public void addRestResources(String basePath, String javaPackages, String attribute, Object attributeValue) {
        JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
        provider.setMapper(ObjectMapperFactory.create());
        ResourceConfig config = new ResourceConfig();
        config.packages("jersey.config.server.provider.packages", javaPackages);
        config.register(provider);
        ServletHolder servletHolder = new ServletHolder(new ServletContainer(config));
        servletHolder.setAsyncSupported(true);
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath(basePath);
        context.addServlet(servletHolder, "/*");
        context.setAttribute(attribute, attributeValue);
        handlers.add(context);
    }

    public int getExternalServicePort() {
        return externalServicePort;
    }

    public void start() throws Exception {
        RequestLogHandler requestLogHandler = new RequestLogHandler();
        Slf4jRequestLog requestLog = new Slf4jRequestLog();
        requestLog.setExtended(true);
        requestLog.setLogTimeZone(TimeZone.getDefault().getID());
        requestLog.setLogLatency(true);
        requestLogHandler.setRequestLog(requestLog);
        handlers.add(0, new ContextHandlerCollection());
        handlers.add(requestLogHandler);

        ContextHandlerCollection contexts = new ContextHandlerCollection();
        contexts.setHandlers(handlers.toArray(new Handler[handlers.size()]));

        HandlerCollection handlerCollection = new HandlerCollection();
        handlerCollection.setHandlers(new Handler[] { contexts, new DefaultHandler(), requestLogHandler });

        // Metrics handler
        StatisticsHandler stats = new StatisticsHandler();
        stats.setHandler(handlerCollection);
        try {
            new JettyStatisticsCollector(stats).register();
        } catch (IllegalArgumentException e) {
            // Already registered. Eg: in unit tests
        }

        server.setHandler(stats);

        try {
            server.start();

            Arrays.stream(server.getConnectors())
                .filter(c -> c instanceof ServerConnector)
                .findFirst().ifPresent(c -> {
                        WebServer.this.externalServicePort = ((ServerConnector) c).getPort();
                    });

            // server reports URI of first servlet, we want to strip that path off
            URI reportedURI = server.getURI();
            serviceURI = new URI(reportedURI.getScheme(),
                                 null,
                                 reportedURI.getHost(),
                                 reportedURI.getPort(),
                                 null, null, null);
        } catch (Exception e) {
            List<Integer> ports = new ArrayList<>();
            for (Connector c : server.getConnectors()) {
                if (c instanceof ServerConnector) {
                    ports.add(((ServerConnector) c).getPort());
                }
            }
            throw new IOException("Failed to start HTTP server on ports " + ports, e);
        }

        log.info("Server started at end point {}", getServiceUri());
    }

    public void stop() throws Exception {
        server.stop();
        webServiceExecutor.stop();
        log.info("Server stopped successfully");
    }

    public boolean isStarted() {
        return server.isStarted();
    }

    private static final Logger log = LoggerFactory.getLogger(WebServer.class);
}


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd">

<TITLE>
Xerces2 Implementation: Class  StartElementImpl
</TITLE>
<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../../../stylesheet.css" TITLE="Style">

      Overview
      Package
    Class
      Tree
      Serialized
      Deprecated
      Index
      Help

 PREV CLASS
 NEXT CLASS

  FRAMES
 NO FRAMES

  SUMMARY:  INNER | FIELD | CONSTR | METHOD

DETAIL:  FIELD | CONSTR | METHOD

org.apache.xerces.stax.events

Class  StartElementImpl

java.lang.Object
  |
  +--org.apache.xerces.stax.events.XMLEventImpl
        |
        +--org.apache.xerces.stax.events.ElementImpl
              |
              +--org.apache.xerces.stax.events.StartElementImpl

All Implemented Interfaces:
javax.xml.stream.events.StartElement, javax.xml.stream.events.XMLEvent, javax.xml.stream.XMLStreamConstants

public final class StartElementImpl
extends org.apache.xerces.stax.events.ElementImpl
implements javax.xml.stream.events.StartElement

INTERNAL:

Usage of this class is not supported. It may be altered or removed at any time.

Version:
$Id: StartElementImpl.java 730796 2009-01-02 17:36:23Z mrglavas $

Author:
Lucian Holland

Fields inherited from interface javax.xml.stream.XMLStreamConstants

ATTRIBUTE, CDATA, CHARACTERS, COMMENT, DTD, END_DOCUMENT, END_ELEMENT, ENTITY_DECLARATION, ENTITY_REFERENCE, NAMESPACE, NOTATION_DECLARATION, PROCESSING_INSTRUCTION, SPACE, START_DOCUMENT, START_ELEMENT

Constructor Summary

StartElementImpl(javax.xml.namespace.QName name,
                 java.util.Iterator attributes,
                 java.util.Iterator namespaces,
                 javax.xml.namespace.NamespaceContext namespaceContext,
                 javax.xml.stream.Location location)

Method Summary

 javax.xml.stream.events.Characters
asCharacters()

 javax.xml.stream.events.EndElement
asEndElement()

 javax.xml.stream.events.StartElement
asStartElement()

 javax.xml.stream.events.Attribute
getAttributeByName(javax.xml.namespace.QName name)

 java.util.Iterator
getAttributes()

 int
getEventType()

 javax.xml.stream.Location
getLocation()

 javax.xml.namespace.QName
getName()

 javax.xml.namespace.NamespaceContext
getNamespaceContext()

 java.util.Iterator
getNamespaces()

 java.lang.String
getNamespaceURI(java.lang.String prefix)

 javax.xml.namespace.QName
getSchemaType()

 boolean
isAttribute()

 boolean
isCharacters()

 boolean
isEndDocument()

 boolean
isEndElement()

 boolean
isEntityReference()

 boolean
isNamespace()

 boolean
isProcessingInstruction()

 boolean
isStartDocument()

 boolean
isStartElement()

 java.lang.String
toString()

 void
writeAsEncodedUnicode(java.io.Writer writer)

Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Methods inherited from interface javax.xml.stream.events.StartElement

getName, getNamespaces

Methods inherited from interface javax.xml.stream.events.XMLEvent

asCharacters, asEndElement, asStartElement, getEventType, getLocation, getSchemaType, isAttribute, isCharacters, isEndDocument, isEndElement, isEntityReference, isNamespace, isProcessingInstruction, isStartDocument, isStartElement

Constructor Detail

StartElementImpl

public StartElementImpl(javax.xml.namespace.QName name,
                        java.util.Iterator attributes,
                        java.util.Iterator namespaces,
                        javax.xml.namespace.NamespaceContext namespaceContext,
                        javax.xml.stream.Location location)

Parameters:
location -
schemaType -

Method Detail

getAttributes

public java.util.Iterator getAttributes()

Specified by:
getAttributes in interface javax.xml.stream.events.StartElement

See Also:
StartElement.getAttributes()

getAttributeByName

public javax.xml.stream.events.Attribute getAttributeByName(javax.xml.namespace.QName name)

Specified by:
getAttributeByName in interface javax.xml.stream.events.StartElement

See Also:
StartElement.getAttributeByName(javax.xml.namespace.QName)

getNamespaceContext

public javax.xml.namespace.NamespaceContext getNamespaceContext()

Specified by:
getNamespaceContext in interface javax.xml.stream.events.StartElement

See Also:
StartElement.getNamespaceContext()

getNamespaceURI

public java.lang.String getNamespaceURI(java.lang.String prefix)

Specified by:
getNamespaceURI in interface javax.xml.stream.events.StartElement

See Also:
StartElement.getNamespaceURI(java.lang.String)

writeAsEncodedUnicode

public void writeAsEncodedUnicode(java.io.Writer writer)
                           throws javax.xml.stream.XMLStreamException

Specified by:
writeAsEncodedUnicode in interface javax.xml.stream.events.XMLEvent

getName

public final javax.xml.namespace.QName getName()

See Also:
StartElement.getName(),
EndElement.getName()

getNamespaces

public final java.util.Iterator getNamespaces()

See Also:
StartElement.getNamespaces(),
EndElement.getNamespaces()

getEventType

public final int getEventType()

Specified by:
getEventType in interface javax.xml.stream.events.XMLEvent

See Also:
XMLEvent.getEventType()

getLocation

public final javax.xml.stream.Location getLocation()

Specified by:
getLocation in interface javax.xml.stream.events.XMLEvent

See Also:
XMLEvent.getLocation()

isStartElement

public final boolean isStartElement()

Specified by:
isStartElement in interface javax.xml.stream.events.XMLEvent

See Also:
XMLEvent.isStartElement()

isAttribute

public final boolean isAttribute()

Specified by:
isAttribute in interface javax.xml.stream.events.XMLEvent

See Also:
XMLEvent.isAttribute()

isNamespace

public final boolean isNamespace()

Specified by:
isNamespace in interface javax.xml.stream.events.XMLEvent

See Also:
XMLEvent.isNamespace()

isEndElement

public final boolean isEndElement()

Specified by:
isEndElement in interface javax.xml.stream.events.XMLEvent

See Also:
XMLEvent.isEndElement()

isEntityReference

public final boolean isEntityReference()

Specified by:
isEntityReference in interface javax.xml.stream.events.XMLEvent

See Also:
XMLEvent.isEntityReference()

isProcessingInstruction

public final boolean isProcessingInstruction()

Specified by:
isProcessingInstruction in interface javax.xml.stream.events.XMLEvent

See Also:
XMLEvent.isProcessingInstruction()

isCharacters

public final boolean isCharacters()

Specified by:
isCharacters in interface javax.xml.stream.events.XMLEvent

See Also:
XMLEvent.isCharacters()

isStartDocument

public final boolean isStartDocument()

Specified by:
isStartDocument in interface javax.xml.stream.events.XMLEvent

See Also:
XMLEvent.isStartDocument()

isEndDocument

public final boolean isEndDocument()

Specified by:
isEndDocument in interface javax.xml.stream.events.XMLEvent

See Also:
XMLEvent.isEndDocument()

asStartElement

public final javax.xml.stream.events.StartElement asStartElement()

Specified by:
asStartElement in interface javax.xml.stream.events.XMLEvent

See Also:
XMLEvent.asStartElement()

asEndElement

public final javax.xml.stream.events.EndElement asEndElement()

Specified by:
asEndElement in interface javax.xml.stream.events.XMLEvent

See Also:
XMLEvent.asEndElement()

asCharacters

public final javax.xml.stream.events.Characters asCharacters()

Specified by:
asCharacters in interface javax.xml.stream.events.XMLEvent

See Also:
XMLEvent.asCharacters()

getSchemaType

public final javax.xml.namespace.QName getSchemaType()

Specified by:
getSchemaType in interface javax.xml.stream.events.XMLEvent

See Also:
XMLEvent.getSchemaType()

toString

public final java.lang.String toString()

Overrides:
toString in class java.lang.Object

      Overview
      Package
    Class
      Tree
      Serialized
      Deprecated
      Index
      Help

 PREV CLASS
 NEXT CLASS

  FRAMES
 NO FRAMES

  SUMMARY:  INNER | FIELD | CONSTR | METHOD

DETAIL:  FIELD | CONSTR | METHOD

Copyright © 1999-2010 The Apache Software Foundation. All Rights Reserved.


text:
import asyncpg
import math
import uvloop

# from datetime import datetime
from sanic import Sanic
from sanic import response

loop = uvloop.new_event_loop()

app = Sanic()
app.debug = False

# Serve files from the static folder to the URL /static
app.static('/', './static')

async def initdb_pool():
    dbdict = {"database": "geo", "user": "postgres", "password": "password", "host": "localhost", "port": 5432}
    return await asyncpg.create_pool(**dbdict)

engine = loop.run_until_complete(initdb_pool())

@app.route("/")
async def test(request):
    return response.json({"test": True})

# http://127.0.0.1:8000/get-data/151.14/-33.85/151.15/-33.84/15/
# http://127.0.0.1:8000/get-data/151.10/-33.85/151.15/-33.80/14/

# LONGITUDE_PATTERN = "^[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$"
# LATITUDE_PATTERN = ""

GET_DATA_URL = "/get-data/<ml>/<mb>/<mr>/<mt>/<z>/"

@app.route(GET_DATA_URL)
async def get_data(request, ml, mb, mr, mt, z):
    # start_time = datetime.now()

    async with engine.acquire() as connection:

        zoom_level = int(z)

        # Set the number of decimal places for the output GeoJSON to reduce response size & speed up rendering
        decimal_places = await get_decimal_places(zoom_level)

        # get hex width for the zoom level
        start_zoom_level = 13
        start_width = 0.3  # in km
        multiple = 2.0
        max_width = 200.0

        curr_width = start_width
        curr_zoom_level = start_zoom_level

        while zoom_level < curr_zoom_level:
            curr_width *= multiple

            if curr_width > max_width:
                curr_width /= multiple

            curr_zoom_level -= 1

        # Get table name
        curr_width_name = str(curr_width).replace(".", "_")
        table_name = "address_counts_{0}".format(curr_width_name.replace("_0", ""), )
        schema_name = "hex"

        # sql = "SELECT x::text || y::text AS id, percent, difference, ST_AsGeoJSON(geom, $1) AS geometry " \
        #       "FROM $2.$3 " \
        #       "WHERE ST_Intersects(ST_SetSRID(ST_MakeBox2D(ST_Point($4, $5), ST_Point($6, $7)), 4326),geom)"
        sql = "SELECT x::text || y::text AS id, percent, difference, ST_AsGeoJSON(geom, {0}) AS geometry " \
              "FROM {1}.{2} " \
              "WHERE ST_Intersects(ST_SetSRID(ST_MakeBox2D(ST_Point({3}, {4}), ST_Point({5}, {6})), 4326),geom)" \
            .format(decimal_places, schema_name, table_name, ml, mb, mr, mt)

        # print(sql)

        async with connection.transaction():
            # query = await connection.prepare(sql)
            # result = await connection.fetchval(sql, decimal_places, schema_name, table_name, ml, mb, mr, mt)
            result = await connection.fetch(sql)

            # print(result)

            if not result:
                return response.json({'ok': False, 'error': 'No data returned'})
            else:
                # print("result is ", result)
                # print("Got data in : {0} seconds".format(datetime.now() - start_time))
                # start_time = datetime.now()

                geojson_result = await convert_to_geojson(result)
                # print("Converted to GeoJSON : {0} seconds".format(datetime.now() - start_time))

                return response.text(geojson_result)

async def convert_to_geojson(result):
    # output is the main content
    output = ['{"type":"FeatureCollection","features":[']
    i = 0

    # For each row returned...
    for record in result:
        # Make sure the geometry exists
        if record["geometry"] is not None:

            # If it's the first record, don't add a comma
            comma = "," if i > 0 else ""
            feature = [''.join([comma, '{"type":"Feature","geometry":', record["geometry"], ',"properties":{'])]

            j = 0
            # For each field returned, assemble the properties object
            for key, value in record.items():
                if key != 'geometry':
                    comma = "," if j > 0 else ""
                    feature.append(''.join([comma, '"', key, '":"', str(value), '"']))

                j += 1

            feature.append('}}')
            row_output = ''.join([item for item in feature])
            output.append(row_output)

        # start over
        i += 1

    output.append(']}')

    # Assemble the GeoJSON
    total_output = ''.join([item for item in output])

    return total_output

# maximum number of decimal places for GeoJSON and other JSON based formats (e.g. ArcGIS services)
async def get_decimal_places(zoom_level):
    # rough metres to degrees conversation, using spherical WGS84 datum radius for simplicity and speed
    metres2degrees = (2.0 * math.pi * 6378137.0) / 360.0

    # default Google/Bing map tile scales
    metres_per_pixel = 156543.03390625 / math.pow(2.0, float(zoom_level))

    # the tolerance for thinning data and limiting decimal places in GeoJSON responses
    degrees_per_pixel = metres_per_pixel / metres2degrees

    scale_string = "{:10.9f}".format(degrees_per_pixel).split(".")[1]
    places = 1

    trigger = "0"

    # find how many zero decimal places there are. e.g. 0.00001234 = 4 zeros
    for c in scale_string:
        if c == trigger:
            places += 1
        else:
            trigger = "don't do anything else"  # used to cleanly exit the loop

    return places

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000, loop=loop, debug=True)


text: module.exports = Backbone.Marionette;


text:
  The swans go on the path of the sun, they go through the ether by
  means of their miraculous power; the wise are led out of this world,
  when they have conquered Mara and his train. 175


text:
<!doctype html>

    <title>Code coverage report for system/classes/register.js</title>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="../../prettify.css" />
    <link rel="stylesheet" href="../../base.css" />
    <meta name="viewport" content="width=device-width, initial-scale=1">

      All files / system/classes register.js

        100%
        Statements
        3/3

        100%
        Branches
        0/0

        100%
        Functions
        1/1

        100%
        Lines
        3/3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

9x
9x

1x

'use strict';

/**
 * Register Class
 *
 * System will use register instance to add all components to the central
 * location
 */
class Register {
  constructor(name, value) {
    this.name = name;
    this.value = value;
  }
}

module.exports = Register;

  Code coverage
  generated by istanbul at Tue Apr 18 2017 16:26:44 GMT-0700 (PDT)


text:
"use strict";

var fluid = require("infusion");

/** A Kettle "app" is an "independently mountable application unit". An app aggregates together
 * a set of "handlers" which are mounted within it to handle one or more request types
 * at certain URL paths. Kettle includes support for handlers of HTTP endpoints and
 * experimental support for WebSockets endpoints. One or more Kettle apps may be
 * aggregated together into a Kettle "servers" of type kettle.server. A Kettle
 * server corresponds directly to a node.js HTTP server (connect/express server) in terms
 * of being "a thing listening on a TCP port". Configuration supplied to the
 * options of all apps at their options path "handlers" is aggregated together to their enclosing
 * server.
 */

fluid.defaults("kettle.app", {
    gradeNames: ["fluid.component"],
    requestHandlers: {},
    listeners: {
        "onCreate.register": {
            listener: "kettle.server.registerApp",
            args: ["{kettle.server}", "{that}"]
        }
    },
    components: {
        requests: {
            type: "kettle.requests"
        }
    }
});


text:
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">×</button>

Edit Todo

        <input type="text" class="form-control" value="{{title}}" data-id="{{id}}">

        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>


text:
layout: portfolio
title: ! 'Cleaning Up a Country: A Data-Driven Examination of Ethiopia''s Growing
  Sanitation Issues'
date: 2014-07-01 06:02:39.000000000 +05:30
categories: []
tags: []
status: publish
type: portfolio
published: true
meta:
  _edit_last: '1'
  _wpas_done_all: '1'
  _thumbnail_id: '3935'
  _jetpack_related_posts_cache: a:1:{s:32:"8aa64d0d47aab35d63f6cd22cecd8e43";a:2:{s:7:"expires";i:1428990766;s:7:"payload";a:3:{i:0;a:1:{s:2:"id";i:3394;}i:1;a:1:{s:2:"id";i:2688;}i:2;a:1:{s:2:"id";i:3980;}}}}
  dsq_thread_id: '3635565049'
author:
  login: cvlewis
  email: carl@carlvlewis.net
  display_name: Carl V. Lewis
  first_name: Carl V.
  last_name: Lewis
excerpt: !ruby/object:Hpricot::Doc
  options: {}
---

Hover over each stage to find out more information.

Embedded above is a pure JavaScript-powered flowchart from a team project that was part of Columbia University's Tow Center for Digital Journalism annual 24-hour digital media hackathon. The project took home first place at the end of the hackathon. I served as the team leader, divvying up content and slides to team members who later iFramed in their content under tight deadlines. I built this particular interactive in less than an hour from scratch using CSS3, HTML5 and JavaScript elements.

I also oversaw the larger presentation, including customizing the open-source Coda Slider jQuery plugin to work within our project's constraints, and performing data analysis to ensure our team's conclusions were of statistical significance. Other team members who participated in the project included Stephanie Vatz, Rani Molla, Erin Richey and Kelty Ortiz.

And, of course, the semester-long preparation we received from Prof. Susan E. McGregor (and the donuts halfway in!) shouldn't go unnoticed.

[su_heading]

Click here to view the project live>>

[/su_heading]


text:
<?php

use \Luracast\Restler\iCache;

class SessionCache implements iCache
{
    function __construct()
    {
        @session_start();
    }

    public function set($name, $data)
    {
        $_SESSION[$name] = $data;
    }

    public function get($name, $ignoreErrors = false)
    {
        return isset($_SESSION[$name]) ? $_SESSION[$name] : null;
    }

    public function clear($name, $ignoreErrors = false)
    {
        unset($_SESSION[$name]);
    }

    public function isCached($name)
    {
        return isset($_SESSION[$name]);
    }
}


text:
@component('mail::message')
# New Feedback from {{ '@' . $user->username }}

Subject: **{{ $feedback->subject }}**

@component('mail::promotion')
{{ $feedback->description }}
@endcomponent

@component('mail::panel')
Hope you're doing great ;)
@endcomponent

Regards,

The Voten Team
@endcomponent


text:
<?php

namespace Magento\Test\Integrity\Library;

use Magento\Framework\App\Utility\Files;
use Magento\Framework\App\Utility\AggregateInvoker;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\TestFramework\Integrity\Library\Injectable;
use Magento\TestFramework\Integrity\Library\PhpParser\ParserFactory;
use Magento\TestFramework\Integrity\Library\PhpParser\Tokens;
use Zend\Code\Reflection\FileReflection;

/**
 * Test check if Magento library components contain incorrect dependencies to application layer
 *
 */
class DependencyTest extends \PHPUnit_Framework_TestCase
{
    /**
     * Collect errors
     *
     * @var array
     */
    protected $errors = [];

    /**
     * Forbidden base namespaces
     *
     * @return array
     */
    protected function getForbiddenNamespaces()
    {
        return ['Magento'];
    }

    public function testCheckDependencies()
    {
        $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
        $invoker(
            /**
             * @param string $file
             */
            function ($file) {
                $componentRegistrar = new ComponentRegistrar();
                $fileReflection = new FileReflection($file);
                $tokens = new Tokens($fileReflection->getContents(), new ParserFactory());
                $tokens->parseContent();

                $dependencies = array_merge(
                    (new Injectable())->getDependencies($fileReflection),
                    $tokens->getDependencies()
                );

                $pattern = '#^(\\\\|)' . implode('|', $this->getForbiddenNamespaces()) . '\\\\#';
                foreach ($dependencies as $dependency) {
                    $dependencyPaths = explode('/', $dependency);
                    $dependencyPaths = array_slice($dependencyPaths, 2);
                    $dependency = implode('\\', $dependencyPaths);
                    $libraryPaths = $componentRegistrar->getPaths(ComponentRegistrar::LIBRARY);
                    foreach ($libraryPaths as $libraryPath) {
                        $filePath = str_replace('\\', '/', $libraryPath .  '/' . $dependency . '.php');
                        if (preg_match($pattern, $dependency) && !file_exists($filePath)) {
                            $this->errors[$fileReflection->getFileName()][] = $dependency;
                        }
                    }
                }

                if (!empty($this->errors)) {
                    $this->fail($this->getFailMessage());
                }
            },
            $this->libraryDataProvider()
        );
    }

    public function testAppCodeUsage()
    {
        $files = Files::init();
        $componentRegistrar = new ComponentRegistrar();
        $libPaths = $componentRegistrar->getPaths(ComponentRegistrar::LIBRARY);
        $invoker = new AggregateInvoker($this);
        $invoker(
            function ($file) use ($libPaths) {
                $content = file_get_contents($file);
                foreach ($libPaths as $libPath) {
                    if (strpos($file, $libPath) === 0) {
                        $this->assertSame(
                            0,
                            preg_match('~(?<![a-z\\d_:]|->|function\\s)__\\s*\\(~iS', $content),
                            'Function __() is defined outside of the library and must not be used there. ' .
                            'Replacement suggestion: new \\Magento\\Framework\\Phrase()'
                        );
                    }
                }
            },
            $files->getPhpFiles(
                Files::INCLUDE_PUB_CODE |
                Files::INCLUDE_LIBS |
                Files::AS_DATA_SET |
                Files::INCLUDE_NON_CLASSES
            )
        );
    }

    /**
     * @inheritdoc
     */
    public function tearDown()
    {
        $this->errors = [];
    }

    /**
     * Prepare failed message
     *
     * @return string
     */
    protected function getFailMessage()
    {
        $failMessage = '';
        foreach ($this->errors as $class => $dependencies) {
            $failMessage .= $class . ' depends for non-library ' . (count($dependencies) > 1 ? 'classes ' : 'class ');
            foreach ($dependencies as $dependency) {
                $failMessage .= $dependency . ' ';
            }
            $failMessage = trim($failMessage) . PHP_EOL;
        }
        return $failMessage;
    }

    /**
     * Contains all library files
     *
     * @return array
     */
    public function libraryDataProvider()
    {
        // @TODO: remove this code when class Magento\Framework\Data\Collection will fixed
        $componentRegistrar = new ComponentRegistrar();
        include_once $componentRegistrar->getPath(ComponentRegistrar::LIBRARY, 'magento/framework')
            . '/Option/ArrayInterface.php';
        $blackList = Files::init()->readLists(__DIR__ . '/_files/blacklist.txt');
        $dataProvider = Files::init()->getPhpFiles(Files::INCLUDE_LIBS | Files::AS_DATA_SET);

        foreach ($dataProvider as $key => $data) {
            $file = str_replace(BP . '/', '', $data[0]);
            if (in_array($file, $blackList)) {
                unset($dataProvider[$key]);
            } else {
                include_once $data[0];
            }
        }
        return $dataProvider;
    }
}


text:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace HotelSupervisorService.Managers
{
    /// <summary>
    /// 提供加密与解密服务。
    /// </summary>
    internal class EncryptionManager
    {
        public static readonly string IV = "SuFjcEmp/TE=";
        public static readonly string Key = "KIPSToILGp6fl+3gXJvMsN4IajizYBBT";

        /// <summary>
        /// 获取加密服务类。
        /// </summary>
        /// <returns>加密服务类。</returns>
        public static TripleDESCryptoServiceProvider GetCryptoProvider()
        {
            TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
            provider.IV = Convert.FromBase64String(IV);
            provider.Key = Convert.FromBase64String(Key);
            return provider;
        }

        /// <summary>
        /// 对字符串加密。
        /// </summary>
        /// <param name="text">需要加密的字符串。</param>
        /// <returns>加密后的字符串。</returns>
        public static string Encrypt(string text)
        {
            TripleDESCryptoServiceProvider provider = GetCryptoProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(), CryptoStreamMode.Write);
            byte[] toEncrypt = new UTF8Encoding().GetBytes(text);
            cStream.Write(toEncrypt, 0, toEncrypt.Length);
            cStream.FlushFinalBlock();
            byte[] ret = mStream.ToArray();
            cStream.Close();
            mStream.Close();
            return Convert.ToBase64String(ret);
        }

        /// <summary>
        /// 对字符串解密。
        /// </summary>
        /// <param name="text">经过加密字符串。</param>
        /// <returns>解密后的字符串。</returns>
        public static string Decrypt(string text)
        {
            TripleDESCryptoServiceProvider provider = GetCryptoProvider();
            byte[] inputEquivalent = Convert.FromBase64String(text);
            MemoryStream msDecrypt = new MemoryStream();
            CryptoStream csDecrypt = new CryptoStream(msDecrypt,
            provider.CreateDecryptor(),
            CryptoStreamMode.Write);
            csDecrypt.Write(inputEquivalent, 0, inputEquivalent.Length);
            csDecrypt.FlushFinalBlock();
            csDecrypt.Close();
            return new UTF8Encoding().GetString(msDecrypt.ToArray());
        }
    }
}


text:
from json import dumps, loads
from pytz import UTC
from cStringIO import StringIO
from datetime import datetime

from twisted.trial.unittest import TestCase

from txaws.credentials import AWSCredentials
from txaws.service import AWSServiceEndpoint
from txaws.ec2.client import Query
from txaws.server.method import Method
from txaws.server.registry import Registry
from txaws.server.resource import QueryAPI

class FakeRequest(object):

    def __init__(self, params, endpoint):
        self.params = params
        self.endpoint = endpoint
        self.written = StringIO()
        self.finished = False
        self.code = None
        self.headers = {"Host": endpoint.get_canonical_host()}

    @property
    def args(self):
        return dict((key, [value]) for key, value in self.params.iteritems())

    @property
    def method(self):
        return self.endpoint.method

    @property
    def path(self):
        return self.endpoint.path

    def write(self, content):
        assert isinstance(content, str), "Only strings should be written"
        self.written.write(content)

    def finish(self):
        if self.code is None:
            self.code = 200
        self.finished = True

    def setResponseCode(self, code):
        self.code = code

    def setHeader(self, key, value):
        self.headers[key] = value

    def getHeader(self, key):
        return self.headers.get(key)

    @property
    def response(self):
        return self.written.getvalue()

class TestMethod(Method):

    def invoke(self, call):
        return "data"

class TestPrincipal(object):

    def __init__(self, creds):
        self.creds = creds

    @property
    def access_key(self):
        return self.creds.access_key

    @property
    def secret_key(self):
        return self.creds.secret_key

class TestQueryAPI(QueryAPI):

    signature_versions = (1, 2)
    content_type = "text/plain"

    def __init__(self, *args, **kwargs):
        QueryAPI.__init__(self, *args, **kwargs)
        self.principal = None

    def get_principal(self, access_key):
        if self.principal and self.principal.access_key == access_key:
            return self.principal

    def dump_error(self, error, request):
        return str("%s - %s" % (error.code, error.message))

class QueryAPITest(TestCase):

    def setUp(self):
        super(QueryAPITest, self).setUp()
        self.registry = Registry()
        self.registry.add(TestMethod, action="SomeAction", version=None)
        self.api = TestQueryAPI(registry=self.registry)

    def test_handle(self):
        """
        L{QueryAPI.handle} forwards valid requests to L{QueryAPI.execute}.
        """
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://uri")
        query = Query(action="SomeAction", creds=creds, endpoint=endpoint)
        query.sign()
        request = FakeRequest(query.params, endpoint)

        def check(ignored):
            self.assertTrue(request.finished)
            self.assertEqual("data", request.response)
            self.assertEqual("4", request.headers["Content-Length"])
            self.assertEqual("text/plain", request.headers["Content-Type"])
            self.assertEqual(200, request.code)

        self.api.principal = TestPrincipal(creds)
        return self.api.handle(request).addCallback(check)

    def test_handle_with_dump_result(self):
        """
        L{QueryAPI.handle} serializes the action result with C{dump_result}.
        """
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://uri")
        query = Query(action="SomeAction", creds=creds, endpoint=endpoint)
        query.sign()
        request = FakeRequest(query.params, endpoint)

        def check(ignored):
            self.assertEqual("data", loads(request.response))

        self.api.dump_result = dumps
        self.api.principal = TestPrincipal(creds)
        return self.api.handle(request).addCallback(check)

    def test_handle_with_deprecated_actions(self):
        """
        L{QueryAPI.handle} supports the legacy 'actions' attribute.
        """
        self.api.actions = ["SomeAction"]
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://uri")
        query = Query(action="SomeAction", creds=creds, endpoint=endpoint)
        query.sign()
        request = FakeRequest(query.params, endpoint)

        def check(ignored):
            self.assertEqual("data", request.response)

        self.api.principal = TestPrincipal(creds)
        return self.api.handle(request).addCallback(check)

    def test_handle_pass_params_to_call(self):
        """
        L{QueryAPI.handle} creates a L{Call} object with the correct
        parameters.
        """
        self.registry.add(TestMethod, "SomeAction", "1.2.3")
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://uri")
        query = Query(action="SomeAction", creds=creds, endpoint=endpoint,
                      other_params={"Foo": "bar", "Version": "1.2.3"})
        query.sign()
        request = FakeRequest(query.params, endpoint)

        def execute(call):
            self.assertEqual({"Foo": "bar"}, call.get_raw_params())
            self.assertIdentical(self.api.principal, call.principal)
            self.assertEqual("SomeAction", call.action)
            self.assertEqual("1.2.3", call.version)
            self.assertEqual(request.id, call.id)
            return "ok"

        def check(ignored):
            self.assertEqual("ok", request.response)
            self.assertEqual(200, request.code)

        self.api.execute = execute
        self.api.principal = TestPrincipal(creds)
        return self.api.handle(request).addCallback(check)

    def test_handle_empty_request(self):
        """
        If an empty request is received a message describing the API is
        returned.
        """
        endpoint = AWSServiceEndpoint("http://uri")
        request = FakeRequest({}, endpoint)
        self.assertEqual("Query API Service", self.api.render(request))
        self.assertEqual("text/plain", request.headers["Content-Type"])
        self.assertEqual(None, request.code)

    def test_handle_with_signature_version_1(self):
        """SignatureVersion 1 is supported as well."""
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://uri")
        query = Query(action="SomeAction", creds=creds, endpoint=endpoint,
                      other_params={"SignatureVersion": "1"})
        query.sign()
        request = FakeRequest(query.params, endpoint)

        def check(ignore):
            self.assertEqual("data", request.response)
            self.assertEqual(200, request.code)

        self.api.principal = TestPrincipal(creds)
        return self.api.handle(request).addCallback(check)

    def test_handle_with_signature_sha1(self):
        """
        The C{HmacSHA1} signature method is supported, in which case the
        signing using sha1 instead.
        """
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://uri")
        query = Query(action="SomeAction", creds=creds, endpoint=endpoint)
        query.sign(hash_type="sha1")
        request = FakeRequest(query.params, endpoint)

        def check(ignore):
            self.assertEqual("data", request.response)
            self.assertEqual(200, request.code)

        self.api.principal = TestPrincipal(creds)
        return self.api.handle(request).addCallback(check)

    def test_handle_with_unsupported_version(self):
        """If signature versions is not supported an error is raised."""
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://uri")
        query = Query(action="SomeAction", creds=creds, endpoint=endpoint)
        query.sign()
        request = FakeRequest(query.params, endpoint)

        def check(ignored):
            self.flushLoggedErrors()
            self.assertEqual("InvalidSignature - SignatureVersion '2' "
                             "not supported", request.response)
            self.assertEqual(403, request.code)

        self.api.signature_versions = (1,)
        return self.api.handle(request).addCallback(check)

    def test_handle_with_internal_error(self):
        """
        If an unknown error occurs while handling the request,
        L{QueryAPI.handle} responds with HTTP status 500.
        """
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://uri")
        query = Query(action="SomeAction", creds=creds, endpoint=endpoint)
        query.sign()
        request = FakeRequest(query.params, endpoint)

        self.api.execute = lambda call: 1 / 0

        def check(ignored):
            self.flushLoggedErrors()
            self.assertTrue(request.finished)
            self.assertEqual("integer division or modulo by zero",
                             request.response)
            self.assertEqual(500, request.code)

        self.api.principal = TestPrincipal(creds)
        return self.api.handle(request).addCallback(check)

    def test_handle_with_parameter_error(self):
        """
        If an error occurs while parsing the parameters, L{QueryAPI.handle}
        responds with HTTP status 400.
        """
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://uri")
        query = Query(action="SomeAction", creds=creds, endpoint=endpoint)
        query.sign()
        query.params.pop("Action")
        request = FakeRequest(query.params, endpoint)

        def check(ignored):
            self.flushLoggedErrors()
            self.assertEqual("MissingParameter - The request must contain "
                             "the parameter Action", request.response)
            self.assertEqual(400, request.code)

        return self.api.handle(request).addCallback(check)

    def test_handle_with_unsupported_action(self):
        """Only actions registered in the L{Registry} are supported."""
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://uri")
        query = Query(action="FooBar", creds=creds, endpoint=endpoint)
        query.sign()
        request = FakeRequest(query.params, endpoint)

        def check(ignored):
            self.flushLoggedErrors()
            self.assertEqual("InvalidAction - The action FooBar is not valid"
                             " for this web service.", request.response)
            self.assertEqual(400, request.code)

        return self.api.handle(request).addCallback(check)

    def test_handle_non_evailable_method(self):
        """Only actions registered in the L{Registry} are supported."""

        class NonAvailableMethod(Method):

            def is_available(self):
                return False

        self.registry.add(NonAvailableMethod, action="CantDoIt")
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://uri")
        query = Query(action="CantDoIt", creds=creds, endpoint=endpoint)
        query.sign()
        request = FakeRequest(query.params, endpoint)

        def check(ignored):
            self.flushLoggedErrors()
            self.assertEqual("InvalidAction - The action CantDoIt is not "
                             "valid for this web service.", request.response)
            self.assertEqual(400, request.code)

        self.api.principal = TestPrincipal(creds)
        return self.api.handle(request).addCallback(check)

    def test_handle_with_deprecated_actions_and_unsupported_action(self):
        """
        If the deprecated L{QueryAPI.actions} attribute is set, it will be
        used for looking up supported actions.
        """
        self.api.actions = ["SomeAction"]
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://uri")
        query = Query(action="FooBar", creds=creds, endpoint=endpoint)
        query.sign()
        request = FakeRequest(query.params, endpoint)

        def check(ignored):
            self.flushLoggedErrors()
            self.assertEqual("InvalidAction - The action FooBar is not valid"
                             " for this web service.", request.response)
            self.assertEqual(400, request.code)

        return self.api.handle(request).addCallback(check)

    def test_handle_with_non_existing_user(self):
        """
        If no L{Principal} can be found with the given access key ID,
        L{QueryAPI.handle} responds with HTTP status 400.
        """
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://uri")
        query = Query(action="SomeAction", creds=creds, endpoint=endpoint)
        query.sign()
        request = FakeRequest(query.params, endpoint)

        def check(ignored):
            self.flushLoggedErrors()
            self.assertEqual("AuthFailure - No user with access key 'access'",
                             request.response)
            self.assertEqual(401, request.code)

        return self.api.handle(request).addCallback(check)

    def test_handle_with_wrong_signature(self):
        """
        If the signature in the request doesn't match the one calculated with
        the locally stored secret access key, and error is returned.
        """
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://uri")
        query = Query(action="SomeAction", creds=creds, endpoint=endpoint)
        query.sign()
        query.params["Signature"] = "wrong"
        request = FakeRequest(query.params, endpoint)

        def check(ignored):
            self.flushLoggedErrors()
            self.assertEqual("SignatureDoesNotMatch - The request signature "
                             "we calculated does not match the signature you "
                             "provided. Check your key and signing method.",
                             request.response)
            self.assertEqual(403, request.code)

        self.api.principal = TestPrincipal(creds)
        return self.api.handle(request).addCallback(check)

    def test_handle_with_timestamp_and_expires(self):
        """
        If the request contains both Expires and Timestamp parameters,
        an error is returned.
        """
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://uri")
        query = Query(action="SomeAction", creds=creds, endpoint=endpoint,
                      other_params={"Timestamp": "2010-01-01T12:00:00Z",
                                    "Expires": "2010-01-01T12:00:00Z"})
        query.sign()
        request = FakeRequest(query.params, endpoint)

        def check(ignored):
            self.flushLoggedErrors()
            self.assertEqual(
                "InvalidParameterCombination - The parameter Timestamp"
                " cannot be used with the parameter Expires",
                request.response)
            self.assertEqual(400, request.code)

        return self.api.handle(request).addCallback(check)

    def test_handle_with_non_expired_signature(self):
        """
        If the request contains an Expires parameter with a time that is after
        the current time, everything is fine.
        """
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://uri")
        query = Query(action="SomeAction", creds=creds, endpoint=endpoint,
                      other_params={"Expires": "2010-01-01T12:00:00Z"})
        query.sign()
        request = FakeRequest(query.params, endpoint)

        def check(ignored):
            self.assertEqual("data", request.response)
            self.assertEqual(200, request.code)

        now = datetime(2009, 12, 31, tzinfo=UTC)
        self.api.get_utc_time = lambda: now
        self.api.principal = TestPrincipal(creds)
        return self.api.handle(request).addCallback(check)

    def test_handle_with_expired_signature(self):
        """
        If the request contains an Expires parameter with a time that is before
        the current time, an error is returned.
        """
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://uri")
        query = Query(action="SomeAction", creds=creds, endpoint=endpoint,
                      other_params={"Expires": "2010-01-01T12:00:00Z"})
        query.sign()
        request = FakeRequest(query.params, endpoint)

        def check(ignored):
            self.flushLoggedErrors()
            self.assertEqual(
                "RequestExpired - Request has expired. Expires date is"
                " 2010-01-01T12:00:00Z", request.response)
            self.assertEqual(400, request.code)

        now = datetime(2010, 1, 1, 12, 0, 1, tzinfo=UTC)
        self.api.get_utc_time = lambda: now
        return self.api.handle(request).addCallback(check)

    def test_handle_with_post_method(self):
        """
        L{QueryAPI.handle} forwards valid requests using the HTTP POST method
        to L{QueryAPI.execute}.
        """
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://uri", method="POST")
        query = Query(action="SomeAction", creds=creds, endpoint=endpoint)
        query.sign()
        request = FakeRequest(query.params, endpoint)

        def check(ignored):
            self.assertEqual("data", request.response)
            self.assertEqual(200, request.code)

        self.api.principal = TestPrincipal(creds)
        return self.api.handle(request).addCallback(check)

    def test_handle_with_port_number(self):
        """
        If the request Host header includes a port number, it's included
        in the text that get signed when checking the signature.
        """
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://endpoint:1234")
        query = Query(action="SomeAction", creds=creds, endpoint=endpoint)
        query.sign()
        request = FakeRequest(query.params, endpoint)

        def check(ignored):
            self.assertEqual("data", request.response)
            self.assertEqual(200, request.code)

        self.api.principal = TestPrincipal(creds)
        return self.api.handle(request).addCallback(check)

    def test_handle_with_endpoint_with_terminating_slash(self):
        """
        Check signature should handle a URI with a terminating slash.
        """
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://endpoint/")
        query = Query(action="SomeAction", creds=creds, endpoint=endpoint)
        query.sign()
        request = FakeRequest(query.params, endpoint)

        def check(ignored):
            self.assertEqual("data", request.response)
            self.assertEqual(200, request.code)

        self.api.principal = TestPrincipal(creds)
        return self.api.handle(request).addCallback(check)

    def test_handle_with_custom_path(self):
        """
        If L{QueryAPI.path} is not C{None} it will be used in place of
        the HTTP request path when calculating the signature.
        """
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://endpoint/path/")
        query = Query(action="SomeAction", creds=creds, endpoint=endpoint)
        query.sign()
        request = FakeRequest(query.params, endpoint)
        # Simulate a request rewrite, like apache would do
        request.endpoint.path = "/"

        def check(ignored):
            self.assertTrue(request.finished)
            self.assertEqual(200, request.code)

        self.api.principal = TestPrincipal(creds)
        self.api.path = "/path/"
        return self.api.handle(request).addCallback(check)

    def test_handle_with_custom_path_and_rest(self):
        """
        If L{QueryAPI.path} is not C{None} it will be used in place of
        the HTTP request path when calculating the signature. The rest
        of the path is appended as for the HTTP request.
        """
        creds = AWSCredentials("access", "secret")
        endpoint = AWSServiceEndpoint("http://endpoint/path/rest")
        query = Query(action="SomeAction", creds=creds, endpoint=endpoint)
        query.sign()
        request = FakeRequest(query.params, endpoint)
        # Simulate a request rewrite, like apache would do
        request.endpoint.path = "/rest"

        def check(ignored):
            self.assertTrue(request.finished)
            self.assertEqual(200, request.code)

        self.api.principal = TestPrincipal(creds)
        self.api.path = "/path/"
        return self.api.handle(request).addCallback(check)


text:
<?php
	/*
This script is use to upload any Excel file into database.
Here, you can browse your Excel file and upload it into
your database.

Download Link: http://www.discussdesk.com/import-excel-file-data-in-mysql-database-using-PHP.htm

Website URL: http://www.discussdesk.com
*/
?>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Demo - Import Excel file data in mysql database using PHP, Upload Excel file data in database</title>
<meta name="description" content="This tutorial will learn how to import excel sheet data in mysql database using php. Here, first upload an excel sheet into your server and then click to import it into database. All column of excel sheet will store into your corrosponding database table."/>
<meta name="keywords" content="import excel file data in mysql, upload ecxel file in mysql, upload data, code to import excel data in mysql database, php, Mysql, Ajax, Jquery, Javascript, download, upload, upload excel file,mysql"/>

<?php
/************************ YOUR DATABASE CONNECTION START HERE   ****************************/

define ("DB_HOST", "localhost"); // set database host
define ("DB_USER", "gloria"); // set database user
define ("DB_PASS","1234"); // set database password
define ("DB_NAME","woodenbox_contents"); // set database name

$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
$db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");

$databasetable = "students";

/************************ YOUR DATABASE CONNECTION END HERE  ****************************/

set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
include 'PHPExcel/IOFactory.php';

// This is the file path to be uploaded.
$inputFileName = 'discussdesk.xlsx';

try {
	$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
} catch(Exception $e) {
	die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}

$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet);  // Here get total count of row in that Excel sheet

for($i=2;$i<=$arrayCount;$i++){
$student_id = trim($allDataInSheet[$i]["A"]);
$first_name = trim($allDataInSheet[$i]["B"]);
$last_name = trim($allDataInSheet[$i]["C"]);
$middle_name = trim($allDataInSheet[$i]["D"]);
$age = trim($allDataInSheet[$i]["E"]);
$grade = trim($allDataInSheet[$i]["F"]);
$fromTime = trim($allDataInSheet[$i]["G"]);
$toTime = trim($allDataInSheet[$i]["H"]);
$academicstatus = trim($allDataInSheet[$i]["I"]);
$paymentmode = trim($allDataInSheet[$i]["J"]);

$query = "SELECT first_name,last_name FROM students WHERE first_name = '$first_name' AND last_name = '$last_name'";
$sql = mysql_query($query);
$recResult = mysql_fetch_array($sql);
$existName = $recResult["first_name"];
if($existName=="") {
$insertTable= mysql_query("insert into students (student_id, first_name, last_name, middle_name, age, grade, fromTime, toTime, academicstatus,paymentmode) values('$student_id', '$first_name', '$last_name', '$middle_name', '$age', '$grade', '$fromTime', '$toTime', '$academicstatus', '$paymentmode');");
$msg = 'Record has been added.
Go Back to tutorial
';
} else {
$msg = 'Record already exist.
Go Back to tutorial
';
}
}
echo "
".$msg."
";

?>


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

namespace Aras.ViewModel.WebService.Models.Responses
{
    public class Empty : Response
    {
        public Empty(ViewModel.Manager.Session Session)
            :base(Session)
        {
            List<ViewModel.Control> viewmodelcontrols = new List<ViewModel.Control>();
            this.QueueControls(Session, viewmodelcontrols);
        }
    }
}


text:
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="VcsDirectoryMappings">
    <mapping directory="" vcs="" />
    <mapping directory="$PROJECT_DIR$/../queue_ding" vcs="Git" />
    <mapping directory="$PROJECT_DIR$/../rubyneat_dashboard" vcs="Git" />
  </component>
</project>


text:
<?php

namespace Currency;

class Zwd extends \Currency\AbstractCurrency
{
    const CURRENCY_ISO_4217 = 'ZWD';
}


text:
<input version="1">

<!--
Event 1 : Keyboard button press
    'id' indicates which button, as defined by irrlicht's EKEY_CODE enum
    'character' contains the associated unicode character.
        Only used as fallback when displaying special characters in the UI.
Event 2 : Gamepad stick motion
    'id' indicates which stick, starting from 0
    'direction' 0 means negative, 1 means positive
Event 3 : Gamepad button press
    'id' indicates which button, starting from 0
-->

<keyboard enabled="true">
    <action name="steerLeft" event="1" id="37" character="0" />
    <action name="steerRight" event="1" id="39" character="0" />
    <action name="accel" event="1" id="38" character="0" />
    <action name="brake" event="1" id="40" character="0" />
    <action name="nitro" event="1" id="78" character="0" />
    <action name="drift" event="1" id="86" character="0" />
    <action name="rescue" event="1" id="8" character="0" />
    <action name="fire" event="1" id="32" character="0" />
    <action name="lookBack" event="1" id="66" character="0" />
    <action name="pauserace" event="1" id="27" character="0" />
    <action name="menuUp" event="1" id="38" character="0" />
    <action name="menuDown" event="1" id="40" character="0" />
    <action name="menuLeft" event="1" id="37" character="0" />
    <action name="menuRight" event="1" id="39" character="0" />
    <action name="menuSelect" event="1" id="13" character="0" />
    <action name="menuCancel" event="1" id="27" character="0" />
</keyboard>

</input>


text:
<?xml version="1.0"?>
<!DOCTYPE import-control PUBLIC
        "-//Puppy Crawl//DTD Import Control 1.1//EN"
        "http://www.puppycrawl.com/dtds/import_control_1_1.dtd">

<import-control pkg="com.puppycrawl.tools.checkstyle">

    <allow pkg="antlr"/>
    <allow pkg="org.antlr.v4.runtime"/>
    <allow pkg="com.google.common"/>
    <allow pkg="com.puppycrawl.tools.checkstyle.api"/>
    <allow pkg="com.puppycrawl.tools.checkstyle.checks"/>
    <allow pkg="java.io"/>
    <allow pkg="java.net"/>
    <allow pkg="java.nio"/>
    <allow pkg="java.util"/>
    <allow pkg="javax.xml.parsers"/>
    <allow pkg="org.apache.commons.beanutils"/>
    <allow pkg="org.apache.commons.logging"/>
    <allow pkg="org.xml.sax"/>
    <allow pkg="com.puppycrawl.tools.checkstyle"/>

    <!-- The local ones -->
    <allow class="java.security.MessageDigest" local-only="true"/>
    <allow pkg="com.puppycrawl.tools.checkstyle.grammars" local-only="true"/>
    <allow pkg="org.apache.commons.cli" local-only="true"/>
    <allow pkg="org.apache.tools.ant" local-only="true"/>

    <subpackage name="api">
        <allow pkg="com.puppycrawl.tools.checkstyle.grammars"/>
        <allow pkg="java.beans"/>
        <allow pkg="java.lang.reflect"/>
        <allow pkg="java.text"/>
        <allow class="com.puppycrawl.tools.checkstyle.grammars.CommentListener"
               local-only="true"/>
        <allow class="com.puppycrawl.tools.checkstyle.grammars.GeneratedJavaTokenTypes"
               local-only="true"/>
        <allow class="com.puppycrawl.tools.checkstyle.Utils"
               local-only="true"/>
    </subpackage>

    <subpackage name="checks">
        <allow pkg="com.puppycrawl.tools.checkstyle.checks"/>
        <allow class="com.puppycrawl.tools.checkstyle.Defn"/>
        <allow pkg="java.math"/>

        <subpackage name="indentation">
            <allow pkg="java.lang.reflect"/>
        </subpackage>
        <subpackage name="header">
            <allow class="java.nio.charset.Charset" local-only="true"/>
        </subpackage>
        <subpackage name="javadoc">
            <allow pkg="com.puppycrawl.tools.checkstyle.grammars.javadoc"/>
            <allow pkg="java.lang.reflect"/>
        </subpackage>
    </subpackage>

    <subpackage name="doclets">
        <allow pkg="com.sun.javadoc"/>
    </subpackage>

    <subpackage name="filters">
        <allow pkg="java.lang.ref"/>
    </subpackage>

    <subpackage name="gui">
        <allow pkg="java.awt"/>
        <allow pkg="javax.swing"/>
    </subpackage>
</import-control>


text:
'use strict';

var mongoose = require('mongoose');
var _ = require('lodash');

require('../models/Time');
var Time = mongoose.model('Time');

// Handle get request
exports.get = function(req, res, next) {
    var user =  req.params.user;

    // Get all entries from current user
    Time.find({owner: user}).sort({startTime: 'desc'}).exec(function (err, times) {
        if (err) {
            return next(err);
        }

        var formattedTimes = _.map(times, function(time) {
            return time.toJSON();
        });

        // Send the formatted time of this user
        res.send(formattedTimes);
    });

};

// Handle put requests
exports.put = function(req, res, next) {
    var user =  req.params.user;
    var submittedData = req.body;

    // Executed when all updates have been made
    var done = _.after(submittedData.length, function() {
        res.send({status: 'OK'});
    });

    // Remove all existing times of this user
    Time.remove({owner: user}, function (err) {
        if (err) {
            return next(err);
        }

        // Iterate over all the time data and save it
        _.each(submittedData, function(timeData) {
            // Create a Time object
            var time = new Time();

            // Pick the right data from the request
            timeData = _.pick(timeData,
                'name',
                'startTime',
                'endTime',
                'pastDuration',
                'totalManualChange',
                'completed'
            );

            // Add the provided user as owner
            timeData.owner = user;

            // Write it to the Time object
            _.assign(time, timeData);

            // Save it
            time.save(function(err) {
                if (err) {
                    return next(err);
                }
                done();
            });
        });
    });
};