fi.evolver.basics.spring.messaging.MessagingController Maven / Gradle / Ivy
package fi.evolver.basics.spring.messaging;
import static fi.evolver.basics.spring.http.HttpInterceptor.setMessageType;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import fi.evolver.basics.spring.http.HttpCommunicator;
import fi.evolver.basics.spring.http.HttpStreamResponse;
import fi.evolver.basics.spring.http.exception.HttpBadRequestException;
import fi.evolver.basics.spring.log.entity.MessageLog.Direction;
import fi.evolver.basics.spring.messaging.model.MessageDetails;
import fi.evolver.utils.NullSafetyUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
@RestController
public class MessagingController {
private static final Logger LOG = LoggerFactory.getLogger(MessagingController.class);
private final MessagingService messagingService;
private final HttpCommunicator httpCommunicator;
@Autowired
public MessagingController(MessagingService messagingService, HttpCommunicator httpCommunicator) {
this.messagingService = messagingService;
this.httpCommunicator = httpCommunicator;
}
@Operation(summary = "Send a message")
@ApiResponses(value = {
@ApiResponse(responseCode = "204", description = "The message was queued for sending successfully"),
@ApiResponse(responseCode = "400", description = "Bad request"),
@ApiResponse(responseCode = "500", description = "Internal server error")
})
@PostMapping("/proxy")
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void sendMessage(@RequestParam String messageType, @RequestBody String message, @RequestParam Map parameters) {
setMessageType(NullSafetyUtils.denull(messageType, "?"));
if (messageType == null)
throw new HttpBadRequestException("Missing parameter messageType");
Map metadata = new LinkedHashMap<>(parameters);
metadata.remove("messageType");
messagingService.send(messageType, MessageDetails.create(message, metadata));
}
@PatchMapping("/test")
public String testPatch(@RequestParam String messageType, @RequestBody(required = false) String message, @RequestParam Map parameters) {
LOG.debug("PATCH - MessageType: {}\nMessage: {}\nParameters: {}", messageType, message, parameters);
return "PATCH";
}
@PostMapping("/test")
public String testPost(@RequestParam String messageType, @RequestBody(required = false) String message, @RequestParam Map parameters) {
LOG.debug("POST - MessageType: {}\nMessage: {}\nParameters: {}", messageType, message, parameters);
return "POST";
}
@PutMapping("/test")
public String testPut(@RequestParam String messageType, @RequestBody(required = false) String message, @RequestParam Map parameters) {
LOG.debug("PUT - MessageType: {}\nMessage: {}\nParameters: {}", messageType, message, parameters);
return "PUT";
}
@GetMapping("/test")
public String testGet(@RequestParam String messageType, @RequestParam Map parameters) {
LOG.debug("GET - MessageType: {}\nParameters: {}", messageType, parameters);
return "GET";
}
@DeleteMapping("/test")
public String testDelete(@RequestParam String messageType, @RequestParam Map parameters) {
LOG.debug("DELETE - MessageType: {}\nParameters: {}", messageType, parameters);
return "DELETE";
}
@PostMapping("/call")
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void call(@RequestParam String url, @RequestParam String method, @RequestBody(required = false) String message) throws IOException {
LOG.debug("CALL: {} {}", method, url);
try {
HttpStreamResponse result = httpCommunicator.createRequest("TestMessage", "OtherSystem", new URL(url), Direction.OUTBOUND)
.setMethod(method)
.setPayload(message, StandardCharsets.UTF_8)
.sendForStream();
LOG.debug("RESULT: {} {}", result.getStatusCode(), result.getStatusMessage());
if (result.getMessage() != null)
LOG.debug("MESSAGE: {}", IOUtils.toString(result.getMessage(), StandardCharsets.UTF_8));
}
catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy