text:
package org.apache.sentry.binding.hive;

import static org.apache.hadoop.hive.metastore.MetaStoreUtils.DEFAULT_DATABASE_NAME;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;

import org.apache.hadoop.hive.common.JavaUtils;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.ql.exec.DDLTask;
import org.apache.hadoop.hive.ql.exec.FunctionRegistry;
import org.apache.hadoop.hive.ql.exec.SentryFilterDDLTask;
import org.apache.hadoop.hive.ql.exec.SentryGrantRevokeTask;
import org.apache.hadoop.hive.ql.exec.Task;
import org.apache.hadoop.hive.ql.hooks.Entity;
import org.apache.hadoop.hive.ql.hooks.Entity.Type;
import org.apache.hadoop.hive.ql.hooks.Hook;
import org.apache.hadoop.hive.ql.hooks.ReadEntity;
import org.apache.hadoop.hive.ql.hooks.WriteEntity;
import org.apache.hadoop.hive.ql.metadata.AuthorizationException;
import org.apache.hadoop.hive.ql.parse.ASTNode;
import org.apache.hadoop.hive.ql.parse.AbstractSemanticAnalyzerHook;
import org.apache.hadoop.hive.ql.parse.BaseSemanticAnalyzer;
import org.apache.hadoop.hive.ql.parse.HiveParser;
import org.apache.hadoop.hive.ql.parse.HiveSemanticAnalyzerHookContext;
import org.apache.hadoop.hive.ql.parse.SemanticException;
import org.apache.hadoop.hive.ql.plan.DDLWork;
import org.apache.hadoop.hive.ql.plan.HiveOperation;
import org.apache.hadoop.hive.ql.session.SessionState;
import org.apache.sentry.binding.hive.authz.HiveAuthzBinding;
import org.apache.sentry.binding.hive.authz.HiveAuthzPrivileges;
import org.apache.sentry.binding.hive.authz.HiveAuthzPrivileges.HiveOperationScope;
import org.apache.sentry.binding.hive.authz.HiveAuthzPrivileges.HiveOperationType;
import org.apache.sentry.binding.hive.authz.HiveAuthzPrivilegesMap;
import org.apache.sentry.binding.hive.conf.HiveAuthzConf;
import org.apache.sentry.core.common.Subject;
import org.apache.sentry.core.common.utils.PathUtils;
import org.apache.sentry.core.model.db.AccessURI;
import org.apache.sentry.core.model.db.Column;
import org.apache.sentry.core.model.db.DBModelAction;
import org.apache.sentry.core.model.db.DBModelAuthorizable;
import org.apache.sentry.core.model.db.DBModelAuthorizable.AuthorizableType;
import org.apache.sentry.core.model.db.Database;
import org.apache.sentry.core.model.db.Table;
import org.apache.sentry.provider.cache.PrivilegeCache;
import org.apache.sentry.provider.cache.SimplePrivilegeCache;
import org.apache.sentry.provider.common.AuthorizationProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;

public class HiveAuthzBindingHook extends AbstractSemanticAnalyzerHook {
  private static final Logger LOG = LoggerFactory
      .getLogger(HiveAuthzBindingHook.class);
  private final HiveAuthzBinding hiveAuthzBinding;
  private final HiveAuthzConf authzConf;
  private Database currDB = Database.ALL;
  private Table currTab;
  private AccessURI udfURI;
  private AccessURI partitionURI;
  private Table currOutTab = null;
  private Database currOutDB = null;

  // True if this is a basic DESCRIBE <table> operation. False for other DESCRIBE variants
  // like DESCRIBE [FORMATTED|EXTENDED]. Required because Hive treats these stmts as the same
  // HiveOperationType, but we want to enforces different privileges on each statement.
  // Basic DESCRIBE <table> is allowed with only column-level privs, while the variants
  // require table-level privileges.
  public boolean isDescTableBasic = false;

  public HiveAuthzBindingHook() throws Exception {
    SessionState session = SessionState.get();
    if(session == null) {
      throw new IllegalStateException("Session has not been started");
    }
    // HACK: set a random classname to force the Auth V2 in Hive
    SessionState.get().setAuthorizer(null);

    HiveConf hiveConf = session.getConf();
    if(hiveConf == null) {
      throw new IllegalStateException("Session HiveConf is null");
    }
    authzConf = loadAuthzConf(hiveConf);
    hiveAuthzBinding = new HiveAuthzBinding(hiveConf, authzConf);

    FunctionRegistry.setupPermissionsForBuiltinUDFs("", HiveAuthzConf.HIVE_UDF_BLACK_LIST);
  }

  public static HiveAuthzConf loadAuthzConf(HiveConf hiveConf) {
    boolean depreicatedConfigFile = false;
    HiveAuthzConf newAuthzConf = null;
    String hiveAuthzConf = hiveConf.get(HiveAuthzConf.HIVE_SENTRY_CONF_URL);
    if(hiveAuthzConf == null || (hiveAuthzConf = hiveAuthzConf.trim()).isEmpty()) {
      hiveAuthzConf = hiveConf.get(HiveAuthzConf.HIVE_ACCESS_CONF_URL);
      depreicatedConfigFile = true;
    }

    if(hiveAuthzConf == null || (hiveAuthzConf = hiveAuthzConf.trim()).isEmpty()) {
      throw new IllegalArgumentException("Configuration key " + HiveAuthzConf.HIVE_SENTRY_CONF_URL
          + " value '" + hiveAuthzConf + "' is invalid.");
    }
    try {
      newAuthzConf = new HiveAuthzConf(new URL(hiveAuthzConf));
    } catch (MalformedURLException e) {
      if (depreicatedConfigFile) {
        throw new IllegalArgumentException("Configuration key " + HiveAuthzConf.HIVE_ACCESS_CONF_URL
            + " specifies a malformed URL '" + hiveAuthzConf + "'", e);
      } else {
        throw new IllegalArgumentException("Configuration key " + HiveAuthzConf.HIVE_SENTRY_CONF_URL
            + " specifies a malformed URL '" + hiveAuthzConf + "'", e);
      }
    }
    return newAuthzConf;
  }

  /**
   * Pre-analyze hook called after compilation and before semantic analysis We
   * extract things for to Database and metadata level operations which are not
   * capture in the input/output entities during semantic analysis. Ideally it
   * should be handled in Hive. We need to move most of these into hive semantic
   * analyzer and then remove it from the access hook.
   */
  @Override
  public ASTNode preAnalyze(HiveSemanticAnalyzerHookContext context, ASTNode ast)
      throws SemanticException {

    switch (ast.getToken().getType()) {
    // Hive parser doesn't capture the database name in output entity, so we store it here for now
      case HiveParser.TOK_CREATEDATABASE:
      case HiveParser.TOK_ALTERDATABASE_PROPERTIES:
      case HiveParser.TOK_DROPDATABASE:
      case HiveParser.TOK_SWITCHDATABASE:
      case HiveParser.TOK_DESCDATABASE:
        currDB = new Database(BaseSemanticAnalyzer.unescapeIdentifier(ast.getChild(0).getText()));
        break;
      case HiveParser.TOK_CREATETABLE:
      case HiveParser.TOK_CREATEVIEW:
        /*
         * Compiler doesn't create read/write entities for create table.
         * Hence we need extract dbname from db.tab format, if applicable
         */
        currDB = extractDatabase((ASTNode)ast.getChild(0));
        break;
      case HiveParser.TOK_DROPTABLE:
      case HiveParser.TOK_DROPVIEW:
      case HiveParser.TOK_SHOW_CREATETABLE:
      case HiveParser.TOK_ALTERTABLE_SERIALIZER:
      case HiveParser.TOK_ALTERVIEW_ADDPARTS:
      case HiveParser.TOK_ALTERVIEW_DROPPARTS:
      case HiveParser.TOK_ALTERVIEW_PROPERTIES:
      case HiveParser.TOK_ALTERVIEW_RENAME:
      case HiveParser.TOK_CREATEINDEX:
      case HiveParser.TOK_DROPINDEX:
      case HiveParser.TOK_LOCKTABLE:
      case HiveParser.TOK_UNLOCKTABLE:
        currTab = extractTable((ASTNode)ast.getFirstChildWithType(HiveParser.TOK_TABNAME));
        currDB = extractDatabase((ASTNode) ast.getChild(0));
        break;
      case HiveParser.TOK_ALTERINDEX_REBUILD:
        currTab = extractTable((ASTNode)ast.getChild(0)); //type is not TOK_TABNAME
        currDB = extractDatabase((ASTNode) ast.getChild(0));
        break;
      case HiveParser.TOK_SHOW_TABLESTATUS:
        currDB = extractDatabase((ASTNode)ast.getChild(0));
        int children = ast.getChildCount();
        for (int i = 1; i < children; i++) {
          ASTNode child = (ASTNode) ast.getChild(i);
          if (child.getToken().getType() == HiveParser.Identifier) {
            currDB = new Database(child.getText());
            break;
          }
        }
        //loosing the requested privileges for possible wildcard tables, since
        //further authorization will be done at the filter step and those unwanted will
        //eventually be filtered out from the output
        currTab = Table.ALL;
        break;
      case HiveParser.TOK_ALTERTABLE_RENAME:
      case HiveParser.TOK_ALTERTABLE_PROPERTIES:
      case HiveParser.TOK_ALTERTABLE_DROPPARTS:
      case HiveParser.TOK_ALTERTABLE_RENAMECOL:
      case HiveParser.TOK_ALTERTABLE_ADDCOLS:
      case HiveParser.TOK_ALTERTABLE_REPLACECOLS:
      case HiveParser.TOK_SHOW_TBLPROPERTIES:
      case HiveParser.TOK_SHOWINDEXES:
      case HiveParser.TOK_SHOWPARTITIONS:
        //token name TOK_TABNAME is not properly set in this case
        currTab = extractTable((ASTNode)ast.getChild(0));
        currDB = extractDatabase((ASTNode)ast.getChild(0));
        break;
      case HiveParser.TOK_MSCK:
        // token name TOK_TABNAME is not properly set in this case and child(0) does
        // not contain the table name.
        // TODO: Fix Hive to capture the table and DB name
        currOutTab = extractTable((ASTNode)ast.getChild(1));
        currOutDB  = extractDatabase((ASTNode)ast.getChild(0));
        break;
      case HiveParser.TOK_ALTERTABLE_ADDPARTS:
        /*
         * Compiler doesn't create read/write entities for create table.
         * Hence we need extract dbname from db.tab format, if applicable
         */
        currTab = extractTable((ASTNode)ast.getChild(0));
        currDB = extractDatabase((ASTNode)ast.getChild(0));
        partitionURI = extractPartition(ast);
        break;
      case HiveParser.TOK_CREATEFUNCTION:
        String udfClassName = BaseSemanticAnalyzer.unescapeSQLString(ast.getChild(1).getText());
        try {
          CodeSource udfSrc = Class.forName(udfClassName).getProtectionDomain().getCodeSource();
          if (udfSrc == null) {
            throw new SemanticException("Could not resolve the jar for UDF class " + udfClassName);
          }
          String udfJar = udfSrc.getLocation().getPath();
          if (udfJar == null || udfJar.isEmpty()) {
            throw new SemanticException("Could not find the jar for UDF class " + udfClassName +
                "to validate privileges");
          }
          udfURI = parseURI(udfSrc.getLocation().toString(), true);
        } catch (ClassNotFoundException e) {
          throw new SemanticException("Error retrieving udf class", e);
        }
        // create/drop function is allowed with any database
        currDB = Database.ALL;
        break;
      case HiveParser.TOK_DROPFUNCTION:
        // create/drop function is allowed with any database
        currDB = Database.ALL;
        break;

      case HiveParser.TOK_LOAD:
        String dbName = BaseSemanticAnalyzer.unescapeIdentifier(ast.getChild(1).getChild(0).getChild(0).getText());
        currDB = new Database(dbName);
        break;
      case HiveParser.TOK_DESCTABLE:
        currDB = getCanonicalDb();
        // For DESCRIBE FORMATTED/EXTENDED ast will have an additional child node with value
        // "FORMATTED/EXTENDED".
        isDescTableBasic = (ast.getChildCount() == 1);
        break;
      default:
        currDB = getCanonicalDb();
        break;
    }
    return ast;
  }

  // Find the current database for session
  private Database getCanonicalDb() {
    return new Database(SessionState.get().getCurrentDatabase());
  }

