Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] API Refactor #147

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 0 additions & 49 deletions src/main/java/ca/bc/gov/iamp/bcparis/api/MessageApi.java

This file was deleted.

31 changes: 0 additions & 31 deletions src/main/java/ca/bc/gov/iamp/bcparis/api/MessageApiTest.java

This file was deleted.

67 changes: 67 additions & 0 deletions src/main/java/ca/bc/gov/iamp/bcparis/api/MessageController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ca.bc.gov.iamp.bcparis.api;

import ca.bc.gov.iamp.bcparis.exception.message.InvalidMessageType;
import ca.bc.gov.iamp.bcparis.repository.Layer7MessageRepository;
import ca.bc.gov.iamp.bcparis.message.DatagramProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import ca.bc.gov.iamp.bcparis.model.message.Layer7Message;
import ca.bc.gov.iamp.bcparis.util.RequestContext;

import java.util.List;
import java.util.Optional;

/**
* The message API Controller accepts request to search drivers, vehicles or por and return information.
*
* There is a global exception handler ca.bc.gov.iamp.api.exception.handling.CustomRestExceptionHandler that handles any exception thrown.
* There is a local exception handler ca.bc.gov.iamp.bcparis.api.exception.ExceptionHandlerController that handles known ICBC, layer7 or POR exception (an email is send in this case)
*
*/
@RestController
@RequestMapping("/api/v1/message")
public class MessageController {

private final Logger log = LoggerFactory.getLogger(MessageController.class);

@Autowired
private RequestContext context;

@Autowired
private List<DatagramProcessor> datagramProcessorList;

@Autowired
private Layer7MessageRepository layer7Repository;

@PutMapping(consumes=MediaType.APPLICATION_JSON_VALUE)
private ResponseEntity<Layer7Message> message( @RequestBody Layer7Message message ){

log.info("Message received");

context.setRequestObject(message);
Layer7Message response = null;

Optional<DatagramProcessor> processor = datagramProcessorList.stream().filter(x -> x.getType() == message.getMessageType()).findFirst();

if(processor.isPresent()) {
response = processor.get().process(message);
} else {
throw new InvalidMessageType("Invalid Message type");
}

return ResponseEntity.ok(response);
}


@PostMapping( path="test/layer7", consumes=MediaType.APPLICATION_JSON_VALUE)
private ResponseEntity<String> testPutMessageLayer7( @RequestBody Layer7Message message ){
final String response = layer7Repository.sendMessage(message);
return ResponseEntity.ok(response);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ca.bc.gov.iamp.bcparis.message;

import ca.bc.gov.iamp.bcparis.model.MessageType;
import ca.bc.gov.iamp.bcparis.model.message.Layer7Message;

public interface DatagramProcessor {

MessageType getType();

Layer7Message process(Layer7Message message);

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@


package ca.bc.gov.iamp.bcparis.processor.datagram;
package ca.bc.gov.iamp.bcparis.message;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import ca.bc.gov.iamp.bcparis.model.MessageType;
import ca.bc.gov.iamp.bcparis.service.MessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -17,7 +19,6 @@
import ca.bc.gov.iamp.bcparis.model.message.body.Body;
import ca.bc.gov.iamp.bcparis.repository.ICBCRestRepository;
import ca.bc.gov.iamp.bcparis.repository.query.IMSRequest;
import ca.bc.gov.iamp.bcparis.service.MessageService;

@Service
public class DriverProcessor implements DatagramProcessor{
Expand All @@ -29,7 +30,12 @@ public class DriverProcessor implements DatagramProcessor{

@Autowired
private MessageService messageService;


@Override
public MessageType getType() {
return MessageType.DRIVER;
}

public Layer7Message process(Layer7Message message) {
log.info("Processing Driver message.");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ca.bc.gov.iamp.bcparis.processor.datagram;
package ca.bc.gov.iamp.bcparis.message;

import java.util.List;

import ca.bc.gov.iamp.bcparis.model.MessageType;
import ca.bc.gov.iamp.bcparis.service.MessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -12,10 +14,9 @@
import ca.bc.gov.iamp.bcparis.model.message.body.Body;
import ca.bc.gov.iamp.bcparis.model.por.POROutput;
import ca.bc.gov.iamp.bcparis.repository.PORRestRepository;
import ca.bc.gov.iamp.bcparis.service.MessageService;

@Service
public class PORProcessor {
public class PORProcessor implements DatagramProcessor {

private final Logger log = LoggerFactory.getLogger(PORProcessor.class);

Expand All @@ -24,7 +25,12 @@ public class PORProcessor {

@Autowired
private MessageService messageService;


@Override
public MessageType getType() {
return MessageType.POR;
}

public Layer7Message process(Layer7Message message) {
log.info("Processing POR message.");
Body body = message.getEnvelope().getBody();
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/ca/bc/gov/iamp/bcparis/message/ReportProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ca.bc.gov.iamp.bcparis.message;

import ca.bc.gov.iamp.bcparis.model.MessageType;
import ca.bc.gov.iamp.bcparis.model.message.Layer7Message;
import org.springframework.stereotype.Service;

@Service
public class ReportProcessor implements DatagramProcessor {
@Override
public MessageType getType() {
return MessageType.REPORT;
}

@Override
public Layer7Message process(Layer7Message message) {
return message;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

package ca.bc.gov.iamp.bcparis.processor.datagram;
package ca.bc.gov.iamp.bcparis.message;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
Expand All @@ -8,6 +8,8 @@
import java.util.List;
import java.util.stream.Collectors;

import ca.bc.gov.iamp.bcparis.model.MessageType;
import ca.bc.gov.iamp.bcparis.service.MessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -19,7 +21,6 @@
import ca.bc.gov.iamp.bcparis.model.message.body.Body;
import ca.bc.gov.iamp.bcparis.repository.ICBCRestRepository;
import ca.bc.gov.iamp.bcparis.repository.query.IMSRequest;
import ca.bc.gov.iamp.bcparis.service.MessageService;

@Service
public class VehicleProcessor implements DatagramProcessor{
Expand All @@ -31,7 +32,12 @@ public class VehicleProcessor implements DatagramProcessor{

@Autowired
private MessageService messageService;


@Override
public MessageType getType() {
return MessageType.VEHICLE;
}

public Layer7Message process(Layer7Message message) {
log.info("Processing Vehicle message.");

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ private String generateAuditTransactionId(final Layer7Message l7message) {
correlationIdByte = correlationIdByte.replaceAll("\\s+", "");
return correlationIdByte;
}

}
return UUID.randomUUID().toString();
}
Expand Down
Loading