Saturday 26 November 2022

Create Rest APT to Encrypt and Decrypt message using AES/CBC/PKCS5PADDING algorithm

Project Structure



Calling Response from Postman tool





------------------------------------------- Controller ---------------------------------------------
package com.ramsis.encrypt.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ramsis.encrypt.util.EncryptionDecryption;
import com.ramsis.encrypt.dto.POJO;

@RestController
public class Controller {
   
@Autowired
EncryptionDecryption ecndnc;

@Value("${EncDncKey}")
String publickey;

@CrossOrigin(origins = "*")
@PostMapping("/Ngdecrypt")
public POJO NGdecrypt(@RequestBody POJO pojo) throws Exception {

String msg = pojo.getMessage();
String enckey = pojo.getKeys();
String decryptString = null;
// String publickey = "3K4G2pb4dl8M/7YaOrGiPA=="; // 128 bit key
try {
if (!enckey.equalsIgnoreCase(publickey)) {
System.out.println("You are not Authorised.");
pojo.setMessage("Not Authorised.");
System.out.println(pojo.getMessage());
return pojo;
}

decryptString = ecndnc.decrypt(publickey, null, msg);
pojo.setMessage(decryptString);
} catch (Exception ex) {
ex.getMessage();
}

return pojo;
}

@CrossOrigin(origins = "*")
@PostMapping("/Ngencrypt")
public POJO Ngencrypt(@RequestBody POJO pojo) throws Exception {

String msg = pojo.getMessage();
String enckey = pojo.getKeys();
String encryptString = null;
try {
if (!enckey.equalsIgnoreCase(publickey)) {
System.out.println("You are not Authorised.");
pojo.setMessage("Not Authorised.");
return pojo;
}
encryptString = ecndnc.encrypt(publickey, null, msg);
pojo.setMessage(encryptString);
//System.out.println(encryptString);
} catch (Exception ex) {
ex.getMessage();
}
return pojo;
}
}

----------------------------------------EncryptionDecryption----------------------------------------

package com.ramsis.encrypt.util;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Component;

@Component
public class EncryptionDecryption {

public static String encrypt(String key, String initVector, String value) {
byte[] encrypted =null;
try {
initVector = key.substring(0, 16); // 16 bytes IV
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

   encrypted = cipher.doFinal(value.getBytes());
System.out.println("encrypted string: " + Base64.encodeBase64String(encrypted));
} catch (Exception ex) {
ex.printStackTrace();
}
return Base64.encodeBase64String(encrypted);
}

public static String decrypt(String key, String initVector, String encrypted) {
byte[] original =null;
try {
initVector = key.substring(0, 16);
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
   original = cipher.doFinal(Base64.decodeBase64(encrypted));
} catch (Exception ex) {
ex.printStackTrace();
}
return new String(original);
}

}

------------------------------------------------ CallAPI ----------------------------------------------
package com.ramsis.encrypt.callApi;

import org.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class CallAPI {

public void restAPITest(String url, String message) {

String key = "3K4G2pb4dl8M/7YaOrGiPA==";
try {
RestTemplate restTemplate = new RestTemplate();
JSONObject personJsonObject = new JSONObject();
personJsonObject.put("keys", key);
personJsonObject.put("message", message);

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<String> entityHttp = new HttpEntity<String>(personJsonObject.toString(), headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entityHttp, String.class);
/*
* String answer = restTemplate.postForObject(url, entity, String.class);
* System.out.println(answer);
*/
if (response.getStatusCodeValue() == 200) {
System.out.println(response.getBody());
}

} catch (Exception ex) {
ex.printStackTrace();
}
}

public static void main(String args[]) throws Exception {
CallAPI call = new CallAPI();
//call.restAPITest("http://localhost:8080/Ngencrypt", "ramsis-code");
call.restAPITest("http://localhost:8080/Ngdecrypt", "TaZydiy5PHtA4ME8744bOA==");

}
}

Sunday 20 November 2022

Spring Boot Rest API OneToMany Relationship with JPA Repository

In this Post I am showing how you can easily create a Rest API using JPA Repository with OneToMany Relationships between two Entities.

Project Structure



Calling Response from Postman tool














------------------------------------------ json ------------------------------------------------

