Initial Commit | Added Galaxy GET Request for Update Files

This commit is contained in:
2022-07-07 10:33:57 -05:00
committed by Julia Butenhoff
commit 78e8940e37
22 changed files with 840 additions and 0 deletions

View File

@@ -0,0 +1,145 @@
package com.icedberries.UBFunkeysServer.Galaxy;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
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.xml.sax.SAXException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@RestController
public class GalaxyServer {
/**
* This is only used as part of the updater to pass files to the client as requested via URL path
*/
@GetMapping("/**")
public void GalaxyGetResponse(HttpServletRequest request, HttpServletResponse response) {
String pathToFile = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
// Change the file path to use spaces
pathToFile = StringUtils.removeStart(pathToFile.replace("%20", " "), "/");
System.out.println("[Galaxy][GET] Request for file: " + pathToFile);
Resource resource = new ClassPathResource("static/UpdateFiles/" + pathToFile);
int errorCode = 0;
try {
// Try to open the file as a resource
InputStream file = resource.getInputStream();
byte[] fileContent = org.apache.commons.io.IOUtils.toByteArray(file);
// Check if the file exists
if (fileContent.length > 0) {
// The file exists - Load the file to the stream
InputStream fileContentStream = new ByteArrayInputStream(fileContent);
// Copy the stream to the response's output stream
IOUtils.copy(fileContentStream, response.getOutputStream());
// Flush the buffer
//TODO: 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);
}
} catch (IOException e) {
// File read errors caught here
System.out.println("[Galaxy][GET][ERROR] Problem loading file: " + pathToFile);
e.printStackTrace();
// Set the error code
errorCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
} catch (Exception e) {
// File doesn't exist error caught here
System.out.println(e.getMessage());
// Set the error code
errorCode = HttpServletResponse.SC_NOT_FOUND;
} finally {
// Send the error code if an error was thrown;
if (errorCode != 0) {
try {
response.sendError(errorCode);
} catch (IOException e) {
System.out.println("[Galaxy][GET][ERROR] Problem sending error response to client: ");
e.printStackTrace();
}
}
}
}
/**
* Handler for all POST Requests
* Postcards, LoadCrib and SaveCrib
*/
@PostMapping("/")
public ResponseEntity<String> GalaxyPostResponse(@RequestBody String xmlBody) {
try {
// Parse the xml body of the request
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlBody);
doc.getDocumentElement().normalize();
// Get the root element
String root = doc.getDocumentElement().getNodeName();
// Handle based on the root element
switch(root) {
case "postcard":
return sendPostcard(doc);
case "savecrib":
return saveCrib(doc);
case "loadcrib":
return loadCrib(doc);
default:
System.out.println("[Galaxy][POST][ERROR] Unhandled type of request for: " + root);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
} catch (ParserConfigurationException | IOException | SAXException e) {
System.out.println("[Galaxy][POST][ERROR] Thrown Error: ");
e.printStackTrace();
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
//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
return null;
}
private ResponseEntity<String> saveCrib(Document request) {
System.out.println("[Galaxy][POST] savecrib request received");
//TODO: IMPLEMENT METHOD
return null;
}
private ResponseEntity<String> sendPostcard(Document request) {
System.out.println("[Galaxy][POST] postcard request received");
//TODO: IMPLEMENT METHOD
return null;
}
}

View File

@@ -0,0 +1,15 @@
package com.icedberries.UBFunkeysServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.icedberries")
public class UBFunkeysServerApplication {
public static void main(String[] args) {
SpringApplication.run(UBFunkeysServerApplication.class, args);
}
}

View File

@@ -0,0 +1,13 @@
package com.icedberries.UBFunkeysServer.config;
import com.icedberries.UBFunkeysServer.UBFunkeysServerApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(UBFunkeysServerApplication.class);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 263 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 330 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 216 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 173 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 260 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 192 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 196 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 257 KiB

View File

@@ -0,0 +1,13 @@
package com.icedberries.UBFunkeysServer;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class UBFunkeysServerApplicationTests {
@Test
void contextLoads() {
}
}