Fixed Postcard Sending for Galaxy POST Requests

- Post Requests Work
- TCP Server connects
This commit is contained in:
2022-07-08 22:00:23 -05:00
parent 08acb3ddc0
commit 58b2c87cd8
3 changed files with 33 additions and 27 deletions

View File

@@ -13,9 +13,13 @@ public class ArkOneController implements TcpHandler {
public void receiveData(Connection connection, byte[] data) {
//TODO: IMPLEMENT PLUGIN CHECKING AND FORWARDING HERE
//Currently just echos back the received data until implemented
String s = new String(data);
// Log the received request
String xmlData = new String(data);
System.out.println("[ArkOne] New Request: " + xmlData);
try {
connection.send(s.toUpperCase().getBytes());
connection.send(xmlData.toUpperCase().getBytes());
} catch (IOException e) {
e.printStackTrace();
}
@@ -23,13 +27,11 @@ public class ArkOneController implements TcpHandler {
@Override
public void connectEvent(Connection connection) {
// Just log connections
System.out.println("[ArkOne][EVENT] Client connection from: " + connection.getAddress().getCanonicalHostName());
// No need to log anything
}
@Override
public void disconnectEvent(Connection connection) {
// Just log disconnections
System.out.println("[ArkOne][EVENT] Client disconnected: " + connection.getAddress().getCanonicalHostName());
// No need to log anything
}
}

View File

@@ -14,6 +14,8 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerMapping;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -60,7 +62,7 @@ public class GalaxyServer {
IOUtils.copy(fileContentStream, response.getOutputStream());
// Flush the buffer
//TODO: MIGHT ALSO NEED TO SET THE FILE CONTENT LENGTH AS A HEADER
// MIGHT ALSO NEED TO SET THE FILE CONTENT LENGTH AS A HEADER
response.flushBuffer();
} else {
throw new Exception("[Galaxy][GET][ERROR] Requested file doesn't exist: " + pathToFile);
@@ -97,6 +99,9 @@ public class GalaxyServer {
*/
@PostMapping("/")
public ResponseEntity<String> GalaxyPostResponse(@RequestBody String xmlBody) {
// Log the request
System.out.println("[Galaxy][POST] New Request: " + xmlBody);
try {
// Parse the xml body of the request
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
@@ -104,19 +109,20 @@ public class GalaxyServer {
Document doc = dBuilder.parse(new InputSource(new StringReader(xmlBody)));
doc.getDocumentElement().normalize();
// Get the root element
String root = doc.getDocumentElement().getNodeName();
// Get the element for the command
NodeList nodes = doc.getDocumentElement().getChildNodes();
String command = nodes.item(0).getNodeName();
// Handle based on the root element
switch(root) {
switch(command) {
case "postcard":
return sendPostcard(doc);
return sendPostcard((Element)nodes.item(0));
case "savecrib":
return saveCrib(doc);
case "loadcrib":
return loadCrib(doc);
default:
System.out.println("[Galaxy][POST][ERROR] Unhandled type of request for: " + root);
System.out.println("[Galaxy][POST][ERROR] Unhandled type of request for: " + command);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
} catch (ParserConfigurationException | IOException | SAXException e) {
@@ -128,8 +134,6 @@ public class GalaxyServer {
//TODO: USE THIS FOR THE XML PARSING: https://www.tutorialspoint.com/java_xml/java_dom_parse_document.htm#
//TODO: USE THIS FOR THE ARKONE SERVER: https://github.com/JavaGrinko/tcp-spring-boot-starter
private ResponseEntity<String> loadCrib(Document request) {
System.out.println("[Galaxy][POST] loadcrib request received");
//TODO: IMPLEMENT METHOD
@@ -142,15 +146,14 @@ public class GalaxyServer {
return null;
}
private ResponseEntity<String> sendPostcard(Document request) {
private ResponseEntity<String> sendPostcard(Element element) {
System.out.println("[Galaxy][POST] postcard request received");
//TODO: PARSE OUT DATA FROM REQUEST
// Parse data from the request
String to = "cockatoo242@gmail.com";
String subject = "Greetings From Funkiki Island";
String body = "Wow, check out this awesome postcard!";
String fileName = "card_0.jpg";
String to = element.getAttribute("to");
String subject = element.getAttribute("subject");
String body = element.getAttribute("body");
String fileName = element.getAttribute("id");
// Try to send the postcard to the email
String response = "";

View File

@@ -30,17 +30,18 @@ public class EmailServiceImpl implements EmailService {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
@Override
public void prepare(MimeMessage mimeMessage) throws Exception {
// Set the main parts of the email
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
mimeMessage.setFrom(new InternetAddress(from));
mimeMessage.setSubject(subject);
mimeMessage.setText(body);
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
// Set the email fields
helper.addTo(to);
helper.setFrom(from);
helper.setSubject(subject);
helper.setText(body, true);
// Attach the postcard
Resource resource = new ClassPathResource("static/Postcards/" + fileToAttach);
byte[] fileContent = org.apache.commons.io.IOUtils.toByteArray(resource.getInputStream());
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.addAttachment(fileToAttach, new ByteArrayResource(fileContent));
helper.addAttachment(fileToAttach, new ByteArrayResource(fileContent), "image/jpeg");
}
};