  private Database extractDatabase(ASTNode ast) throws SemanticException {
    String tableName = BaseSemanticAnalyzer.getUnescapedName(ast);
    if (tableName.contains(".")) {
      return new Database((tableName.split("\\."))[0]);
    } else {
      return getCanonicalDb();
    }
  }
  private Table extractTable(ASTNode ast) throws SemanticException {
    String tableName = BaseSemanticAnalyzer.getUnescapedName(ast);
    if (tableName.contains(".")) {
      return new Table((tableName.split("\\."))[1]);
    } else {
      return new Table(tableName);
    }
  }

  @VisibleForTesting
  protected static AccessURI extractPartition(ASTNode ast) throws SemanticException {
    for (int i = 0; i < ast.getChildCount(); i++) {
      ASTNode child = (ASTNode)ast.getChild(i);
      if (child.getToken().getType() == HiveParser.TOK_PARTITIONLOCATION &&
          child.getChildCount() == 1) {
        return parseURI(BaseSemanticAnalyzer.
          unescapeSQLString(child.getChild(0).getText()));
      }
    }
    return null;
  }

  @VisibleForTesting
  protected static AccessURI parseURI(String uri) throws SemanticException {
    return parseURI(uri, false);
  }

  @VisibleForTesting
  protected static AccessURI parseURI(String uri, boolean isLocal)
      throws SemanticException {
    try {
      HiveConf conf = SessionState.get().getConf();
      String warehouseDir = conf.getVar(ConfVars.METASTOREWAREHOUSE);
      return new AccessURI(PathUtils.parseURI(warehouseDir, uri, isLocal));
    } catch (Exception e) {
      throw new SemanticException("Error parsing URI " + uri + ": " +
        e.getMessage(), e);
    }
  }

  /**
   * Post analyze hook that invokes hive auth bindings
   */
  @Override
  public void postAnalyze(HiveSemanticAnalyzerHookContext context,
      List<Task<? extends Serializable>> rootTasks) throws SemanticException {
    HiveOperation stmtOperation = getCurrentHiveStmtOp();
    HiveAuthzPrivileges stmtAuthObject;

    stmtAuthObject = HiveAuthzPrivilegesMap.getHiveAuthzPrivileges(stmtOperation);

    // must occur above the null check on stmtAuthObject
    // since GRANT/REVOKE/etc are not authorized by binding layer at present
    Subject subject = getCurrentSubject(context);
    Set<String> subjectGroups = hiveAuthzBinding.getGroups(subject);
    for (Task<? extends Serializable> task : rootTasks) {
      if (task instanceof SentryGrantRevokeTask) {
        SentryGrantRevokeTask sentryTask = (SentryGrantRevokeTask)task;
        sentryTask.setHiveAuthzBinding(hiveAuthzBinding);
        sentryTask.setAuthzConf(authzConf);
        sentryTask.setSubject(subject);
        sentryTask.setSubjectGroups(subjectGroups);
        sentryTask.setIpAddress(context.getIpAddress());
        sentryTask.setOperation(stmtOperation);
      }
    }

    try {
      if (stmtAuthObject == null) {
        // We don't handle authorizing this statement
        return;
      }

      /**
       * Replace DDLTask using the SentryFilterDDLTask for protection,
       * such as "show column" only allow show some column that user can access to.
       * SENTRY-847
       */
      for (int i = 0; i < rootTasks.size(); i++) {
        Task<? extends Serializable> task = rootTasks.get(i);
        if (task instanceof DDLTask) {
          SentryFilterDDLTask filterTask =
              new SentryFilterDDLTask(hiveAuthzBinding, subject, stmtOperation);
          filterTask.setWork((DDLWork)task.getWork());
          rootTasks.set(i, filterTask);
        }
      }

      authorizeWithHiveBindings(context, stmtAuthObject, stmtOperation);
    } catch (AuthorizationException e) {
      executeOnFailureHooks(context, stmtOperation, e);
      String permsRequired = "";
      for (String perm : hiveAuthzBinding.getLastQueryPrivilegeErrors()) {
        permsRequired += perm + ";";
      }
      SessionState.get().getConf().set(HiveAuthzConf.HIVE_SENTRY_AUTH_ERRORS, permsRequired);
      String msgForLog = HiveAuthzConf.HIVE_SENTRY_PRIVILEGE_ERROR_MESSAGE
          + "\n Required privileges for this query: "
          + permsRequired;
      String msgForConsole = HiveAuthzConf.HIVE_SENTRY_PRIVILEGE_ERROR_MESSAGE + "\n "
          + e.getMessage();
      // AuthorizationException is not a real exception, use the info level to record this.
      LOG.info(msgForLog);
      throw new SemanticException(msgForConsole, e);
    } finally {
      hiveAuthzBinding.close();
    }

    if ("true".equalsIgnoreCase(context.getConf().
        get(HiveAuthzConf.HIVE_SENTRY_MOCK_COMPILATION))) {
      throw new SemanticException(HiveAuthzConf.HIVE_SENTRY_MOCK_ERROR + " Mock query compilation aborted. Set " +
          HiveAuthzConf.HIVE_SENTRY_MOCK_COMPILATION + " to 'false' for normal query processing");
    }
  }

  private void executeOnFailureHooks(HiveSemanticAnalyzerHookContext context,
      HiveOperation hiveOp, AuthorizationException e) {
    SentryOnFailureHookContext hookCtx = new SentryOnFailureHookContextImpl(
        context.getCommand(), context.getInputs(), context.getOutputs(),
        hiveOp, currDB, currTab, udfURI, null, context.getUserName(),
        context.getIpAddress(), e, context.getConf());
    String csHooks = authzConf.get(
        HiveAuthzConf.AuthzConfVars.AUTHZ_ONFAILURE_HOOKS.getVar(), "").trim();

    try {
      for (Hook aofh : getHooks(csHooks)) {
        ((SentryOnFailureHook)aofh).run(hookCtx);
      }
    } catch (Exception ex) {
      LOG.error("Error executing hook:", ex);
    }
  }

  public static void runFailureHook(SentryOnFailureHookContext hookContext,
      String csHooks) {
    try {
      for (Hook aofh : getHooks(csHooks)) {
        ((SentryOnFailureHook) aofh).run(hookContext);
      }
    } catch (Exception ex) {
      LOG.error("Error executing hook:", ex);
    }
  }
  /**
   * Convert the input/output entities into authorizables. generate
   * authorizables for cases like Database and metadata operations where the
   * compiler doesn't capture entities. invoke the hive binding to validate
   * permissions
   *
   * @param context
   * @param stmtAuthObject
   * @param stmtOperation
   * @throws AuthorizationException
   */
  private void authorizeWithHiveBindings(HiveSemanticAnalyzerHookContext context,
      HiveAuthzPrivileges stmtAuthObject, HiveOperation stmtOperation) throws  AuthorizationException {
    Set<ReadEntity> inputs = context.getInputs();
    Set<WriteEntity> outputs = context.getOutputs();
    List<List<DBModelAuthorizable>> inputHierarchy = new ArrayList<List<DBModelAuthorizable>>();
    List<List<DBModelAuthorizable>> outputHierarchy = new ArrayList<List<DBModelAuthorizable>>();

    if(LOG.isDebugEnabled()) {
      LOG.debug("stmtAuthObject.getOperationScope() = " + stmtAuthObject.getOperationScope());
      LOG.debug("context.getInputs() = " + context.getInputs());
      LOG.debug("context.getOutputs() = " + context.getOutputs());
    }

    // Workaround to allow DESCRIBE <table> to be executed with only column-level privileges, while
    // still authorizing DESCRIBE [EXTENDED|FORMATTED] as table-level.
    // This is done by treating DESCRIBE <table> the same as SHOW COLUMNS, which only requires column
    // level privs.
    if (isDescTableBasic) {
      stmtAuthObject = HiveAuthzPrivilegesMap.getHiveAuthzPrivileges(HiveOperation.SHOWCOLUMNS);
    }

    switch (stmtAuthObject.getOperationScope()) {

    case SERVER :
      // validate server level privileges if applicable. Eg create UDF,register jar etc ..
      List<DBModelAuthorizable> serverHierarchy = new ArrayList<DBModelAuthorizable>();
      serverHierarchy.add(hiveAuthzBinding.getAuthServer());
      inputHierarchy.add(serverHierarchy);
      break;
    case DATABASE:
      // workaround for database scope statements (create/alter/drop db)
      List<DBModelAuthorizable> dbHierarchy = new ArrayList<DBModelAuthorizable>();
      dbHierarchy.add(hiveAuthzBinding.getAuthServer());
      dbHierarchy.add(currDB);
      inputHierarchy.add(dbHierarchy);
      outputHierarchy.add(dbHierarchy);

      getInputHierarchyFromInputs(inputHierarchy, inputs);
      break;
    case TABLE:
      // workaround for add partitions
      if(partitionURI != null) {
        inputHierarchy.add(ImmutableList.of(hiveAuthzBinding.getAuthServer(), partitionURI));
      }

      getInputHierarchyFromInputs(inputHierarchy, inputs);
      for (WriteEntity writeEntity: outputs) {
        if (filterWriteEntity(writeEntity)) {
          continue;
        }
        List<DBModelAuthorizable> entityHierarchy = new ArrayList<DBModelAuthorizable>();
        entityHierarchy.add(hiveAuthzBinding.getAuthServer());
        entityHierarchy.addAll(getAuthzHierarchyFromEntity(writeEntity));
        outputHierarchy.add(entityHierarchy);
      }
      // workaround for metadata queries.
      // Capture the table name in pre-analyze and include that in the input entity list
      if (currTab != null) {
        List<DBModelAuthorizable> externalAuthorizableHierarchy = new ArrayList<DBModelAuthorizable>();
        externalAuthorizableHierarchy.add(hiveAuthzBinding.getAuthServer());
        externalAuthorizableHierarchy.add(currDB);
        externalAuthorizableHierarchy.add(currTab);
        inputHierarchy.add(externalAuthorizableHierarchy);
      }

      // workaround for DDL statements
      // Capture the table name in pre-analyze and include that in the output entity list
      if (currOutTab != null) {
        List<DBModelAuthorizable> externalAuthorizableHierarchy = new ArrayList<DBModelAuthorizable>();
        externalAuthorizableHierarchy.add(hiveAuthzBinding.getAuthServer());
        externalAuthorizableHierarchy.add(currOutDB);
        externalAuthorizableHierarchy.add(currOutTab);
        outputHierarchy.add(externalAuthorizableHierarchy);
      }
      break;
    case FUNCTION:
      /* The 'FUNCTION' privilege scope currently used for
       *  - CREATE TEMP FUNCTION
       *  - DROP TEMP FUNCTION.
       */
      if (udfURI != null) {
        List<DBModelAuthorizable> udfUriHierarchy = new ArrayList<DBModelAuthorizable>();
        udfUriHierarchy.add(hiveAuthzBinding.getAuthServer());
        udfUriHierarchy.add(udfURI);
        inputHierarchy.add(udfUriHierarchy);
        for (WriteEntity writeEntity : outputs) {
          List<DBModelAuthorizable> entityHierarchy = new ArrayList<DBModelAuthorizable>();
          entityHierarchy.add(hiveAuthzBinding.getAuthServer());
          entityHierarchy.addAll(getAuthzHierarchyFromEntity(writeEntity));
          outputHierarchy.add(entityHierarchy);
        }
      }
      break;
    case CONNECT:
      /* The 'CONNECT' is an implicit privilege scope currently used for
       *  - USE <db>
       *  It's allowed when the user has any privilege on the current database. For application
       *  backward compatibility, we allow (optional) implicit connect permission on 'default' db.
       */
      List<DBModelAuthorizable> connectHierarchy = new ArrayList<DBModelAuthorizable>();
      connectHierarchy.add(hiveAuthzBinding.getAuthServer());
      // by default allow connect access to default db
      Table currTbl = Table.ALL;
      Column currCol = Column.ALL;
      if ((DEFAULT_DATABASE_NAME.equalsIgnoreCase(currDB.getName()) &&
          "false".equalsIgnoreCase(authzConf.
              get(HiveAuthzConf.AuthzConfVars.AUTHZ_RESTRICT_DEFAULT_DB.getVar(), "false")))) {
        currDB = Database.ALL;
        currTbl = Table.SOME;
      }

      connectHierarchy.add(currDB);
      connectHierarchy.add(currTbl);
      connectHierarchy.add(currCol);

      inputHierarchy.add(connectHierarchy);
      outputHierarchy.add(connectHierarchy);
      break;
    case COLUMN:
      for (ReadEntity readEntity: inputs) {
        if (readEntity.getAccessedColumns() != null && !readEntity.getAccessedColumns().isEmpty()) {
          addColumnHierarchy(inputHierarchy, readEntity);
        } else {
          List<DBModelAuthorizable> entityHierarchy = new ArrayList<DBModelAuthorizable>();
          entityHierarchy.add(hiveAuthzBinding.getAuthServer());
          entityHierarchy.addAll(getAuthzHierarchyFromEntity(readEntity));
          entityHierarchy.add(Column.ALL);
          inputHierarchy.add(entityHierarchy);
        }
      }
      break;
    default:
      throw new AuthorizationException("Unknown operation scope type " +
          stmtAuthObject.getOperationScope().toString());
    }

    // validate permission
    hiveAuthzBinding.authorize(stmtOperation, stmtAuthObject, getCurrentSubject(context),
        inputHierarchy, outputHierarchy);
  }