{
"title":"Linux",
"author": "Jimmy",
"isbn": "890900",
    "pages" : [{ "number": "30", "content": "Switches", "chapter" : "3"},{ "number": "125", "content":          "Router", "chapter" : "8"}]
}


------------------------------------------ pom.xml -------------------------------------------

# Changing the server port
server.port=9090

# MySQL connection properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=cctns@123
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?createDatabaseIfNotExist=true&useSSL=false

# Log JPA queries
# Comment this in production
spring.jpa.show-sql=true

# Drop and create new tables (create, create-drop, validate, update)
# Only for testing purposes - comment this in production
spring.jpa.hibernate.ddl-auto=update

# Hibernate SQL dialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect


---------------------------------------- Book Entity ------------------------------------------------

package com.ramsis.onetomany.entity;

import java.io.Serializable;
import java.util.Objects;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "books")
public class Book implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "title" ,nullable = false)
    private String title;
    @Column(name = "author" ,nullable = false)
    private String author;
    @Column(name = "isbn" ,unique = true)
    private String isbn;
    @JsonIgnoreProperties("book")
    @OneToMany(mappedBy = "book", fetch = FetchType.LAZY,
            cascade = CascadeType.ALL)
    private Set<Page> pages;

    public Book() {
    }

    public Book(String title, String author, String isbn) {
        this.title = title;
        this.author = author;
        this.isbn = isbn;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public Set<Page> getPages() {
        return pages;
    }

    public void setPages(Set<Page> pages) {
        this.pages = pages;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return id.equals(book.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", isbn='" + isbn + '\'' +
                '}';
    }
}

------------------------------------------ Page Entity -----------------------------------------------

package com.ramsis.onetomany.entity;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Objects;

@Entity
@Table(name = "pages")
public class Page implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
   
    @Column(name = "number", nullable = false)
    private int number;
   
    @Column(name = "content" ,nullable = false)
    private String content;
   
    @Column(name = "chapter", nullable = false)
    private String chapter;
   
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "book_id", nullable = false)
    private Book book;

    public Page() {
    }

    public Page(int number, String content, String chapter, Book book) {
        this.number = number;
        this.content = content;
        this.chapter = chapter;
        this.book = book;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getChapter() {
        return chapter;
    }

    public void setChapter(String chapter) {
        this.chapter = chapter;
    }

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Page page = (Page) o;
        return id.equals(page.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return "Page{" +
                "id=" + id +
                ", number=" + number +
                ", content='" + content + '\'' +
                ", chapter='" + chapter + '\'' +
                ", book=" + book +
                '}';
    }
}

----------------------------------------------- BookService -----------------------------------------

package com.ramsis.onetomany.service;

import java.util.List;
import java.util.Set;

import com.ramsis.onetomany.dto.BookDto;
import com.ramsis.onetomany.entity.Book;
import com.ramsis.onetomany.entity.Page;

public interface BookService {

public List<Book> getBooks();
public Book addBook(BookDto book);
public Book updateBook(BookDto book);
public Book getBookByIsbn(String id);

}

-------------------------------------------------BookServiceImpl ----------------------------------

package com.ramsis.onetomany.service;

import java.util.List;
import java.util.Set;

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

import com.ramsis.onetomany.dto.BookDto;
import com.ramsis.onetomany.dto.PageDto;
import com.ramsis.onetomany.entity.Book;
import com.ramsis.onetomany.entity.Page;
import com.ramsis.onetomany.repository.BookRepository;
import com.ramsis.onetomany.repository.PageRepository;

