-
Notifications
You must be signed in to change notification settings - Fork 0
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
c05262a
commit 7a9014f
Showing
12 changed files
with
485 additions
and
0 deletions.
There are no files selected for viewing
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
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,86 @@ | ||
package com.vinodh.config; | ||
|
||
import com.bol.crypt.CryptVault; | ||
import com.bol.secure.CachedEncryptionEventListener; | ||
import com.bol.secure.ReflectionEncryptionEventListener; | ||
import com.mongodb.MongoClient; | ||
import com.mongodb.MongoClientURI; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.context.annotation.PropertySource; | ||
import org.springframework.data.mongodb.config.AbstractMongoConfiguration; | ||
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper; | ||
import org.springframework.data.mongodb.core.convert.MappingMongoConverter; | ||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; | ||
|
||
import java.util.Base64; | ||
|
||
/** | ||
* @author thimmv | ||
* @createdAt 23-06-2019 22:00 | ||
*/ | ||
@Configuration | ||
@EnableMongoRepositories(basePackages = "com.vinodh.repository") | ||
@PropertySource("classpath:application.yml") | ||
public class MongoConfig extends AbstractMongoConfiguration { | ||
|
||
// normally you would use @Value to wire a property here | ||
private static final byte[] secretKey = Base64.getDecoder().decode("hqHKBLV83LpCqzKpf8OvutbCs+O5wX5BPu3btWpEvXA="); | ||
private static final byte[] oldKey = Base64.getDecoder().decode("cUzurmCcL+K252XDJhhWI/A/+wxYXLgIm678bwsE2QM="); | ||
|
||
// @Value("${spring.data.mongodb.secretKey}") | ||
// private String secretKey; | ||
// | ||
// @Value("${spring.data.mongodb.oldKey}") | ||
// private String oldKey; | ||
|
||
@Value("${spring.data.mongodb.database}") | ||
private String database; | ||
|
||
@Value("${spring.data.mongodb.uri}") | ||
private String uri; | ||
|
||
@Override | ||
public MongoClient mongoClient() { | ||
return new MongoClient(new MongoClientURI(uri)); | ||
} | ||
|
||
@Override | ||
@Bean | ||
public MappingMongoConverter mappingMongoConverter() throws Exception { | ||
MappingMongoConverter converter = super.mappingMongoConverter(); | ||
// NB: without overriding defaultMongoTypeMapper, an _class field is put in every document | ||
// since we know exactly which java class a specific document maps to, this is surplus | ||
converter.setTypeMapper(new DefaultMongoTypeMapper(null)); | ||
return converter; | ||
} | ||
|
||
@Override | ||
protected String getDatabaseName() { | ||
return database; | ||
} | ||
|
||
@Bean | ||
public CryptVault cryptVault() { | ||
return new CryptVault() | ||
.with256BitAesCbcPkcs5PaddingAnd128BitSaltKey(0, oldKey) | ||
.with256BitAesCbcPkcs5PaddingAnd128BitSaltKey(1, secretKey) | ||
// can be omitted if it's the highest version | ||
.withDefaultKeyVersion(1); | ||
} | ||
|
||
@Bean | ||
@Primary | ||
public ReflectionEncryptionEventListener encryptionEventListener(CryptVault cryptVault) { | ||
return new ReflectionEncryptionEventListener(cryptVault); | ||
} | ||
|
||
// @Bean | ||
// public MongoEncryptionListener encryptionEventListener() { | ||
// return new MongoEncryptionListener(); | ||
// } | ||
|
||
|
||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/com/vinodh/config/MongoEncryptionListener.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,76 @@ | ||
//package com.vinodh.config; | ||
// | ||
//import com.bol.crypt.CryptVault; | ||
//import com.bol.crypt.FieldCryptException; | ||
//import com.bol.secure.AbstractEncryptionEventListener; | ||
//import com.bol.secure.Encrypted; | ||
//import org.bson.Document; | ||
//import org.springframework.data.mongodb.core.mapping.event.AfterConvertEvent; | ||
//import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent; | ||
//import org.springframework.stereotype.Component; | ||
//import org.springframework.util.ReflectionUtils; | ||
// | ||
//import java.lang.reflect.Field; | ||
//import java.util.Map; | ||
// | ||
///** | ||
// * @author thimmv | ||
// * @createdAt 23-06-2019 22:40 | ||
// */ | ||
////@Component | ||
//public class MongoEncryptionListener extends AbstractEncryptionEventListener<MongoEncryptionListener> { | ||
// | ||
// public MongoEncryptionListener(CryptVault cryptVault) { | ||
// super(cryptVault); | ||
// } | ||
// | ||
// @Override | ||
// public void onBeforeConvert(BeforeConvertEvent event) { | ||
// Class<? extends String> targetClass = event.getCollectionName().getClass(); | ||
// Document targetDocument = event.getDocument(); | ||
// for (Map.Entry<String, Object> fieldsMap : targetDocument.entrySet()) { | ||
// String fieldName = fieldsMap.getKey(); | ||
// if (fieldName.equals("_class")) continue; | ||
// Field field = ReflectionUtils.findField(targetClass, fieldName); | ||
// field.setAccessible(true); | ||
// if (field == null) continue; | ||
// | ||
// Object fieldValue = fieldsMap.getValue(); | ||
// if (field.isAnnotationPresent(Encrypted.class)) { | ||
// // direct encryption | ||
// try { | ||
// targetDocument.put(fieldName, cryptVault.encrypt(fieldValue.toString().getBytes())); | ||
// } catch (Exception e) { | ||
// throw new FieldCryptException(fieldName, e); | ||
// } | ||
// | ||
// } | ||
// field.setAccessible(true); | ||
// } | ||
// } | ||
// | ||
// @Override | ||
// public void onAfterConvert(AfterConvertEvent event) { | ||
// Class<? extends String> targetClass = event.getCollectionName().getClass(); | ||
// Document targetDocument = event.getDocument(); | ||
// for (Map.Entry<String, Object> fieldsMap : targetDocument.entrySet()) { | ||
// String fieldName = fieldsMap.getKey(); | ||
// if (fieldName.equals("_class")) continue; | ||
// Field field = ReflectionUtils.findField(targetClass, fieldName); | ||
// field.setAccessible(true); | ||
// if (field == null) continue; | ||
// | ||
// Object fieldValue = fieldsMap.getValue(); | ||
// if (field.isAnnotationPresent(Encrypted.class)) { | ||
// // direct encryption | ||
// try { | ||
// targetDocument.put(fieldName, cryptVault.decrypt(fieldValue.toString().getBytes())); | ||
// } catch (Exception e) { | ||
// throw new FieldCryptException(fieldName, e); | ||
// } | ||
// | ||
// } | ||
// field.setAccessible(true); | ||
// } | ||
// } | ||
//} |
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,87 @@ | ||
package com.vinodh.controller; | ||
|
||
import com.bol.crypt.CryptVault; | ||
import com.vinodh.documents.Movie; | ||
import com.vinodh.service.MovieService; | ||
import com.vinodh.service.NextSequenceService; | ||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import io.swagger.annotations.ApiParam; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author thimmv | ||
* @createdAt 23-06-2019 15:41 | ||
*/ | ||
@RestController | ||
@RequestMapping("/movie") | ||
@Api(value = "Set of endpoints for Creating, Retrieving, Updating and Deleting of Movie information.") | ||
@Slf4j | ||
public class MovieController { | ||
|
||
|
||
@Autowired | ||
private MovieService movieService; | ||
|
||
@Autowired | ||
private CryptVault cryptVault; | ||
|
||
@Autowired | ||
private NextSequenceService nextSequenceService; | ||
|
||
@GetMapping("/findAll") | ||
@ApiOperation(value = "Find All available movies") | ||
public ResponseEntity<List<Movie>> getAllMovies() { | ||
return ResponseEntity.ok(movieService.findAll()); | ||
} | ||
|
||
@GetMapping("/findById") | ||
@ApiOperation(value = "Find Movie based on ID") | ||
public ResponseEntity<Movie> findById(@ApiParam(value = "Find Movie based on ID.", required = true) @RequestParam Long id) { | ||
return ResponseEntity.ok(movieService.findById(id)); | ||
} | ||
|
||
@GetMapping("/findByTitle") | ||
@ApiOperation(value = "Find Movie based on NAME") | ||
public ResponseEntity<List<Movie>> findByTitle(@ApiParam(value = "Find Movie based on Name.", required = true) @RequestParam String title) { | ||
return ResponseEntity.ok(movieService.findByTitle(title)); | ||
} | ||
|
||
@PostMapping("/save") | ||
@ApiOperation(value = "Save new Movie details") | ||
public ResponseEntity<Movie> save(@ApiParam(value = "Movie information to be created.", required = true) @RequestBody Movie movie) { | ||
if (movie.getId() == null) movie.setId(nextSequenceService.getNextSequence()); | ||
return ResponseEntity.ok(movieService.save(movie)); | ||
} | ||
|
||
@PutMapping("/update") | ||
@ApiOperation(value = "Update Movie details") | ||
public ResponseEntity<Movie> update(@ApiParam(value = "Movie information to be updated.", required = true) @RequestBody Movie movie) { | ||
return ResponseEntity.ok(movieService.update(movie)); | ||
} | ||
|
||
@DeleteMapping("/delete") | ||
@ApiOperation(value = "Delete Movie details") | ||
public void deleteUserByID(@ApiParam(value = "Delete Movie based on ID.", required = true) @RequestParam Long id) { | ||
movieService.deleteById(id); | ||
} | ||
|
||
@DeleteMapping("/deleteAll") | ||
@ApiOperation(value = "Delete All Movie details") | ||
public void deleteAll() { | ||
movieService.deleteAll(); | ||
} | ||
|
||
} |
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,17 @@ | ||
package com.vinodh.documents; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
@NoArgsConstructor | ||
@Setter | ||
@Getter | ||
@Document(collection = "customSequences") | ||
public class CustomSequences { | ||
@Id | ||
private String id; | ||
private Long seq; | ||
} |
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,18 @@ | ||
package com.vinodh.documents; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
/** | ||
* @author thimmv | ||
* @createdAt 23-06-2019 15:49 | ||
*/ | ||
@NoArgsConstructor | ||
@Setter | ||
@Getter | ||
public class ImdbRating { | ||
private Long id; | ||
private double rating; | ||
private long votes; | ||
} |
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,44 @@ | ||
package com.vinodh.documents; | ||
|
||
import com.bol.secure.Encrypted; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author thimmv | ||
* @createdAt 23-06-2019 15:42 | ||
*/ | ||
@Document("movies") | ||
@NoArgsConstructor | ||
@Setter | ||
@Getter | ||
public class Movie { | ||
|
||
@Id | ||
private Long id; | ||
|
||
@Encrypted | ||
private String title; | ||
|
||
private int year, runtime; | ||
|
||
private LocalDateTime released; | ||
|
||
@Encrypted | ||
private String poster, plot; | ||
|
||
private Set<String> cast = new HashSet<>(); | ||
private Set<String> directors = new HashSet<>(); | ||
private Set<String> countries = new HashSet<>(); | ||
private Set<String> genres = new HashSet<>(); | ||
|
||
private ImdbRating imdb; | ||
|
||
} |
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,17 @@ | ||
package com.vinodh.repository; | ||
|
||
import com.vinodh.documents.Movie; | ||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author thimmv | ||
* @createdAt 23-06-2019 15:43 | ||
*/ | ||
@Repository | ||
public interface MovieRepository extends MongoRepository<Movie, Long> { | ||
List<Movie> findByTitle(String name); | ||
|
||
} |
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
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,26 @@ | ||
package com.vinodh.service; | ||
|
||
import com.vinodh.documents.Movie; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author thimmv | ||
* @createdAt 23-06-2019 15:42 | ||
*/ | ||
public interface MovieService { | ||
|
||
List<Movie> findAll(); | ||
|
||
Movie findById(Long id); | ||
|
||
List<Movie> findByTitle(String name); | ||
|
||
Movie save(Movie movie); | ||
|
||
Movie update(Movie movie); | ||
|
||
void deleteById(Long id); | ||
|
||
void deleteAll(); | ||
} |
Oops, something went wrong.