  private HiveOperation getCurrentHiveStmtOp() {
    SessionState sessState = SessionState.get();
    if (sessState == null) {
      // TODO: Warn
      return null;
    }
    return sessState.getHiveOperation();
  }

  private Subject getCurrentSubject(HiveSemanticAnalyzerHookContext context) {
    // Extract the username from the hook context
    return new Subject(context.getUserName());
  }

  // Build the hierarchy of authorizable object for the given entity type.
  private List<DBModelAuthorizable> getAuthzHierarchyFromEntity(Entity entity) {
    List<DBModelAuthorizable> objectHierarchy = new ArrayList<DBModelAuthorizable>();
    switch (entity.getType()) {
    case TABLE:
      objectHierarchy.add(new Database(entity.getTable().getDbName()));
      objectHierarchy.add(new Table(entity.getTable().getTableName()));
      break;
    case PARTITION:
    case DUMMYPARTITION:
      objectHierarchy.add(new Database(entity.getPartition().getTable().getDbName()));
      objectHierarchy.add(new Table(entity.getPartition().getTable().getTableName()));
      break;
    case DFS_DIR:
    case LOCAL_DIR:
      try {
        objectHierarchy.add(parseURI(entity.toString(),
            entity.getType().equals(Entity.Type.LOCAL_DIR)));
      } catch (Exception e) {
        throw new AuthorizationException("Failed to get File URI", e);
      }
      break;
    case DATABASE:
    case FUNCTION:
      // TODO use database entities from compiler instead of capturing from AST
      break;
    default:
      throw new UnsupportedOperationException("Unsupported entity type " +
          entity.getType().name());
    }
    return objectHierarchy;
  }

  /**
   * Add column level hierarchy to inputHierarchy
   *
   * @param inputHierarchy
   * @param entity
   * @param sentryContext
   */
  private void addColumnHierarchy(List<List<DBModelAuthorizable>> inputHierarchy,
      ReadEntity entity) {
    List<DBModelAuthorizable> entityHierarchy = new ArrayList<DBModelAuthorizable>();
    entityHierarchy.add(hiveAuthzBinding.getAuthServer());
    entityHierarchy.addAll(getAuthzHierarchyFromEntity(entity));

    switch (entity.getType()) {
    case TABLE:
    case PARTITION:
      List<String> cols = entity.getAccessedColumns();
      for (String col : cols) {
        List<DBModelAuthorizable> colHierarchy = new ArrayList<DBModelAuthorizable>(entityHierarchy);
        colHierarchy.add(new Column(col));
        inputHierarchy.add(colHierarchy);
      }
      break;
    default:
      inputHierarchy.add(entityHierarchy);
    }
  }

  /**
   * Get Authorizable from inputs and put into inputHierarchy
   *
   * @param inputHierarchy
   * @param entity
   * @param sentryContext
   */
  private void getInputHierarchyFromInputs(List<List<DBModelAuthorizable>> inputHierarchy,
      Set<ReadEntity> inputs) {
    for (ReadEntity readEntity: inputs) {
      // skip the tables/view that are part of expanded view definition
      // skip the Hive generated dummy entities created for queries like 'select <expr>'
      if (isChildTabForView(readEntity) || isDummyEntity(readEntity)) {
        continue;
      }
      if (readEntity.getAccessedColumns() != null && !readEntity.getAccessedColumns().isEmpty()) {
        addColumnHierarchy(inputHierarchy, readEntity);
      } else {
        List<DBModelAuthorizable> entityHierarchy = new ArrayList<DBModelAuthorizable>();
        entityHierarchy.add(hiveAuthzBinding.getAuthServer());
        entityHierarchy.addAll(getAuthzHierarchyFromEntity(readEntity));
        inputHierarchy.add(entityHierarchy);
      }
    }
  }

  // Check if this write entity needs to skipped
  private boolean filterWriteEntity(WriteEntity writeEntity)
      throws AuthorizationException {
    // skip URI validation for session scratch file URIs
    if (writeEntity.isTempURI()) {
      return true;
    }
    try {
      if (writeEntity.getTyp().equals(Type.DFS_DIR)
          || writeEntity.getTyp().equals(Type.LOCAL_DIR)) {
        HiveConf conf = SessionState.get().getConf();
        String warehouseDir = conf.getVar(ConfVars.METASTOREWAREHOUSE);
        URI scratchURI = new URI(PathUtils.parseDFSURI(warehouseDir,
          conf.getVar(HiveConf.ConfVars.SCRATCHDIR)));
        URI requestURI = new URI(PathUtils.parseDFSURI(warehouseDir,
          writeEntity.getLocation().getPath()));
        LOG.debug("scratchURI = " + scratchURI + ", requestURI = " + requestURI);
        if (PathUtils.impliesURI(scratchURI, requestURI)) {
          return true;
        }
        URI localScratchURI = new URI(PathUtils.parseLocalURI(conf.getVar(HiveConf.ConfVars.LOCALSCRATCHDIR)));
        URI localRequestURI = new URI(PathUtils.parseLocalURI(writeEntity.getLocation().getPath()));
        LOG.debug("localScratchURI = " + localScratchURI + ", localRequestURI = " + localRequestURI);
        if (PathUtils.impliesURI(localScratchURI, localRequestURI)) {
          return true;
        }
      }
    } catch (Exception e) {
      throw new AuthorizationException("Failed to extract uri details", e);
    }
    return false;
  }

  public static List<String> filterShowTables(
      HiveAuthzBinding hiveAuthzBinding, List<String> queryResult,
      HiveOperation operation, String userName, String dbName)
          throws SemanticException {
    List<String> filteredResult = new ArrayList<String>();
    Subject subject = new Subject(userName);
    HiveAuthzPrivileges tableMetaDataPrivilege = new HiveAuthzPrivileges.AuthzPrivilegeBuilder().
        addInputObjectPriviledge(AuthorizableType.Column, EnumSet.of(DBModelAction.SELECT, DBModelAction.INSERT)).
        setOperationScope(HiveOperationScope.TABLE).
        setOperationType(HiveOperationType.INFO).
        build();

    HiveAuthzBinding hiveBindingWithPrivilegeCache = getHiveBindingWithPrivilegeCache(hiveAuthzBinding, userName);

    for (String tableName : queryResult) {
      // if user has privileges on table, add to filtered list, else discard
      Table table = new Table(tableName);
      Database database;
      database = new Database(dbName);

      List<List<DBModelAuthorizable>> inputHierarchy = new ArrayList<List<DBModelAuthorizable>>();
      List<List<DBModelAuthorizable>> outputHierarchy = new ArrayList<List<DBModelAuthorizable>>();
      List<DBModelAuthorizable> externalAuthorizableHierarchy = new ArrayList<DBModelAuthorizable>();
      externalAuthorizableHierarchy.add(hiveAuthzBinding.getAuthServer());
      externalAuthorizableHierarchy.add(database);
      externalAuthorizableHierarchy.add(table);
      externalAuthorizableHierarchy.add(Column.ALL);
      inputHierarchy.add(externalAuthorizableHierarchy);

      try {
        // do the authorization by new HiveAuthzBinding with PrivilegeCache
        hiveBindingWithPrivilegeCache.authorize(operation, tableMetaDataPrivilege, subject,
            inputHierarchy, outputHierarchy);
        filteredResult.add(table.getName());
      } catch (AuthorizationException e) {
        // squash the exception, user doesn't have privileges, so the table is
        // not added to
        // filtered list.
        ;
      }
    }
    return filteredResult;
  }

  public static List<FieldSchema> filterShowColumns(
      HiveAuthzBinding hiveAuthzBinding, List<FieldSchema> cols,
      HiveOperation operation, String userName, String tableName, String dbName)
          throws SemanticException {
    List<FieldSchema> filteredResult = new ArrayList<FieldSchema>();
    Subject subject = new Subject(userName);
    HiveAuthzPrivileges columnMetaDataPrivilege =
        HiveAuthzPrivilegesMap.getHiveAuthzPrivileges(HiveOperation.SHOWCOLUMNS);
    HiveAuthzBinding hiveBindingWithPrivilegeCache = getHiveBindingWithPrivilegeCache(hiveAuthzBinding, userName);

    Database database = new Database(dbName);
    Table table = new Table(tableName);
    for (FieldSchema col : cols) {
      // if user has privileges on column, add to filtered list, else discard
      List<List<DBModelAuthorizable>> inputHierarchy = new ArrayList<List<DBModelAuthorizable>>();
      List<List<DBModelAuthorizable>> outputHierarchy = new ArrayList<List<DBModelAuthorizable>>();
      List<DBModelAuthorizable> externalAuthorizableHierarchy = new ArrayList<DBModelAuthorizable>();
      externalAuthorizableHierarchy.add(hiveAuthzBinding.getAuthServer());
      externalAuthorizableHierarchy.add(database);
      externalAuthorizableHierarchy.add(table);
      externalAuthorizableHierarchy.add(new Column(col.getName()));
      inputHierarchy.add(externalAuthorizableHierarchy);

      try {
        // do the authorization by new HiveAuthzBinding with PrivilegeCache
        hiveBindingWithPrivilegeCache.authorize(operation, columnMetaDataPrivilege, subject,
            inputHierarchy, outputHierarchy);
        filteredResult.add(col);
      } catch (AuthorizationException e) {
        // squash the exception, user doesn't have privileges, so the column is
        // not added to
        // filtered list.
        ;
      }
    }
    return filteredResult;
  }

  public static List<String> filterShowDatabases(
      HiveAuthzBinding hiveAuthzBinding, List<String> queryResult,
      HiveOperation operation, String userName) throws SemanticException {
    List<String> filteredResult = new ArrayList<String>();
    Subject subject = new Subject(userName);
    HiveAuthzBinding hiveBindingWithPrivilegeCache = getHiveBindingWithPrivilegeCache(hiveAuthzBinding, userName);

    HiveAuthzPrivileges anyPrivilege = new HiveAuthzPrivileges.AuthzPrivilegeBuilder().
        addInputObjectPriviledge(AuthorizableType.Column, EnumSet.of(DBModelAction.SELECT, DBModelAction.INSERT)).
        addInputObjectPriviledge(AuthorizableType.URI, EnumSet.of(DBModelAction.SELECT)).
        setOperationScope(HiveOperationScope.CONNECT).
        setOperationType(HiveOperationType.QUERY).
        build();

    for (String dbName:queryResult) {
      // if user has privileges on database, add to filtered list, else discard
      Database database = null;

      // if default is not restricted, continue
      if (DEFAULT_DATABASE_NAME.equalsIgnoreCase(dbName) && "false".equalsIgnoreCase(
        hiveAuthzBinding.getAuthzConf().get(
              HiveAuthzConf.AuthzConfVars.AUTHZ_RESTRICT_DEFAULT_DB.getVar(),
              "false"))) {
        filteredResult.add(DEFAULT_DATABASE_NAME);
        continue;
      }

      database = new Database(dbName);

      List<List<DBModelAuthorizable>> inputHierarchy = new ArrayList<List<DBModelAuthorizable>>();
      List<List<DBModelAuthorizable>> outputHierarchy = new ArrayList<List<DBModelAuthorizable>>();
      List<DBModelAuthorizable> externalAuthorizableHierarchy = new ArrayList<DBModelAuthorizable>();
      externalAuthorizableHierarchy.add(hiveAuthzBinding.getAuthServer());
      externalAuthorizableHierarchy.add(database);
      externalAuthorizableHierarchy.add(Table.ALL);
      externalAuthorizableHierarchy.add(Column.ALL);
      inputHierarchy.add(externalAuthorizableHierarchy);

      try {
        // do the authorization by new HiveAuthzBinding with PrivilegeCache
        hiveBindingWithPrivilegeCache.authorize(operation, anyPrivilege, subject,
            inputHierarchy, outputHierarchy);
        filteredResult.add(database.getName());
      } catch (AuthorizationException e) {
        // squash the exception, user doesn't have privileges, so the table is
        // not added to
        // filtered list.
        ;
      }
    }

    return filteredResult;
  }

  /**
   * Check if the given read entity is a table that has parents of type Table
   * Hive compiler performs a query rewrite by replacing view with its definition. In the process, tt captures both
   * the original view and the tables/view that it selects from .
   * The access authorization is only interested in the top level views and not the underlying tables.
   * @param readEntity
   * @return
   */
  private boolean isChildTabForView(ReadEntity readEntity) {
    // If this is a table added for view, then we need to skip that
    if (!readEntity.getType().equals(Type.TABLE) && !readEntity.getType().equals(Type.PARTITION)) {
      return false;
    }
    if ((readEntity.getParents() != null) && (readEntity.getParents().size() > 0)) {
      for (ReadEntity parentEntity : readEntity.getParents()) {
        if (!parentEntity.getType().equals(Type.TABLE)) {
          return false;
        }
      }
      return true;
    } else {
      return false;
    }
  }

  /**
   * Returns a set of hooks specified in a configuration variable.
   *
   * See getHooks(HiveAuthzConf.AuthzConfVars hookConfVar, Class<T> clazz)
   * @param hookConfVar
   * @return
   * @throws Exception
   */
  private static List<Hook> getHooks(String csHooks) throws Exception {
    return getHooks(csHooks, Hook.class);
  }

  /**
   * Returns the hooks specified in a configuration variable.  The hooks are returned in a list in
   * the order they were specified in the configuration variable.
   *
   * @param hookConfVar The configuration variable specifying a comma separated list of the hook
   *                    class names.
   * @param clazz       The super type of the hooks.
   * @return            A list of the hooks cast as the type specified in clazz, in the order
   *                    they are listed in the value of hookConfVar
   * @throws Exception
   */
  private static <T extends Hook> List<T> getHooks(String csHooks,
      Class<T> clazz)
      throws Exception {

    List<T> hooks = new ArrayList<T>();
    if (csHooks.isEmpty()) {
      return hooks;
    }
    for (String hookClass : Splitter.on(",").omitEmptyStrings().trimResults().split(csHooks)) {
      try {
        @SuppressWarnings("unchecked")
        T hook =
            (T) Class.forName(hookClass, true, JavaUtils.getClassLoader()).newInstance();
        hooks.add(hook);
      } catch (ClassNotFoundException e) {
        LOG.error(hookClass + " Class not found:" + e.getMessage());
        throw e;
      }
    }

    return hooks;
  }

  // Check if the given entity is identified as dummy by Hive compilers.
  private boolean isDummyEntity(Entity entity) {
    return entity.isDummy();
  }

  // create hiveBinding with PrivilegeCache
  private static HiveAuthzBinding getHiveBindingWithPrivilegeCache(HiveAuthzBinding hiveAuthzBinding,
      String userName) throws SemanticException {
    // get the original HiveAuthzBinding, and get the user's privileges by AuthorizationProvider
    AuthorizationProvider authProvider = hiveAuthzBinding.getCurrentAuthProvider();
    Set<String> userPrivileges = authProvider.getPolicyEngine().getPrivileges(
            authProvider.getGroupMapping().getGroups(userName), hiveAuthzBinding.getActiveRoleSet(),
            hiveAuthzBinding.getAuthServer());

    // create PrivilegeCache using user's privileges
    PrivilegeCache privilegeCache = new SimplePrivilegeCache(userPrivileges);
    try {
      // create new instance of HiveAuthzBinding whose backend provider should be SimpleCacheProviderBackend
      return new HiveAuthzBinding(HiveAuthzBinding.HiveHook.HiveServer2, hiveAuthzBinding.getHiveConf(),
              hiveAuthzBinding.getAuthzConf(), privilegeCache);
    } catch (Exception e) {
      LOG.error("Can not create HiveAuthzBinding with privilege cache.");
      throw new SemanticException(e);
    }
  }
}


text:
package example.springdata.jpa.showcase.before;

import static org.assertj.core.api.Assertions.*;

import example.springdata.jpa.showcase.AbstractShowcaseTest;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

/**
 * Integration test for {@link CustomerService}.
 *
 * @author Oliver Gierke
 * @author Divya Srivastava
 */
class CustomerServiceIntegrationTest extends AbstractShowcaseTest {

	@Autowired CustomerService repository;

	@Test
	void findsAllCustomers() throws Exception {

		var result = repository.findAll();

		assertThat(result).isNotNull();
		assertThat(result).isNotEmpty();
	}

	@Test
	void findsPageOfMatthews() throws Exception {

		var customers = repository.findByLastname("Matthews", 0, 2);

		assertThat(customers).hasSize(2);
	}

	@Test
	void findsCustomerById() throws Exception {

		var customer = repository.findById(2L);

		assertThat(customer.getFirstname()).isEqualTo("Carter");
		assertThat(customer.getLastname()).isEqualTo("Beauford");
	}
}

redpajama_set_name: RedPajamaGithub


text:
<!DOCTYPE html>

  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width initial-scale=1" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">

  <title>Iain Whiteside | Four Categories</title>
  <meta name="description" content="Iain Whiteside's Professional and Personal Website
">

  <link rel="shortcut icon" href="/assets/img/favicon.ico">

  <link rel="stylesheet" href="/assets/css/main.css">
  <link rel="canonical" href="/blog/2017/four-categories/">

<title>Four Categories | Iain Whiteside's Website</title>
<meta property="og:title" content="Four Categories" />
<meta name="author" content="Iain Whiteside" />
<meta property="og:locale" content="en_US" />
<meta name="description" content="I believe that all of humankind can be neatly categorized across two simple dimensions" />
<meta property="og:description" content="I believe that all of humankind can be neatly categorized across two simple dimensions" />
<meta property="og:site_name" content="Iain Whiteside's Website" />
<meta property="og:image" content="/assets/img/sock.jpg" />
<meta property="og:type" content="article" />
<meta property="article:published_time" content="2017-09-11T16:01:00-05:00" />

        Iain Whiteside

        home

        about me

        blog

        projects

        publications

Four Categories

September 11, 2017

Are you one of those people who always seems to be losing things? Yeah, me too.

Here's a brief list, from memory, of the (known) victims in the past six months:

My bear hat knitted for me by Angela Mudge

My nice reverable jacket

My bike helmet

Apartment keys

My colorado buff

My special pen

My friend's tent bag

My replacement apartment keys

… you get the point.

I've always believed that there are two types of people in the world:

Those that lose things regularly;

Those that just never seem to lose anything. Goddamnit! How do you do that?!

After spending a lot of time with people in San Francisco, I realized that my categorization could be more nuanced. There is an additional dimension to my categorization: the what-have-i-lost worry factor.

Some people seem spend an inordinate amount of time worrying that they are going to lose something. This is a closely related group of people to the faffers (see bio).

I am convinced that these two categories neatly characterize humankind. To illustrate, I have made this handy graph.

The two dimensions combine to form four categories that can best be understood with a real-world example: checking of a hotel room.

Category 1: The Iain never checks under the bed, usually there'll be something there. No doubt my nice reversable jacket is under one somewhere.

Category 2: The Justified Pedant will always check under the bed, twice. Often they'll find something of theirs there. And maybe my hat. Of course, they will have missed the sock tucked behind the door.

Category 3: Dr Reliable will always check under the bed. There's never anything under it. But they don't let that stop them.

Category 3: The Unicorn is the mythic person who never checks under the bed and there is never anything under it. Scientists are still searching for one of these creatures in the wild.

Which one are you?

<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.css">

<link rel="stylesheet" href="/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="/assets/css/academicons.min.css">


text:
<?php

namespace Cetera\Widget;

/**
 * Виджет "Контейнер"
 *
 * Предназначен для хранения в себе других виджетов в заданном порядке
 *
 * @package FastsiteCMS
 */
class Container extends Templateable {

	/**
	 * Виджеты, хранящиеся в контейнере
	 */
    protected $widgets = [];

	public static $name = 'Container';

    protected $_params = array(
		'template'    => 'default.twig',
    );

	public function enum()
	{
		$res = array();
		$r = self::getDbConnection()->executeQuery( 'SELECT * FROM widgets WHERE widgetName=?', array("Container") );
		while($f = $r->fetch()) {
			$res[] = new self($f['id'], $f);
		}
		return $res;
	}

	/**
	 * Конструктор
	 *
	 * @param int $id ID виджета
	 */
    public function __construct($id = 0, $params = null)
    {
		if (isset($params['alias'])) {
            $f = self::getDbConnection()->fetchAssoc('SELECT * FROM widgets WHERE widgetAlias=? ORDER BY id DESC', array( $params['alias'] ));
            if ($f) {
                $id = $f['id'];
				$params = array_merge(unserialize($f['params']), $params);
            }
		}

        parent::__construct($id, $params);

        if ($this->getId()) {
			$r = self::getDbConnection()->fetchAll('SELECT widget_id FROM widgets_containers WHERE container_id=? ORDER BY position', array( $this->getId() ));
            foreach ($r as $f) try {
				$this->widgets[] = $this->application->getWidget( (int)$f['widget_id'] );
            } catch (\Exception $e) {}
        }

		if ($params['data']) {
			$data = json_decode($params['data'], true);
			foreach ($data as $params) try {
				$this->widgets[] = $this->application->getWidget( $params );
			} catch (\Exception $e) {}
		}
    }

	/**
	 * Устанавливает в контейнер виджеты
	 *
	 * @param array $widgets массив виджетов
	 */
    protected function setWidgets($widgets) {
		$this->widgets = [];
		foreach ($widgets as $w)  {
			if (is_subclass_of($w, 'Cetera\Widget\Widget')) $this->widgets[] = $w;
			elseif ($w) $this->widgets[] = $this->application->getWidget( (int)$w );
		}
    }

	/**
	 * Добавляет в контейнер виджет
	 *
	 * @param int $id ID добавляемого виджета
	 */
    public function addWidget($id)
    {
        if (!(int)$id) return $this;
        $this->widgets[] = $this->application->getWidget( (int)$id );
        return $this;
    }

	/**
	 * Удаляет виджет из контейнера
	 *
	 * @param int $id ID удаляемого виджета
	 */
    public function removeWidget($id)
    {
        if (!(int)$id) return $this;

		foreach($this->widgets as $key => $w) {
			if ($w->getId() == $id) {
				unset($this->widgets[$key]);
				return $this;
			}
		}
        return $this;
    }

	/**
	 * Сохраняет виджет в БД
	 */
    public function save()
    {
		$widgets = $this->getParam('widgets');
		if (is_array($widgets)) $this->widgets = $widgets;
		unset( $this->_params['widgets'] );

        parent::save();

        $this->getDbConnection()->delete('widgets_containers', array( 'container_id' => $this->getId() ));
        foreach($this->widgets as $i => $w)
            $this->getDbConnection()->insert('widgets_containers', array(
                'container_id' => $this->getId(),
                'widget_id'    => $w->getId(),
                'position'     => $i
            ));
    }

	/**
	 * Возвращает содержимое контейнера
	 */
    public function getChildren()
    {
        return $this->widgets;
    }

	/**
	 * Экспорт виджета в XML
	 */
	public function getXml()
    {
		$res  = '<widgetcontainer widgetAlias="'.htmlspecialchars($this->widgetAlias).'" widgetTitle="'.htmlspecialchars($this->widgetTitle).'" widgetDisabled="'.(int)$this->widgetDisabled.'" widgetProtected="'.(int)$this->widgetProtected.'">'."\n\n";
		foreach ($this->getChildren() as $w) {
			$res .= $w->getXml()."\n";
		}
		$res .= '</widgetcontainer>'."\n";
		return $res;
	}

}


text:
export default {
  standings: [],
  results: []
};


text:
package co.edu.uniquindio.ingesis.suturno.web.bean;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import co.edu.uniquindio.ingesis.suturno.entidades.Ciudad;
import co.edu.uniquindio.ingesis.suturno.entidades.Depto;
import co.edu.uniquindio.ingesis.suturno.entidades.Persona;
import co.edu.uniquindio.ingesis.suturno.entidades.Servicio;
import co.edu.uniquindio.ingesis.suturno.entidades.Turno;
import co.edu.uniquindio.ingesis.suturno.negocio.ClienteEJB;
import co.edu.uniquindio.ingesis.suturno.negocio.GeografiaEJB;
import co.edu.uniquindio.ingesis.suturno.negocio.TurnoEJB;
import co.edu.uniquindio.ingesis.suturno.utils.Genero;
import co.edu.uniquindio.ingesis.suturno.utils.TipoDocumento;