@Service
public class BookServiceImpl implements BookService {

@Autowired
BookRepository bookRepo;

@Autowired
PageRepository pageRepo;

@Override
public Book getBookByIsbn(String id) {
// TODO Auto-generated method stub
return bookRepo.findByIsbn(id);
}

@Override
public Book addBook(BookDto book) {
Book bookEntity = new Book();
Page pageEntity = null;
bookEntity.setAuthor(book.getAuthor());
bookEntity.setIsbn(book.getIsbn());
bookEntity.setTitle(book.getTitle());
Set<PageDto> pageList = book.getPages();
Book bookEntityobj = bookRepo.save(bookEntity);

for (PageDto pagedata : pageList) {
pageEntity = new Page(
pagedata.getNumber(),
pagedata.getContent(),
pagedata.getChapter(),
bookEntityobj);
pageRepo.save(pageEntity);
}

return bookEntityobj;
}

@Override
public Book updateBook(BookDto book) {
Book bookEntity = new Book();
Page pageEntity = null;
bookEntity.setAuthor(book.getAuthor());
bookEntity.setIsbn(book.getIsbn());
bookEntity.setTitle(book.getTitle());
Set<PageDto> pageList = book.getPages();
Book bookEntityobj = bookRepo.save(bookEntity);

for (PageDto pagedata : pageList) {
pageEntity = new Page(
pagedata.getNumber(),
pagedata.getContent(),
pagedata.getChapter(),
bookEntityobj);
pageRepo.save(pageEntity);
}

return bookEntityobj;
}

@Override
public List<Book> getBooks() {
// TODO Auto-generated method stub
return  (List<Book>) bookRepo.findAll();
}

}

---------------------------------------------- Controller ---------------------------------------------

package com.ramsis.onetomany.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.ramsis.onetomany.dto.BookDto;
import com.ramsis.onetomany.entity.Book;
import com.ramsis.onetomany.service.BookService;

@RestController
public class PageBookController {
   
@Autowired
BookService bookService;

@GetMapping("/home")
public String home() {
return "Welcome in RestApi with OneToMany Relation, home Page";
}

@PostMapping("/addBook" )
public Book addBook(@RequestBody  BookDto book) {
return this.bookService.addBook(book);
}

@PostMapping("/updateBook" )
public Book updateBook(@RequestBody  BookDto book) {
return this.bookService.updateBook(book);
}

@GetMapping("/getBookIsbn/{eid}")
public Book getBookByIsbn(@PathVariable("eid") String eid) {
return this.bookService.getBookByIsbn(eid);
}

@GetMapping("/getBooks")
public List<Book> getBooks() {
return this.bookService.getBooks();
}
}

------------------------------------------------- BookDto --------------------------------------------

package com.ramsis.onetomany.dto;

import java.util.Set;

public class BookDto {
private Long id;
private String title;
private String author;
private String isbn;
private Set<PageDto> pages;

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public Set<PageDto> getPages() {
return pages;
}
public void setPages(Set<PageDto> pages) {
this.pages = pages;
}

}

-------------------------------------------------- PageDto -------------------------------------------

package com.ramsis.onetomany.dto;

public class PageDto {
private Long id;
private int number;
private String content;
private String chapter;
private BookDto book;

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getChapter() {
return chapter;
}
public void setChapter(String chapter) {
this.chapter = chapter;
}
public BookDto getBook() {
return book;
}
public void setBook(BookDto book) {
this.book = book;
}

}


------------------------------------------------- OnetomanyApplication -------------------------

package com.ramsis.onetomany;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

import com.ramsis.onetomany.repository.BookRepository;
import com.ramsis.onetomany.repository.PageRepository;

@SpringBootApplication
@EnableJpaAuditing
public class OnetomanyApplication {

public static void main(String[] args) {
SpringApplication.run(OnetomanyApplication.class, args);
}


@Bean
   public CommandLineRunner mappingDemo(BookRepository bookRepository,
                                        PageRepository pageRepository) {
       return args -> {
        /*
        System.out.print( bookRepository.findById(Long.valueOf(1)));
           // create a new book
           Book book = new Book("Linux", "Cdac", "99844");

           // save the book
           bookRepository.save(book);

           // create and save new pages
           pageRepository.save(new Page(2, "Introduction contents of Linux", "Intro", book));
           pageRepository.save(new Page(102, "Switches", "Switches WAN", book));
           pageRepository.save(new Page(70, "Router", "Router WAN", book));
         
           */
       };
   }
}

-------------------------------------------- BookRepository ---------------------------------------

package com.ramsis.onetomany.repository;

import org.springframework.data.repository.CrudRepository;

import com.ramsis.onetomany.entity.Book;

public interface BookRepository extends CrudRepository<Book, Long> {

    Book findByIsbn(String isbn);
    Book save(Book book);
}

--------------------------------------------------------