<commit_before>/*******************************************************************************
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.github.lothar.security.acl.jpa.query;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.query.PartTreeJpaQuery;
import org.springframework.data.repository.query.QueryMethod;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.util.ReflectionUtils;

import com.github.lothar.security.acl.jpa.JpaSpecProvider;

public class AclJpaQuery implements RepositoryQuery {


  private Logger logger = LoggerFactory.getLogger(getClass());
  private RepositoryQuery query;
  private Class<?> domainType;
  private EntityManager em;
  private JpaSpecProvider<Object> jpaSpecProvider;
  private Method method;

  public AclJpaQuery(Method method, RepositoryQuery query, Class<?> domainType, EntityManager em,
      JpaSpecProvider<Object> jpaSpecProvider) {
    this.method = method;
    this.query = query;
    this.domainType = domainType;
    this.em = em;
    this.jpaSpecProvider = jpaSpecProvider;
  }

  @Override
  public Object execute(Object[] parameters) {
    AclPredicateTargetSource aclPredicateTargetSource = installAclSpec();
    try {
      return query.execute(parameters);
    } finally {
      if (aclPredicateTargetSource != null) {
          uninstallAclSpec(aclPredicateTargetSource);
      }
    }
  }

  private void uninstallAclSpec(AclPredicateTargetSource aclPredicateTargetSource) {
    try {
      aclPredicateTargetSource.uninstallAcl();
      logger.debug("ACL Jpa Specification uninstalled from method '{}' and query {}", method,
          query);
    } catch (Exception e) {
      logger.warn("Unable to uninstall ACL Jpa Specification from method '" + method
          + "' and query: " + query, e);
    }
  }

  private AclPredicateTargetSource installAclSpec() {
    try {
      CriteriaQuery<?> criteriaQuery = criteriaQuery();
      AclPredicateTargetSource aclPredicateTargetSource = aclPredicateTargetSource(criteriaQuery);
      
      Specification<Object> jpaSpec = jpaSpecProvider.jpaSpecFor(domainType);
      Predicate aclPredicate =
          jpaSpec.toPredicate(root(criteriaQuery), criteriaQuery, em.getCriteriaBuilder());

      // install restriction
      aclPredicateTargetSource.installAcl(aclPredicate);
      
      logger.debug("ACL Jpa Specification installed for method '{}' and query {}: {}", method,
          query, jpaSpec);
      return aclPredicateTargetSource;
    } catch (Exception e) {<commit_msg>warning message when no cached criteria found<commit_after>/*******************************************************************************
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.github.lothar.security.acl.jpa.query;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.query.PartTreeJpaQuery;
import org.springframework.data.repository.query.QueryMethod;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.util.ReflectionUtils;

import com.github.lothar.security.acl.jpa.JpaSpecProvider;

public class AclJpaQuery implements RepositoryQuery {


  private Logger logger = LoggerFactory.getLogger(getClass());
  private RepositoryQuery query;
  private Class<?> domainType;
  private EntityManager em;
  private JpaSpecProvider<Object> jpaSpecProvider;
  private Method method;

  public AclJpaQuery(Method method, RepositoryQuery query, Class<?> domainType, EntityManager em,
      JpaSpecProvider<Object> jpaSpecProvider) {
    this.method = method;
    this.query = query;
    this.domainType = domainType;
    this.em = em;
    this.jpaSpecProvider = jpaSpecProvider;
  }

  @Override
  public Object execute(Object[] parameters) {
    AclPredicateTargetSource aclPredicateTargetSource = installAclSpec();
    try {
      return query.execute(parameters);
    } finally {
      if (aclPredicateTargetSource != null) {
          uninstallAclSpec(aclPredicateTargetSource);
      }
    }
  }

  private void uninstallAclSpec(AclPredicateTargetSource aclPredicateTargetSource) {
    try {
      aclPredicateTargetSource.uninstallAcl();
      logger.debug("ACL Jpa Specification uninstalled from method '{}' and query {}", method,
          query);
    } catch (Exception e) {
      logger.warn("Unable to uninstall ACL Jpa Specification from method '" + method
          + "' and query: " + query, e);
    }
  }

  private AclPredicateTargetSource installAclSpec() {
    try {
      CriteriaQuery<?> criteriaQuery = criteriaQuery();
      if (criteriaQuery == null) {
        logger.warn("Unable to install ACL Jpa Specification for method '" + method
                + "' and query: " + query + " : unable to get cached criteria inside query");
        return null;
      }
      AclPredicateTargetSource aclPredicateTargetSource = aclPredicateTargetSource(criteriaQuery);
      
      Specification<Object> jpaSpec = jpaSpecProvider.jpaSpecFor(domainType);
      Predicate aclPredicate =
          jpaSpec.toPredicate(root(criteriaQuery), criteriaQuery, em.getCriteriaBuilder());

      // install restriction
      aclPredicateTargetSource.installAcl(aclPredicate);
      
      logger.debug("ACL Jpa Specification installed for method '{}' and query {}: {}", method,
          query, jpaSpec);
      return aclPredicateTargetSource;
    } catch (Exception e) {
{'fraction_non_alphanumeric': 0.1251621271076524, 'fraction_numerical': 0.0036316472114137485, 'mean_word_length': 4.303301237964237, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '141697', 'n_tokens_mistral': 2075, 'n_tokens_neox': 2040, 'n_words': 538}