/**
 * Bean de la persona de la aplicacion suturno
 *
 * @author Gustavo Salgado y Laura Julieth Rua
 * @author Ingenieria de Sistemas y Computacion
 * @author Universidad del Quindio
 * @since 06/05/2017
 * @version 1.0
 *
 */
@ManagedBean
@SessionScoped
public class PersonaBean implements Serializable {
	/**
	 * Variable que representa el atributo identificacion de la persona
	 */
	private String identificacion;
	/**
	 * Variable que representa el atributo tipo de documento de la persona
	 */
	private TipoDocumento tDoc;
	/**
	 * Variable que representa el atributo genero de la persona
	 */
	private Genero genero;
	/**
	 * Variable que representa el primer nombre de la persona
	 */
	private String nombre1;
	/**
	 * Variable que representa el atributo del segundo nombre de la persona
	 */
	private String nombre2;
	/**
	 * Varible que representa el atributo del primer apellido de la persona
	 */
	private String apellido1;
	/**
	 * Variable que representa el atributo del segundo apellido de la persona
	 */
	private String apellido2;
	/**
	 * Variable que representa el atributo email de la Persona
	 */
	private String email;
	/**
	 * Variable que representa el atributo del primer telefono de la persona
	 */
	private String tel1;
	/**
	 * Variable que representa el atributo del segundo telefono de la persona
	 */
	private String tel2;

	/**
	 * Variable que represent el atributo de la fecha de nacimiento de la
	 * persona
	 */
	private Date fecha;

	/**
	 * Variable que representa el atributo de la direccion de la persona
	 */
	private String dir;
	/**
	 * Variable que representa el atributo departamento de la persona
	 */
	private Depto dpto;
	/**
	 * Variable que representa el atributo ciudad de la persona
	 */
	private Ciudad ciudad;
	/**
	 * Variable que representa el atributo activo de la persona
	 */
	private boolean activo;

	private Persona cliente;

	private Servicio servicio;

	/**
	 * Permite identificar si el usuario ya tiene un servicio y mostrarle
	 * informacion al respecto
	 */
	private Turno ultimoTurno;

	/**
	 * Puente para acceder a funcionalidades persistencia de Cliente
	 */
	@EJB
	private ClienteEJB clienteEJB;
	/**
	 * Puente para acceder a funcionalidades persistencia de geografia
	 */
	@EJB
	private GeografiaEJB geografiaEJB;

	/**
	 * Puente para acceder a funcionalidades persistencia de Turno
	 */
	@EJB
	private TurnoEJB turnoEJB;

	/**
	 * Lista de Departamentos disponibles
	 */
	private List<Depto> departamentos;
	/**
	 * Lista de Ciudades en funcion del departametno seleccionado
	 */
	private List<Ciudad> ciudades;
	/**
	 * Listado de Turnos
	 */
	private List<Turno> turnos;

	@PostConstruct
	public void inicializar() {
		departamentos = geografiaEJB.listarDepartamentos();
		// turnos = turnoEJB.turnosDeCliente(cliente);
	}

	/**
	 * Metodo constructor del bean de persona
	 */
	public PersonaBean() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * Permite buscar el cliente por la identificacion
	 *
	 * @return nulo sin no encuentra el cliente o la direccion del solicitar
	 *         turno si el cliente tiene turnos en espera o la direccion del
	 *         registrar cliente si el cliente no existe
	 */
	public String buscarCliente() {
		String retorno = null;
		FacesMessage mensaje;
		try {
			cliente = clienteEJB.buscarClientePorIdentificacion(identificacion);
			if (cliente != null) {
				mensaje = new FacesMessage("Se encontro cliente");

				// buscar turnos existentes
				ultimoTurno = turnoEJB.turnosPendientesCliente(cliente);

				retorno = "/cliente/solicitarTurno";
			} else {
				mensaje = new FacesMessage("No encontro cliente");
				retorno = "/cliente/registrar";
			}
		} catch (Throwable e) {
			mensaje = new FacesMessage(e.getCause().getMessage());
		}
		FacesContext.getCurrentInstance().addMessage(null, mensaje);
		return retorno;
	}

	/**
	 * Permite registrar el cliente si este no existe
	 *
	 * @return nulo si no es posible registrarlo o la direccion del solicitar
	 *         turno
	 */
	public String registrarCliente() {
		String retorno = null;
		FacesMessage mensaje;
		cliente = new Persona();
		cliente.setIdentificacion(identificacion);
		cliente.setNombre1(nombre1);
		cliente.setNombre2(nombre2);
		cliente.setApellido1(apellido1);
		cliente.setApellido2(apellido2);
		cliente.setTel1(tel1);
		cliente.setTel2(tel2);
		cliente.setCiudad(ciudad);
		cliente.setDir(dir);
		cliente.setEmail(email);
		cliente.setGenero(genero);
		cliente.settDoc(tDoc);

		try {
			clienteEJB.registrarCliente(cliente);
			mensaje = new FacesMessage("Se registro cliente");
			System.out.println("Se registro");
			retorno = "/cliente/solicitarTurno";

		} catch (Throwable e) {
			e.printStackTrace();
			mensaje = new FacesMessage(e.getCause().getMessage());
		}
		FacesContext.getCurrentInstance().addMessage(null, mensaje);
		return retorno;
	}

	/**
	 * Permite solicitar el turno al cliente
	 *
	 * @return nulo si no es posible solicitar el turno y la direccion del index
	 *         del cliente
	 */
	public String solicitarTurno() {
		Turno turno = new Turno();
		turno.setCliente(cliente);
		turno.setServicio(servicio);
		String retorno = null;
		FacesMessage mensaje;
		try {
			clienteEJB.solicitarTurno(turno);
			mensaje = new FacesMessage("Se registro turno");
			retorno = "/cliente/index";
		} catch (Throwable e) {
			mensaje = new FacesMessage(e.getCause().getMessage());
		}
		FacesContext.getCurrentInstance().addMessage(null, mensaje);
		return retorno;
	}

	/**
	 * public String verTurnoCliente() { List<Turno> turno = turnos;
	 * FacesMessage mensaje; String retorno = null; if (turnos == null) {
	 * mensaje = new FacesMessage("No posee ningún turno."); } else { retorno =
	 * "/cliente/verTurno"; } return retorno;
	 *
	 * }
	 */

	/**
	 * Metodo get del atributo identificacion
	 *
	 * @return identificacion la identificacion de la persona
	 */
	public String getIdentificacion() {
		return identificacion;
	}

	/**
	 * Metodo set del atributo identificacion
	 *
	 * @param identificacion
	 *            la identificacion de la persona
	 */
	public void setIdentificacion(String identificacion) {
		this.identificacion = identificacion;
	}

	/**
	 * Metodo get del array de tipos de documentos
	 *
	 * @return array con los tipos de documentos
	 */
	public TipoDocumento[] gettdocs() {
		return TipoDocumento.values();
	}

	/**
	 * Metodo get del atributo tipo de documento
	 *
	 * @return tDoc la enumeracion tipo de documento de la persona
	 */
	public TipoDocumento gettDoc() {
		return tDoc;
	}

	/**
	 * Metodo set del atributo del tipo de documento
	 *
	 * @param tDoc
	 *            la enumeracion del tipo de cliente de la persona
	 */
	public void settDoc(TipoDocumento tDoc) {
		this.tDoc = tDoc;
	}

	/**
	 * Metodo get del atributo genero
	 *
	 * @return genero la enumeracion genero de la persona
	 */
	public Genero getGenero() {
		return genero;
	}

	/**
	 * Metodo get del array de tipos de generos
	 *
	 * @return array con los tipos de documentos
	 */
	public Genero[] getGeneros() {
		return Genero.values();
	}

	/**
	 * Metodo set del atributo genero
	 *
	 * @param genero
	 *            la enumeracion genero de la persona
	 */
	public void setGenero(Genero genero) {
		this.genero = genero;
	}

	/**
	 * Metodo get del atributo primer nombre
	 *
	 * @return nombre1 el primer nombre de la persona
	 */
	public String getNombre1() {
		return nombre1;
	}

	/**
	 * Metodo set del atributo primer nombre
	 *
	 * @param nombre1
	 *            el primer nombre de la persona
	 */
	public void setNombre1(String nombre1) {
		this.nombre1 = nombre1;
	}

	/**
	 * Metodo get del atributo segundo nombre
	 *
	 * @return nombre2 el segundo nombre de la persona
	 */
	public String getNombre2() {
		return nombre2;
	}

	/**
	 * Metodo set del atributo segundo nombre
	 *
	 * @param nombre2
	 *            el segundo nombre de la persona
	 */
	public void setNombre2(String nombre2) {
		this.nombre2 = nombre2;
	}

	/**
	 * Metodo get del atributo del primer apellido
	 *
	 * @return apellido1 el primer apellido de la persona
	 */
	public String getApellido1() {
		return apellido1;
	}

	/**
	 * Metodo set del atributo del primer apellido
	 *
	 * @param apellido1
	 *            el primer apellido de la persona
	 */
	public void setApellido1(String apellido1) {
		this.apellido1 = apellido1;
	}

	/**
	 * Metodo get del atributo segundo apellido
	 *
	 * @return apellido2 el segundo apellido de la persona
	 */
	public String getApellido2() {
		return apellido2;
	}

	/**
	 * Metodo set del atributo segundo apellido
	 *
	 * @param apellido2
	 *            el segundo apellido de la persona
	 */
	public void setApellido2(String apellido2) {
		this.apellido2 = apellido2;
	}

	/**
	 * Metodo get del atributo primer telefono
	 *
	 * @return tel1 el primer telefono de la persona
	 */
	public String getTel1() {
		return tel1;
	}

	/**
	 * Metodo set del atributo primer telefono
	 *
	 * @param tel1
	 *            el primer telefono de la persona
	 */
	public void setTel1(String tel1) {
		this.tel1 = tel1;
	}

	/**
	 * Metodo get del atributo segundo telefono
	 *
	 * @return tel2 el segundo telefono de la persona
	 */
	public String getTel2() {
		return tel2;
	}

	/**
	 * Metodo set del atributo segundo telefono
	 *
	 * @param tel2
	 *            el segundo telefono de la persona
	 */
	public void setTel2(String tel2) {
		this.tel2 = tel2;
	}

	/**
	 * Metodo get del atributo direccion
	 *
	 * @return dir la direccion de la persona
	 */
	public String getDir() {
		return dir;
	}

	/**
	 * Metodo set del atributo direccion
	 *
	 * @param dir
	 *            la direccion de la persona
	 */
	public void setDir(String dir) {
		this.dir = dir;
	}

	/**
	 * Metodo get del atributo departamento
	 *
	 * @return dpto el departamento de la persona
	 */
	public Depto getDpto() {
		return dpto;
	}

	/**
	 * Metodo set del atributo departamento
	 *
	 * @param dpto
	 *            el departamento de la persona
	 */
	public void setDpto(Depto dpto) {
		this.dpto = dpto;
	}

	/**
	 * Metodo get del atributo ciudad
	 *
	 * @return ciudad la ciudad de la persona
	 */
	public Ciudad getCiudad() {
		return ciudad;
	}

	/**
	 * Metodo set del atributo ciudad
	 *
	 * @param ciudad
	 *            la ciudad de la persona
	 */
	public void setCiudad(Ciudad ciudad) {
		this.ciudad = ciudad;
	}

	/**
	 * Metodo get del atributo activo
	 *
	 * @return activo true o false si la persona esta activa
	 */
	public boolean isActivo() {
		return activo;
	}

	/**
	 * Metodo set del atributo activo
	 *
	 * @param activo
	 *            true o false si la persona esta activa
	 */
	public void setActivo(boolean activo) {
		this.activo = activo;
	}

	/**
	 * Metodo get de la fecha de nacimiento
	 *
	 * @return fecha la fecha de nacimiento de la persona
	 */
	public Date getFecha() {
		return fecha;
	}

