This repository has been archived by the owner on Jul 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e2caf33
commit 347b9c4
Showing
6 changed files
with
345 additions
and
0 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
src/main/java/io/gravitee/repository/mongodb/management/MongoAuditRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* Copyright (C) 2015 The Gravitee team (http://gravitee.io) | ||
* | ||
* 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 io.gravitee.repository.mongodb.management; | ||
|
||
import io.gravitee.common.data.domain.Page; | ||
import io.gravitee.repository.exceptions.TechnicalException; | ||
import io.gravitee.repository.management.api.AuditRepository; | ||
import io.gravitee.repository.management.api.ViewRepository; | ||
import io.gravitee.repository.management.api.search.AuditCriteria; | ||
import io.gravitee.repository.management.api.search.Pageable; | ||
import io.gravitee.repository.management.model.Audit; | ||
import io.gravitee.repository.management.model.View; | ||
import io.gravitee.repository.mongodb.management.internal.api.ViewMongoRepository; | ||
import io.gravitee.repository.mongodb.management.internal.audit.AuditMongoRepository; | ||
import io.gravitee.repository.mongodb.management.internal.model.AuditMongo; | ||
import io.gravitee.repository.mongodb.management.internal.model.ViewMongo; | ||
import io.gravitee.repository.mongodb.management.mapper.GraviteeMapper; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* @author Nicolas GERAUD (nicolas.geraud at graviteesource.com) | ||
* @author GraviteeSource Team | ||
*/ | ||
@Component | ||
public class MongoAuditRepository implements AuditRepository { | ||
|
||
private final Logger LOGGER = LoggerFactory.getLogger(MongoAuditRepository.class); | ||
|
||
@Autowired | ||
private AuditMongoRepository internalAuditRepo; | ||
|
||
@Autowired | ||
private GraviteeMapper mapper; | ||
|
||
@Override | ||
public Page<Audit> search(AuditCriteria filter, Pageable pageable) { | ||
Page<AuditMongo> auditsMongo = internalAuditRepo.search(filter, pageable); | ||
|
||
List<Audit> content = mapper.collection2list(auditsMongo.getContent(), AuditMongo.class, Audit.class); | ||
return new Page<>(content, auditsMongo.getPageNumber(), (int) auditsMongo.getPageElements(), auditsMongo.getTotalElements()); | ||
} | ||
|
||
@Override | ||
public Optional<Audit> findById(String id) throws TechnicalException { | ||
LOGGER.debug("Find view by ID [{}]", id); | ||
|
||
final AuditMongo audit = internalAuditRepo.findOne(id); | ||
|
||
LOGGER.debug("Find view by ID [{}] - Done", id); | ||
return Optional.ofNullable(mapper.map(audit, Audit.class)); | ||
} | ||
|
||
@Override | ||
public Audit create(Audit audit) throws TechnicalException { | ||
LOGGER.debug("Create audit {}", audit.toString()); | ||
|
||
AuditMongo auditMongo = mapper.map(audit, AuditMongo.class); | ||
AuditMongo createdAuditMongo = internalAuditRepo.insert(auditMongo); | ||
|
||
Audit res = mapper.map(createdAuditMongo, Audit.class); | ||
|
||
LOGGER.debug("Create view [{}] - Done", audit.toString()); | ||
|
||
return res; | ||
} | ||
|
||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...n/java/io/gravitee/repository/mongodb/management/internal/audit/AuditMongoRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Copyright (C) 2015 The Gravitee team (http://gravitee.io) | ||
* | ||
* 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 io.gravitee.repository.mongodb.management.internal.audit; | ||
|
||
import io.gravitee.repository.mongodb.management.internal.model.AuditMongo; | ||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
/** | ||
* @author Nicolas GERAUD (nicolas.geraud at graviteesource.com) | ||
* @author GraviteeSource Team | ||
*/ | ||
@Repository | ||
public interface AuditMongoRepository extends MongoRepository<AuditMongo, String>, AuditMongoRepositoryCustom { | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
.../io/gravitee/repository/mongodb/management/internal/audit/AuditMongoRepositoryCustom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Copyright (C) 2015 The Gravitee team (http://gravitee.io) | ||
* | ||
* 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 io.gravitee.repository.mongodb.management.internal.audit; | ||
|
||
import io.gravitee.common.data.domain.Page; | ||
import io.gravitee.repository.management.api.search.AuditCriteria; | ||
import io.gravitee.repository.management.api.search.Pageable; | ||
import io.gravitee.repository.mongodb.management.internal.model.AuditMongo; | ||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
/** | ||
* @author Nicolas GERAUD (nicolas.geraud at graviteesource.com) | ||
* @author GraviteeSource Team | ||
*/ | ||
public interface AuditMongoRepositoryCustom { | ||
|
||
Page<AuditMongo> search(AuditCriteria filter, Pageable pageable); | ||
} |
83 changes: 83 additions & 0 deletions
83
...va/io/gravitee/repository/mongodb/management/internal/audit/AuditMongoRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* Copyright (C) 2015 The Gravitee team (http://gravitee.io) | ||
* | ||
* 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 io.gravitee.repository.mongodb.management.internal.audit; | ||
|
||
import io.gravitee.common.data.domain.Page; | ||
import io.gravitee.repository.management.api.search.AuditCriteria; | ||
import io.gravitee.repository.management.api.search.Pageable; | ||
import io.gravitee.repository.mongodb.management.internal.model.AuditMongo; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.data.mongodb.core.query.Criteria; | ||
import org.springframework.data.mongodb.core.query.Query; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import static org.springframework.data.mongodb.core.query.Criteria.where; | ||
|
||
/** | ||
* @author Nicolas GERAUD (nicolas.geraud at graviteesource.com) | ||
* @author GraviteeSource Team | ||
*/ | ||
public class AuditMongoRepositoryImpl implements AuditMongoRepositoryCustom { | ||
|
||
@Autowired | ||
private MongoTemplate mongoTemplate; | ||
|
||
@Override | ||
public Page<AuditMongo> search(AuditCriteria filter, Pageable pageable) { | ||
Query query = new Query(); | ||
|
||
if (filter.getReferences() != null && !filter.getReferences().isEmpty()) { | ||
filter.getReferences().forEach( | ||
(referenceType, referenceIds) -> query.addCriteria( | ||
where("referenceType").is(referenceType). | ||
andOperator( | ||
where("referenceId").in(referenceIds)))); | ||
} | ||
|
||
if (filter.getFrom() != 0 && filter.getTo() != 0) { | ||
query.addCriteria( | ||
where("createdAt").gte(new Date(filter.getFrom())). | ||
andOperator( | ||
where("createdAt").lte(new Date(filter.getTo())))); | ||
} else { | ||
if (filter.getFrom() != 0) { | ||
query.addCriteria(where("createdAt").gte(new Date(filter.getFrom()))); | ||
} | ||
if (filter.getTo() != 0) { | ||
query.addCriteria(where("createdAt").lte(new Date(filter.getTo()))); | ||
} | ||
} | ||
|
||
if (filter.getEvents() != null && !filter.getEvents().isEmpty()) { | ||
query.addCriteria(where("event").in(filter.getEvents())); | ||
} | ||
|
||
query.with(new PageRequest( | ||
pageable.pageNumber() - 1 < 0 ? 0 : pageable.pageNumber() - 1, | ||
pageable.pageSize(), | ||
new Sort(Sort.Direction.DESC, "createdAt"))); | ||
|
||
List<AuditMongo> audits = mongoTemplate.find(query, AuditMongo.class); | ||
long total = mongoTemplate.count(query, AuditMongo.class); | ||
|
||
return new Page<>(audits, pageable.pageNumber(), pageable.pageSize(), total); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
src/main/java/io/gravitee/repository/mongodb/management/internal/model/AuditMongo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/** | ||
* Copyright (C) 2015 The Gravitee team (http://gravitee.io) | ||
* | ||
* 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 io.gravitee.repository.mongodb.management.internal.model; | ||
|
||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
import java.util.Date; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
/** | ||
* @author Nicolas GERAUD (nicolas.geraud at graviteesource.com) | ||
* @author GraviteeSource Team | ||
*/ | ||
@Document(collection = "audits") | ||
public class AuditMongo extends Auditable { | ||
|
||
@Id | ||
private String id; | ||
private String referenceId; | ||
private String referenceType; | ||
private String username; | ||
private String event; | ||
private Map<String,String> properties; | ||
private String patch; | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getReferenceId() { | ||
return referenceId; | ||
} | ||
|
||
public void setReferenceId(String referenceId) { | ||
this.referenceId = referenceId; | ||
} | ||
|
||
public String getReferenceType() { | ||
return referenceType; | ||
} | ||
|
||
public void setReferenceType(String referenceType) { | ||
this.referenceType = referenceType; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public String getEvent() { | ||
return event; | ||
} | ||
|
||
public void setEvent(String event) { | ||
this.event = event; | ||
} | ||
|
||
public Map<String,String> getProperties() { | ||
return properties; | ||
} | ||
|
||
public void setProperties(Map<String,String> properties) { | ||
this.properties = properties; | ||
} | ||
|
||
public String getPatch() { | ||
return patch; | ||
} | ||
|
||
public void setPatch(String patch) { | ||
this.patch = patch; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof AuditMongo)) return false; | ||
AuditMongo auditMongo = (AuditMongo) o; | ||
return Objects.equals(id, auditMongo.id); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters