001/******************************************************************************* 002 * Copyright 2017 The MIT Internet Trust Consortium 003 * 004 * Portions copyright 2011-2013 The MITRE Corporation 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 *******************************************************************************/ 018/** 019 * 020 */ 021package org.mitre.oauth2.repository.impl; 022 023import java.util.Collection; 024import java.util.Date; 025 026import javax.persistence.EntityManager; 027import javax.persistence.PersistenceContext; 028import javax.persistence.TypedQuery; 029 030import org.mitre.data.PageCriteria; 031import org.mitre.oauth2.model.AuthorizationCodeEntity; 032import org.mitre.oauth2.repository.AuthorizationCodeRepository; 033import org.mitre.util.jpa.JpaUtil; 034import org.springframework.stereotype.Repository; 035import org.springframework.transaction.annotation.Transactional; 036 037/** 038 * JPA AuthorizationCodeRepository implementation. 039 * 040 * @author aanganes 041 * 042 */ 043@Repository 044@Transactional(value="defaultTransactionManager") 045public class JpaAuthorizationCodeRepository implements AuthorizationCodeRepository { 046 047 @PersistenceContext(unitName="defaultPersistenceUnit") 048 EntityManager manager; 049 050 /* (non-Javadoc) 051 * @see org.mitre.oauth2.repository.AuthorizationCodeRepository#save(org.mitre.oauth2.model.AuthorizationCodeEntity) 052 */ 053 @Override 054 @Transactional(value="defaultTransactionManager") 055 public AuthorizationCodeEntity save(AuthorizationCodeEntity authorizationCode) { 056 057 return JpaUtil.saveOrUpdate(authorizationCode.getId(), manager, authorizationCode); 058 059 } 060 061 /* (non-Javadoc) 062 * @see org.mitre.oauth2.repository.AuthorizationCodeRepository#getByCode(java.lang.String) 063 */ 064 @Override 065 @Transactional(value="defaultTransactionManager") 066 public AuthorizationCodeEntity getByCode(String code) { 067 TypedQuery<AuthorizationCodeEntity> query = manager.createNamedQuery(AuthorizationCodeEntity.QUERY_BY_VALUE, AuthorizationCodeEntity.class); 068 query.setParameter("code", code); 069 070 AuthorizationCodeEntity result = JpaUtil.getSingleResult(query.getResultList()); 071 return result; 072 } 073 074 /* (non-Javadoc) 075 * @see org.mitre.oauth2.repository.AuthorizationCodeRepository#remove(org.mitre.oauth2.model.AuthorizationCodeEntity) 076 */ 077 @Override 078 public void remove(AuthorizationCodeEntity authorizationCodeEntity) { 079 AuthorizationCodeEntity found = manager.find(AuthorizationCodeEntity.class, authorizationCodeEntity.getId()); 080 if (found != null) { 081 manager.remove(found); 082 } 083 } 084 085 /* (non-Javadoc) 086 * @see org.mitre.oauth2.repository.AuthorizationCodeRepository#getExpiredCodes() 087 */ 088 @Override 089 public Collection<AuthorizationCodeEntity> getExpiredCodes() { 090 TypedQuery<AuthorizationCodeEntity> query = manager.createNamedQuery(AuthorizationCodeEntity.QUERY_EXPIRATION_BY_DATE, AuthorizationCodeEntity.class); 091 query.setParameter(AuthorizationCodeEntity.PARAM_DATE, new Date()); // this gets anything that's already expired 092 return query.getResultList(); 093 } 094 095 096 @Override 097 public Collection<AuthorizationCodeEntity> getExpiredCodes(PageCriteria pageCriteria) { 098 TypedQuery<AuthorizationCodeEntity> query = manager.createNamedQuery(AuthorizationCodeEntity.QUERY_EXPIRATION_BY_DATE, AuthorizationCodeEntity.class); 099 query.setParameter(AuthorizationCodeEntity.PARAM_DATE, new Date()); // this gets anything that's already expired 100 return JpaUtil.getResultPage(query, pageCriteria); 101 } 102 103 104 105}