	/**
	 * Metodo set de la fecha de nacimiento
	 *
	 * @param fecha
	 *            la fecha de nacimiento de la persona
	 */
	public void setFecha(Date fecha) {
		this.fecha = fecha;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the cliente
	 */
	public Persona getCliente() {
		return cliente;
	}

	/**
	 * @param cliente the cliente to set
	 */
	public void setCliente(Persona cliente) {
		this.cliente = cliente;
	}

	/**
	 * @return the servicio
	 */
	public Servicio getServicio() {
		return servicio;
	}

	/**
	 * @param servicio the servicio to set
	 */
	public void setServicio(Servicio servicio) {
		this.servicio = servicio;
	}

	/**
	 * @return the departamentos
	 */
	public List<Depto> getDepartamentos() {
		return departamentos;
	}

	/**
	 * @param departamentos the departamentos to set
	 */
	public void setDepartamentos(List<Depto> departamentos) {
		this.departamentos = departamentos;
	}

	/**
	 * @return the ciudades
	 */
	public List<Ciudad> getCiudades() {
		return ciudades;
	}

	/**
	 * @param ciudades the ciudades to set
	 */
	public void setCiudades(List<Ciudad> ciudades) {
		this.ciudades = ciudades;
	}

	/**
	 * @return the turnos
	 */
	public List<Turno> getTurnos() {
		return turnos;
	}

	/**
	 * @param turnos the turnos to set
	 */
	public void setTurnos(List<Turno> turnos) {
		this.turnos = turnos;
	}

	/**
	 * @return the ultimoTurno
	 */
	public Turno getUltimoTurno() {
		return ultimoTurno;
	}

	/**
	 * @param ultimoTurno
	 *            the ultimoTurno to set
	 */
	public void setUltimoTurno(Turno ultimoTurno) {
		this.ultimoTurno = ultimoTurno;
	}

}


text:
<?

	$subject = "Your bug at MediaBugs.org has been closed.";

?>

Your bug "<?= $document->headline; ?>" is being marked "Closed: Unresolved" because it has been inactive for two months.

Thanks for helping fix the news!

You can help even more by answering three short questions about your experience with this bug. Just click through below:

<? $document->POD->siteRoot(); ?>/r/bugs/edit?id=<?= $document->id; ?>

If you think your bug should be reopened, you can do so at the bottom of that same page.

-- the MediaBugs team


text:
namespace google {
namespace cloud {
namespace pubsub_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

class SchemaServiceMetadata : public SchemaServiceStub {
 public:
  ~SchemaServiceMetadata() override = default;
  explicit SchemaServiceMetadata(std::shared_ptr<SchemaServiceStub> child);

  StatusOr<google::pubsub::v1::Schema> CreateSchema(
      grpc::ClientContext& context,
      google::pubsub::v1::CreateSchemaRequest const& request) override;

  StatusOr<google::pubsub::v1::Schema> GetSchema(
      grpc::ClientContext& context,
      google::pubsub::v1::GetSchemaRequest const& request) override;

  StatusOr<google::pubsub::v1::ListSchemasResponse> ListSchemas(
      grpc::ClientContext& context,
      google::pubsub::v1::ListSchemasRequest const& request) override;

  Status DeleteSchema(
      grpc::ClientContext& context,
      google::pubsub::v1::DeleteSchemaRequest const& request) override;

  StatusOr<google::pubsub::v1::ValidateSchemaResponse> ValidateSchema(
      grpc::ClientContext& context,
      google::pubsub::v1::ValidateSchemaRequest const& request) override;

  StatusOr<google::pubsub::v1::ValidateMessageResponse> ValidateMessage(
      grpc::ClientContext& context,
      google::pubsub::v1::ValidateMessageRequest const& request) override;

 private:
  void SetMetadata(grpc::ClientContext& context,
                   std::string const& request_params);
  void SetMetadata(grpc::ClientContext& context);

  std::shared_ptr<SchemaServiceStub> child_;
  std::string api_client_header_;
};

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
}  // namespace pubsub_internal
}  // namespace cloud
}  // namespace google

#endif  // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_INTERNAL_SCHEMA_METADATA_DECORATOR_H


text:
var fs = require('fs');
var http = require('http');
http.globalAgent.maxSockets = Infinity;
var models = require('./models');
var log = require('./log');
var capabilityMatcher = require('./capabilitymatcher');
var store = require('./store');
var async = require('async');

exports.pendingRequests = [];

exports.NODE_TIMEOUT = 10000;
exports.TEST_TIMEOUT = 120000;
exports.MAX_DURATION = 1800000;
exports.timeouts = {};
exports.busyProcessingPendingRequests = false;

var _hubConfiguration = null;

String.prototype.endsWith = function(suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
};

setInterval(function() {
	var nodes = store.getAllNodes();
	async.each(Object.keys(nodes), function(key, callback) {
		var node = nodes[key];
		if (node) {
			if (((new Date()).getTime() - node.lastSeen) > exports.NODE_TIMEOUT) {
				// node no longer available?
				if (node.host.indexOf('testingbot') === -1) {
					exports.removeNode(node.host, node.port, function() {
						log.info("Node " + node.host + " removed from pool");
					});
				}
			}
		}

		callback();
	}, function(err) {});

}, 5000);

exports.processPendingRequest = function() {
	var len = exports.pendingRequests.length;

	if (len === 0) {
		return;
	}

	log.info("Processing pending requests, " + len);
	var now = (new Date()).getTime();
	async.eachSeries(exports.pendingRequests, function(request, callback) {
		if ((now - request.since) > 600000) {
			for (var i = 0, len = exports.pendingRequests.length; i < len; i++) {
				if (exports.pendingRequests[i] === request) {
					exports.pendingRequests.splice(i, 1);
				}
			}
			callback();
		}
		capabilityMatcher.findNode(request.desiredCapabilities, function(node, nodeCapabilities) {
			for (var i = 0, len = exports.pendingRequests.length; i < len; i++) {
				if (exports.pendingRequests[i] === request) {
					exports.pendingRequests.splice(i, 1);
				}
			}
			log.info("Found pending match");
			// run the actual callback
			if (request) {
				request.callback(node, nodeCapabilities);
			}
			log.info("Callback pending match for ", request);
			// remove the pending request, since we are processing it
			callback();
		}, true);
	}, function(err) {
		exports.pendingRequests = [];
		log.info("Done processing pending requests");
	});
};

exports.sendDeleteRequest = function(sessionID, type, nodeHost, nodePort, cb, retries) {
	var path, method;

	log.info("Sending Delete request");
	if (type === 'RC') {
		path = '/selenium-server/driver?cmd=testComplete&sessionId=' + sessionID;
		method = 'POST';
	} else {
		path = "/wd/hub/session/" + sessionID;
		method = 'DELETE';
	}

	var options = {
	  host: nodeHost,
	  port: nodePort,
	  path: path,
	  method: method
	};
	var req = http.request(options, function(res) {
	    res.on('data', function(chunk) {

	    });

		log.info("Delete request done, node response: " + res.statusCode);
		cb();
	});

	req.on('error', function(e) {
	  log.warn("error sending delete request", e);
	  // try sending it again
	  if (retries < 3) {
	  	log.warn("trying to send delete request again");
	  	setTimeout(function() {
	  		retries += 1;
	  		exports.sendDeleteRequest(sessionID, type, nodeHost, nodePort, cb, retries);
	  	}, 2000);
	  } else {
	  	// couldn't clean up, still run the callback to cleanup server side
	  	log.warn("failed to send delete requests, cleaning up server side");
	  	cb();
	  }
	});

	req.end();
};

exports.getHubConfiguration = function(cb) {
	if (_hubConfiguration !== null) {
		return cb(_hubConfiguration);
	}

	fs.readFile('config.json', function(err, data) {
		if (err) {
			log.warn("Unable to read config.json");
			throw err;
		}
		_hubConfiguration = data.toString();
		data.success = true;
		cb(_hubConfiguration);
	});
};

exports.addNode = function(json, cb) {
	var hostParts = json['configuration']['remoteHost'].replace('http://', '').split(':')
	var host = hostParts[0];
	var port = hostParts[1];

	store.existsNode(host, port, function(exists) {
		if (exists === true)
		{
			return cb(true);
		}

		var capabilities = [];

		// make sure this proxy is not blocked
		if (json.capabilities) {
			for (var i = 0, len = json.capabilities.length; i < len; i++) {
				capabilities.push(new models.Capability(json.capabilities[i]));
			}

			var node = new models.Node(host, port, capabilities, json);

			store.addNode(host, port, node, function() {
				store.addAvailableNode(node, function() {
					cb(true);

					exports.processPendingRequest();
				});
			});
		} else {
			log.warn("Trying to add a node without any available capabilities");
			cb(false);
		}
	});
};

exports.removeNode = function(host, port, cb) {
	// when removing a node, make sure no activesessions are left with this node
	async.waterfall(
	[
		function(callback) {
			log.info("Removing node " + (host + "_" + port));
			store.removeSessionsForNode(host, port, callback);
		},
		function(success, callback) {
			store.removeNode(host, port, callback);
		}
	], function(err, result) {
			log.info("Node removed successfully");
			cb(true);
	});
};

var _sessionCheck = function(sessionID) {
	exports.timeouts[sessionID] = setInterval(function() {
		var that = this;

		store.getSession(sessionID, function(session) {
			if (!session) {
				log.info("Can not find session " + sessionID);
				clearInterval(that);
				delete exports.timeouts[sessionID];
				return;
			}

			var now = (new Date()).getTime();
			var diff = (now - session.lastUsed);

			var timeRunning = (now - session.startTime);

			var idleTimeout = exports.TEST_TIMEOUT;
			var maxDuration = exports.MAX_DURATION;

			if (session.desiredCapabilities.idletimeout) {
				idleTimeout = parseInt(session.desiredCapabilities.idletimeout, 10) * 1000;
			}

			// the hub should give the node the chance to do a timeout
			idleTimeout += 15000;

			var node = store.getNode(session.nodeHost, session.nodePort);

			if (session.desiredCapabilities.maxduration) {
				maxDuration = parseInt(session.desiredCapabilities.maxduration, 10) * 1000;
			}

			if (diff > 5000) {
				log.info("checking for timeouts during test: " + (node.host + ":" + node.port) + " (" + sessionID + ")\n" + diff + " vs " + idleTimeout);
			}

			if ((timeRunning > maxDuration) || (diff > maxDuration)) {
				log.info("Test has exceeded max duration of " + maxDuration);
				clearInterval(that);
				delete exports.timeouts[session.sessionID];
				exports.sendDeleteRequest(sessionID, session.type, session.nodeHost, session.nodePort, function() {}, 0);
				if (session.response) {
					var response = new models.Response(500, '[' + sessionID + '] Test has timed out after ' + secs + " seconds");
					session.response.writeHead(response.statusCode, response.headers);
					session.response.end(response.body);
				}
				// remove the session from the registry
				exports.removeSession(sessionID, function() { log.debug("Session removed"); });
			} else if (diff > idleTimeout) {
				log.info("Timeout occurred: " + diff + " , " + (node.host + "_" + node.port));
				var secs = Math.round(((new Date()).getTime() - session.lastUsed) / 1000);
				clearInterval(that);
				delete exports.timeouts[session.sessionID];

				// when a test times out, it's important to clear resources on the node.
				// send a delete/testComplete command to the node
				log.info("Charge for timeout " + sessionID);
				exports.sendDeleteRequest(sessionID, session.type, session.nodeHost, session.nodePort, function() {}, 0);
				if (session.response) {
					var response = new models.Response(500, '[' + sessionID + '] Test has timed out after ' + secs + " seconds");
					session.response.writeHead(response.statusCode, response.headers);
					session.response.end(response.body);
				}
				// last used in future, don't immediately use this node
				store.updateNode(node, function() {
					// remove the session from the registry
					exports.removeSession(sessionID, function() { log.debug("Session removed"); });
				});
			}
		});

	}, 5000);
};

exports.addSession = function(sessionID, session, desiredCapabilities, cb) {
	store.addSession(sessionID, session, function() {
		cb();
		_sessionCheck(sessionID);
	});
};

exports.removeSession = function(sessionID, cb) {
	log.info("Remove session: " + sessionID);

	store.getSession(sessionID, function(session) {
		if (session === undefined) {
			log.warn("Trying to remove a session that is not active: " + sessionID);
			cb();
		} else {
			if (exports.timeouts[sessionID] !== null) {
				clearInterval(exports.timeouts[sessionID]);
				delete exports.timeouts[sessionID];
			}

			// node is available again
			store.removeSession(sessionID, function() {
				var node = store.getNode(session.nodeHost, session.nodePort);

				store.addAvailableNode(node, function() {
					log.info("Session " + sessionID + " has been removed");
					// when stopping a test, immediately execute callback
					cb();

					// and have the node check for pending requests
					exports.processPendingRequest();
				});
			});
		}
	});
};

exports.getSessionById = function(sessionID, cb) {
	store.getSession(sessionID, cb);
};

exports.addPendingRequest = function(desiredCapabilities, cb) {
	var uniqueID = Math.round(Math.random() * 100000000);
	exports.pendingRequests.push({ 'desiredCapabilities' : desiredCapabilities, 'callback' : cb, 'uniqueID' : uniqueID, 'since' : (new Date()).getTime() });
};


text:
package exporterhelper

import (
	"go.opencensus.io/trace"
)

var (
	okStatus = trace.Status{Code: trace.StatusCodeOK}
)

// ExporterOptions contains options concerning how an Exporter is configured.
type ExporterOptions struct {
	// TOOD: Retry logic must be in the same place as metrics recording because
	// if a request is retried we should not record metrics otherwise number of
	// spans received + dropped will be different than the number of received spans
	// in the receiver.
	recordMetrics bool
	spanName      string
}

// ExporterOption apply changes to ExporterOptions.
type ExporterOption func(*ExporterOptions)

// WithRecordMetrics makes new Exporter to record metrics for every request.
func WithRecordMetrics(recordMetrics bool) ExporterOption {
	return func(o *ExporterOptions) {
		o.recordMetrics = recordMetrics
	}
}

// WithSpanName makes new Exporter to wrap every request with a Span.
func WithSpanName(spanName string) ExporterOption {
	return func(o *ExporterOptions) {
		o.spanName = spanName
	}
}

// Construct the ExporterOptions from multiple ExporterOption.
func newExporterOptions(options ...ExporterOption) ExporterOptions {
	var opts ExporterOptions
	for _, op := range options {
		op(&opts)
	}
	return opts
}

func errToStatus(err error) trace.Status {
	if err != nil {
		return trace.Status{Code: trace.StatusCodeUnknown, Message: err.Error()}
	}
	return okStatus
}


text:
package org.apache.camel.component.vertx;

import java.io.InputStream;
import java.util.List;

import io.netty.buffer.Unpooled;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonArray;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class VertxJsonArrayConverterTest extends CamelTestSupport {

    private static final String BODY = "[\"Hello\",\"World\"]";

    @Test
    public void testBufferToJsonArray() {
        JsonArray jsonArray = context.getTypeConverter().convertTo(JsonArray.class, Buffer.buffer(BODY));
        Assertions.assertEquals(BODY, jsonArray.toString());
    }

    @Test
    public void testStringToJsonArray() {
        JsonArray jsonArray = context.getTypeConverter().convertTo(JsonArray.class, BODY);
        Assertions.assertEquals(BODY, jsonArray.toString());
    }

    @Test
    public void testByteArrayToJsonArray() {
        JsonArray jsonArray = context.getTypeConverter().convertTo(JsonArray.class, BODY.getBytes());
        Assertions.assertEquals(BODY, jsonArray.toString());
    }

    @Test
    public void testByteBufToJsonArray() {
        JsonArray jsonArray = context.getTypeConverter().convertTo(JsonArray.class, Unpooled.wrappedBuffer(BODY.getBytes()));
        Assertions.assertEquals(BODY, jsonArray.toString());
    }

    @Test
    public void testListArrayToJsonArray() {
        JsonArray jsonArray = context.getTypeConverter().convertTo(JsonArray.class, new JsonArray(BODY).getList());
        Assertions.assertEquals(BODY, jsonArray.toString());
    }

    @Test
    public void testInputStreamToJsonArray() {
        InputStream inputStream = context.getTypeConverter().convertTo(InputStream.class, BODY);
        JsonArray jsonArray = context.getTypeConverter().convertTo(JsonArray.class, inputStream);
        Assertions.assertEquals(BODY, jsonArray.toString());
    }

    @Test
    public void testJsonArrayToBuffer() {
        Buffer result = context.getTypeConverter().convertTo(Buffer.class, Buffer.buffer(BODY).toJsonArray());
        Assertions.assertEquals(BODY, result.toString());
    }

    @Test
    public void testJsonArrayToString() {
        String result = context.getTypeConverter().convertTo(String.class, Buffer.buffer(BODY).toJsonArray());
        Assertions.assertEquals(BODY, result);
    }

    @Test
    public void testJsonArrayToByteArray() {
        byte[] result = context.getTypeConverter().convertTo(byte[].class, Buffer.buffer(BODY.getBytes()).toJsonArray());
        Assertions.assertEquals(BODY, new String(result));
    }

    @Test
    public void testJsonArrayToList() {
        List result = context.getTypeConverter().convertTo(List.class, Buffer.buffer(BODY).toJsonArray());
        Assertions.assertEquals(BODY, new JsonArray(result).toString());
    }
}


text:
package com.sequenceiq.redbeams.service.stack;

import java.util.Optional;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.common.annotations.VisibleForTesting;
import com.sequenceiq.cloudbreak.auth.crn.Crn;
import com.sequenceiq.cloudbreak.cloud.CloudConnector;
import com.sequenceiq.cloudbreak.cloud.exception.TemplatingNotSupportedException;
import com.sequenceiq.cloudbreak.cloud.init.CloudPlatformConnectors;
import com.sequenceiq.cloudbreak.cloud.model.CloudPlatformVariant;
import com.sequenceiq.cloudbreak.common.exception.BadRequestException;
import com.sequenceiq.redbeams.api.endpoint.v4.ResourceStatus;
import com.sequenceiq.redbeams.domain.DatabaseServerConfig;
import com.sequenceiq.redbeams.domain.stack.DBStack;
import com.sequenceiq.redbeams.domain.stack.DatabaseServer;
import com.sequenceiq.redbeams.exception.RedbeamsException;
import com.sequenceiq.redbeams.flow.RedbeamsFlowManager;
import com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsEvent;
import com.sequenceiq.redbeams.flow.redbeams.provision.RedbeamsProvisionEvent;
import com.sequenceiq.redbeams.service.dbserverconfig.DatabaseServerConfigService;

@Service
public class RedbeamsCreationService {

    // TODO: Adjust workspace to something non-default when and if necessary
    @VisibleForTesting
    static final long DEFAULT_WORKSPACE = 0L;

    private static final Logger LOGGER = LoggerFactory.getLogger(RedbeamsCreationService.class);

    @Inject
    private CloudPlatformConnectors cloudPlatformConnectors;

    @Inject
    private DBStackService dbStackService;

    @Inject
    private DatabaseServerConfigService databaseServerConfigService;

    @Inject
    private RedbeamsFlowManager flowManager;

    public DBStack launchDatabaseServer(DBStack dbStack, String clusterCrn) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Create called with: {}", dbStack);
        }

        if (dbStackService.findByNameAndEnvironmentCrn(dbStack.getName(), dbStack.getEnvironmentId()).isPresent()) {
            throw new BadRequestException("A stack for this database server already exists in the environment");
        }

        Optional<DatabaseServerConfig> optionalDBServerConfig =
                databaseServerConfigService.findByEnvironmentCrnAndClusterCrn(dbStack.getEnvironmentId(), clusterCrn);

        DBStack savedDbStack;
        boolean startFlow = false;
        if (optionalDBServerConfig.isEmpty()) {
            LOGGER.debug("DataBaseServerConfig is not available by cluster crn '{}'", clusterCrn);
            savedDbStack = saveDbStackAndRegisterDatabaseServerConfig(dbStack, clusterCrn);
            startFlow = true;
        } else {
            DatabaseServerConfig dbServerConfig = optionalDBServerConfig.get();
            if (dbServerConfig.getDbStack().isEmpty()) {
                LOGGER.debug("DBStack is not available in DatabaseServerConfig '{}'", dbServerConfig.getResourceCrn());
                savedDbStack = saveDbStackInDatabaseServerConfig(dbServerConfig, dbStack);
                startFlow = true;
            } else {
                savedDbStack = dbServerConfig.getDbStack().get();
            }
        }

        if (startFlow) {
            flowManager.notify(RedbeamsProvisionEvent.REDBEAMS_PROVISION_EVENT.selector(),
                    new RedbeamsEvent(RedbeamsProvisionEvent.REDBEAMS_PROVISION_EVENT.selector(), savedDbStack.getId()));
        }

        return savedDbStack;
    }

    private DBStack saveDbStackAndRegisterDatabaseServerConfig(DBStack dbStack, String clusterCrn) {
        DBStack savedStack = saveDbStack(dbStack);
        registerDatabaseServerConfig(savedStack, clusterCrn);

        return savedStack;
    }

    private DBStack saveDbStackInDatabaseServerConfig(DatabaseServerConfig databaseServerConfig,  DBStack dbStack) {
        DBStack savedDbStack = saveDbStack(dbStack);
        databaseServerConfig.setDbStack(savedDbStack);
        databaseServerConfigService.update(databaseServerConfig);

        return savedDbStack;
    }

    private DBStack saveDbStack(DBStack dbStack) {
        // possible future change is to use a flow here (GetPlatformTemplateRequest, modified for database server)
        // for now, just get it synchronously / within this thread, it ought to be quick
        CloudPlatformVariant platformVariant = new CloudPlatformVariant(dbStack.getCloudPlatform(), dbStack.getPlatformVariant());
        try {
            CloudConnector connector = cloudPlatformConnectors.get(platformVariant);
            if (connector == null) {
                throw new RedbeamsException("Failed to find cloud connector for platform variant " + platformVariant);
            }
            String template = connector.resources().getDBStackTemplate();
            if (template == null) {
                throw new RedbeamsException("No database stack template is available for platform variant " + platformVariant);
            }
            dbStack.setTemplate(template);
        } catch (TemplatingNotSupportedException e) {
            throw new RedbeamsException("Failed to retrieve database stack template for cloud platform", e);
        }

        DatabaseServer databaseServer = dbStack.getDatabaseServer();
        if (databaseServer.getConnectionDriver() == null) {
            String connectionDriver = databaseServer.getDatabaseVendor().connectionDriver();
            databaseServer.setConnectionDriver(connectionDriver);
            LOGGER.info("Database server allocation request lacked a connection driver; defaulting to {}", connectionDriver);
        }

        return dbStackService.save(dbStack);
    }

    private void registerDatabaseServerConfig(DBStack dbStack, String clusterCrn) {
        DatabaseServer databaseServer = dbStack.getDatabaseServer();
        DatabaseServerConfig dbServerConfig = new DatabaseServerConfig();

        dbServerConfig.setResourceStatus(ResourceStatus.SERVICE_MANAGED);
        dbServerConfig.setAccountId(databaseServer.getAccountId());
        dbServerConfig.setName(dbStack.getName());
        dbServerConfig.setDescription(dbStack.getDescription());
        dbServerConfig.setEnvironmentId(dbStack.getEnvironmentId());
        dbServerConfig.setConnectionDriver(databaseServer.getConnectionDriver());
        // username and password are set during conversion to DBStack
        dbServerConfig.setConnectionUserName(databaseServer.getRootUserName());
        dbServerConfig.setConnectionPassword(databaseServer.getRootPassword());
        dbServerConfig.setDatabaseVendor(databaseServer.getDatabaseVendor());
        dbServerConfig.setDbStack(dbStack);
        // host and port are set after allocation is complete, so leave as null
        dbServerConfig.setResourceCrn(Crn.safeFromString(dbStack.getResourceCrn()));
        dbServerConfig.setClusterCrn(clusterCrn);

        databaseServerConfigService.create(dbServerConfig, DEFAULT_WORKSPACE, false);
    }

}


text:
package com.sohu.jafka.mx;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;

import com.sohu.jafka.log.Log;

/**
 * @author adyliu (imxylz@gmail.com)
 * @since 1.0
 */
public class LogStats implements LogStatsMBean, IMBeanName {

    final Log log;

    private final AtomicLong numCumulatedMessages = new AtomicLong(0);

    private String mbeanName;

    public LogStats(Log log) {
        this.log = log;
    }

    public String getMbeanName() {
        return mbeanName;
    }

    public void setMbeanName(String mbeanName) {
        this.mbeanName = mbeanName;
    }

    public String getName() {
        return log.name;
    }

    public void recordAppendedMessages(int nMessages) {
        numCumulatedMessages.getAndAdd(nMessages);
    }

    @Override
    public int getSegmentNum() {
        return log.getNumberOfSegments();
    }

    @Override
    public long getStartingAppendedMessagesNum() {
        return numCumulatedMessages.get();
    }

    @Override
    public long getLastSegmentSize() {
        return log.getHighwaterMark();
    }

    @Override
    public long getLastSegmentAddressingSize() {
        return log.getLastSegmentAddressingSize();
    }

    @Override
    public long getTotalSegmentSize() {
        return log.size();
    }

    @Override
    public long getTotalOffset() {
        return log.getTotalOffset();
    }

    @Override
    public String getLastFlushedTime() {
        long time = log.getLastFlushedTime();
        if (time == 0) return "--";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date(time));
    }
}


text:
import 'core-js';
import 'zone.js/dist/zone';
import 'ts-helpers';

import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app.module';

platformBrowserDynamic().bootstrapModule(AppModule);


text:
"use strict";

Object.defineProperty(exports, "__esModule", {
	value: true
});

var _minBy = require("./minBy.js");

var _minBy2 = _interopRequireDefault(_minBy);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var Iterable;

var compareNumber = function compareNumber(a, b) {
	return a - b;
};

function maxBy(as, f, compare) {
	compare = compare || compareNumber;

	return (0, _minBy2.default)(as, f, function (u, v) {
		return compare(v, u);
	});
}

exports.default = maxBy;


text:
package org.shenit.tutorial.android.drawable;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import org.shenit.tutorial.android.R;

public class StateListDrawableExampleActivity extends AppCompatActivity {
    Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_state_list_example);
        mButton = (Button) findViewById(R.id.button);
    }

    public void onSetSelected(View v){
        mButton.setSelected(true);
    }

    public void onSetUnselected(View v){
        mButton.setSelected(false);
    }

    public void onSetEnabled(View v){
        mButton.setEnabled(true);
    }

    public void onSetDisabled(View v){
        mButton.setEnabled(false);
    }
}


text:
'use strict';

// security.js
var secure     = require('express-secure-only'),
  rateLimit    = require('express-rate-limit'),
  csrf         = require('csurf'),
  cookieParser = require('cookie-parser'),
  helmet       = require('helmet'),
  morgan       = require('morgan'),
  fs           = require('fs');

module.exports = function (app) {
  app.enable('trust proxy');

  // 0. setup the logger
  var logStream = fs.createWriteStream(__dirname + '/../logs/access.log', {flags: 'a'});
  app.use('/api/', morgan('combined', {stream: logStream}));

  // 1. redirects http to https
  app.use(secure());

  // 2. helmet with defaults
  app.use(helmet());

  // 3. setup cookies
  var secret = Math.random().toString(36).substring(7);
  app.use(cookieParser(secret));

  // 4. csrf
  var csrfProtection = csrf({ cookie: true });
  app.get('/', csrfProtection, function(req, res, next) {
    req._csrfToken = req.csrfToken();
    next();
  });

  // 5. rate limiting.
  app.use('/api/', csrfProtection, rateLimit({
    windowMs: 30 * 1000, // seconds
    delayMs: 0,
    max: 3,
    message: JSON.stringify({
      error:'Too many requests, please try again in 30 seconds.',
      code: 429
    }),
  }));
};


text:
package helpers

import (
	"encoding/json"
	"errors"
	"fmt"
	"math/rand"
	"net"
	"net/http"
	"reflect"
	"time"

	"github.com/sensu/uchiwa/uchiwa/logger"
	"github.com/sensu/uchiwa/uchiwa/structs"
)

// return true if String in Slice, false otherwise
func StringInSlice(a string, list []string) bool {
	for _, b := range list {
		if b == a {
			return true
		}
	}
	return false
}

// BuildClientsMetrics builds the metrics for the events
func BuildClientsMetrics(clients *[]interface{}) *structs.StatusMetrics {
	metrics := structs.StatusMetrics{}

	metrics.Total = len(*clients)
	for _, c := range *clients {
		client := c.(map[string]interface{})

		silenced, ok := client["silenced"].(bool)
		if ok { // Do not ignore the client if we don't have a silenced attribute
			if silenced {
				metrics.Silenced++
				continue
			}
		}

		status, ok := client["status"].(int)
		if !ok {
			logger.Warningf("Could not assert the status to an int: %+v", client["status"])
			continue
		}

		if status == 2.0 {
			metrics.Critical++
			continue
		} else if status == 1.0 {
			metrics.Warning++
			continue
		} else if status == 0.0 {
			metrics.Healthy++
			continue
		}
		metrics.Unknown++
	}

	return &metrics
}

// BuildEventsMetrics builds the metrics for the events
func BuildEventsMetrics(events *[]interface{}) *structs.StatusMetrics {
	metrics := structs.StatusMetrics{}

	metrics.Total = len(*events)

	for _, e := range *events {
		event := e.(map[string]interface{})

		silenced, ok := event["silenced"].(bool)
		if ok { // Do not ignore the event if we don't have a silenced attribute
			if silenced {
				metrics.Silenced++
				continue
			}
		}

		check, ok := event["check"].(map[string]interface{})
		if !ok {
			logger.Warningf("Could not assert this check to an interface: %+v", event["check"])
			continue
		}

		status, ok := check["status"].(float64)
		if !ok {
			logger.Warningf("Could not assert this status to a flot64: %+v", check["status"])
			continue
		}

		if status == 2.0 {
			metrics.Critical++
			continue
		} else if status == 1.0 {
			metrics.Warning++
			continue
		}
		metrics.Unknown++
	}

	return &metrics
}

// GetBoolFromInterface ...
func GetBoolFromInterface(i interface{}) (bool, error) {
	if i == nil {
		logger.Debug("The interface is nil")
		return false, errors.New("The interface is nil")
	}

	b, ok := i.(bool)
	if !ok {
		logger.Debugf("Could not assert to a boolean the interface: %+v", i)
		return false, errors.New("Could not assert to a boolean the interface")
	}

	return b, nil
}

// GetEvent returns an event associated to a specific check
func GetEvent(check, client, dc string, events *[]interface{}) (map[string]interface{}, error) {
	if check == "" || client == "" || dc == "" || len(*events) == 0 {
		return nil, errors.New("No parameters should be empty")
	}

	for _, e := range *events {
		event, ok := e.(map[string]interface{})
		if !ok {
			continue
		}

		if event["dc"] == nil || event["dc"] != dc {
			continue
		}

		c, ok := event["client"].(map[string]interface{})
		if !ok {
			if event["client"] == nil || event["client"] != client {
				continue
			}
		} else if c["name"] == nil || c["name"] != client {
			continue
		}

		k, ok := event["check"].(map[string]interface{})
		if !ok {
			if event["check"] == nil || event["check"] != check {
				continue
			} else {
				return map[string]interface{}{"check": event["check"], "client": event["client"], "occurrences": event["occurrences"], "output": event["output"], "status": event["status"]}, nil
			}
		} else if k["name"] == nil || k["name"] != check {
			continue
		}

		if event["action"] != nil {
			k["action"] = event["action"]
		}
		if event["occurrences"] != nil {
			k["occurrences"] = event["occurrences"]
		}

		return k, nil
	}

	return nil, errors.New("No event found")
}

// GetInterfacesFromBytes returns a slice of interfaces from a slice of byte
func GetInterfacesFromBytes(bytes []byte) ([]interface{}, error) {
	var interfaces []interface{}
	if err := json.Unmarshal(bytes, &interfaces); err != nil {
		return nil, err
	}
	return interfaces, nil
}

// GetMapFromBytes returns a map from a slice of byte
func GetMapFromBytes(bytes []byte) (map[string]interface{}, error) {
	var m map[string]interface{}

	if len(bytes) == 0 {
		return m, nil
	}

	if err := json.Unmarshal(bytes, &m); err != nil {
		return nil, err
	}
	return m, nil
}

// GetMapFromInterface returns a map from an interface
func GetMapFromInterface(i interface{}) map[string]interface{} {
	m, ok := i.(map[string]interface{})
	if !ok {
		logger.Debugf("Could not assert to a map the interface: %+v", i)
		return nil
	}

	return m
}

// GetIP returns the real user IP address
func GetIP(r *http.Request) string {
	if xForwardedFor := r.Header.Get("X-FORWARDED-FOR"); len(xForwardedFor) > 0 {
		return xForwardedFor
	}
	ip, _, _ := net.SplitHostPort(r.RemoteAddr)
	return ip
}

// InterfaceToSlice takes a slice of type interface{} and returns a slice of interface
func InterfaceToSlice(slice interface{}) ([]interface{}, error) {
	value := reflect.ValueOf(slice)
	if value.Kind() != reflect.Slice {
		return nil, fmt.Errorf("The interface provided is not a slice: %+v", slice)
	}

	result := make([]interface{}, value.Len())

	for i := 0; i < value.Len(); i++ {
		result[i] = value.Index(i).Interface()
	}

	return result, nil
}

// InterfaceToString takes a slice of interface{} a slice of string
func InterfaceToString(i []interface{}) []string {
	var result []string

	for _, value := range i {
		v, ok := value.(string)
		if ok {
			result = append(result, v)
		}
	}

	return result
}

// IsCheckSilenced determines whether a check for a particular client is silenced.
// Returns true if the check is silenced and a slice of silence entries IDs
func IsCheckSilenced(check, client map[string]interface{}, dc string, silenced []interface{}) (bool, []string) {
	var isSilenced bool
	var commonSubscriptions, isSilencedBy, subscribers, subscriptions []string

	if dc == "" || len(silenced) == 0 {
		return false, isSilencedBy
	}

	checkName, ok := check["name"].(string)
	if !ok {
		return false, isSilencedBy
	}

	clientName, ok := client["name"].(string)
	if !ok {
		clientName = ""
	}

	// Retrieve the check subscribers
	if check["subscribers"] != nil {
		s, ok := check["subscribers"].([]interface{})
		if !ok {
			return false, isSilencedBy
		}
		subscribers = InterfaceToString(s)
	}

	// Retrieve the client subscriptions
	if client["subscriptions"] != nil {
		s, ok := client["subscriptions"].([]interface{})
		if !ok {
			return false, isSilencedBy
		}
		subscriptions = InterfaceToString(s)
	}

	// Get the subscriptions both check and client have in common
	for _, subscriber := range subscribers {
		if IsStringInArray(subscriber, subscriptions) {
			commonSubscriptions = append(commonSubscriptions, subscriber)
		}
	}

	for _, silence := range silenced {
		m, ok := silence.(map[string]interface{})
		if !ok {
			logger.Warningf("Could not assert this silence entry to a map: %+v", silence)
			continue
		}

		if m["dc"] != dc {
			continue
		}

		// Ignore silenced entries that have not begun yet
		if m["begin"] != nil {
			begin := time.Unix(int64(m["begin"].(float64)), 0)
			now := time.Now()
			if now.Before(begin) {
				continue
			}
		}

		// Check (e.g. *:check_cpu)
		if m["id"] == fmt.Sprintf("*:%s", checkName) {
			isSilenced = true
			isSilencedBy = append(isSilencedBy, m["id"].(string))
			continue
		}

		// Client subscription (e.g. client:foo:* )
		if m["id"] == fmt.Sprintf("client:%s:*", clientName) {
			isSilenced = true
			isSilencedBy = append(isSilencedBy, m["id"].(string))
			continue
		}

		// Client's check subscription (e.g. client:foo:check_cpu )
		if m["id"] == fmt.Sprintf("client:%s:%s", clientName, checkName) {
			isSilenced = true
			isSilencedBy = append(isSilencedBy, m["id"].(string))
			continue
		}

		for _, subscription := range commonSubscriptions {
			// Subscription (e.g. load-balancer:* )
			if m["id"] == fmt.Sprintf("%s:*", subscription) {
				isSilenced = true
				isSilencedBy = append(isSilencedBy, m["id"].(string))
				continue
			}

			// Subscription' check (e.g. load-balancer:check_cpu)
			if m["id"] == fmt.Sprintf("%s:%s", subscription, checkName) {
				isSilenced = true
				isSilencedBy = append(isSilencedBy, m["id"].(string))
				continue
			}
		}

	}

	return isSilenced, isSilencedBy
}

// IsClientSilenced determines whether a client is silenced.
// Returns true if the client is silenced.
func IsClientSilenced(client, dc string, silenced []interface{}) bool {
	if client == "" || dc == "" || len(silenced) == 0 {
		return false
	}

	for _, silence := range silenced {
		m, ok := silence.(map[string]interface{})
		if !ok {
			continue
		}

		if m["dc"] == dc && m["id"] == fmt.Sprintf("client:%s:*", client) {
			return true
		}
	}

	return false
}

// IsStringInArray searches 'array' for 'item' string
// Returns true 'item' is a value of 'array'
func IsStringInArray(item string, array []string) bool {
	if item == "" || len(array) == 0 {
		return false
	}

	for _, element := range array {
		if element == item {
			return true
		}
	}

	return false
}

// RandomString generates a random string of the provided length
func RandomString(length int) string {
	if length == 0 {
		length = 32
	}

	char := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
	rand.Seed(time.Now().UTC().UnixNano())
	buf := make([]byte, length)
	for i := 0; i < length; i++ {
		buf[i] = char[rand.Intn(len(char)-1)]
	}
	return string(buf)
}